InputEvent.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #ifndef INPUTEVENT_H
  2. #define INPUTEVENT_H
  3. #include <cstdint>
  4. #include <memory.h>
  5. #include "String.hpp"
  6. namespace godot {
  7. enum {
  8. BUTTON_LEFT=1,
  9. BUTTON_RIGHT=2,
  10. BUTTON_MIDDLE=3,
  11. BUTTON_WHEEL_UP=4,
  12. BUTTON_WHEEL_DOWN=5,
  13. BUTTON_WHEEL_LEFT=6,
  14. BUTTON_WHEEL_RIGHT=7,
  15. BUTTON_MASK_LEFT=(1<<(BUTTON_LEFT-1)),
  16. BUTTON_MASK_RIGHT=(1<<(BUTTON_RIGHT-1)),
  17. BUTTON_MASK_MIDDLE=(1<<(BUTTON_MIDDLE-1)),
  18. };
  19. enum {
  20. JOY_BUTTON_0 = 0,
  21. JOY_BUTTON_1 = 1,
  22. JOY_BUTTON_2 = 2,
  23. JOY_BUTTON_3 = 3,
  24. JOY_BUTTON_4 = 4,
  25. JOY_BUTTON_5 = 5,
  26. JOY_BUTTON_6 = 6,
  27. JOY_BUTTON_7 = 7,
  28. JOY_BUTTON_8 = 8,
  29. JOY_BUTTON_9 = 9,
  30. JOY_BUTTON_10 = 10,
  31. JOY_BUTTON_11 = 11,
  32. JOY_BUTTON_12 = 12,
  33. JOY_BUTTON_13 = 13,
  34. JOY_BUTTON_14 = 14,
  35. JOY_BUTTON_15 = 15,
  36. JOY_BUTTON_MAX = 16,
  37. JOY_L = JOY_BUTTON_4,
  38. JOY_R = JOY_BUTTON_5,
  39. JOY_L2 = JOY_BUTTON_6,
  40. JOY_R2 = JOY_BUTTON_7,
  41. JOY_L3 = JOY_BUTTON_8,
  42. JOY_R3 = JOY_BUTTON_9,
  43. JOY_SELECT = JOY_BUTTON_10,
  44. JOY_START = JOY_BUTTON_11,
  45. JOY_DPAD_UP = JOY_BUTTON_12,
  46. JOY_DPAD_DOWN = JOY_BUTTON_13,
  47. JOY_DPAD_LEFT = JOY_BUTTON_14,
  48. JOY_DPAD_RIGHT = JOY_BUTTON_15,
  49. // a little history about game controllers (who copied who)
  50. JOY_SNES_B = JOY_BUTTON_0,
  51. JOY_SNES_A = JOY_BUTTON_1,
  52. JOY_SNES_Y = JOY_BUTTON_2,
  53. JOY_SNES_X = JOY_BUTTON_3,
  54. JOY_SONY_CIRCLE=JOY_SNES_A,
  55. JOY_SONY_X=JOY_SNES_B,
  56. JOY_SONY_SQUARE=JOY_SNES_Y,
  57. JOY_SONY_TRIANGLE=JOY_SNES_X,
  58. JOY_SEGA_B=JOY_SNES_A,
  59. JOY_SEGA_A=JOY_SNES_B,
  60. JOY_SEGA_X=JOY_SNES_Y,
  61. JOY_SEGA_Y=JOY_SNES_X,
  62. JOY_XBOX_B=JOY_SEGA_B,
  63. JOY_XBOX_A=JOY_SEGA_A,
  64. JOY_XBOX_X=JOY_SEGA_X,
  65. JOY_XBOX_Y=JOY_SEGA_Y,
  66. JOY_DS_A = JOY_SNES_A,
  67. JOY_DS_B = JOY_SNES_B,
  68. JOY_DS_X = JOY_SNES_X,
  69. JOY_DS_Y = JOY_SNES_Y,
  70. JOY_WII_C = JOY_BUTTON_5,
  71. JOY_WII_Z = JOY_BUTTON_6,
  72. JOY_WII_MINUS = JOY_BUTTON_9,
  73. JOY_WII_PLUS = JOY_BUTTON_10,
  74. // end of history
  75. JOY_AXIS_0=0,
  76. JOY_AXIS_1=1,
  77. JOY_AXIS_2=2,
  78. JOY_AXIS_3=3,
  79. JOY_AXIS_4=4,
  80. JOY_AXIS_5=5,
  81. JOY_AXIS_6=6,
  82. JOY_AXIS_7=7,
  83. JOY_AXIS_MAX=8,
  84. JOY_ANALOG_0_X = JOY_AXIS_0,
  85. JOY_ANALOG_0_Y = JOY_AXIS_1,
  86. JOY_ANALOG_1_X = JOY_AXIS_2,
  87. JOY_ANALOG_1_Y = JOY_AXIS_3,
  88. JOY_ANALOG_2_X = JOY_AXIS_4,
  89. JOY_ANALOG_2_Y = JOY_AXIS_5,
  90. JOY_ANALOG_L2 = JOY_AXIS_6,
  91. JOY_ANALOG_R2 = JOY_AXIS_7,
  92. };
  93. /**
  94. * Input Modifier Status
  95. * for keyboard/mouse events.
  96. */
  97. struct InputModifierState {
  98. bool shift;
  99. bool alt;
  100. #ifdef APPLE_STYLE_KEYS
  101. union {
  102. bool command;
  103. bool meta; //< windows/mac key
  104. };
  105. bool control;
  106. #else
  107. union {
  108. bool command; //< windows/mac key
  109. bool control;
  110. };
  111. bool meta; //< windows/mac key
  112. #endif
  113. inline bool operator==(const InputModifierState& rvalue) const {
  114. return ( (shift==rvalue.shift) && (alt==rvalue.alt) && (control==rvalue.control) && (meta==rvalue.meta));
  115. }
  116. };
  117. struct InputEventKey {
  118. InputModifierState mod;
  119. bool pressed; /// otherwise release
  120. uint32_t scancode; ///< check keyboard.h , KeyCode enum, without modifier masks
  121. uint32_t unicode; ///unicode
  122. bool echo; /// true if this is an echo key
  123. };
  124. struct InputEventMouse {
  125. InputModifierState mod;
  126. int button_mask;
  127. float x,y;
  128. float global_x,global_y;
  129. int pointer_index;
  130. };
  131. struct InputEventMouseButton : public InputEventMouse {
  132. int button_index;
  133. bool pressed; //otherwise released
  134. bool doubleclick; //last even less than doubleclick time
  135. };
  136. struct InputEventMouseMotion : public InputEventMouse {
  137. float relative_x,relative_y;
  138. float speed_x,speed_y;
  139. };
  140. struct InputEventJoypadMotion {
  141. int axis; ///< Joypad axis
  142. float axis_value; ///< -1 to 1
  143. };
  144. struct InputEventJoypadButton {
  145. int button_index;
  146. bool pressed;
  147. float pressure; //0 to 1
  148. };
  149. struct InputEventScreenTouch {
  150. int index;
  151. float x,y;
  152. bool pressed;
  153. };
  154. struct InputEventScreenDrag {
  155. int index;
  156. float x,y;
  157. float relative_x,relative_y;
  158. float speed_x,speed_y;
  159. };
  160. struct InputEventAction {
  161. int action;
  162. bool pressed;
  163. };
  164. class Transform2D;
  165. struct InputEvent {
  166. enum Type {
  167. NONE,
  168. KEY,
  169. MOUSE_MOTION,
  170. MOUSE_BUTTON,
  171. JOYPAD_MOTION,
  172. JOYPAD_BUTTON,
  173. SCREEN_TOUCH,
  174. SCREEN_DRAG,
  175. ACTION,
  176. TYPE_MAX
  177. };
  178. uint32_t ID;
  179. int type;
  180. int device;
  181. union {
  182. InputEventMouseMotion mouse_motion;
  183. InputEventMouseButton mouse_button;
  184. InputEventJoypadMotion joy_motion;
  185. InputEventJoypadButton joy_button;
  186. InputEventKey key;
  187. InputEventScreenTouch screen_touch;
  188. InputEventScreenDrag screen_drag;
  189. InputEventAction action;
  190. };
  191. bool is_pressed() const;
  192. bool is_action(const String& p_action) const;
  193. bool is_action_pressed(const String& p_action) const;
  194. bool is_action_released(const String& p_action) const;
  195. bool is_echo() const;
  196. void set_as_action(const String& p_action, bool p_pressed);
  197. InputEvent xform_by(const Transform2D& p_xform) const;
  198. bool operator==(const InputEvent &p_event) const;
  199. operator String() const;
  200. inline InputEvent() { memset(this,0,sizeof(InputEvent)); }
  201. };
  202. }
  203. #endif // INPUTEVENT_H