input_event.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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 <reduzio@gmail.com>
  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. InputEvent();
  160. };
  161. class InputEventWithModifiers : public InputEvent {
  162. GDCLASS(InputEventWithModifiers, InputEvent)
  163. bool shift;
  164. bool alt;
  165. #ifdef APPLE_STYLE_KEYS
  166. union {
  167. bool command;
  168. bool meta; //< windows/mac key
  169. };
  170. bool control;
  171. #else
  172. union {
  173. bool command; //< windows/mac key
  174. bool control;
  175. };
  176. bool meta; //< windows/mac key
  177. #endif
  178. protected:
  179. static void _bind_methods();
  180. public:
  181. void set_shift(bool p_enabled);
  182. bool get_shift() const;
  183. void set_alt(bool p_enabled);
  184. bool get_alt() const;
  185. void set_control(bool p_enabled);
  186. bool get_control() const;
  187. void set_metakey(bool p_enabled);
  188. bool get_metakey() const;
  189. void set_command(bool p_enabled);
  190. bool get_command() const;
  191. void set_modifiers_from_event(const InputEventWithModifiers *event);
  192. InputEventWithModifiers();
  193. };
  194. class InputEventKey : public InputEventWithModifiers {
  195. GDCLASS(InputEventKey, InputEventWithModifiers)
  196. bool pressed; /// otherwise release
  197. uint32_t scancode; ///< check keyboard.h , KeyCode enum, without modifier masks
  198. uint32_t unicode; ///unicode
  199. bool echo; /// true if this is an echo key
  200. protected:
  201. static void _bind_methods();
  202. public:
  203. void set_pressed(bool p_pressed);
  204. virtual bool is_pressed() const;
  205. void set_scancode(uint32_t p_scancode);
  206. uint32_t get_scancode() const;
  207. void set_unicode(uint32_t p_unicode);
  208. uint32_t get_unicode() const;
  209. void set_echo(bool p_enable);
  210. virtual bool is_echo() const;
  211. uint32_t get_scancode_with_modifiers() const;
  212. virtual bool action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float p_deadzone) const;
  213. virtual bool shortcut_match(const Ref<InputEvent> &p_event) const;
  214. virtual bool is_action_type() const { return true; }
  215. virtual String as_text() const;
  216. InputEventKey();
  217. };
  218. class InputEventMouse : public InputEventWithModifiers {
  219. GDCLASS(InputEventMouse, InputEventWithModifiers)
  220. int button_mask;
  221. Vector2 pos;
  222. Vector2 global_pos;
  223. protected:
  224. static void _bind_methods();
  225. public:
  226. void set_button_mask(int p_mask);
  227. int get_button_mask() const;
  228. void set_position(const Vector2 &p_pos);
  229. Vector2 get_position() const;
  230. void set_global_position(const Vector2 &p_global_pos);
  231. Vector2 get_global_position() const;
  232. InputEventMouse();
  233. };
  234. class InputEventMouseButton : public InputEventMouse {
  235. GDCLASS(InputEventMouseButton, InputEventMouse)
  236. float factor;
  237. int button_index;
  238. bool pressed; //otherwise released
  239. bool doubleclick; //last even less than doubleclick time
  240. protected:
  241. static void _bind_methods();
  242. public:
  243. void set_factor(float p_factor);
  244. float get_factor();
  245. void set_button_index(int p_index);
  246. int get_button_index() const;
  247. void set_pressed(bool p_pressed);
  248. virtual bool is_pressed() const;
  249. void set_doubleclick(bool p_doubleclick);
  250. bool is_doubleclick() const;
  251. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const;
  252. virtual bool action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float p_deadzone) const;
  253. virtual bool is_action_type() const { return true; }
  254. virtual String as_text() const;
  255. InputEventMouseButton();
  256. };
  257. class InputEventMouseMotion : public InputEventMouse {
  258. GDCLASS(InputEventMouseMotion, InputEventMouse)
  259. Vector2 relative;
  260. Vector2 speed;
  261. protected:
  262. static void _bind_methods();
  263. public:
  264. void set_relative(const Vector2 &p_relative);
  265. Vector2 get_relative() const;
  266. void set_speed(const Vector2 &p_speed);
  267. Vector2 get_speed() const;
  268. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const;
  269. virtual String as_text() const;
  270. InputEventMouseMotion();
  271. };
  272. class InputEventJoypadMotion : public InputEvent {
  273. GDCLASS(InputEventJoypadMotion, InputEvent)
  274. int axis; ///< Joypad axis
  275. float axis_value; ///< -1 to 1
  276. protected:
  277. static void _bind_methods();
  278. public:
  279. void set_axis(int p_axis);
  280. int get_axis() const;
  281. void set_axis_value(float p_value);
  282. float get_axis_value() const;
  283. virtual bool is_pressed() const;
  284. virtual bool action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float p_deadzone) const;
  285. virtual bool is_action_type() const { return true; }
  286. virtual String as_text() const;
  287. InputEventJoypadMotion();
  288. };
  289. class InputEventJoypadButton : public InputEvent {
  290. GDCLASS(InputEventJoypadButton, InputEvent)
  291. int button_index;
  292. bool pressed;
  293. float pressure; //0 to 1
  294. protected:
  295. static void _bind_methods();
  296. public:
  297. void set_button_index(int p_index);
  298. int get_button_index() const;
  299. void set_pressed(bool p_pressed);
  300. virtual bool is_pressed() const;
  301. void set_pressure(float p_pressure);
  302. float get_pressure() const;
  303. virtual bool action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float p_deadzone) const;
  304. virtual bool is_action_type() const { return true; }
  305. virtual String as_text() const;
  306. InputEventJoypadButton();
  307. };
  308. class InputEventScreenTouch : public InputEvent {
  309. GDCLASS(InputEventScreenTouch, InputEvent)
  310. int index;
  311. Vector2 pos;
  312. bool pressed;
  313. protected:
  314. static void _bind_methods();
  315. public:
  316. void set_index(int p_index);
  317. int get_index() const;
  318. void set_position(const Vector2 &p_pos);
  319. Vector2 get_position() const;
  320. void set_pressed(bool p_pressed);
  321. virtual bool is_pressed() const;
  322. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const;
  323. virtual String as_text() const;
  324. InputEventScreenTouch();
  325. };
  326. class InputEventScreenDrag : public InputEvent {
  327. GDCLASS(InputEventScreenDrag, InputEvent)
  328. int index;
  329. Vector2 pos;
  330. Vector2 relative;
  331. Vector2 speed;
  332. protected:
  333. static void _bind_methods();
  334. public:
  335. void set_index(int p_index);
  336. int get_index() const;
  337. void set_position(const Vector2 &p_pos);
  338. Vector2 get_position() const;
  339. void set_relative(const Vector2 &p_relative);
  340. Vector2 get_relative() const;
  341. void set_speed(const Vector2 &p_speed);
  342. Vector2 get_speed() const;
  343. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const;
  344. virtual String as_text() const;
  345. InputEventScreenDrag();
  346. };
  347. class InputEventAction : public InputEvent {
  348. GDCLASS(InputEventAction, InputEvent)
  349. StringName action;
  350. bool pressed;
  351. protected:
  352. static void _bind_methods();
  353. public:
  354. void set_action(const StringName &p_action);
  355. StringName get_action() const;
  356. void set_pressed(bool p_pressed);
  357. virtual bool is_pressed() const;
  358. virtual bool is_action(const StringName &p_action) const;
  359. virtual bool action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float p_deadzone) const;
  360. virtual bool shortcut_match(const Ref<InputEvent> &p_event) const;
  361. virtual bool is_action_type() const { return true; }
  362. virtual String as_text() const;
  363. InputEventAction();
  364. };
  365. class InputEventGesture : public InputEventWithModifiers {
  366. GDCLASS(InputEventGesture, InputEventWithModifiers)
  367. Vector2 pos;
  368. protected:
  369. static void _bind_methods();
  370. public:
  371. void set_position(const Vector2 &p_pos);
  372. Vector2 get_position() const;
  373. };
  374. class InputEventMagnifyGesture : public InputEventGesture {
  375. GDCLASS(InputEventMagnifyGesture, InputEventGesture)
  376. real_t factor;
  377. protected:
  378. static void _bind_methods();
  379. public:
  380. void set_factor(real_t p_factor);
  381. real_t get_factor() const;
  382. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const;
  383. virtual String as_text() const;
  384. InputEventMagnifyGesture();
  385. };
  386. class InputEventPanGesture : public InputEventGesture {
  387. GDCLASS(InputEventPanGesture, InputEventGesture)
  388. Vector2 delta;
  389. protected:
  390. static void _bind_methods();
  391. public:
  392. void set_delta(const Vector2 &p_delta);
  393. Vector2 get_delta() const;
  394. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const;
  395. virtual String as_text() const;
  396. InputEventPanGesture();
  397. };
  398. class InputEventMIDI : public InputEvent {
  399. GDCLASS(InputEventMIDI, InputEvent)
  400. int channel;
  401. int message;
  402. int pitch;
  403. int velocity;
  404. int instrument;
  405. int pressure;
  406. int controller_number;
  407. int controller_value;
  408. protected:
  409. static void _bind_methods();
  410. public:
  411. void set_channel(const int p_channel);
  412. int get_channel() const;
  413. void set_message(const int p_message);
  414. int get_message() const;
  415. void set_pitch(const int p_pitch);
  416. int get_pitch() const;
  417. void set_velocity(const int p_velocity);
  418. int get_velocity() const;
  419. void set_instrument(const int p_instrument);
  420. int get_instrument() const;
  421. void set_pressure(const int p_pressure);
  422. int get_pressure() const;
  423. void set_controller_number(const int p_controller_number);
  424. int get_controller_number() const;
  425. void set_controller_value(const int p_controller_value);
  426. int get_controller_value() const;
  427. virtual String as_text() const;
  428. InputEventMIDI();
  429. };
  430. #endif