input_event.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*************************************************************************/
  2. /* input_event.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "input_event.h"
  31. #include "input_map.h"
  32. #include "os/keyboard.h"
  33. /**
  34. *
  35. */
  36. bool InputEvent::operator==(const InputEvent &p_event) const {
  37. if (type != p_event.type) {
  38. return false;
  39. }
  40. switch (type) {
  41. /** Current clang-format style doesn't play well with the aligned return values of that switch. */
  42. /* clang-format off */
  43. case NONE:
  44. return true;
  45. case KEY:
  46. return key.unicode == p_event.key.unicode
  47. && key.scancode == p_event.key.scancode
  48. && key.echo == p_event.key.echo
  49. && key.pressed == p_event.key.pressed
  50. && key.mod == p_event.key.mod;
  51. case MOUSE_MOTION:
  52. return mouse_motion.x == p_event.mouse_motion.x
  53. && mouse_motion.y == p_event.mouse_motion.y
  54. && mouse_motion.relative_x == p_event.mouse_motion.relative_x
  55. && mouse_motion.relative_y == p_event.mouse_motion.relative_y
  56. && mouse_motion.button_mask == p_event.mouse_motion.button_mask
  57. && key.mod == p_event.key.mod;
  58. case MOUSE_BUTTON:
  59. return mouse_button.pressed == p_event.mouse_button.pressed
  60. && mouse_button.x == p_event.mouse_button.x
  61. && mouse_button.y == p_event.mouse_button.y
  62. && mouse_button.button_index == p_event.mouse_button.button_index
  63. && mouse_button.button_mask == p_event.mouse_button.button_mask
  64. && key.mod == p_event.key.mod;
  65. case JOYSTICK_MOTION:
  66. return joy_motion.axis == p_event.joy_motion.axis
  67. && joy_motion.axis_value == p_event.joy_motion.axis_value;
  68. case JOYSTICK_BUTTON:
  69. return joy_button.pressed == p_event.joy_button.pressed
  70. && joy_button.button_index == p_event.joy_button.button_index
  71. && joy_button.pressure == p_event.joy_button.pressure;
  72. case SCREEN_TOUCH:
  73. return screen_touch.pressed == p_event.screen_touch.pressed
  74. && screen_touch.index == p_event.screen_touch.index
  75. && screen_touch.x == p_event.screen_touch.x
  76. && screen_touch.y == p_event.screen_touch.y;
  77. case SCREEN_DRAG:
  78. return screen_drag.index == p_event.screen_drag.index
  79. && screen_drag.x == p_event.screen_drag.x
  80. && screen_drag.y == p_event.screen_drag.y;
  81. case ACTION:
  82. return action.action == p_event.action.action
  83. && action.pressed == p_event.action.pressed;
  84. /* clang-format on */
  85. default:
  86. ERR_PRINT("No logic to compare InputEvents of this type, this shouldn't happen.");
  87. }
  88. return false;
  89. }
  90. InputEvent::operator String() const {
  91. String str = "Device " + itos(device) + " ID " + itos(ID) + " ";
  92. switch (type) {
  93. case NONE: {
  94. return "Event: None";
  95. } break;
  96. case KEY: {
  97. str += "Event: Key ";
  98. str = str + "Unicode: " + String::chr(key.unicode) + " Scan: " + itos(key.scancode) + " Echo: " + String(key.echo ? "True" : "False") + " Pressed" + String(key.pressed ? "True" : "False") + " Mod: ";
  99. if (key.mod.shift)
  100. str += "S";
  101. if (key.mod.control)
  102. str += "C";
  103. if (key.mod.alt)
  104. str += "A";
  105. if (key.mod.meta)
  106. str += "M";
  107. return str;
  108. } break;
  109. case MOUSE_MOTION: {
  110. str += "Event: Motion ";
  111. str = str + " Pos: " + itos(mouse_motion.x) + "," + itos(mouse_motion.y) + " Rel: " + itos(mouse_motion.relative_x) + "," + itos(mouse_motion.relative_y) + " Mask: ";
  112. for (int i = 0; i < 8; i++) {
  113. if ((1 << i) & mouse_motion.button_mask)
  114. str += itos(i + 1);
  115. }
  116. str += " Mod: ";
  117. if (key.mod.shift)
  118. str += "S";
  119. if (key.mod.control)
  120. str += "C";
  121. if (key.mod.alt)
  122. str += "A";
  123. if (key.mod.meta)
  124. str += "M";
  125. return str;
  126. } break;
  127. case MOUSE_BUTTON: {
  128. str += "Event: Button ";
  129. str = str + "Pressed: " + itos(mouse_button.pressed) + " Pos: " + itos(mouse_button.x) + "," + itos(mouse_button.y) + " Button: " + itos(mouse_button.button_index) + " Mask: ";
  130. for (int i = 0; i < 8; i++) {
  131. if ((1 << i) & mouse_button.button_mask)
  132. str += itos(i + 1);
  133. }
  134. str += " Mod: ";
  135. if (key.mod.shift)
  136. str += "S";
  137. if (key.mod.control)
  138. str += "C";
  139. if (key.mod.alt)
  140. str += "A";
  141. if (key.mod.meta)
  142. str += "M";
  143. str += String(" DoubleClick: ") + (mouse_button.doubleclick ? "Yes" : "No");
  144. return str;
  145. } break;
  146. case JOYSTICK_MOTION: {
  147. str += "Event: JoystickMotion ";
  148. str = str + "Axis: " + itos(joy_motion.axis) + " Value: " + rtos(joy_motion.axis_value);
  149. return str;
  150. } break;
  151. case JOYSTICK_BUTTON: {
  152. str += "Event: JoystickButton ";
  153. str = str + "Pressed: " + itos(joy_button.pressed) + " Index: " + itos(joy_button.button_index) + " pressure " + rtos(joy_button.pressure);
  154. return str;
  155. } break;
  156. case SCREEN_TOUCH: {
  157. str += "Event: ScreenTouch ";
  158. str = str + "Pressed: " + itos(screen_touch.pressed) + " Index: " + itos(screen_touch.index) + " pos " + rtos(screen_touch.x) + "," + rtos(screen_touch.y);
  159. return str;
  160. } break;
  161. case SCREEN_DRAG: {
  162. str += "Event: ScreenDrag ";
  163. str = str + " Index: " + itos(screen_drag.index) + " pos " + rtos(screen_drag.x) + "," + rtos(screen_drag.y);
  164. return str;
  165. } break;
  166. case ACTION: {
  167. str += "Event: Action: " + InputMap::get_singleton()->get_action_from_id(action.action) + " Pressed: " + itos(action.pressed);
  168. return str;
  169. } break;
  170. }
  171. return "";
  172. }
  173. void InputEvent::set_as_action(const String &p_action, bool p_pressed) {
  174. type = ACTION;
  175. action.action = InputMap::get_singleton()->get_action_id(p_action);
  176. action.pressed = p_pressed;
  177. }
  178. bool InputEvent::is_pressed() const {
  179. switch (type) {
  180. case KEY: return key.pressed;
  181. case MOUSE_BUTTON: return mouse_button.pressed;
  182. case JOYSTICK_BUTTON: return joy_button.pressed;
  183. case SCREEN_TOUCH: return screen_touch.pressed;
  184. case JOYSTICK_MOTION: return ABS(joy_motion.axis_value) > 0.5;
  185. case ACTION: return action.pressed;
  186. default: {
  187. }
  188. }
  189. return false;
  190. }
  191. bool InputEvent::is_echo() const {
  192. return (type == KEY && key.echo);
  193. }
  194. bool InputEvent::is_action(const String &p_action) const {
  195. return InputMap::get_singleton()->event_is_action(*this, p_action);
  196. }
  197. bool InputEvent::is_action_pressed(const String &p_action) const {
  198. return is_action(p_action) && is_pressed() && !is_echo();
  199. }
  200. bool InputEvent::is_action_released(const String &p_action) const {
  201. return is_action(p_action) && !is_pressed();
  202. }
  203. uint32_t InputEventKey::get_scancode_with_modifiers() const {
  204. uint32_t sc = scancode;
  205. if (mod.control)
  206. sc |= KEY_MASK_CTRL;
  207. if (mod.alt)
  208. sc |= KEY_MASK_ALT;
  209. if (mod.shift)
  210. sc |= KEY_MASK_SHIFT;
  211. if (mod.meta)
  212. sc |= KEY_MASK_META;
  213. return sc;
  214. }
  215. InputEvent InputEvent::xform_by(const Matrix32 &p_xform) const {
  216. InputEvent ev = *this;
  217. switch (ev.type) {
  218. case InputEvent::MOUSE_BUTTON: {
  219. Vector2 g = p_xform.xform(Vector2(ev.mouse_button.global_x, ev.mouse_button.global_y));
  220. Vector2 l = p_xform.xform(Vector2(ev.mouse_button.x, ev.mouse_button.y));
  221. ev.mouse_button.x = l.x;
  222. ev.mouse_button.y = l.y;
  223. ev.mouse_button.global_x = g.x;
  224. ev.mouse_button.global_y = g.y;
  225. } break;
  226. case InputEvent::MOUSE_MOTION: {
  227. Vector2 g = p_xform.xform(Vector2(ev.mouse_motion.global_x, ev.mouse_motion.global_y));
  228. Vector2 l = p_xform.xform(Vector2(ev.mouse_motion.x, ev.mouse_motion.y));
  229. Vector2 r = p_xform.basis_xform(Vector2(ev.mouse_motion.relative_x, ev.mouse_motion.relative_y));
  230. Vector2 s = p_xform.basis_xform(Vector2(ev.mouse_motion.speed_x, ev.mouse_motion.speed_y));
  231. ev.mouse_motion.x = l.x;
  232. ev.mouse_motion.y = l.y;
  233. ev.mouse_motion.global_x = g.x;
  234. ev.mouse_motion.global_y = g.y;
  235. ev.mouse_motion.relative_x = r.x;
  236. ev.mouse_motion.relative_y = r.y;
  237. ev.mouse_motion.speed_x = s.x;
  238. ev.mouse_motion.speed_y = s.y;
  239. } break;
  240. case InputEvent::SCREEN_TOUCH: {
  241. Vector2 t = p_xform.xform(Vector2(ev.screen_touch.x, ev.screen_touch.y));
  242. ev.screen_touch.x = t.x;
  243. ev.screen_touch.y = t.y;
  244. } break;
  245. case InputEvent::SCREEN_DRAG: {
  246. Vector2 t = p_xform.xform(Vector2(ev.screen_drag.x, ev.screen_drag.y));
  247. Vector2 r = p_xform.basis_xform(Vector2(ev.screen_drag.relative_x, ev.screen_drag.relative_y));
  248. Vector2 s = p_xform.basis_xform(Vector2(ev.screen_drag.speed_x, ev.screen_drag.speed_y));
  249. ev.screen_drag.x = t.x;
  250. ev.screen_drag.y = t.y;
  251. ev.screen_drag.relative_x = r.x;
  252. ev.screen_drag.relative_y = r.y;
  253. ev.screen_drag.speed_x = s.x;
  254. ev.screen_drag.speed_y = s.y;
  255. } break;
  256. }
  257. return ev;
  258. }