android_input_handler.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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_event(Ref<InputEvent> &p_event) {
  55. input->parse_input_event(p_event);
  56. }
  57. void AndroidInputHandler::process_key_event(int p_scancode, int p_physical_scancode, int p_unicode, bool p_pressed) {
  58. Ref<InputEventKey> ev;
  59. ev.instance();
  60. unsigned int physical_scancode = godot_code_from_android_code(p_physical_scancode);
  61. unsigned int scancode = physical_scancode;
  62. if (p_scancode != 0) {
  63. scancode = godot_code_from_unicode(p_scancode);
  64. }
  65. switch (physical_scancode) {
  66. case KEY_SHIFT: {
  67. shift_mem = p_pressed;
  68. } break;
  69. case KEY_ALT: {
  70. alt_mem = p_pressed;
  71. } break;
  72. case KEY_CONTROL: {
  73. control_mem = p_pressed;
  74. } break;
  75. case KEY_META: {
  76. meta_mem = p_pressed;
  77. } break;
  78. default:
  79. break;
  80. }
  81. ev->set_scancode(scancode);
  82. ev->set_physical_scancode(physical_scancode);
  83. ev->set_unicode(p_unicode);
  84. ev->set_pressed(p_pressed);
  85. _set_key_modifier_state(ev);
  86. if (p_physical_scancode == AKEYCODE_BACK) {
  87. if (MainLoop *main_loop = OS::get_singleton()->get_main_loop()) {
  88. main_loop->call_deferred("notification", MainLoop::NOTIFICATION_WM_GO_BACK_REQUEST);
  89. }
  90. }
  91. input->parse_input_event(ev);
  92. }
  93. void AndroidInputHandler::process_touch(int p_event, int p_pointer, const Vector<TouchPos> &p_points) {
  94. switch (p_event) {
  95. case AMOTION_EVENT_ACTION_DOWN: { //gesture begin
  96. if (touch.size()) {
  97. //end all if exist
  98. for (int i = 0; i < touch.size(); i++) {
  99. Ref<InputEventScreenTouch> ev;
  100. ev.instance();
  101. ev->set_index(touch[i].id);
  102. ev->set_pressed(false);
  103. ev->set_position(touch[i].pos);
  104. input->parse_input_event(ev);
  105. }
  106. }
  107. touch.resize(p_points.size());
  108. for (int i = 0; i < p_points.size(); i++) {
  109. touch.write[i].id = p_points[i].id;
  110. touch.write[i].pos = p_points[i].pos;
  111. }
  112. //send touch
  113. for (int i = 0; i < touch.size(); i++) {
  114. Ref<InputEventScreenTouch> ev;
  115. ev.instance();
  116. ev->set_index(touch[i].id);
  117. ev->set_pressed(true);
  118. ev->set_position(touch[i].pos);
  119. input->parse_input_event(ev);
  120. }
  121. } break;
  122. case AMOTION_EVENT_ACTION_MOVE: { //motion
  123. ERR_FAIL_COND(touch.size() != p_points.size());
  124. for (int i = 0; i < touch.size(); i++) {
  125. int idx = -1;
  126. for (int j = 0; j < p_points.size(); j++) {
  127. if (touch[i].id == p_points[j].id) {
  128. idx = j;
  129. break;
  130. }
  131. }
  132. ERR_CONTINUE(idx == -1);
  133. if (touch[i].pos == p_points[idx].pos) {
  134. continue; //no move unncesearily
  135. }
  136. Ref<InputEventScreenDrag> ev;
  137. ev.instance();
  138. ev->set_index(touch[i].id);
  139. ev->set_position(p_points[idx].pos);
  140. ev->set_relative(p_points[idx].pos - touch[i].pos);
  141. input->parse_input_event(ev);
  142. touch.write[i].pos = p_points[idx].pos;
  143. }
  144. } break;
  145. case AMOTION_EVENT_ACTION_CANCEL:
  146. case AMOTION_EVENT_ACTION_UP: { //release
  147. if (touch.size()) {
  148. //end all if exist
  149. for (int i = 0; i < touch.size(); i++) {
  150. Ref<InputEventScreenTouch> ev;
  151. ev.instance();
  152. ev->set_index(touch[i].id);
  153. ev->set_pressed(false);
  154. ev->set_position(touch[i].pos);
  155. input->parse_input_event(ev);
  156. }
  157. touch.clear();
  158. }
  159. } break;
  160. case AMOTION_EVENT_ACTION_POINTER_DOWN: { // add touch
  161. for (int i = 0; i < p_points.size(); i++) {
  162. if (p_points[i].id == p_pointer) {
  163. TouchPos tp = p_points[i];
  164. touch.push_back(tp);
  165. Ref<InputEventScreenTouch> ev;
  166. ev.instance();
  167. ev->set_index(tp.id);
  168. ev->set_pressed(true);
  169. ev->set_position(tp.pos);
  170. input->parse_input_event(ev);
  171. break;
  172. }
  173. }
  174. } break;
  175. case AMOTION_EVENT_ACTION_POINTER_UP: { // remove touch
  176. for (int i = 0; i < touch.size(); i++) {
  177. if (touch[i].id == p_pointer) {
  178. Ref<InputEventScreenTouch> ev;
  179. ev.instance();
  180. ev->set_index(touch[i].id);
  181. ev->set_pressed(false);
  182. ev->set_position(touch[i].pos);
  183. input->parse_input_event(ev);
  184. touch.remove(i);
  185. break;
  186. }
  187. }
  188. } break;
  189. }
  190. }
  191. void AndroidInputHandler::process_hover(int p_type, Point2 p_pos) {
  192. // https://developer.android.com/reference/android/view/MotionEvent.html#ACTION_HOVER_ENTER
  193. switch (p_type) {
  194. case AMOTION_EVENT_ACTION_HOVER_MOVE: // hover move
  195. case AMOTION_EVENT_ACTION_HOVER_ENTER: // hover enter
  196. case AMOTION_EVENT_ACTION_HOVER_EXIT: { // hover exit
  197. Ref<InputEventMouseMotion> ev;
  198. ev.instance();
  199. _set_key_modifier_state(ev);
  200. ev->set_position(p_pos);
  201. ev->set_global_position(p_pos);
  202. ev->set_relative(p_pos - hover_prev_pos);
  203. input->parse_input_event(ev);
  204. hover_prev_pos = p_pos;
  205. } break;
  206. }
  207. }
  208. void AndroidInputHandler::process_mouse_event(int event_action, int event_android_buttons_mask, Point2 event_pos, float event_vertical_factor, float event_horizontal_factor) {
  209. int event_buttons_mask = _android_button_mask_to_godot_button_mask(event_android_buttons_mask);
  210. switch (event_action) {
  211. case AMOTION_EVENT_ACTION_BUTTON_PRESS:
  212. case AMOTION_EVENT_ACTION_BUTTON_RELEASE: {
  213. Ref<InputEventMouseButton> ev;
  214. ev.instance();
  215. _set_key_modifier_state(ev);
  216. ev->set_position(event_pos);
  217. ev->set_global_position(event_pos);
  218. ev->set_pressed(event_action == AMOTION_EVENT_ACTION_BUTTON_PRESS);
  219. int changed_button_mask = buttons_state ^ event_buttons_mask;
  220. buttons_state = event_buttons_mask;
  221. ev->set_button_index(_button_index_from_mask(changed_button_mask));
  222. ev->set_button_mask(event_buttons_mask);
  223. input->parse_input_event(ev);
  224. } break;
  225. case AMOTION_EVENT_ACTION_MOVE: {
  226. Ref<InputEventMouseMotion> ev;
  227. ev.instance();
  228. _set_key_modifier_state(ev);
  229. ev->set_position(event_pos);
  230. ev->set_global_position(event_pos);
  231. ev->set_relative(event_pos - hover_prev_pos);
  232. ev->set_button_mask(event_buttons_mask);
  233. input->parse_input_event(ev);
  234. hover_prev_pos = event_pos;
  235. } break;
  236. case AMOTION_EVENT_ACTION_SCROLL: {
  237. Ref<InputEventMouseButton> ev;
  238. ev.instance();
  239. _set_key_modifier_state(ev);
  240. ev->set_position(event_pos);
  241. ev->set_global_position(event_pos);
  242. ev->set_pressed(true);
  243. buttons_state = event_buttons_mask;
  244. if (event_vertical_factor > 0) {
  245. _wheel_button_click(event_buttons_mask, ev, BUTTON_WHEEL_UP, event_vertical_factor);
  246. } else if (event_vertical_factor < 0) {
  247. _wheel_button_click(event_buttons_mask, ev, BUTTON_WHEEL_DOWN, -event_vertical_factor);
  248. }
  249. if (event_horizontal_factor > 0) {
  250. _wheel_button_click(event_buttons_mask, ev, BUTTON_WHEEL_RIGHT, event_horizontal_factor);
  251. } else if (event_horizontal_factor < 0) {
  252. _wheel_button_click(event_buttons_mask, ev, BUTTON_WHEEL_LEFT, -event_horizontal_factor);
  253. }
  254. } break;
  255. }
  256. }
  257. void AndroidInputHandler::_wheel_button_click(int event_buttons_mask, const Ref<InputEventMouseButton> &ev, int wheel_button, float factor) {
  258. Ref<InputEventMouseButton> evd = ev->duplicate();
  259. evd->set_button_index(wheel_button);
  260. evd->set_button_mask(event_buttons_mask ^ (1 << (wheel_button - 1)));
  261. evd->set_factor(factor);
  262. input->parse_input_event(evd);
  263. Ref<InputEventMouseButton> evdd = evd->duplicate();
  264. evdd->set_pressed(false);
  265. evdd->set_button_mask(event_buttons_mask);
  266. input->parse_input_event(evdd);
  267. }
  268. void AndroidInputHandler::process_double_tap(int event_android_button_mask, Point2 p_pos) {
  269. int event_button_mask = _android_button_mask_to_godot_button_mask(event_android_button_mask);
  270. Ref<InputEventMouseButton> ev;
  271. ev.instance();
  272. _set_key_modifier_state(ev);
  273. ev->set_position(p_pos);
  274. ev->set_global_position(p_pos);
  275. ev->set_pressed(event_button_mask != 0);
  276. ev->set_button_index(_button_index_from_mask(event_button_mask));
  277. ev->set_button_mask(event_button_mask);
  278. ev->set_doubleclick(true);
  279. input->parse_input_event(ev);
  280. }
  281. int AndroidInputHandler::_button_index_from_mask(int button_mask) {
  282. switch (button_mask) {
  283. case BUTTON_MASK_LEFT:
  284. return BUTTON_LEFT;
  285. case BUTTON_MASK_RIGHT:
  286. return BUTTON_RIGHT;
  287. case BUTTON_MASK_MIDDLE:
  288. return BUTTON_MIDDLE;
  289. case BUTTON_MASK_XBUTTON1:
  290. return BUTTON_XBUTTON1;
  291. case BUTTON_MASK_XBUTTON2:
  292. return BUTTON_XBUTTON2;
  293. default:
  294. return 0;
  295. }
  296. }
  297. int AndroidInputHandler::_android_button_mask_to_godot_button_mask(int android_button_mask) {
  298. int godot_button_mask = 0;
  299. if (android_button_mask & AMOTION_EVENT_BUTTON_PRIMARY) {
  300. godot_button_mask |= BUTTON_MASK_LEFT;
  301. }
  302. if (android_button_mask & AMOTION_EVENT_BUTTON_SECONDARY) {
  303. godot_button_mask |= BUTTON_MASK_RIGHT;
  304. }
  305. if (android_button_mask & AMOTION_EVENT_BUTTON_TERTIARY) {
  306. godot_button_mask |= BUTTON_MASK_MIDDLE;
  307. }
  308. if (android_button_mask & AMOTION_EVENT_BUTTON_BACK) {
  309. godot_button_mask |= BUTTON_MASK_XBUTTON1;
  310. }
  311. if (android_button_mask & AMOTION_EVENT_BUTTON_FORWARD) {
  312. godot_button_mask |= BUTTON_MASK_XBUTTON2;
  313. }
  314. return godot_button_mask;
  315. }
  316. void AndroidInputHandler::joy_connection_changed(int p_device, bool p_connected, String p_name) {
  317. input->joy_connection_changed(p_device, p_connected, p_name, "");
  318. }