input_event.h 15 KB

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