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