android_input_handler.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*************************************************************************/
  2. /* android_input_handler.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "android_input_handler.h"
  31. #include "android_keys_utils.h"
  32. #include "core/os/os.h"
  33. void AndroidInputHandler::process_joy_event(const JoypadEvent &p_event) {
  34. switch (p_event.type) {
  35. case JOY_EVENT_BUTTON:
  36. input->joy_button(p_event.device, p_event.index, p_event.pressed);
  37. break;
  38. case JOY_EVENT_AXIS:
  39. InputDefault::JoyAxis value;
  40. value.min = -1;
  41. value.value = p_event.value;
  42. input->joy_axis(p_event.device, p_event.index, value);
  43. break;
  44. case JOY_EVENT_HAT:
  45. input->joy_hat(p_event.device, p_event.hat);
  46. break;
  47. default:
  48. return;
  49. }
  50. }
  51. void AndroidInputHandler::_set_key_modifier_state(Ref<InputEventWithModifiers> ev) const {
  52. ev->set_shift(shift_mem);
  53. ev->set_alt(alt_mem);
  54. ev->set_metakey(meta_mem);
  55. ev->set_control(control_mem);
  56. }
  57. void AndroidInputHandler::process_event(Ref<InputEvent> &p_event) {
  58. input->parse_input_event(p_event);
  59. }
  60. void AndroidInputHandler::process_key_event(int p_keycode, int p_scancode, int p_unicode_char, bool p_pressed) {
  61. Ref<InputEventKey> ev;
  62. ev.instance();
  63. int val = p_unicode_char;
  64. unsigned int scancode = android_get_keysym(p_keycode);
  65. unsigned int phy_scancode = android_get_keysym(p_scancode);
  66. switch (scancode) {
  67. case KEY_SHIFT: {
  68. shift_mem = p_pressed;
  69. } break;
  70. case KEY_ALT: {
  71. alt_mem = p_pressed;
  72. } break;
  73. case KEY_CONTROL: {
  74. control_mem = p_pressed;
  75. } break;
  76. case KEY_META: {
  77. meta_mem = p_pressed;
  78. } break;
  79. }
  80. ev->set_scancode(scancode);
  81. ev->set_physical_scancode(phy_scancode);
  82. ev->set_unicode(val);
  83. ev->set_pressed(p_pressed);
  84. _set_key_modifier_state(ev);
  85. if (val == '\n') {
  86. ev->set_scancode(KEY_ENTER);
  87. } else if (val == 61448) {
  88. ev->set_scancode(KEY_BACKSPACE);
  89. ev->set_unicode(KEY_BACKSPACE);
  90. } else if (val == 61453) {
  91. ev->set_scancode(KEY_ENTER);
  92. ev->set_unicode(KEY_ENTER);
  93. } else if (p_scancode == 4) {
  94. if (MainLoop *main_loop = OS::get_singleton()->get_main_loop()) {
  95. main_loop->call_deferred("notification", MainLoop::NOTIFICATION_WM_GO_BACK_REQUEST);
  96. }
  97. }
  98. input->parse_input_event(ev);
  99. }
  100. void AndroidInputHandler::process_touch(int p_event, int p_pointer, const Vector<TouchPos> &p_points) {
  101. switch (p_event) {
  102. case AMOTION_EVENT_ACTION_DOWN: { //gesture begin
  103. if (touch.size()) {
  104. //end all if exist
  105. for (int i = 0; i < touch.size(); i++) {
  106. Ref<InputEventScreenTouch> ev;
  107. ev.instance();
  108. ev->set_index(touch[i].id);
  109. ev->set_pressed(false);
  110. ev->set_position(touch[i].pos);
  111. input->parse_input_event(ev);
  112. }
  113. }
  114. touch.resize(p_points.size());
  115. for (int i = 0; i < p_points.size(); i++) {
  116. touch.write[i].id = p_points[i].id;
  117. touch.write[i].pos = p_points[i].pos;
  118. }
  119. //send touch
  120. for (int i = 0; i < touch.size(); i++) {
  121. Ref<InputEventScreenTouch> ev;
  122. ev.instance();
  123. ev->set_index(touch[i].id);
  124. ev->set_pressed(true);
  125. ev->set_position(touch[i].pos);
  126. input->parse_input_event(ev);
  127. }
  128. } break;
  129. case AMOTION_EVENT_ACTION_MOVE: { //motion
  130. ERR_FAIL_COND(touch.size() != p_points.size());
  131. for (int i = 0; i < touch.size(); i++) {
  132. int idx = -1;
  133. for (int j = 0; j < p_points.size(); j++) {
  134. if (touch[i].id == p_points[j].id) {
  135. idx = j;
  136. break;
  137. }
  138. }
  139. ERR_CONTINUE(idx == -1);
  140. if (touch[i].pos == p_points[idx].pos)
  141. continue; //no move unncesearily
  142. Ref<InputEventScreenDrag> ev;
  143. ev.instance();
  144. ev->set_index(touch[i].id);
  145. ev->set_position(p_points[idx].pos);
  146. ev->set_relative(p_points[idx].pos - touch[i].pos);
  147. input->parse_input_event(ev);
  148. touch.write[i].pos = p_points[idx].pos;
  149. }
  150. } break;
  151. case AMOTION_EVENT_ACTION_CANCEL:
  152. case AMOTION_EVENT_ACTION_UP: { //release
  153. if (touch.size()) {
  154. //end all if exist
  155. for (int i = 0; i < touch.size(); i++) {
  156. Ref<InputEventScreenTouch> ev;
  157. ev.instance();
  158. ev->set_index(touch[i].id);
  159. ev->set_pressed(false);
  160. ev->set_position(touch[i].pos);
  161. input->parse_input_event(ev);
  162. }
  163. touch.clear();
  164. }
  165. } break;
  166. case AMOTION_EVENT_ACTION_POINTER_DOWN: { // add touch
  167. for (int i = 0; i < p_points.size(); i++) {
  168. if (p_points[i].id == p_pointer) {
  169. TouchPos tp = p_points[i];
  170. touch.push_back(tp);
  171. Ref<InputEventScreenTouch> ev;
  172. ev.instance();
  173. ev->set_index(tp.id);
  174. ev->set_pressed(true);
  175. ev->set_position(tp.pos);
  176. input->parse_input_event(ev);
  177. break;
  178. }
  179. }
  180. } break;
  181. case AMOTION_EVENT_ACTION_POINTER_UP: { // remove touch
  182. for (int i = 0; i < touch.size(); i++) {
  183. if (touch[i].id == p_pointer) {
  184. Ref<InputEventScreenTouch> ev;
  185. ev.instance();
  186. ev->set_index(touch[i].id);
  187. ev->set_pressed(false);
  188. ev->set_position(touch[i].pos);
  189. input->parse_input_event(ev);
  190. touch.remove(i);
  191. break;
  192. }
  193. }
  194. } break;
  195. }
  196. }
  197. void AndroidInputHandler::process_hover(int p_type, Point2 p_pos) {
  198. // https://developer.android.com/reference/android/view/MotionEvent.html#ACTION_HOVER_ENTER
  199. switch (p_type) {
  200. case AMOTION_EVENT_ACTION_HOVER_MOVE: // hover move
  201. case AMOTION_EVENT_ACTION_HOVER_ENTER: // hover enter
  202. case AMOTION_EVENT_ACTION_HOVER_EXIT: { // hover exit
  203. Ref<InputEventMouseMotion> ev;
  204. ev.instance();
  205. _set_key_modifier_state(ev);
  206. ev->set_position(p_pos);
  207. ev->set_global_position(p_pos);
  208. ev->set_relative(p_pos - hover_prev_pos);
  209. input->parse_input_event(ev);
  210. hover_prev_pos = p_pos;
  211. } break;
  212. }
  213. }
  214. void AndroidInputHandler::process_mouse_event(int event_action, int event_android_buttons_mask, Point2 event_pos, float event_vertical_factor, float event_horizontal_factor) {
  215. int event_buttons_mask = _android_button_mask_to_godot_button_mask(event_android_buttons_mask);
  216. switch (event_action) {
  217. case AMOTION_EVENT_ACTION_BUTTON_PRESS:
  218. case AMOTION_EVENT_ACTION_BUTTON_RELEASE: {
  219. Ref<InputEventMouseButton> ev;
  220. ev.instance();
  221. _set_key_modifier_state(ev);
  222. ev->set_position(event_pos);
  223. ev->set_global_position(event_pos);
  224. ev->set_pressed(event_action == AMOTION_EVENT_ACTION_BUTTON_PRESS);
  225. int changed_button_mask = buttons_state ^ event_buttons_mask;
  226. buttons_state = event_buttons_mask;
  227. ev->set_button_index(_button_index_from_mask(changed_button_mask));
  228. ev->set_button_mask(event_buttons_mask);
  229. input->parse_input_event(ev);
  230. } break;
  231. case AMOTION_EVENT_ACTION_MOVE: {
  232. Ref<InputEventMouseMotion> ev;
  233. ev.instance();
  234. _set_key_modifier_state(ev);
  235. ev->set_position(event_pos);
  236. ev->set_global_position(event_pos);
  237. ev->set_relative(event_pos - hover_prev_pos);
  238. ev->set_button_mask(event_buttons_mask);
  239. input->parse_input_event(ev);
  240. hover_prev_pos = event_pos;
  241. } break;
  242. case AMOTION_EVENT_ACTION_SCROLL: {
  243. Ref<InputEventMouseButton> ev;
  244. ev.instance();
  245. _set_key_modifier_state(ev);
  246. ev->set_position(event_pos);
  247. ev->set_global_position(event_pos);
  248. ev->set_pressed(true);
  249. buttons_state = event_buttons_mask;
  250. if (event_vertical_factor > 0) {
  251. _wheel_button_click(event_buttons_mask, ev, BUTTON_WHEEL_UP, event_vertical_factor);
  252. } else if (event_vertical_factor < 0) {
  253. _wheel_button_click(event_buttons_mask, ev, BUTTON_WHEEL_DOWN, -event_vertical_factor);
  254. }
  255. if (event_horizontal_factor > 0) {
  256. _wheel_button_click(event_buttons_mask, ev, BUTTON_WHEEL_RIGHT, event_horizontal_factor);
  257. } else if (event_horizontal_factor < 0) {
  258. _wheel_button_click(event_buttons_mask, ev, BUTTON_WHEEL_LEFT, -event_horizontal_factor);
  259. }
  260. } break;
  261. }
  262. }
  263. void AndroidInputHandler::_wheel_button_click(int event_buttons_mask, const Ref<InputEventMouseButton> &ev, int wheel_button, float factor) {
  264. Ref<InputEventMouseButton> evd = ev->duplicate();
  265. evd->set_button_index(wheel_button);
  266. evd->set_button_mask(event_buttons_mask ^ (1 << (wheel_button - 1)));
  267. evd->set_factor(factor);
  268. input->parse_input_event(evd);
  269. Ref<InputEventMouseButton> evdd = evd->duplicate();
  270. evdd->set_pressed(false);
  271. evdd->set_button_mask(event_buttons_mask);
  272. input->parse_input_event(evdd);
  273. }
  274. void AndroidInputHandler::process_double_tap(int event_android_button_mask, Point2 p_pos) {
  275. int event_button_mask = _android_button_mask_to_godot_button_mask(event_android_button_mask);
  276. Ref<InputEventMouseButton> ev;
  277. ev.instance();
  278. _set_key_modifier_state(ev);
  279. ev->set_position(p_pos);
  280. ev->set_global_position(p_pos);
  281. ev->set_pressed(event_button_mask != 0);
  282. ev->set_button_index(_button_index_from_mask(event_button_mask));
  283. ev->set_button_mask(event_button_mask);
  284. ev->set_doubleclick(true);
  285. input->parse_input_event(ev);
  286. }
  287. void AndroidInputHandler::process_scroll(Point2 p_pos) {
  288. Ref<InputEventPanGesture> ev;
  289. ev.instance();
  290. _set_key_modifier_state(ev);
  291. ev->set_position(p_pos);
  292. ev->set_delta(p_pos - scroll_prev_pos);
  293. input->parse_input_event(ev);
  294. scroll_prev_pos = p_pos;
  295. }
  296. int AndroidInputHandler::_button_index_from_mask(int button_mask) {
  297. switch (button_mask) {
  298. case BUTTON_MASK_LEFT:
  299. return BUTTON_LEFT;
  300. case BUTTON_MASK_RIGHT:
  301. return BUTTON_RIGHT;
  302. case BUTTON_MASK_MIDDLE:
  303. return BUTTON_MIDDLE;
  304. case BUTTON_MASK_XBUTTON1:
  305. return BUTTON_XBUTTON1;
  306. case BUTTON_MASK_XBUTTON2:
  307. return BUTTON_XBUTTON2;
  308. default:
  309. return 0;
  310. }
  311. }
  312. int AndroidInputHandler::_android_button_mask_to_godot_button_mask(int android_button_mask) {
  313. int godot_button_mask = 0;
  314. if (android_button_mask & AMOTION_EVENT_BUTTON_PRIMARY) {
  315. godot_button_mask |= BUTTON_MASK_LEFT;
  316. }
  317. if (android_button_mask & AMOTION_EVENT_BUTTON_SECONDARY) {
  318. godot_button_mask |= BUTTON_MASK_RIGHT;
  319. }
  320. if (android_button_mask & AMOTION_EVENT_BUTTON_TERTIARY) {
  321. godot_button_mask |= BUTTON_MASK_MIDDLE;
  322. }
  323. if (android_button_mask & AMOTION_EVENT_BUTTON_BACK) {
  324. godot_button_mask |= BUTTON_MASK_XBUTTON1;
  325. }
  326. if (android_button_mask & AMOTION_EVENT_BUTTON_SECONDARY) {
  327. godot_button_mask |= BUTTON_MASK_XBUTTON2;
  328. }
  329. return godot_button_mask;
  330. }
  331. void AndroidInputHandler::joy_connection_changed(int p_device, bool p_connected, String p_name) {
  332. input->joy_connection_changed(p_device, p_connected, p_name, "");
  333. }