input_event.h 15 KB

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