InputEvent.hpp 4.9 KB

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