input_event.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. /*************************************************************************/
  2. /* input_event.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 "core/math/transform_2d.h"
  33. #include "core/os/copymem.h"
  34. #include "core/resource.h"
  35. #include "core/typedefs.h"
  36. #include "core/ustring.h"
  37. /**
  38. @author Juan Linietsky <[email protected]>
  39. */
  40. /**
  41. * Input Event classes. These are used in the main loop.
  42. * The events are pretty obvious.
  43. */
  44. enum ButtonList {
  45. BUTTON_LEFT = 1,
  46. BUTTON_RIGHT = 2,
  47. BUTTON_MIDDLE = 3,
  48. BUTTON_WHEEL_UP = 4,
  49. BUTTON_WHEEL_DOWN = 5,
  50. BUTTON_WHEEL_LEFT = 6,
  51. BUTTON_WHEEL_RIGHT = 7,
  52. BUTTON_XBUTTON1 = 8,
  53. BUTTON_XBUTTON2 = 9,
  54. BUTTON_MASK_LEFT = (1 << (BUTTON_LEFT - 1)),
  55. BUTTON_MASK_RIGHT = (1 << (BUTTON_RIGHT - 1)),
  56. BUTTON_MASK_MIDDLE = (1 << (BUTTON_MIDDLE - 1)),
  57. BUTTON_MASK_XBUTTON1 = (1 << (BUTTON_XBUTTON1 - 1)),
  58. BUTTON_MASK_XBUTTON2 = (1 << (BUTTON_XBUTTON2 - 1))
  59. };
  60. enum JoystickList {
  61. JOY_BUTTON_0 = 0,
  62. JOY_BUTTON_1 = 1,
  63. JOY_BUTTON_2 = 2,
  64. JOY_BUTTON_3 = 3,
  65. JOY_BUTTON_4 = 4,
  66. JOY_BUTTON_5 = 5,
  67. JOY_BUTTON_6 = 6,
  68. JOY_BUTTON_7 = 7,
  69. JOY_BUTTON_8 = 8,
  70. JOY_BUTTON_9 = 9,
  71. JOY_BUTTON_10 = 10,
  72. JOY_BUTTON_11 = 11,
  73. JOY_BUTTON_12 = 12,
  74. JOY_BUTTON_13 = 13,
  75. JOY_BUTTON_14 = 14,
  76. JOY_BUTTON_15 = 15,
  77. JOY_BUTTON_MAX = 16,
  78. JOY_L = JOY_BUTTON_4,
  79. JOY_R = JOY_BUTTON_5,
  80. JOY_L2 = JOY_BUTTON_6,
  81. JOY_R2 = JOY_BUTTON_7,
  82. JOY_L3 = JOY_BUTTON_8,
  83. JOY_R3 = JOY_BUTTON_9,
  84. JOY_SELECT = JOY_BUTTON_10,
  85. JOY_START = JOY_BUTTON_11,
  86. JOY_DPAD_UP = JOY_BUTTON_12,
  87. JOY_DPAD_DOWN = JOY_BUTTON_13,
  88. JOY_DPAD_LEFT = JOY_BUTTON_14,
  89. JOY_DPAD_RIGHT = JOY_BUTTON_15,
  90. JOY_SONY_CIRCLE = JOY_BUTTON_1,
  91. JOY_SONY_X = JOY_BUTTON_0,
  92. JOY_SONY_SQUARE = JOY_BUTTON_2,
  93. JOY_SONY_TRIANGLE = JOY_BUTTON_3,
  94. JOY_XBOX_A = JOY_BUTTON_0,
  95. JOY_XBOX_B = JOY_BUTTON_1,
  96. JOY_XBOX_X = JOY_BUTTON_2,
  97. JOY_XBOX_Y = JOY_BUTTON_3,
  98. JOY_DS_A = JOY_BUTTON_1,
  99. JOY_DS_B = JOY_BUTTON_0,
  100. JOY_DS_X = JOY_BUTTON_3,
  101. JOY_DS_Y = JOY_BUTTON_2,
  102. JOY_WII_C = JOY_BUTTON_5,
  103. JOY_WII_Z = JOY_BUTTON_6,
  104. JOY_WII_MINUS = JOY_BUTTON_10,
  105. JOY_WII_PLUS = JOY_BUTTON_11,
  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_8 = 8,
  116. JOY_AXIS_9 = 9,
  117. JOY_AXIS_MAX = 10,
  118. JOY_ANALOG_LX = JOY_AXIS_0,
  119. JOY_ANALOG_LY = JOY_AXIS_1,
  120. JOY_ANALOG_RX = JOY_AXIS_2,
  121. JOY_ANALOG_RY = JOY_AXIS_3,
  122. JOY_ANALOG_L2 = JOY_AXIS_6,
  123. JOY_ANALOG_R2 = JOY_AXIS_7,
  124. };
  125. enum MidiMessageList {
  126. MIDI_MESSAGE_NOTE_OFF = 0x8,
  127. MIDI_MESSAGE_NOTE_ON = 0x9,
  128. MIDI_MESSAGE_AFTERTOUCH = 0xA,
  129. MIDI_MESSAGE_CONTROL_CHANGE = 0xB,
  130. MIDI_MESSAGE_PROGRAM_CHANGE = 0xC,
  131. MIDI_MESSAGE_CHANNEL_PRESSURE = 0xD,
  132. MIDI_MESSAGE_PITCH_BEND = 0xE,
  133. };
  134. /**
  135. * Input Modifier Status
  136. * for keyboard/mouse events.
  137. */
  138. class InputEvent : public Resource {
  139. GDCLASS(InputEvent, Resource)
  140. int device;
  141. protected:
  142. static void _bind_methods();
  143. public:
  144. void set_device(int p_device);
  145. int get_device() const;
  146. bool is_action(const StringName &p_action) const;
  147. bool is_action_pressed(const StringName &p_action) const;
  148. bool is_action_released(const StringName &p_action) const;
  149. float get_action_strength(const StringName &p_action) const;
  150. // To be removed someday, since they do not make sense for all events
  151. virtual bool is_pressed() const;
  152. virtual bool is_echo() const;
  153. // ...-.
  154. virtual String as_text() const;
  155. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const;
  156. virtual bool action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float p_deadzone) const;
  157. virtual bool shortcut_match(const Ref<InputEvent> &p_event) const;
  158. virtual bool is_action_type() const;
  159. virtual bool accumulate(const Ref<InputEvent> &p_event) { return false; }
  160. InputEvent();
  161. };
  162. class InputEventWithModifiers : public InputEvent {
  163. GDCLASS(InputEventWithModifiers, InputEvent)
  164. bool shift;
  165. bool alt;
  166. #ifdef APPLE_STYLE_KEYS
  167. union {
  168. bool command;
  169. bool meta; //< windows/mac key
  170. };
  171. bool control;
  172. #else
  173. union {
  174. bool command; //< windows/mac key
  175. bool control;
  176. };
  177. bool meta; //< windows/mac key
  178. #endif
  179. protected:
  180. static void _bind_methods();
  181. public:
  182. void set_shift(bool p_enabled);
  183. bool get_shift() const;
  184. void set_alt(bool p_enabled);
  185. bool get_alt() const;
  186. void set_control(bool p_enabled);
  187. bool get_control() const;
  188. void set_metakey(bool p_enabled);
  189. bool get_metakey() const;
  190. void set_command(bool p_enabled);
  191. bool get_command() const;
  192. void set_modifiers_from_event(const InputEventWithModifiers *event);
  193. InputEventWithModifiers();
  194. };
  195. class InputEventKey : public InputEventWithModifiers {
  196. GDCLASS(InputEventKey, InputEventWithModifiers)
  197. bool pressed; /// otherwise release
  198. uint32_t scancode; ///< check keyboard.h , KeyCode enum, without modifier masks
  199. uint32_t unicode; ///unicode
  200. bool echo; /// true if this is an echo key
  201. protected:
  202. static void _bind_methods();
  203. public:
  204. void set_pressed(bool p_pressed);
  205. virtual bool is_pressed() const;
  206. void set_scancode(uint32_t p_scancode);
  207. uint32_t get_scancode() const;
  208. void set_unicode(uint32_t p_unicode);
  209. uint32_t get_unicode() const;
  210. void set_echo(bool p_enable);
  211. virtual bool is_echo() const;
  212. uint32_t get_scancode_with_modifiers() const;
  213. virtual bool action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float p_deadzone) const;
  214. virtual bool shortcut_match(const Ref<InputEvent> &p_event) const;
  215. virtual bool is_action_type() const { return true; }
  216. virtual String as_text() const;
  217. InputEventKey();
  218. };
  219. class InputEventMouse : public InputEventWithModifiers {
  220. GDCLASS(InputEventMouse, InputEventWithModifiers)
  221. int button_mask;
  222. Vector2 pos;
  223. Vector2 global_pos;
  224. protected:
  225. static void _bind_methods();
  226. public:
  227. void set_button_mask(int p_mask);
  228. int get_button_mask() const;
  229. void set_position(const Vector2 &p_pos);
  230. Vector2 get_position() const;
  231. void set_global_position(const Vector2 &p_global_pos);
  232. Vector2 get_global_position() const;
  233. InputEventMouse();
  234. };
  235. class InputEventMouseButton : public InputEventMouse {
  236. GDCLASS(InputEventMouseButton, InputEventMouse)
  237. float factor;
  238. int button_index;
  239. bool pressed; //otherwise released
  240. bool doubleclick; //last even less than doubleclick time
  241. protected:
  242. static void _bind_methods();
  243. public:
  244. void set_factor(float p_factor);
  245. float get_factor();
  246. void set_button_index(int p_index);
  247. int get_button_index() const;
  248. void set_pressed(bool p_pressed);
  249. virtual bool is_pressed() const;
  250. void set_doubleclick(bool p_doubleclick);
  251. bool is_doubleclick() const;
  252. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const;
  253. virtual bool action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float p_deadzone) const;
  254. virtual bool is_action_type() const { return true; }
  255. virtual String as_text() const;
  256. InputEventMouseButton();
  257. };
  258. class InputEventMouseMotion : public InputEventMouse {
  259. GDCLASS(InputEventMouseMotion, InputEventMouse)
  260. Vector2 relative;
  261. Vector2 speed;
  262. protected:
  263. static void _bind_methods();
  264. public:
  265. void set_relative(const Vector2 &p_relative);
  266. Vector2 get_relative() const;
  267. void set_speed(const Vector2 &p_speed);
  268. Vector2 get_speed() const;
  269. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const;
  270. virtual String as_text() const;
  271. virtual bool accumulate(const Ref<InputEvent> &p_event);
  272. InputEventMouseMotion();
  273. };
  274. class InputEventJoypadMotion : public InputEvent {
  275. GDCLASS(InputEventJoypadMotion, InputEvent)
  276. int axis; ///< Joypad axis
  277. float axis_value; ///< -1 to 1
  278. protected:
  279. static void _bind_methods();
  280. public:
  281. void set_axis(int p_axis);
  282. int get_axis() const;
  283. void set_axis_value(float p_value);
  284. float get_axis_value() const;
  285. virtual bool is_pressed() const;
  286. virtual bool action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float p_deadzone) const;
  287. virtual bool is_action_type() const { return true; }
  288. virtual String as_text() const;
  289. InputEventJoypadMotion();
  290. };
  291. class InputEventJoypadButton : public InputEvent {
  292. GDCLASS(InputEventJoypadButton, InputEvent)
  293. int button_index;
  294. bool pressed;
  295. float pressure; //0 to 1
  296. protected:
  297. static void _bind_methods();
  298. public:
  299. void set_button_index(int p_index);
  300. int get_button_index() const;
  301. void set_pressed(bool p_pressed);
  302. virtual bool is_pressed() const;
  303. void set_pressure(float p_pressure);
  304. float get_pressure() const;
  305. virtual bool action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float p_deadzone) const;
  306. virtual bool shortcut_match(const Ref<InputEvent> &p_event) const;
  307. virtual bool is_action_type() const { return true; }
  308. virtual String as_text() const;
  309. InputEventJoypadButton();
  310. };
  311. class InputEventScreenTouch : public InputEvent {
  312. GDCLASS(InputEventScreenTouch, InputEvent)
  313. int index;
  314. Vector2 pos;
  315. bool pressed;
  316. protected:
  317. static void _bind_methods();
  318. public:
  319. void set_index(int p_index);
  320. int get_index() const;
  321. void set_position(const Vector2 &p_pos);
  322. Vector2 get_position() const;
  323. void set_pressed(bool p_pressed);
  324. virtual bool is_pressed() const;
  325. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const;
  326. virtual String as_text() const;
  327. InputEventScreenTouch();
  328. };
  329. class InputEventScreenDrag : public InputEvent {
  330. GDCLASS(InputEventScreenDrag, InputEvent)
  331. int index;
  332. Vector2 pos;
  333. Vector2 relative;
  334. Vector2 speed;
  335. protected:
  336. static void _bind_methods();
  337. public:
  338. void set_index(int p_index);
  339. int get_index() const;
  340. void set_position(const Vector2 &p_pos);
  341. Vector2 get_position() const;
  342. void set_relative(const Vector2 &p_relative);
  343. Vector2 get_relative() const;
  344. void set_speed(const Vector2 &p_speed);
  345. Vector2 get_speed() const;
  346. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const;
  347. virtual String as_text() const;
  348. InputEventScreenDrag();
  349. };
  350. class InputEventAction : public InputEvent {
  351. GDCLASS(InputEventAction, InputEvent)
  352. StringName action;
  353. bool pressed;
  354. protected:
  355. static void _bind_methods();
  356. public:
  357. void set_action(const StringName &p_action);
  358. StringName get_action() const;
  359. void set_pressed(bool p_pressed);
  360. virtual bool is_pressed() const;
  361. virtual bool is_action(const StringName &p_action) const;
  362. virtual bool action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float p_deadzone) const;
  363. virtual bool shortcut_match(const Ref<InputEvent> &p_event) const;
  364. virtual bool is_action_type() const { return true; }
  365. virtual String as_text() const;
  366. InputEventAction();
  367. };
  368. class InputEventGesture : public InputEventWithModifiers {
  369. GDCLASS(InputEventGesture, InputEventWithModifiers)
  370. Vector2 pos;
  371. protected:
  372. static void _bind_methods();
  373. public:
  374. void set_position(const Vector2 &p_pos);
  375. Vector2 get_position() const;
  376. };
  377. class InputEventMagnifyGesture : public InputEventGesture {
  378. GDCLASS(InputEventMagnifyGesture, InputEventGesture)
  379. real_t factor;
  380. protected:
  381. static void _bind_methods();
  382. public:
  383. void set_factor(real_t p_factor);
  384. real_t get_factor() const;
  385. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const;
  386. virtual String as_text() const;
  387. InputEventMagnifyGesture();
  388. };
  389. class InputEventPanGesture : public InputEventGesture {
  390. GDCLASS(InputEventPanGesture, InputEventGesture)
  391. Vector2 delta;
  392. protected:
  393. static void _bind_methods();
  394. public:
  395. void set_delta(const Vector2 &p_delta);
  396. Vector2 get_delta() const;
  397. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const;
  398. virtual String as_text() const;
  399. InputEventPanGesture();
  400. };
  401. class InputEventMIDI : public InputEvent {
  402. GDCLASS(InputEventMIDI, InputEvent)
  403. int channel;
  404. int message;
  405. int pitch;
  406. int velocity;
  407. int instrument;
  408. int pressure;
  409. int controller_number;
  410. int controller_value;
  411. protected:
  412. static void _bind_methods();
  413. public:
  414. void set_channel(const int p_channel);
  415. int get_channel() const;
  416. void set_message(const int p_message);
  417. int get_message() const;
  418. void set_pitch(const int p_pitch);
  419. int get_pitch() const;
  420. void set_velocity(const int p_velocity);
  421. int get_velocity() const;
  422. void set_instrument(const int p_instrument);
  423. int get_instrument() const;
  424. void set_pressure(const int p_pressure);
  425. int get_pressure() const;
  426. void set_controller_number(const int p_controller_number);
  427. int get_controller_number() const;
  428. void set_controller_value(const int p_controller_value);
  429. int get_controller_value() const;
  430. virtual String as_text() const;
  431. InputEventMIDI();
  432. };
  433. #endif