android_input_handler.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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) {
  183. if (!mouse_event_info.valid) {
  184. return;
  185. }
  186. Ref<InputEventMouseButton> ev;
  187. ev.instance();
  188. _set_key_modifier_state(ev);
  189. ev->set_position(mouse_event_info.pos);
  190. ev->set_global_position(mouse_event_info.pos);
  191. ev->set_pressed(p_pressed);
  192. int changed_button_mask = buttons_state ^ buttons_mask;
  193. buttons_state = buttons_mask;
  194. ev->set_button_index(_button_index_from_mask(changed_button_mask));
  195. ev->set_button_mask(buttons_mask);
  196. ev->set_doubleclick(p_double_click);
  197. input->parse_input_event(ev);
  198. hover_prev_pos = mouse_event_info.pos;
  199. }
  200. void AndroidInputHandler::_release_mouse_event_info() {
  201. _parse_mouse_event_info(0, false, false);
  202. mouse_event_info.valid = false;
  203. }
  204. 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) {
  205. int event_buttons_mask = _android_button_mask_to_godot_button_mask(p_event_android_buttons_mask);
  206. switch (p_event_action) {
  207. case AMOTION_EVENT_ACTION_HOVER_MOVE: // hover move
  208. case AMOTION_EVENT_ACTION_HOVER_ENTER: // hover enter
  209. case AMOTION_EVENT_ACTION_HOVER_EXIT: { // hover exit
  210. // https://developer.android.com/reference/android/view/MotionEvent.html#ACTION_HOVER_ENTER
  211. Ref<InputEventMouseMotion> ev;
  212. ev.instance();
  213. _set_key_modifier_state(ev);
  214. ev->set_position(p_event_pos);
  215. ev->set_global_position(p_event_pos);
  216. ev->set_relative(p_event_pos - hover_prev_pos);
  217. input->parse_input_event(ev);
  218. hover_prev_pos = p_event_pos;
  219. } break;
  220. case AMOTION_EVENT_ACTION_DOWN:
  221. case AMOTION_EVENT_ACTION_BUTTON_PRESS: {
  222. // Release any remaining touches or mouse event
  223. _release_mouse_event_info();
  224. _release_all_touch();
  225. mouse_event_info.valid = true;
  226. mouse_event_info.pos = p_event_pos;
  227. _parse_mouse_event_info(event_buttons_mask, true, p_double_click);
  228. } break;
  229. case AMOTION_EVENT_ACTION_UP:
  230. case AMOTION_EVENT_ACTION_CANCEL:
  231. case AMOTION_EVENT_ACTION_BUTTON_RELEASE: {
  232. _release_mouse_event_info();
  233. } break;
  234. case AMOTION_EVENT_ACTION_MOVE: {
  235. if (!mouse_event_info.valid) {
  236. return;
  237. }
  238. Ref<InputEventMouseMotion> ev;
  239. ev.instance();
  240. _set_key_modifier_state(ev);
  241. ev->set_position(p_event_pos);
  242. ev->set_global_position(p_event_pos);
  243. ev->set_relative(p_event_pos - hover_prev_pos);
  244. ev->set_button_mask(event_buttons_mask);
  245. input->parse_input_event(ev);
  246. mouse_event_info.pos = p_event_pos;
  247. hover_prev_pos = p_event_pos;
  248. } break;
  249. case AMOTION_EVENT_ACTION_SCROLL: {
  250. Ref<InputEventMouseButton> ev;
  251. ev.instance();
  252. _set_key_modifier_state(ev);
  253. ev->set_position(p_event_pos);
  254. ev->set_global_position(p_event_pos);
  255. ev->set_pressed(true);
  256. buttons_state = event_buttons_mask;
  257. if (p_delta.y > 0) {
  258. _wheel_button_click(event_buttons_mask, ev, BUTTON_WHEEL_UP, p_delta.y);
  259. } else if (p_delta.y < 0) {
  260. _wheel_button_click(event_buttons_mask, ev, BUTTON_WHEEL_DOWN, -p_delta.y);
  261. }
  262. if (p_delta.x > 0) {
  263. _wheel_button_click(event_buttons_mask, ev, BUTTON_WHEEL_RIGHT, p_delta.x);
  264. } else if (p_delta.x < 0) {
  265. _wheel_button_click(event_buttons_mask, ev, BUTTON_WHEEL_LEFT, -p_delta.x);
  266. }
  267. } break;
  268. }
  269. }
  270. void AndroidInputHandler::_wheel_button_click(int event_buttons_mask, const Ref<InputEventMouseButton> &ev, int wheel_button, float factor) {
  271. Ref<InputEventMouseButton> evd = ev->duplicate();
  272. evd->set_button_index(wheel_button);
  273. evd->set_button_mask(event_buttons_mask ^ (1 << (wheel_button - 1)));
  274. evd->set_factor(factor);
  275. input->parse_input_event(evd);
  276. Ref<InputEventMouseButton> evdd = evd->duplicate();
  277. evdd->set_pressed(false);
  278. evdd->set_button_mask(event_buttons_mask);
  279. input->parse_input_event(evdd);
  280. }
  281. void AndroidInputHandler::process_magnify(Point2 p_pos, float p_factor) {
  282. Ref<InputEventMagnifyGesture> magnify_event;
  283. magnify_event.instance();
  284. _set_key_modifier_state(magnify_event);
  285. magnify_event->set_position(p_pos);
  286. magnify_event->set_factor(p_factor);
  287. input->parse_input_event(magnify_event);
  288. }
  289. void AndroidInputHandler::process_pan(Point2 p_pos, Vector2 p_delta) {
  290. Ref<InputEventPanGesture> pan_event;
  291. pan_event.instance();
  292. _set_key_modifier_state(pan_event);
  293. pan_event->set_position(p_pos);
  294. pan_event->set_delta(p_delta);
  295. input->parse_input_event(pan_event);
  296. }
  297. int AndroidInputHandler::_button_index_from_mask(int button_mask) {
  298. switch (button_mask) {
  299. case BUTTON_MASK_LEFT:
  300. return BUTTON_LEFT;
  301. case BUTTON_MASK_RIGHT:
  302. return BUTTON_RIGHT;
  303. case BUTTON_MASK_MIDDLE:
  304. return BUTTON_MIDDLE;
  305. case BUTTON_MASK_XBUTTON1:
  306. return BUTTON_XBUTTON1;
  307. case BUTTON_MASK_XBUTTON2:
  308. return BUTTON_XBUTTON2;
  309. default:
  310. return 0;
  311. }
  312. }
  313. int AndroidInputHandler::_android_button_mask_to_godot_button_mask(int android_button_mask) {
  314. int godot_button_mask = 0;
  315. if (android_button_mask & AMOTION_EVENT_BUTTON_PRIMARY) {
  316. godot_button_mask |= BUTTON_MASK_LEFT;
  317. }
  318. if (android_button_mask & AMOTION_EVENT_BUTTON_SECONDARY) {
  319. godot_button_mask |= BUTTON_MASK_RIGHT;
  320. }
  321. if (android_button_mask & AMOTION_EVENT_BUTTON_TERTIARY) {
  322. godot_button_mask |= BUTTON_MASK_MIDDLE;
  323. }
  324. if (android_button_mask & AMOTION_EVENT_BUTTON_BACK) {
  325. godot_button_mask |= BUTTON_MASK_XBUTTON1;
  326. }
  327. if (android_button_mask & AMOTION_EVENT_BUTTON_FORWARD) {
  328. godot_button_mask |= BUTTON_MASK_XBUTTON2;
  329. }
  330. return godot_button_mask;
  331. }
  332. void AndroidInputHandler::joy_connection_changed(int p_device, bool p_connected, String p_name) {
  333. input->joy_connection_changed(p_device, p_connected, p_name, "");
  334. }