android_input_handler.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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. input->joy_axis(p_event.device, p_event.index, p_event.value);
  40. break;
  41. case JOY_EVENT_HAT:
  42. input->joy_hat(p_event.device, p_event.hat);
  43. break;
  44. default:
  45. return;
  46. }
  47. }
  48. void AndroidInputHandler::_set_key_modifier_state(Ref<InputEventWithModifiers> ev) const {
  49. ev->set_shift(shift_mem);
  50. ev->set_alt(alt_mem);
  51. ev->set_metakey(meta_mem);
  52. ev->set_control(control_mem);
  53. }
  54. void AndroidInputHandler::process_key_event(int p_scancode, int p_physical_scancode, int p_unicode, bool p_pressed) {
  55. Ref<InputEventKey> ev;
  56. ev.instance();
  57. unsigned int physical_scancode = godot_code_from_android_code(p_physical_scancode);
  58. unsigned int scancode = physical_scancode;
  59. if (p_scancode != 0) {
  60. scancode = godot_code_from_unicode(p_scancode);
  61. }
  62. switch (physical_scancode) {
  63. case KEY_SHIFT: {
  64. shift_mem = p_pressed;
  65. } break;
  66. case KEY_ALT: {
  67. alt_mem = p_pressed;
  68. } break;
  69. case KEY_CONTROL: {
  70. control_mem = p_pressed;
  71. } break;
  72. case KEY_META: {
  73. meta_mem = p_pressed;
  74. } break;
  75. default:
  76. break;
  77. }
  78. ev->set_scancode(scancode);
  79. ev->set_physical_scancode(physical_scancode);
  80. ev->set_unicode(p_unicode);
  81. ev->set_pressed(p_pressed);
  82. _set_key_modifier_state(ev);
  83. if (p_physical_scancode == AKEYCODE_BACK) {
  84. if (MainLoop *main_loop = OS::get_singleton()->get_main_loop()) {
  85. main_loop->call_deferred("notification", MainLoop::NOTIFICATION_WM_GO_BACK_REQUEST);
  86. }
  87. }
  88. input->parse_input_event(ev);
  89. }
  90. void AndroidInputHandler::_parse_all_touch(bool p_pressed, bool p_double_tap) {
  91. if (touch.size()) {
  92. //end all if exist
  93. for (int i = 0; i < touch.size(); i++) {
  94. Ref<InputEventScreenTouch> ev;
  95. ev.instance();
  96. ev->set_index(touch[i].id);
  97. ev->set_pressed(p_pressed);
  98. ev->set_position(touch[i].pos);
  99. ev->set_double_tap(p_double_tap);
  100. input->parse_input_event(ev);
  101. }
  102. }
  103. }
  104. void AndroidInputHandler::_release_all_touch() {
  105. _parse_all_touch(false, false);
  106. touch.clear();
  107. }
  108. void AndroidInputHandler::process_touch_event(int p_event, int p_pointer, const Vector<TouchPos> &p_points, bool p_double_tap) {
  109. switch (p_event) {
  110. case AMOTION_EVENT_ACTION_DOWN: { //gesture begin
  111. // Release any remaining touches or mouse event
  112. _release_mouse_event_info();
  113. _release_all_touch();
  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. _parse_all_touch(true, p_double_tap);
  121. } break;
  122. case AMOTION_EVENT_ACTION_MOVE: { //motion
  123. if (touch.size() != p_points.size()) {
  124. return;
  125. }
  126. for (int i = 0; i < touch.size(); i++) {
  127. int idx = -1;
  128. for (int j = 0; j < p_points.size(); j++) {
  129. if (touch[i].id == p_points[j].id) {
  130. idx = j;
  131. break;
  132. }
  133. }
  134. ERR_CONTINUE(idx == -1);
  135. if (touch[i].pos == p_points[idx].pos) {
  136. continue; //no move unncesearily
  137. }
  138. Ref<InputEventScreenDrag> ev;
  139. ev.instance();
  140. ev->set_index(touch[i].id);
  141. ev->set_position(p_points[idx].pos);
  142. ev->set_relative(p_points[idx].pos - touch[i].pos);
  143. input->parse_input_event(ev);
  144. touch.write[i].pos = p_points[idx].pos;
  145. }
  146. } break;
  147. case AMOTION_EVENT_ACTION_CANCEL:
  148. case AMOTION_EVENT_ACTION_UP: { //release
  149. _release_all_touch();
  150. } break;
  151. case AMOTION_EVENT_ACTION_POINTER_DOWN: { // add touch
  152. for (int i = 0; i < p_points.size(); i++) {
  153. if (p_points[i].id == p_pointer) {
  154. TouchPos tp = p_points[i];
  155. touch.push_back(tp);
  156. Ref<InputEventScreenTouch> ev;
  157. ev.instance();
  158. ev->set_index(tp.id);
  159. ev->set_pressed(true);
  160. ev->set_position(tp.pos);
  161. input->parse_input_event(ev);
  162. break;
  163. }
  164. }
  165. } break;
  166. case AMOTION_EVENT_ACTION_POINTER_UP: { // remove touch
  167. for (int i = 0; i < touch.size(); i++) {
  168. if (touch[i].id == p_pointer) {
  169. Ref<InputEventScreenTouch> ev;
  170. ev.instance();
  171. ev->set_index(touch[i].id);
  172. ev->set_pressed(false);
  173. ev->set_position(touch[i].pos);
  174. input->parse_input_event(ev);
  175. touch.remove(i);
  176. break;
  177. }
  178. }
  179. } break;
  180. }
  181. }
  182. void AndroidInputHandler::_parse_mouse_event_info(int buttons_mask, bool p_pressed, bool p_double_click, bool p_source_mouse_relative) {
  183. if (!mouse_event_info.valid) {
  184. return;
  185. }
  186. Ref<InputEventMouseButton> ev;
  187. ev.instance();
  188. _set_key_modifier_state(ev);
  189. if (p_source_mouse_relative) {
  190. ev->set_position(hover_prev_pos);
  191. ev->set_global_position(hover_prev_pos);
  192. } else {
  193. ev->set_position(mouse_event_info.pos);
  194. ev->set_global_position(mouse_event_info.pos);
  195. hover_prev_pos = mouse_event_info.pos;
  196. }
  197. ev->set_pressed(p_pressed);
  198. int changed_button_mask = buttons_state ^ buttons_mask;
  199. buttons_state = buttons_mask;
  200. ev->set_button_index(_button_index_from_mask(changed_button_mask));
  201. ev->set_button_mask(buttons_mask);
  202. ev->set_doubleclick(p_double_click);
  203. input->parse_input_event(ev);
  204. }
  205. void AndroidInputHandler::_release_mouse_event_info(bool p_source_mouse_relative) {
  206. _parse_mouse_event_info(0, false, false, p_source_mouse_relative);
  207. mouse_event_info.valid = false;
  208. }
  209. void AndroidInputHandler::process_mouse_event(int p_event_action, int p_event_android_buttons_mask, Point2 p_event_pos, Vector2 p_delta, bool p_double_click, bool p_source_mouse_relative) {
  210. int event_buttons_mask = _android_button_mask_to_godot_button_mask(p_event_android_buttons_mask);
  211. switch (p_event_action) {
  212. case AMOTION_EVENT_ACTION_HOVER_MOVE: // hover move
  213. case AMOTION_EVENT_ACTION_HOVER_ENTER: // hover enter
  214. case AMOTION_EVENT_ACTION_HOVER_EXIT: { // hover exit
  215. // https://developer.android.com/reference/android/view/MotionEvent.html#ACTION_HOVER_ENTER
  216. Ref<InputEventMouseMotion> ev;
  217. ev.instance();
  218. _set_key_modifier_state(ev);
  219. ev->set_position(p_event_pos);
  220. ev->set_global_position(p_event_pos);
  221. ev->set_relative(p_event_pos - hover_prev_pos);
  222. input->parse_input_event(ev);
  223. hover_prev_pos = p_event_pos;
  224. } break;
  225. case AMOTION_EVENT_ACTION_DOWN:
  226. case AMOTION_EVENT_ACTION_BUTTON_PRESS: {
  227. // Release any remaining touches or mouse event
  228. _release_mouse_event_info();
  229. _release_all_touch();
  230. mouse_event_info.valid = true;
  231. mouse_event_info.pos = p_event_pos;
  232. _parse_mouse_event_info(event_buttons_mask, true, p_double_click, p_source_mouse_relative);
  233. } break;
  234. case AMOTION_EVENT_ACTION_UP:
  235. case AMOTION_EVENT_ACTION_CANCEL:
  236. case AMOTION_EVENT_ACTION_BUTTON_RELEASE: {
  237. _release_mouse_event_info(p_source_mouse_relative);
  238. } break;
  239. case AMOTION_EVENT_ACTION_MOVE: {
  240. if (!mouse_event_info.valid) {
  241. return;
  242. }
  243. Ref<InputEventMouseMotion> ev;
  244. ev.instance();
  245. _set_key_modifier_state(ev);
  246. if (p_source_mouse_relative) {
  247. ev->set_position(hover_prev_pos);
  248. ev->set_global_position(hover_prev_pos);
  249. ev->set_relative(p_event_pos);
  250. } else {
  251. ev->set_position(p_event_pos);
  252. ev->set_global_position(p_event_pos);
  253. ev->set_relative(p_event_pos - hover_prev_pos);
  254. mouse_event_info.pos = p_event_pos;
  255. hover_prev_pos = p_event_pos;
  256. }
  257. ev->set_button_mask(event_buttons_mask);
  258. input->parse_input_event(ev);
  259. } break;
  260. case AMOTION_EVENT_ACTION_SCROLL: {
  261. Ref<InputEventMouseButton> ev;
  262. ev.instance();
  263. _set_key_modifier_state(ev);
  264. if (p_source_mouse_relative) {
  265. ev->set_position(hover_prev_pos);
  266. ev->set_global_position(hover_prev_pos);
  267. } else {
  268. ev->set_position(p_event_pos);
  269. ev->set_global_position(p_event_pos);
  270. }
  271. ev->set_pressed(true);
  272. buttons_state = event_buttons_mask;
  273. if (p_delta.y > 0) {
  274. _wheel_button_click(event_buttons_mask, ev, BUTTON_WHEEL_UP, p_delta.y);
  275. } else if (p_delta.y < 0) {
  276. _wheel_button_click(event_buttons_mask, ev, BUTTON_WHEEL_DOWN, -p_delta.y);
  277. }
  278. if (p_delta.x > 0) {
  279. _wheel_button_click(event_buttons_mask, ev, BUTTON_WHEEL_RIGHT, p_delta.x);
  280. } else if (p_delta.x < 0) {
  281. _wheel_button_click(event_buttons_mask, ev, BUTTON_WHEEL_LEFT, -p_delta.x);
  282. }
  283. } break;
  284. }
  285. }
  286. void AndroidInputHandler::_wheel_button_click(int event_buttons_mask, const Ref<InputEventMouseButton> &ev, int wheel_button, float factor) {
  287. Ref<InputEventMouseButton> evd = ev->duplicate();
  288. evd->set_button_index(wheel_button);
  289. evd->set_button_mask(event_buttons_mask ^ (1 << (wheel_button - 1)));
  290. evd->set_factor(factor);
  291. input->parse_input_event(evd);
  292. Ref<InputEventMouseButton> evdd = evd->duplicate();
  293. evdd->set_pressed(false);
  294. evdd->set_button_mask(event_buttons_mask);
  295. input->parse_input_event(evdd);
  296. }
  297. void AndroidInputHandler::process_magnify(Point2 p_pos, float p_factor) {
  298. Ref<InputEventMagnifyGesture> magnify_event;
  299. magnify_event.instance();
  300. _set_key_modifier_state(magnify_event);
  301. magnify_event->set_position(p_pos);
  302. magnify_event->set_factor(p_factor);
  303. input->parse_input_event(magnify_event);
  304. }
  305. void AndroidInputHandler::process_pan(Point2 p_pos, Vector2 p_delta) {
  306. Ref<InputEventPanGesture> pan_event;
  307. pan_event.instance();
  308. _set_key_modifier_state(pan_event);
  309. pan_event->set_position(p_pos);
  310. pan_event->set_delta(p_delta);
  311. input->parse_input_event(pan_event);
  312. }
  313. int AndroidInputHandler::_button_index_from_mask(int button_mask) {
  314. switch (button_mask) {
  315. case BUTTON_MASK_LEFT:
  316. return BUTTON_LEFT;
  317. case BUTTON_MASK_RIGHT:
  318. return BUTTON_RIGHT;
  319. case BUTTON_MASK_MIDDLE:
  320. return BUTTON_MIDDLE;
  321. case BUTTON_MASK_XBUTTON1:
  322. return BUTTON_XBUTTON1;
  323. case BUTTON_MASK_XBUTTON2:
  324. return BUTTON_XBUTTON2;
  325. default:
  326. return 0;
  327. }
  328. }
  329. int AndroidInputHandler::_android_button_mask_to_godot_button_mask(int android_button_mask) {
  330. int godot_button_mask = 0;
  331. if (android_button_mask & AMOTION_EVENT_BUTTON_PRIMARY) {
  332. godot_button_mask |= BUTTON_MASK_LEFT;
  333. }
  334. if (android_button_mask & AMOTION_EVENT_BUTTON_SECONDARY) {
  335. godot_button_mask |= BUTTON_MASK_RIGHT;
  336. }
  337. if (android_button_mask & AMOTION_EVENT_BUTTON_TERTIARY) {
  338. godot_button_mask |= BUTTON_MASK_MIDDLE;
  339. }
  340. if (android_button_mask & AMOTION_EVENT_BUTTON_BACK) {
  341. godot_button_mask |= BUTTON_MASK_XBUTTON1;
  342. }
  343. if (android_button_mask & AMOTION_EVENT_BUTTON_FORWARD) {
  344. godot_button_mask |= BUTTON_MASK_XBUTTON2;
  345. }
  346. return godot_button_mask;
  347. }
  348. void AndroidInputHandler::joy_connection_changed(int p_device, bool p_connected, String p_name) {
  349. input->joy_connection_changed(p_device, p_connected, p_name, "");
  350. }