input_event.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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/io/resource.h"
  33. #include "core/math/transform_2d.h"
  34. #include "core/os/copymem.h"
  35. #include "core/string/ustring.h"
  36. #include "core/typedefs.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 JoyButtonList {
  58. JOY_BUTTON_INVALID = -1,
  59. JOY_BUTTON_A = 0,
  60. JOY_BUTTON_B = 1,
  61. JOY_BUTTON_X = 2,
  62. JOY_BUTTON_Y = 3,
  63. JOY_BUTTON_BACK = 4,
  64. JOY_BUTTON_GUIDE = 5,
  65. JOY_BUTTON_START = 6,
  66. JOY_BUTTON_LEFT_STICK = 7,
  67. JOY_BUTTON_RIGHT_STICK = 8,
  68. JOY_BUTTON_LEFT_SHOULDER = 9,
  69. JOY_BUTTON_RIGHT_SHOULDER = 10,
  70. JOY_BUTTON_DPAD_UP = 11,
  71. JOY_BUTTON_DPAD_DOWN = 12,
  72. JOY_BUTTON_DPAD_LEFT = 13,
  73. JOY_BUTTON_DPAD_RIGHT = 14,
  74. JOY_BUTTON_MISC1 = 15,
  75. JOY_BUTTON_PADDLE1 = 16,
  76. JOY_BUTTON_PADDLE2 = 17,
  77. JOY_BUTTON_PADDLE3 = 18,
  78. JOY_BUTTON_PADDLE4 = 19,
  79. JOY_BUTTON_TOUCHPAD = 20,
  80. JOY_BUTTON_SDL_MAX = 21,
  81. JOY_BUTTON_MAX = 36, // Android supports up to 36 buttons.
  82. };
  83. enum JoyAxisList {
  84. JOY_AXIS_INVALID = -1,
  85. JOY_AXIS_LEFT_X = 0,
  86. JOY_AXIS_LEFT_Y = 1,
  87. JOY_AXIS_RIGHT_X = 2,
  88. JOY_AXIS_RIGHT_Y = 3,
  89. JOY_AXIS_TRIGGER_LEFT = 4,
  90. JOY_AXIS_TRIGGER_RIGHT = 5,
  91. JOY_AXIS_SDL_MAX = 6,
  92. JOY_AXIS_MAX = 10, // OpenVR supports up to 5 Joysticks making a total of 10 axes.
  93. };
  94. enum MidiMessageList {
  95. MIDI_MESSAGE_NOTE_OFF = 0x8,
  96. MIDI_MESSAGE_NOTE_ON = 0x9,
  97. MIDI_MESSAGE_AFTERTOUCH = 0xA,
  98. MIDI_MESSAGE_CONTROL_CHANGE = 0xB,
  99. MIDI_MESSAGE_PROGRAM_CHANGE = 0xC,
  100. MIDI_MESSAGE_CHANNEL_PRESSURE = 0xD,
  101. MIDI_MESSAGE_PITCH_BEND = 0xE,
  102. };
  103. /**
  104. * Input Modifier Status
  105. * for keyboard/mouse events.
  106. */
  107. class InputEvent : public Resource {
  108. GDCLASS(InputEvent, Resource);
  109. int device = 0;
  110. protected:
  111. static void _bind_methods();
  112. public:
  113. static const int DEVICE_ID_TOUCH_MOUSE;
  114. static const int DEVICE_ID_INTERNAL;
  115. void set_device(int p_device);
  116. int get_device() const;
  117. bool is_action(const StringName &p_action, bool p_exact_match = false) const;
  118. bool is_action_pressed(const StringName &p_action, bool p_allow_echo = false, bool p_exact_match = false) const;
  119. bool is_action_released(const StringName &p_action, bool p_exact_match = false) const;
  120. float get_action_strength(const StringName &p_action, bool p_exact_match = false) const;
  121. float get_action_raw_strength(const StringName &p_action, bool p_exact_match = false) const;
  122. // To be removed someday, since they do not make sense for all events
  123. virtual bool is_pressed() const;
  124. virtual bool is_echo() const;
  125. virtual String as_text() const = 0;
  126. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const;
  127. virtual bool action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const;
  128. virtual bool shortcut_match(const Ref<InputEvent> &p_event) const;
  129. virtual bool is_action_type() const;
  130. virtual bool accumulate(const Ref<InputEvent> &p_event) { return false; }
  131. InputEvent() {}
  132. };
  133. class InputEventFromWindow : public InputEvent {
  134. GDCLASS(InputEventFromWindow, InputEvent);
  135. int64_t window_id = 0;
  136. protected:
  137. static void _bind_methods();
  138. public:
  139. void set_window_id(int64_t p_id);
  140. int64_t get_window_id() const;
  141. InputEventFromWindow() {}
  142. };
  143. class InputEventWithModifiers : public InputEventFromWindow {
  144. GDCLASS(InputEventWithModifiers, InputEventFromWindow);
  145. bool store_command = true;
  146. bool shift = false;
  147. bool alt = false;
  148. #ifdef APPLE_STYLE_KEYS
  149. union {
  150. bool command;
  151. bool meta = false; //< windows/mac key
  152. };
  153. bool control = false;
  154. #else
  155. union {
  156. bool command; //< windows/mac key
  157. bool control = false;
  158. };
  159. bool meta = false; //< windows/mac key
  160. #endif
  161. protected:
  162. static void _bind_methods();
  163. virtual void _validate_property(PropertyInfo &property) const override;
  164. public:
  165. void set_store_command(bool p_enabled);
  166. bool is_storing_command() const;
  167. void set_shift(bool p_enabled);
  168. bool get_shift() const;
  169. void set_alt(bool p_enabled);
  170. bool get_alt() const;
  171. void set_control(bool p_enabled);
  172. bool get_control() const;
  173. void set_metakey(bool p_enabled);
  174. bool get_metakey() const;
  175. void set_command(bool p_enabled);
  176. bool get_command() const;
  177. void set_modifiers_from_event(const InputEventWithModifiers *event);
  178. virtual String as_text() const override;
  179. virtual String to_string() override;
  180. InputEventWithModifiers() {}
  181. };
  182. class InputEventKey : public InputEventWithModifiers {
  183. GDCLASS(InputEventKey, InputEventWithModifiers);
  184. bool pressed = false; /// otherwise release
  185. uint32_t keycode = 0; ///< check keyboard.h , KeyCode enum, without modifier masks
  186. uint32_t physical_keycode = 0;
  187. uint32_t unicode = 0; ///unicode
  188. bool echo = false; /// true if this is an echo key
  189. protected:
  190. static void _bind_methods();
  191. public:
  192. void set_pressed(bool p_pressed);
  193. virtual bool is_pressed() const override;
  194. void set_keycode(uint32_t p_keycode);
  195. uint32_t get_keycode() const;
  196. void set_physical_keycode(uint32_t p_keycode);
  197. uint32_t get_physical_keycode() const;
  198. void set_unicode(uint32_t p_unicode);
  199. uint32_t get_unicode() const;
  200. void set_echo(bool p_enable);
  201. virtual bool is_echo() const override;
  202. uint32_t get_keycode_with_modifiers() const;
  203. uint32_t get_physical_keycode_with_modifiers() const;
  204. virtual bool action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const override;
  205. virtual bool shortcut_match(const Ref<InputEvent> &p_event) const override;
  206. virtual bool is_action_type() const override { return true; }
  207. virtual String as_text() const override;
  208. virtual String to_string() override;
  209. static Ref<InputEventKey> create_reference(uint32_t p_keycode_with_modifier_masks);
  210. InputEventKey() {}
  211. };
  212. class InputEventMouse : public InputEventWithModifiers {
  213. GDCLASS(InputEventMouse, InputEventWithModifiers);
  214. int button_mask = 0;
  215. Vector2 pos;
  216. Vector2 global_pos;
  217. protected:
  218. static void _bind_methods();
  219. public:
  220. void set_button_mask(int p_mask);
  221. int get_button_mask() const;
  222. void set_position(const Vector2 &p_pos);
  223. Vector2 get_position() const;
  224. void set_global_position(const Vector2 &p_global_pos);
  225. Vector2 get_global_position() const;
  226. InputEventMouse() {}
  227. };
  228. class InputEventMouseButton : public InputEventMouse {
  229. GDCLASS(InputEventMouseButton, InputEventMouse);
  230. float factor = 1;
  231. int button_index = 0;
  232. bool pressed = false; //otherwise released
  233. bool doubleclick = false; //last even less than doubleclick time
  234. protected:
  235. static void _bind_methods();
  236. public:
  237. void set_factor(float p_factor);
  238. float get_factor() const;
  239. void set_button_index(int p_index);
  240. int get_button_index() const;
  241. void set_pressed(bool p_pressed);
  242. virtual bool is_pressed() const override;
  243. void set_doubleclick(bool p_doubleclick);
  244. bool is_doubleclick() const;
  245. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  246. virtual bool action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const override;
  247. virtual bool is_action_type() const override { return true; }
  248. virtual String as_text() const override;
  249. virtual String to_string() override;
  250. InputEventMouseButton() {}
  251. };
  252. class InputEventMouseMotion : public InputEventMouse {
  253. GDCLASS(InputEventMouseMotion, InputEventMouse);
  254. Vector2 tilt;
  255. float pressure = 0;
  256. Vector2 relative;
  257. Vector2 speed;
  258. protected:
  259. static void _bind_methods();
  260. public:
  261. void set_tilt(const Vector2 &p_tilt);
  262. Vector2 get_tilt() const;
  263. void set_pressure(float p_pressure);
  264. float get_pressure() const;
  265. void set_relative(const Vector2 &p_relative);
  266. Vector2 get_relative() const;
  267. void set_speed(const Vector2 &p_speed);
  268. Vector2 get_speed() const;
  269. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  270. virtual String as_text() const override;
  271. virtual String to_string() override;
  272. virtual bool accumulate(const Ref<InputEvent> &p_event) override;
  273. InputEventMouseMotion() {}
  274. };
  275. class InputEventJoypadMotion : public InputEvent {
  276. GDCLASS(InputEventJoypadMotion, InputEvent);
  277. int axis = 0; ///< Joypad axis
  278. float axis_value = 0; ///< -1 to 1
  279. protected:
  280. static void _bind_methods();
  281. public:
  282. void set_axis(int p_axis);
  283. int get_axis() const;
  284. void set_axis_value(float p_value);
  285. float get_axis_value() const;
  286. virtual bool is_pressed() const override;
  287. virtual bool action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const override;
  288. virtual bool is_action_type() const override { return true; }
  289. virtual String as_text() const override;
  290. virtual String to_string() override;
  291. InputEventJoypadMotion() {}
  292. };
  293. class InputEventJoypadButton : public InputEvent {
  294. GDCLASS(InputEventJoypadButton, InputEvent);
  295. int button_index = 0;
  296. bool pressed = false;
  297. float pressure = 0; //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 override;
  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_raw_strength, float p_deadzone) const override;
  308. virtual bool shortcut_match(const Ref<InputEvent> &p_event) const override;
  309. virtual bool is_action_type() const override { return true; }
  310. virtual String as_text() const override;
  311. virtual String to_string() override;
  312. static Ref<InputEventJoypadButton> create_reference(int p_btn_index);
  313. InputEventJoypadButton() {}
  314. };
  315. class InputEventScreenTouch : public InputEventFromWindow {
  316. GDCLASS(InputEventScreenTouch, InputEventFromWindow);
  317. int index = 0;
  318. Vector2 pos;
  319. bool pressed = false;
  320. protected:
  321. static void _bind_methods();
  322. public:
  323. void set_index(int p_index);
  324. int get_index() const;
  325. void set_position(const Vector2 &p_pos);
  326. Vector2 get_position() const;
  327. void set_pressed(bool p_pressed);
  328. virtual bool is_pressed() const override;
  329. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  330. virtual String as_text() const override;
  331. virtual String to_string() override;
  332. InputEventScreenTouch() {}
  333. };
  334. class InputEventScreenDrag : public InputEventFromWindow {
  335. GDCLASS(InputEventScreenDrag, InputEventFromWindow);
  336. int index = 0;
  337. Vector2 pos;
  338. Vector2 relative;
  339. Vector2 speed;
  340. protected:
  341. static void _bind_methods();
  342. public:
  343. void set_index(int p_index);
  344. int get_index() const;
  345. void set_position(const Vector2 &p_pos);
  346. Vector2 get_position() const;
  347. void set_relative(const Vector2 &p_relative);
  348. Vector2 get_relative() const;
  349. void set_speed(const Vector2 &p_speed);
  350. Vector2 get_speed() const;
  351. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  352. virtual String as_text() const override;
  353. virtual String to_string() override;
  354. InputEventScreenDrag() {}
  355. };
  356. class InputEventAction : public InputEvent {
  357. GDCLASS(InputEventAction, InputEvent);
  358. StringName action;
  359. bool pressed = false;
  360. float strength = 1.0f;
  361. protected:
  362. static void _bind_methods();
  363. public:
  364. void set_action(const StringName &p_action);
  365. StringName get_action() const;
  366. void set_pressed(bool p_pressed);
  367. virtual bool is_pressed() const override;
  368. void set_strength(float p_strength);
  369. float get_strength() const;
  370. virtual bool is_action(const StringName &p_action) const;
  371. virtual bool action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const override;
  372. virtual bool shortcut_match(const Ref<InputEvent> &p_event) const override;
  373. virtual bool is_action_type() const override { return true; }
  374. virtual String as_text() const override;
  375. virtual String to_string() override;
  376. InputEventAction() {}
  377. };
  378. class InputEventGesture : public InputEventWithModifiers {
  379. GDCLASS(InputEventGesture, InputEventWithModifiers);
  380. Vector2 pos;
  381. protected:
  382. static void _bind_methods();
  383. public:
  384. void set_position(const Vector2 &p_pos);
  385. Vector2 get_position() const;
  386. };
  387. class InputEventMagnifyGesture : public InputEventGesture {
  388. GDCLASS(InputEventMagnifyGesture, InputEventGesture);
  389. real_t factor = 1.0;
  390. protected:
  391. static void _bind_methods();
  392. public:
  393. void set_factor(real_t p_factor);
  394. real_t get_factor() const;
  395. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  396. virtual String as_text() const override;
  397. virtual String to_string() override;
  398. InputEventMagnifyGesture() {}
  399. };
  400. class InputEventPanGesture : public InputEventGesture {
  401. GDCLASS(InputEventPanGesture, InputEventGesture);
  402. Vector2 delta;
  403. protected:
  404. static void _bind_methods();
  405. public:
  406. void set_delta(const Vector2 &p_delta);
  407. Vector2 get_delta() const;
  408. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  409. virtual String as_text() const override;
  410. virtual String to_string() override;
  411. InputEventPanGesture() {}
  412. };
  413. class InputEventMIDI : public InputEvent {
  414. GDCLASS(InputEventMIDI, InputEvent);
  415. int channel = 0;
  416. int message = 0;
  417. int pitch = 0;
  418. int velocity = 0;
  419. int instrument = 0;
  420. int pressure = 0;
  421. int controller_number = 0;
  422. int controller_value = 0;
  423. protected:
  424. static void _bind_methods();
  425. public:
  426. void set_channel(const int p_channel);
  427. int get_channel() const;
  428. void set_message(const int p_message);
  429. int get_message() const;
  430. void set_pitch(const int p_pitch);
  431. int get_pitch() const;
  432. void set_velocity(const int p_velocity);
  433. int get_velocity() const;
  434. void set_instrument(const int p_instrument);
  435. int get_instrument() const;
  436. void set_pressure(const int p_pressure);
  437. int get_pressure() const;
  438. void set_controller_number(const int p_controller_number);
  439. int get_controller_number() const;
  440. void set_controller_value(const int p_controller_value);
  441. int get_controller_value() const;
  442. virtual String as_text() const override;
  443. virtual String to_string() override;
  444. InputEventMIDI() {}
  445. };
  446. #endif // INPUT_EVENT_H