android_input_handler.cpp 13 KB

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