android_input_handler.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /**************************************************************************/
  2. /* android_input_handler.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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::_cancel_all_touch() {
  91. _parse_all_touch(false, true);
  92. touch.clear();
  93. }
  94. void AndroidInputHandler::_parse_all_touch(bool p_pressed, bool p_canceled, bool p_double_tap) {
  95. if (touch.size()) {
  96. //end all if exist
  97. for (int i = 0; i < touch.size(); i++) {
  98. Ref<InputEventScreenTouch> ev;
  99. ev.instance();
  100. ev->set_index(touch[i].id);
  101. ev->set_pressed(p_pressed);
  102. ev->set_canceled(p_canceled);
  103. ev->set_position(touch[i].pos);
  104. ev->set_double_tap(p_double_tap);
  105. input->parse_input_event(ev);
  106. }
  107. }
  108. }
  109. void AndroidInputHandler::_release_all_touch() {
  110. _parse_all_touch(false);
  111. touch.clear();
  112. }
  113. void AndroidInputHandler::process_touch_event(int p_event, int p_pointer, const Vector<TouchPos> &p_points, bool p_double_tap) {
  114. switch (p_event) {
  115. case AMOTION_EVENT_ACTION_DOWN: { //gesture begin
  116. // Release any remaining touches or mouse event
  117. _release_mouse_event_info();
  118. _release_all_touch();
  119. touch.resize(p_points.size());
  120. for (int i = 0; i < p_points.size(); i++) {
  121. touch.write[i].id = p_points[i].id;
  122. touch.write[i].pos = p_points[i].pos;
  123. }
  124. //send touch
  125. _parse_all_touch(true, false, p_double_tap);
  126. } break;
  127. case AMOTION_EVENT_ACTION_MOVE: { //motion
  128. if (touch.size() != p_points.size()) {
  129. return;
  130. }
  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. }
  143. Ref<InputEventScreenDrag> ev;
  144. ev.instance();
  145. ev->set_index(touch[i].id);
  146. ev->set_position(p_points[idx].pos);
  147. ev->set_relative(p_points[idx].pos - touch[i].pos);
  148. input->parse_input_event(ev);
  149. touch.write[i].pos = p_points[idx].pos;
  150. }
  151. } break;
  152. case AMOTION_EVENT_ACTION_CANCEL: {
  153. _cancel_all_touch();
  154. } break;
  155. case AMOTION_EVENT_ACTION_UP: { //release
  156. _release_all_touch();
  157. } break;
  158. case AMOTION_EVENT_ACTION_POINTER_DOWN: { // add touch
  159. for (int i = 0; i < p_points.size(); i++) {
  160. if (p_points[i].id == p_pointer) {
  161. TouchPos tp = p_points[i];
  162. touch.push_back(tp);
  163. Ref<InputEventScreenTouch> ev;
  164. ev.instance();
  165. ev->set_index(tp.id);
  166. ev->set_pressed(true);
  167. ev->set_position(tp.pos);
  168. input->parse_input_event(ev);
  169. break;
  170. }
  171. }
  172. } break;
  173. case AMOTION_EVENT_ACTION_POINTER_UP: { // remove touch
  174. for (int i = 0; i < touch.size(); i++) {
  175. if (touch[i].id == p_pointer) {
  176. Ref<InputEventScreenTouch> ev;
  177. ev.instance();
  178. ev->set_index(touch[i].id);
  179. ev->set_pressed(false);
  180. ev->set_position(touch[i].pos);
  181. input->parse_input_event(ev);
  182. touch.remove(i);
  183. break;
  184. }
  185. }
  186. } break;
  187. }
  188. }
  189. void AndroidInputHandler::_cancel_mouse_event_info(bool p_source_mouse_relative) {
  190. buttons_state = 0;
  191. _parse_mouse_event_info(0, false, true, false, p_source_mouse_relative);
  192. mouse_event_info.valid = false;
  193. }
  194. void AndroidInputHandler::_parse_mouse_event_info(int buttons_mask, bool p_pressed, bool p_canceled, bool p_double_click, bool p_source_mouse_relative) {
  195. if (!mouse_event_info.valid) {
  196. return;
  197. }
  198. Ref<InputEventMouseButton> ev;
  199. ev.instance();
  200. _set_key_modifier_state(ev);
  201. if (p_source_mouse_relative) {
  202. ev->set_position(hover_prev_pos);
  203. ev->set_global_position(hover_prev_pos);
  204. } else {
  205. ev->set_position(mouse_event_info.pos);
  206. ev->set_global_position(mouse_event_info.pos);
  207. hover_prev_pos = mouse_event_info.pos;
  208. }
  209. ev->set_pressed(p_pressed);
  210. ev->set_canceled(p_canceled);
  211. int changed_button_mask = buttons_state ^ buttons_mask;
  212. buttons_state = buttons_mask;
  213. ev->set_button_index(_button_index_from_mask(changed_button_mask));
  214. ev->set_button_mask(buttons_mask);
  215. ev->set_doubleclick(p_double_click);
  216. input->parse_input_event(ev);
  217. }
  218. void AndroidInputHandler::_release_mouse_event_info(bool p_source_mouse_relative) {
  219. _parse_mouse_event_info(0, false, false, false, p_source_mouse_relative);
  220. mouse_event_info.valid = false;
  221. }
  222. 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) {
  223. int event_buttons_mask = _android_button_mask_to_godot_button_mask(p_event_android_buttons_mask);
  224. switch (p_event_action) {
  225. case AMOTION_EVENT_ACTION_HOVER_MOVE: // hover move
  226. case AMOTION_EVENT_ACTION_HOVER_ENTER: // hover enter
  227. case AMOTION_EVENT_ACTION_HOVER_EXIT: { // hover exit
  228. // https://developer.android.com/reference/android/view/MotionEvent.html#ACTION_HOVER_ENTER
  229. Ref<InputEventMouseMotion> ev;
  230. ev.instance();
  231. _set_key_modifier_state(ev);
  232. ev->set_position(p_event_pos);
  233. ev->set_global_position(p_event_pos);
  234. ev->set_relative(p_event_pos - hover_prev_pos);
  235. input->parse_input_event(ev);
  236. hover_prev_pos = p_event_pos;
  237. } break;
  238. case AMOTION_EVENT_ACTION_DOWN:
  239. case AMOTION_EVENT_ACTION_BUTTON_PRESS: {
  240. // Release any remaining touches or mouse event
  241. _release_mouse_event_info();
  242. _release_all_touch();
  243. mouse_event_info.valid = true;
  244. mouse_event_info.pos = p_event_pos;
  245. _parse_mouse_event_info(event_buttons_mask, true, false, p_double_click, p_source_mouse_relative);
  246. } break;
  247. case AMOTION_EVENT_ACTION_CANCEL: {
  248. _cancel_mouse_event_info(p_source_mouse_relative);
  249. } break;
  250. case AMOTION_EVENT_ACTION_UP:
  251. case AMOTION_EVENT_ACTION_BUTTON_RELEASE: {
  252. _release_mouse_event_info(p_source_mouse_relative);
  253. } break;
  254. case AMOTION_EVENT_ACTION_MOVE: {
  255. if (!mouse_event_info.valid) {
  256. return;
  257. }
  258. Ref<InputEventMouseMotion> ev;
  259. ev.instance();
  260. _set_key_modifier_state(ev);
  261. if (p_source_mouse_relative) {
  262. ev->set_position(hover_prev_pos);
  263. ev->set_global_position(hover_prev_pos);
  264. ev->set_relative(p_event_pos);
  265. } else {
  266. ev->set_position(p_event_pos);
  267. ev->set_global_position(p_event_pos);
  268. ev->set_relative(p_event_pos - hover_prev_pos);
  269. mouse_event_info.pos = p_event_pos;
  270. hover_prev_pos = p_event_pos;
  271. }
  272. ev->set_button_mask(event_buttons_mask);
  273. input->parse_input_event(ev);
  274. } break;
  275. case AMOTION_EVENT_ACTION_SCROLL: {
  276. Ref<InputEventMouseButton> ev;
  277. ev.instance();
  278. _set_key_modifier_state(ev);
  279. if (p_source_mouse_relative) {
  280. ev->set_position(hover_prev_pos);
  281. ev->set_global_position(hover_prev_pos);
  282. } else {
  283. ev->set_position(p_event_pos);
  284. ev->set_global_position(p_event_pos);
  285. }
  286. ev->set_pressed(true);
  287. buttons_state = event_buttons_mask;
  288. if (p_delta.y > 0) {
  289. _wheel_button_click(event_buttons_mask, ev, BUTTON_WHEEL_UP, p_delta.y);
  290. } else if (p_delta.y < 0) {
  291. _wheel_button_click(event_buttons_mask, ev, BUTTON_WHEEL_DOWN, -p_delta.y);
  292. }
  293. if (p_delta.x > 0) {
  294. _wheel_button_click(event_buttons_mask, ev, BUTTON_WHEEL_RIGHT, p_delta.x);
  295. } else if (p_delta.x < 0) {
  296. _wheel_button_click(event_buttons_mask, ev, BUTTON_WHEEL_LEFT, -p_delta.x);
  297. }
  298. } break;
  299. }
  300. }
  301. void AndroidInputHandler::_wheel_button_click(int event_buttons_mask, const Ref<InputEventMouseButton> &ev, int wheel_button, float factor) {
  302. Ref<InputEventMouseButton> evd = ev->duplicate();
  303. evd->set_button_index(wheel_button);
  304. evd->set_button_mask(event_buttons_mask ^ (1 << (wheel_button - 1)));
  305. evd->set_factor(factor);
  306. input->parse_input_event(evd);
  307. Ref<InputEventMouseButton> evdd = evd->duplicate();
  308. evdd->set_pressed(false);
  309. evdd->set_button_mask(event_buttons_mask);
  310. input->parse_input_event(evdd);
  311. }
  312. void AndroidInputHandler::process_magnify(Point2 p_pos, float p_factor) {
  313. Ref<InputEventMagnifyGesture> magnify_event;
  314. magnify_event.instance();
  315. _set_key_modifier_state(magnify_event);
  316. magnify_event->set_position(p_pos);
  317. magnify_event->set_factor(p_factor);
  318. input->parse_input_event(magnify_event);
  319. }
  320. void AndroidInputHandler::process_pan(Point2 p_pos, Vector2 p_delta) {
  321. Ref<InputEventPanGesture> pan_event;
  322. pan_event.instance();
  323. _set_key_modifier_state(pan_event);
  324. pan_event->set_position(p_pos);
  325. pan_event->set_delta(p_delta);
  326. input->parse_input_event(pan_event);
  327. }
  328. int AndroidInputHandler::_button_index_from_mask(int button_mask) {
  329. switch (button_mask) {
  330. case BUTTON_MASK_LEFT:
  331. return BUTTON_LEFT;
  332. case BUTTON_MASK_RIGHT:
  333. return BUTTON_RIGHT;
  334. case BUTTON_MASK_MIDDLE:
  335. return BUTTON_MIDDLE;
  336. case BUTTON_MASK_XBUTTON1:
  337. return BUTTON_XBUTTON1;
  338. case BUTTON_MASK_XBUTTON2:
  339. return BUTTON_XBUTTON2;
  340. default:
  341. return 0;
  342. }
  343. }
  344. int AndroidInputHandler::_android_button_mask_to_godot_button_mask(int android_button_mask) {
  345. int godot_button_mask = 0;
  346. if (android_button_mask & AMOTION_EVENT_BUTTON_PRIMARY) {
  347. godot_button_mask |= BUTTON_MASK_LEFT;
  348. }
  349. if (android_button_mask & AMOTION_EVENT_BUTTON_SECONDARY) {
  350. godot_button_mask |= BUTTON_MASK_RIGHT;
  351. }
  352. if (android_button_mask & AMOTION_EVENT_BUTTON_TERTIARY) {
  353. godot_button_mask |= BUTTON_MASK_MIDDLE;
  354. }
  355. if (android_button_mask & AMOTION_EVENT_BUTTON_BACK) {
  356. godot_button_mask |= BUTTON_MASK_XBUTTON1;
  357. }
  358. if (android_button_mask & AMOTION_EVENT_BUTTON_FORWARD) {
  359. godot_button_mask |= BUTTON_MASK_XBUTTON2;
  360. }
  361. return godot_button_mask;
  362. }
  363. void AndroidInputHandler::joy_connection_changed(int p_device, bool p_connected, String p_name) {
  364. input->joy_connection_changed(p_device, p_connected, p_name, "");
  365. }