input_event.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*************************************************************************/
  2. /* input_event.h */
  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. #ifndef INPUT_EVENT_H
  31. #define INPUT_EVENT_H
  32. #include "math_2d.h"
  33. #include "os/copymem.h"
  34. #include "typedefs.h"
  35. #include "ustring.h"
  36. /**
  37. @author Juan Linietsky <[email protected]>
  38. */
  39. /**
  40. * Input Event classes. These are used in the main loop.
  41. * The events are pretty obvious.
  42. */
  43. enum {
  44. BUTTON_LEFT = 1,
  45. BUTTON_RIGHT = 2,
  46. BUTTON_MIDDLE = 3,
  47. BUTTON_WHEEL_UP = 4,
  48. BUTTON_WHEEL_DOWN = 5,
  49. BUTTON_WHEEL_LEFT = 6,
  50. BUTTON_WHEEL_RIGHT = 7,
  51. BUTTON_MASK_LEFT = (1 << (BUTTON_LEFT - 1)),
  52. BUTTON_MASK_RIGHT = (1 << (BUTTON_RIGHT - 1)),
  53. BUTTON_MASK_MIDDLE = (1 << (BUTTON_MIDDLE - 1)),
  54. };
  55. enum {
  56. JOY_BUTTON_0 = 0,
  57. JOY_BUTTON_1 = 1,
  58. JOY_BUTTON_2 = 2,
  59. JOY_BUTTON_3 = 3,
  60. JOY_BUTTON_4 = 4,
  61. JOY_BUTTON_5 = 5,
  62. JOY_BUTTON_6 = 6,
  63. JOY_BUTTON_7 = 7,
  64. JOY_BUTTON_8 = 8,
  65. JOY_BUTTON_9 = 9,
  66. JOY_BUTTON_10 = 10,
  67. JOY_BUTTON_11 = 11,
  68. JOY_BUTTON_12 = 12,
  69. JOY_BUTTON_13 = 13,
  70. JOY_BUTTON_14 = 14,
  71. JOY_BUTTON_15 = 15,
  72. JOY_BUTTON_MAX = 16,
  73. JOY_L = JOY_BUTTON_4,
  74. JOY_R = JOY_BUTTON_5,
  75. JOY_L2 = JOY_BUTTON_6,
  76. JOY_R2 = JOY_BUTTON_7,
  77. JOY_L3 = JOY_BUTTON_8,
  78. JOY_R3 = JOY_BUTTON_9,
  79. JOY_SELECT = JOY_BUTTON_10,
  80. JOY_START = JOY_BUTTON_11,
  81. JOY_DPAD_UP = JOY_BUTTON_12,
  82. JOY_DPAD_DOWN = JOY_BUTTON_13,
  83. JOY_DPAD_LEFT = JOY_BUTTON_14,
  84. JOY_DPAD_RIGHT = JOY_BUTTON_15,
  85. // a little history about game controllers (who copied who)
  86. JOY_SNES_B = JOY_BUTTON_0,
  87. JOY_SNES_A = JOY_BUTTON_1,
  88. JOY_SNES_Y = JOY_BUTTON_2,
  89. JOY_SNES_X = JOY_BUTTON_3,
  90. JOY_SONY_CIRCLE = JOY_SNES_A,
  91. JOY_SONY_X = JOY_SNES_B,
  92. JOY_SONY_SQUARE = JOY_SNES_Y,
  93. JOY_SONY_TRIANGLE = JOY_SNES_X,
  94. JOY_SEGA_B = JOY_SNES_A,
  95. JOY_SEGA_A = JOY_SNES_B,
  96. JOY_SEGA_X = JOY_SNES_Y,
  97. JOY_SEGA_Y = JOY_SNES_X,
  98. JOY_XBOX_B = JOY_SEGA_B,
  99. JOY_XBOX_A = JOY_SEGA_A,
  100. JOY_XBOX_X = JOY_SEGA_X,
  101. JOY_XBOX_Y = JOY_SEGA_Y,
  102. JOY_DS_A = JOY_SNES_A,
  103. JOY_DS_B = JOY_SNES_B,
  104. JOY_DS_X = JOY_SNES_X,
  105. JOY_DS_Y = JOY_SNES_Y,
  106. JOY_WII_C = JOY_BUTTON_5,
  107. JOY_WII_Z = JOY_BUTTON_6,
  108. JOY_WII_MINUS = JOY_BUTTON_9,
  109. JOY_WII_PLUS = JOY_BUTTON_10,
  110. // end of history
  111. JOY_AXIS_0 = 0,
  112. JOY_AXIS_1 = 1,
  113. JOY_AXIS_2 = 2,
  114. JOY_AXIS_3 = 3,
  115. JOY_AXIS_4 = 4,
  116. JOY_AXIS_5 = 5,
  117. JOY_AXIS_6 = 6,
  118. JOY_AXIS_7 = 7,
  119. JOY_AXIS_MAX = 8,
  120. JOY_ANALOG_0_X = JOY_AXIS_0,
  121. JOY_ANALOG_0_Y = JOY_AXIS_1,
  122. JOY_ANALOG_1_X = JOY_AXIS_2,
  123. JOY_ANALOG_1_Y = JOY_AXIS_3,
  124. JOY_ANALOG_2_X = JOY_AXIS_4,
  125. JOY_ANALOG_2_Y = JOY_AXIS_5,
  126. JOY_ANALOG_L2 = JOY_AXIS_6,
  127. JOY_ANALOG_R2 = JOY_AXIS_7,
  128. };
  129. /**
  130. * Input Modifier Status
  131. * for keyboard/mouse events.
  132. */
  133. struct InputModifierState {
  134. bool shift;
  135. bool alt;
  136. #ifdef APPLE_STYLE_KEYS
  137. union {
  138. bool command;
  139. bool meta; //< windows/mac key
  140. };
  141. bool control;
  142. #else
  143. union {
  144. bool command; //< windows/mac key
  145. bool control;
  146. };
  147. bool meta; //< windows/mac key
  148. #endif
  149. bool operator==(const InputModifierState &rvalue) const {
  150. return ((shift == rvalue.shift) && (alt == rvalue.alt) && (control == rvalue.control) && (meta == rvalue.meta));
  151. }
  152. };
  153. struct InputEventKey {
  154. InputModifierState mod;
  155. bool pressed; /// otherwise release
  156. uint32_t scancode; ///< check keyboard.h , KeyCode enum, without modifier masks
  157. uint32_t unicode; ///unicode
  158. uint32_t get_scancode_with_modifiers() const;
  159. bool echo; /// true if this is an echo key
  160. };
  161. struct InputEventMouse {
  162. InputModifierState mod;
  163. int button_mask;
  164. int x, y;
  165. int global_x, global_y;
  166. int pointer_index;
  167. };
  168. struct InputEventMouseButton : public InputEventMouse {
  169. double factor;
  170. int button_index;
  171. bool pressed; //otherwise released
  172. bool doubleclick; //last even less than doubleclick time
  173. };
  174. struct InputEventMouseMotion : public InputEventMouse {
  175. int relative_x, relative_y;
  176. float speed_x, speed_y;
  177. };
  178. struct InputEventJoystickMotion {
  179. int axis; ///< Joystick axis
  180. float axis_value; ///< -1 to 1
  181. };
  182. struct InputEventJoystickButton {
  183. int button_index;
  184. bool pressed;
  185. float pressure; //0 to 1
  186. };
  187. struct InputEventScreenTouch {
  188. int index;
  189. int x, y;
  190. bool pressed;
  191. };
  192. struct InputEventScreenDrag {
  193. int index;
  194. int x, y;
  195. int relative_x, relative_y;
  196. float speed_x, speed_y;
  197. };
  198. struct InputEventAction {
  199. int action;
  200. bool pressed;
  201. };
  202. struct InputEvent {
  203. enum Type {
  204. NONE,
  205. KEY,
  206. MOUSE_MOTION,
  207. MOUSE_BUTTON,
  208. JOYSTICK_MOTION,
  209. JOYSTICK_BUTTON,
  210. SCREEN_TOUCH,
  211. SCREEN_DRAG,
  212. ACTION,
  213. TYPE_MAX
  214. };
  215. uint32_t ID;
  216. int type;
  217. int device;
  218. union {
  219. InputEventMouseMotion mouse_motion;
  220. InputEventMouseButton mouse_button;
  221. InputEventJoystickMotion joy_motion;
  222. InputEventJoystickButton joy_button;
  223. InputEventKey key;
  224. InputEventScreenTouch screen_touch;
  225. InputEventScreenDrag screen_drag;
  226. InputEventAction action;
  227. };
  228. bool is_pressed() const;
  229. bool is_action(const String &p_action) const;
  230. bool is_action_pressed(const String &p_action) const;
  231. bool is_action_released(const String &p_action) const;
  232. bool is_echo() const;
  233. void set_as_action(const String &p_action, bool p_pressed);
  234. InputEvent xform_by(const Matrix32 &p_xform) const;
  235. bool operator==(const InputEvent &p_event) const;
  236. operator String() const;
  237. InputEvent() {
  238. zeromem(this, sizeof(InputEvent));
  239. mouse_button.factor = 1;
  240. }
  241. };
  242. #endif