input_event.h 16 KB

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