android_input_handler.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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) {
  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. input->parse_input_event(ev);
  100. }
  101. }
  102. }
  103. void AndroidInputHandler::_release_all_touch() {
  104. _parse_all_touch(false);
  105. touch.clear();
  106. }
  107. void AndroidInputHandler::process_touch_event(int p_event, int p_pointer, const Vector<TouchPos> &p_points) {
  108. switch (p_event) {
  109. case AMOTION_EVENT_ACTION_DOWN: { //gesture begin
  110. // Release any remaining touches or mouse event
  111. _release_mouse_event_info();
  112. _release_all_touch();
  113. touch.resize(p_points.size());
  114. for (int i = 0; i < p_points.size(); i++) {
  115. touch.write[i].id = p_points[i].id;
  116. touch.write[i].pos = p_points[i].pos;
  117. }
  118. //send touch
  119. _parse_all_touch(true);
  120. } break;
  121. case AMOTION_EVENT_ACTION_MOVE: { //motion
  122. if (touch.size() != p_points.size()) {
  123. return;
  124. }
  125. for (int i = 0; i < touch.size(); i++) {
  126. int idx = -1;
  127. for (int j = 0; j < p_points.size(); j++) {
  128. if (touch[i].id == p_points[j].id) {
  129. idx = j;
  130. break;
  131. }
  132. }
  133. ERR_CONTINUE(idx == -1);
  134. if (touch[i].pos == p_points[idx].pos) {
  135. continue; //no move unncesearily
  136. }
  137. Ref<InputEventScreenDrag> ev;
  138. ev.instance();
  139. ev->set_index(touch[i].id);
  140. ev->set_position(p_points[idx].pos);
  141. ev->set_relative(p_points[idx].pos - touch[i].pos);
  142. input->parse_input_event(ev);
  143. touch.write[i].pos = p_points[idx].pos;
  144. }
  145. } break;
  146. case AMOTION_EVENT_ACTION_CANCEL:
  147. case AMOTION_EVENT_ACTION_UP: { //release
  148. _release_all_touch();
  149. } break;
  150. case AMOTION_EVENT_ACTION_POINTER_DOWN: { // add touch
  151. for (int i = 0; i < p_points.size(); i++) {
  152. if (p_points[i].id == p_pointer) {
  153. TouchPos tp = p_points[i];
  154. touch.push_back(tp);
  155. Ref<InputEventScreenTouch> ev;
  156. ev.instance();
  157. ev->set_index(tp.id);
  158. ev->set_pressed(true);
  159. ev->set_position(tp.pos);
  160. input->parse_input_event(ev);
  161. break;
  162. }
  163. }
  164. } break;
  165. case AMOTION_EVENT_ACTION_POINTER_UP: { // remove touch
  166. for (int i = 0; i < touch.size(); i++) {
  167. if (touch[i].id == p_pointer) {
  168. Ref<InputEventScreenTouch> ev;
  169. ev.instance();
  170. ev->set_index(touch[i].id);
  171. ev->set_pressed(false);
  172. ev->set_position(touch[i].pos);
  173. input->parse_input_event(ev);
  174. touch.remove(i);
  175. break;
  176. }
  177. }
  178. } break;
  179. }
  180. }
  181. void AndroidInputHandler::_parse_mouse_event_info(int buttons_mask, bool p_pressed, bool p_double_click) {
  182. if (!mouse_event_info.valid) {
  183. return;
  184. }
  185. Ref<InputEventMouseButton> ev;
  186. ev.instance();
  187. _set_key_modifier_state(ev);
  188. ev->set_position(mouse_event_info.pos);
  189. ev->set_global_position(mouse_event_info.pos);
  190. ev->set_pressed(p_pressed);
  191. int changed_button_mask = buttons_state ^ buttons_mask;
  192. buttons_state = buttons_mask;
  193. ev->set_button_index(_button_index_from_mask(changed_button_mask));
  194. ev->set_button_mask(buttons_mask);
  195. ev->set_doubleclick(p_double_click);
  196. input->parse_input_event(ev);
  197. hover_prev_pos = mouse_event_info.pos;
  198. }
  199. void AndroidInputHandler::_release_mouse_event_info() {
  200. _parse_mouse_event_info(0, false, false);
  201. mouse_event_info.valid = false;
  202. }
  203. 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) {
  204. int event_buttons_mask = _android_button_mask_to_godot_button_mask(p_event_android_buttons_mask);
  205. switch (p_event_action) {
  206. case AMOTION_EVENT_ACTION_HOVER_MOVE: // hover move
  207. case AMOTION_EVENT_ACTION_HOVER_ENTER: // hover enter
  208. case AMOTION_EVENT_ACTION_HOVER_EXIT: { // hover exit
  209. // https://developer.android.com/reference/android/view/MotionEvent.html#ACTION_HOVER_ENTER
  210. Ref<InputEventMouseMotion> ev;
  211. ev.instance();
  212. _set_key_modifier_state(ev);
  213. ev->set_position(p_event_pos);
  214. ev->set_global_position(p_event_pos);
  215. ev->set_relative(p_event_pos - hover_prev_pos);
  216. input->parse_input_event(ev);
  217. hover_prev_pos = p_event_pos;
  218. } break;
  219. case AMOTION_EVENT_ACTION_DOWN:
  220. case AMOTION_EVENT_ACTION_BUTTON_PRESS: {
  221. // Release any remaining touches or mouse event
  222. _release_mouse_event_info();
  223. _release_all_touch();
  224. mouse_event_info.valid = true;
  225. mouse_event_info.pos = p_event_pos;
  226. _parse_mouse_event_info(event_buttons_mask, true, p_double_click);
  227. } break;
  228. case AMOTION_EVENT_ACTION_UP:
  229. case AMOTION_EVENT_ACTION_CANCEL:
  230. case AMOTION_EVENT_ACTION_BUTTON_RELEASE: {
  231. _release_mouse_event_info();
  232. } break;
  233. case AMOTION_EVENT_ACTION_MOVE: {
  234. if (!mouse_event_info.valid) {
  235. return;
  236. }
  237. Ref<InputEventMouseMotion> ev;
  238. ev.instance();
  239. _set_key_modifier_state(ev);
  240. ev->set_position(p_event_pos);
  241. ev->set_global_position(p_event_pos);
  242. ev->set_relative(p_event_pos - hover_prev_pos);
  243. ev->set_button_mask(event_buttons_mask);
  244. input->parse_input_event(ev);
  245. mouse_event_info.pos = p_event_pos;
  246. hover_prev_pos = p_event_pos;
  247. } break;
  248. case AMOTION_EVENT_ACTION_SCROLL: {
  249. Ref<InputEventMouseButton> ev;
  250. ev.instance();
  251. _set_key_modifier_state(ev);
  252. ev->set_position(p_event_pos);
  253. ev->set_global_position(p_event_pos);
  254. ev->set_pressed(true);
  255. buttons_state = event_buttons_mask;
  256. if (p_delta.y > 0) {
  257. _wheel_button_click(event_buttons_mask, ev, BUTTON_WHEEL_UP, p_delta.y);
  258. } else if (p_delta.y < 0) {
  259. _wheel_button_click(event_buttons_mask, ev, BUTTON_WHEEL_DOWN, -p_delta.y);
  260. }
  261. if (p_delta.x > 0) {
  262. _wheel_button_click(event_buttons_mask, ev, BUTTON_WHEEL_RIGHT, p_delta.x);
  263. } else if (p_delta.x < 0) {
  264. _wheel_button_click(event_buttons_mask, ev, BUTTON_WHEEL_LEFT, -p_delta.x);
  265. }
  266. } break;
  267. }
  268. }
  269. void AndroidInputHandler::_wheel_button_click(int event_buttons_mask, const Ref<InputEventMouseButton> &ev, int wheel_button, float factor) {
  270. Ref<InputEventMouseButton> evd = ev->duplicate();
  271. evd->set_button_index(wheel_button);
  272. evd->set_button_mask(event_buttons_mask ^ (1 << (wheel_button - 1)));
  273. evd->set_factor(factor);
  274. input->parse_input_event(evd);
  275. Ref<InputEventMouseButton> evdd = evd->duplicate();
  276. evdd->set_pressed(false);
  277. evdd->set_button_mask(event_buttons_mask);
  278. input->parse_input_event(evdd);
  279. }
  280. void AndroidInputHandler::process_magnify(Point2 p_pos, float p_factor) {
  281. Ref<InputEventMagnifyGesture> magnify_event;
  282. magnify_event.instance();
  283. _set_key_modifier_state(magnify_event);
  284. magnify_event->set_position(p_pos);
  285. magnify_event->set_factor(p_factor);
  286. input->parse_input_event(magnify_event);
  287. }
  288. void AndroidInputHandler::process_pan(Point2 p_pos, Vector2 p_delta) {
  289. Ref<InputEventPanGesture> pan_event;
  290. pan_event.instance();
  291. _set_key_modifier_state(pan_event);
  292. pan_event->set_position(p_pos);
  293. pan_event->set_delta(p_delta);
  294. input->parse_input_event(pan_event);
  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_FORWARD) {
  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. }