input_event.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*************************************************************************/
  2. /* input_event.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef INPUT_EVENT_H
  30. #define INPUT_EVENT_H
  31. #include "typedefs.h"
  32. #include "os/copymem.h"
  33. #include "ustring.h"
  34. /**
  35. @author Juan Linietsky <[email protected]>
  36. */
  37. /**
  38. * Input Event classes. These are used in the main loop.
  39. * The events are pretty obvious.
  40. */
  41. enum {
  42. BUTTON_LEFT=1,
  43. BUTTON_RIGHT=2,
  44. BUTTON_MIDDLE=3,
  45. BUTTON_WHEEL_UP=4,
  46. BUTTON_WHEEL_DOWN=5,
  47. BUTTON_MASK_LEFT=(1<<(BUTTON_LEFT-1)),
  48. BUTTON_MASK_RIGHT=(1<<(BUTTON_RIGHT-1)),
  49. BUTTON_MASK_MIDDLE=(1<<(BUTTON_MIDDLE-1)),
  50. };
  51. enum {
  52. JOY_BUTTON_0 = 0,
  53. JOY_BUTTON_1 = 1,
  54. JOY_BUTTON_2 = 2,
  55. JOY_BUTTON_3 = 3,
  56. JOY_BUTTON_4 = 4,
  57. JOY_BUTTON_5 = 5,
  58. JOY_BUTTON_6 = 6,
  59. JOY_BUTTON_7 = 7,
  60. JOY_BUTTON_8 = 8,
  61. JOY_BUTTON_9 = 9,
  62. JOY_BUTTON_10 = 10,
  63. JOY_BUTTON_11 = 11,
  64. JOY_BUTTON_12 = 12,
  65. JOY_BUTTON_13 = 13,
  66. JOY_BUTTON_14 = 14,
  67. JOY_BUTTON_15 = 15,
  68. JOY_BUTTON_MAX = 16,
  69. JOY_L = JOY_BUTTON_4,
  70. JOY_R = JOY_BUTTON_5,
  71. JOY_L2 = JOY_BUTTON_6,
  72. JOY_R2 = JOY_BUTTON_7,
  73. JOY_L3 = JOY_BUTTON_8,
  74. JOY_R3 = JOY_BUTTON_9,
  75. JOY_SELECT = JOY_BUTTON_10,
  76. JOY_START = JOY_BUTTON_11,
  77. JOY_DPAD_UP = JOY_BUTTON_12,
  78. JOY_DPAD_DOWN = JOY_BUTTON_13,
  79. JOY_DPAD_LEFT = JOY_BUTTON_14,
  80. JOY_DPAD_RIGHT = JOY_BUTTON_15,
  81. // a little history about game controllers (who copied who)
  82. JOY_SNES_B = JOY_BUTTON_0,
  83. JOY_SNES_A = JOY_BUTTON_1,
  84. JOY_SNES_Y = JOY_BUTTON_2,
  85. JOY_SNES_X = JOY_BUTTON_3,
  86. JOY_SONY_CIRCLE=JOY_SNES_A,
  87. JOY_SONY_X=JOY_SNES_B,
  88. JOY_SONY_SQUARE=JOY_SNES_Y,
  89. JOY_SONY_TRIANGLE=JOY_SNES_X,
  90. JOY_SEGA_B=JOY_SNES_A,
  91. JOY_SEGA_A=JOY_SNES_B,
  92. JOY_SEGA_X=JOY_SNES_Y,
  93. JOY_SEGA_Y=JOY_SNES_X,
  94. JOY_XBOX_B=JOY_SEGA_B,
  95. JOY_XBOX_A=JOY_SEGA_A,
  96. JOY_XBOX_X=JOY_SEGA_X,
  97. JOY_XBOX_Y=JOY_SEGA_Y,
  98. JOY_DS_A = JOY_SNES_A,
  99. JOY_DS_B = JOY_SNES_B,
  100. JOY_DS_X = JOY_SNES_X,
  101. JOY_DS_Y = JOY_SNES_Y,
  102. JOY_WII_C = JOY_BUTTON_5,
  103. JOY_WII_Z = JOY_BUTTON_6,
  104. JOY_WII_MINUS = JOY_BUTTON_9,
  105. JOY_WII_PLUS = JOY_BUTTON_10,
  106. // end of history
  107. JOY_AXIS_0=0,
  108. JOY_AXIS_1=1,
  109. JOY_AXIS_2=2,
  110. JOY_AXIS_3=3,
  111. JOY_AXIS_4=4,
  112. JOY_AXIS_5=5,
  113. JOY_AXIS_6=6,
  114. JOY_AXIS_7=7,
  115. JOY_AXIS_MAX=8,
  116. JOY_ANALOG_0_X = JOY_AXIS_0,
  117. JOY_ANALOG_0_Y = JOY_AXIS_1,
  118. JOY_ANALOG_1_X = JOY_AXIS_2,
  119. JOY_ANALOG_1_Y = JOY_AXIS_3,
  120. JOY_ANALOG_2_X = JOY_AXIS_4,
  121. JOY_ANALOG_2_Y = JOY_AXIS_5,
  122. };
  123. /**
  124. * Input Modifier Status
  125. * for keyboard/mouse events.
  126. */
  127. struct InputModifierState {
  128. bool shift;
  129. bool alt;
  130. #ifdef APPLE_STYLE_KEYS
  131. union {
  132. bool command;
  133. bool meta; //< windows/mac key
  134. };
  135. bool control;
  136. #else
  137. union {
  138. bool command; //< windows/mac key
  139. bool control;
  140. };
  141. bool meta; //< windows/mac key
  142. #endif
  143. bool operator==(const InputModifierState& rvalue) const {
  144. return ( (shift==rvalue.shift) && (alt==rvalue.alt) && (control==rvalue.control) && (meta==rvalue.meta));
  145. }
  146. };
  147. struct InputEventKey {
  148. InputModifierState mod;
  149. bool pressed; /// otherwise release
  150. uint32_t scancode; ///< check keyboard.h , KeyCode enum, without modifier masks
  151. uint32_t unicode; ///unicode
  152. uint32_t get_scancode_with_modifiers() const;
  153. bool echo; /// true if this is an echo key
  154. };
  155. struct InputEventMouse {
  156. InputModifierState mod;
  157. int button_mask;
  158. int x,y;
  159. int global_x,global_y;
  160. int pointer_index;
  161. };
  162. struct InputEventMouseButton : public InputEventMouse {
  163. int button_index;
  164. bool pressed; //otherwise released
  165. bool doubleclick; //last even less than doubleclick time
  166. };
  167. struct InputEventMouseMotion : public InputEventMouse {
  168. int relative_x,relative_y;
  169. float speed_x,speed_y;
  170. };
  171. struct InputEventJoystickMotion {
  172. int axis; ///< Joystick axis
  173. float axis_value; ///< -1 to 1
  174. };
  175. struct InputEventJoystickButton {
  176. int button_index;
  177. bool pressed;
  178. float pressure; //0 to 1
  179. };
  180. struct InputEventScreenTouch {
  181. int index;
  182. int x,y;
  183. bool pressed;
  184. };
  185. struct InputEventScreenDrag {
  186. int index;
  187. int x,y;
  188. int relative_x,relative_y;
  189. float speed_x,speed_y;
  190. };
  191. struct InputEventAction {
  192. int action;
  193. bool pressed;
  194. };
  195. struct InputEvent {
  196. enum Type {
  197. NONE,
  198. KEY,
  199. MOUSE_MOTION,
  200. MOUSE_BUTTON,
  201. JOYSTICK_MOTION,
  202. JOYSTICK_BUTTON,
  203. SCREEN_TOUCH,
  204. SCREEN_DRAG,
  205. ACTION,
  206. TYPE_MAX
  207. };
  208. uint32_t ID;
  209. int type;
  210. int device;
  211. union {
  212. InputEventMouseMotion mouse_motion;
  213. InputEventMouseButton mouse_button;
  214. InputEventJoystickMotion joy_motion;
  215. InputEventJoystickButton joy_button;
  216. InputEventKey key;
  217. InputEventScreenTouch screen_touch;
  218. InputEventScreenDrag screen_drag;
  219. InputEventAction action;
  220. };
  221. bool is_pressed() const;
  222. bool is_action(const String& p_action) const;
  223. bool is_echo() const;
  224. void set_as_action(const String& p_action, bool p_pressed);
  225. bool operator==(const InputEvent &p_event) const;
  226. operator String() const;
  227. InputEvent() { zeromem(this,sizeof(InputEvent)); }
  228. };
  229. #endif