android_input_handler.cpp 12 KB

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