input_event.h 16 KB

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