input_event.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. /**************************************************************************/
  2. /* input_event.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. #pragma once
  31. #include "core/input/input_enums.h"
  32. #include "core/io/resource.h"
  33. #include "core/math/transform_2d.h"
  34. #include "core/os/keyboard.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. class Shortcut;
  42. /**
  43. * Input Modifier Status
  44. * for keyboard/mouse events.
  45. */
  46. class InputEvent : public Resource {
  47. GDCLASS(InputEvent, Resource);
  48. int device = 0;
  49. protected:
  50. bool canceled = false;
  51. bool pressed = false;
  52. static void _bind_methods();
  53. public:
  54. static constexpr int DEVICE_ID_EMULATION = -1;
  55. static constexpr int DEVICE_ID_INTERNAL = -2;
  56. void set_device(int p_device);
  57. int get_device() const;
  58. bool is_action(const StringName &p_action, bool p_exact_match = false) const;
  59. bool is_action_pressed(const StringName &p_action, bool p_allow_echo = false, bool p_exact_match = false) const;
  60. bool is_action_released(const StringName &p_action, bool p_exact_match = false) const;
  61. float get_action_strength(const StringName &p_action, bool p_exact_match = false) const;
  62. float get_action_raw_strength(const StringName &p_action, bool p_exact_match = false) const;
  63. bool is_canceled() const;
  64. bool is_pressed() const;
  65. bool is_released() const;
  66. virtual bool is_echo() const;
  67. virtual String as_text() const = 0;
  68. virtual RequiredResult<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const;
  69. virtual bool action_match(const Ref<InputEvent> &p_event, bool p_exact_match, float p_deadzone, bool *r_pressed, float *r_strength, float *r_raw_strength) const;
  70. virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const;
  71. virtual bool is_action_type() const;
  72. virtual bool accumulate(const Ref<InputEvent> &p_event) { return false; }
  73. virtual InputEventType get_type() const { return InputEventType::INVALID; }
  74. };
  75. class InputEventFromWindow : public InputEvent {
  76. GDCLASS(InputEventFromWindow, InputEvent);
  77. int64_t window_id = 0;
  78. protected:
  79. static void _bind_methods();
  80. public:
  81. void set_window_id(int64_t p_id);
  82. int64_t get_window_id() const;
  83. };
  84. class InputEventWithModifiers : public InputEventFromWindow {
  85. GDCLASS(InputEventWithModifiers, InputEventFromWindow);
  86. bool command_or_control_autoremap = false;
  87. bool shift_pressed = false;
  88. bool alt_pressed = false;
  89. bool meta_pressed = false; // "Command" on macOS, "Meta/Win" key on other platforms.
  90. bool ctrl_pressed = false;
  91. protected:
  92. static void _bind_methods();
  93. void _validate_property(PropertyInfo &p_property) const;
  94. public:
  95. void set_command_or_control_autoremap(bool p_enabled);
  96. bool is_command_or_control_autoremap() const;
  97. bool is_command_or_control_pressed() const;
  98. void set_shift_pressed(bool p_pressed);
  99. bool is_shift_pressed() const;
  100. void set_alt_pressed(bool p_pressed);
  101. bool is_alt_pressed() const;
  102. void set_ctrl_pressed(bool p_pressed);
  103. bool is_ctrl_pressed() const;
  104. void set_meta_pressed(bool p_pressed);
  105. bool is_meta_pressed() const;
  106. void set_modifiers_from_event(const InputEventWithModifiers *event);
  107. BitField<KeyModifierMask> get_modifiers_mask() const;
  108. virtual String as_text() const override;
  109. virtual String _to_string() override;
  110. };
  111. class InputEventKey : public InputEventWithModifiers {
  112. GDCLASS(InputEventKey, InputEventWithModifiers);
  113. Key keycode = Key::NONE; // Key enum, without modifier masks.
  114. Key physical_keycode = Key::NONE;
  115. Key key_label = Key::NONE;
  116. uint32_t unicode = 0; ///unicode
  117. KeyLocation location = KeyLocation::UNSPECIFIED;
  118. bool echo = false; /// true if this is an echo key
  119. protected:
  120. static void _bind_methods();
  121. public:
  122. void set_pressed(bool p_pressed);
  123. void set_keycode(Key p_keycode);
  124. Key get_keycode() const;
  125. void set_physical_keycode(Key p_keycode);
  126. Key get_physical_keycode() const;
  127. void set_key_label(Key p_key_label);
  128. Key get_key_label() const;
  129. void set_unicode(char32_t p_unicode);
  130. char32_t get_unicode() const;
  131. void set_location(KeyLocation p_key_location);
  132. KeyLocation get_location() const;
  133. void set_echo(bool p_enable);
  134. virtual bool is_echo() const override;
  135. Key get_keycode_with_modifiers() const;
  136. Key get_physical_keycode_with_modifiers() const;
  137. Key get_key_label_with_modifiers() const;
  138. virtual bool action_match(const Ref<InputEvent> &p_event, bool p_exact_match, float p_deadzone, bool *r_pressed, float *r_strength, float *r_raw_strength) const override;
  139. virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const override;
  140. virtual bool is_action_type() const override { return true; }
  141. virtual String as_text_physical_keycode() const;
  142. virtual String as_text_keycode() const;
  143. virtual String as_text_key_label() const;
  144. virtual String as_text_location() const;
  145. virtual String as_text() const override;
  146. virtual String _to_string() override;
  147. static Ref<InputEventKey> create_reference(Key p_keycode_with_modifier_masks, bool p_physical = false);
  148. InputEventType get_type() const final override { return InputEventType::KEY; }
  149. };
  150. class InputEventMouse : public InputEventWithModifiers {
  151. GDCLASS(InputEventMouse, InputEventWithModifiers);
  152. BitField<MouseButtonMask> button_mask = MouseButtonMask::NONE;
  153. Vector2 pos;
  154. Vector2 global_pos;
  155. protected:
  156. static void _bind_methods();
  157. public:
  158. void set_button_mask(BitField<MouseButtonMask> p_mask);
  159. BitField<MouseButtonMask> get_button_mask() const;
  160. void set_position(const Vector2 &p_pos);
  161. Vector2 get_position() const;
  162. void set_global_position(const Vector2 &p_global_pos);
  163. Vector2 get_global_position() const;
  164. };
  165. class InputEventMouseButton : public InputEventMouse {
  166. GDCLASS(InputEventMouseButton, InputEventMouse);
  167. float factor = 1;
  168. MouseButton button_index = MouseButton::NONE;
  169. bool double_click = false; //last even less than double click time
  170. protected:
  171. static void _bind_methods();
  172. public:
  173. void set_factor(float p_factor);
  174. float get_factor() const;
  175. void set_button_index(MouseButton p_index);
  176. MouseButton get_button_index() const;
  177. void set_pressed(bool p_pressed);
  178. void set_canceled(bool p_canceled);
  179. void set_double_click(bool p_double_click);
  180. bool is_double_click() const;
  181. virtual RequiredResult<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  182. virtual bool action_match(const Ref<InputEvent> &p_event, bool p_exact_match, float p_deadzone, bool *r_pressed, float *r_strength, float *r_raw_strength) const override;
  183. virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const override;
  184. virtual bool is_action_type() const override { return true; }
  185. virtual String as_text() const override;
  186. virtual String _to_string() override;
  187. InputEventType get_type() const final override { return InputEventType::MOUSE_BUTTON; }
  188. };
  189. class InputEventMouseMotion : public InputEventMouse {
  190. GDCLASS(InputEventMouseMotion, InputEventMouse);
  191. Vector2 tilt;
  192. float pressure = 0;
  193. Vector2 relative;
  194. Vector2 screen_relative;
  195. Vector2 velocity;
  196. Vector2 screen_velocity;
  197. bool pen_inverted = false;
  198. protected:
  199. static void _bind_methods();
  200. public:
  201. void set_tilt(const Vector2 &p_tilt);
  202. Vector2 get_tilt() const;
  203. void set_pressure(float p_pressure);
  204. float get_pressure() const;
  205. void set_pen_inverted(bool p_inverted);
  206. bool get_pen_inverted() const;
  207. void set_relative(const Vector2 &p_relative);
  208. Vector2 get_relative() const;
  209. void set_relative_screen_position(const Vector2 &p_relative);
  210. Vector2 get_relative_screen_position() const;
  211. void set_velocity(const Vector2 &p_velocity);
  212. Vector2 get_velocity() const;
  213. void set_screen_velocity(const Vector2 &p_velocity);
  214. Vector2 get_screen_velocity() const;
  215. virtual RequiredResult<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  216. virtual String as_text() const override;
  217. virtual String _to_string() override;
  218. virtual bool accumulate(const Ref<InputEvent> &p_event) override;
  219. InputEventType get_type() const final override { return InputEventType::MOUSE_MOTION; }
  220. };
  221. class InputEventJoypadMotion : public InputEvent {
  222. GDCLASS(InputEventJoypadMotion, InputEvent);
  223. JoyAxis axis = (JoyAxis)0; ///< Joypad axis
  224. float axis_value = 0; ///< -1 to 1
  225. protected:
  226. static void _bind_methods();
  227. public:
  228. void set_axis(JoyAxis p_axis);
  229. JoyAxis get_axis() const;
  230. void set_axis_value(float p_value);
  231. float get_axis_value() const;
  232. virtual bool action_match(const Ref<InputEvent> &p_event, bool p_exact_match, float p_deadzone, bool *r_pressed, float *r_strength, float *r_raw_strength) const override;
  233. virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const override;
  234. virtual bool is_action_type() const override { return true; }
  235. virtual String as_text() const override;
  236. virtual String _to_string() override;
  237. // The default device ID is `InputMap::ALL_DEVICES`.
  238. static Ref<InputEventJoypadMotion> create_reference(JoyAxis p_axis, float p_value, int p_device = -1);
  239. InputEventType get_type() const final override { return InputEventType::JOY_MOTION; }
  240. };
  241. class InputEventJoypadButton : public InputEvent {
  242. GDCLASS(InputEventJoypadButton, InputEvent);
  243. JoyButton button_index = (JoyButton)0;
  244. float pressure = 0; //0 to 1
  245. protected:
  246. static void _bind_methods();
  247. public:
  248. void set_button_index(JoyButton p_index);
  249. JoyButton get_button_index() const;
  250. void set_pressed(bool p_pressed);
  251. void set_pressure(float p_pressure);
  252. float get_pressure() const;
  253. virtual bool action_match(const Ref<InputEvent> &p_event, bool p_exact_match, float p_deadzone, bool *r_pressed, float *r_strength, float *r_raw_strength) const override;
  254. virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const override;
  255. virtual bool is_action_type() const override { return true; }
  256. virtual String as_text() const override;
  257. virtual String _to_string() override;
  258. // The default device ID is `InputMap::ALL_DEVICES`.
  259. static Ref<InputEventJoypadButton> create_reference(JoyButton p_btn_index, int p_device = -1);
  260. InputEventType get_type() const final override { return InputEventType::JOY_BUTTON; }
  261. };
  262. class InputEventScreenTouch : public InputEventFromWindow {
  263. GDCLASS(InputEventScreenTouch, InputEventFromWindow);
  264. int index = 0;
  265. Vector2 pos;
  266. bool double_tap = false;
  267. protected:
  268. static void _bind_methods();
  269. public:
  270. void set_index(int p_index);
  271. int get_index() const;
  272. void set_position(const Vector2 &p_pos);
  273. Vector2 get_position() const;
  274. void set_pressed(bool p_pressed);
  275. void set_canceled(bool p_canceled);
  276. void set_double_tap(bool p_double_tap);
  277. bool is_double_tap() const;
  278. virtual RequiredResult<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  279. virtual String as_text() const override;
  280. virtual String _to_string() override;
  281. InputEventType get_type() const final override { return InputEventType::SCREEN_TOUCH; }
  282. };
  283. class InputEventScreenDrag : public InputEventFromWindow {
  284. GDCLASS(InputEventScreenDrag, InputEventFromWindow);
  285. int index = 0;
  286. Vector2 pos;
  287. Vector2 relative;
  288. Vector2 screen_relative;
  289. Vector2 velocity;
  290. Vector2 screen_velocity;
  291. Vector2 tilt;
  292. float pressure = 0;
  293. bool pen_inverted = false;
  294. protected:
  295. static void _bind_methods();
  296. public:
  297. void set_index(int p_index);
  298. int get_index() const;
  299. void set_tilt(const Vector2 &p_tilt);
  300. Vector2 get_tilt() const;
  301. void set_pressure(float p_pressure);
  302. float get_pressure() const;
  303. void set_pen_inverted(bool p_inverted);
  304. bool get_pen_inverted() const;
  305. void set_position(const Vector2 &p_pos);
  306. Vector2 get_position() const;
  307. void set_relative(const Vector2 &p_relative);
  308. Vector2 get_relative() const;
  309. void set_relative_screen_position(const Vector2 &p_relative);
  310. Vector2 get_relative_screen_position() const;
  311. void set_velocity(const Vector2 &p_velocity);
  312. Vector2 get_velocity() const;
  313. void set_screen_velocity(const Vector2 &p_velocity);
  314. Vector2 get_screen_velocity() const;
  315. virtual RequiredResult<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  316. virtual String as_text() const override;
  317. virtual String _to_string() override;
  318. virtual bool accumulate(const Ref<InputEvent> &p_event) override;
  319. InputEventType get_type() const final override { return InputEventType::SCREEN_DRAG; }
  320. };
  321. class InputEventAction : public InputEvent {
  322. GDCLASS(InputEventAction, InputEvent);
  323. StringName action;
  324. float strength = 1.0f;
  325. int event_index = -1;
  326. protected:
  327. static void _bind_methods();
  328. public:
  329. void set_action(const StringName &p_action);
  330. StringName get_action() const;
  331. void set_pressed(bool p_pressed);
  332. void set_strength(float p_strength);
  333. float get_strength() const;
  334. void set_event_index(int p_index);
  335. int get_event_index() const;
  336. virtual bool is_action(const StringName &p_action) const;
  337. virtual bool action_match(const Ref<InputEvent> &p_event, bool p_exact_match, float p_deadzone, bool *r_pressed, float *r_strength, float *r_raw_strength) const override;
  338. virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const override;
  339. virtual bool is_action_type() const override { return true; }
  340. virtual String as_text() const override;
  341. virtual String _to_string() override;
  342. InputEventType get_type() const final override { return InputEventType::ACTION; }
  343. };
  344. class InputEventGesture : public InputEventWithModifiers {
  345. GDCLASS(InputEventGesture, InputEventWithModifiers);
  346. Vector2 pos;
  347. protected:
  348. static void _bind_methods();
  349. public:
  350. void set_position(const Vector2 &p_pos);
  351. Vector2 get_position() const;
  352. };
  353. class InputEventMagnifyGesture : public InputEventGesture {
  354. GDCLASS(InputEventMagnifyGesture, InputEventGesture);
  355. real_t factor = 1.0;
  356. protected:
  357. static void _bind_methods();
  358. public:
  359. void set_factor(real_t p_factor);
  360. real_t get_factor() const;
  361. virtual RequiredResult<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  362. virtual String as_text() const override;
  363. virtual String _to_string() override;
  364. InputEventType get_type() const final override { return InputEventType::MAGNIFY_GESTURE; }
  365. };
  366. class InputEventPanGesture : public InputEventGesture {
  367. GDCLASS(InputEventPanGesture, InputEventGesture);
  368. Vector2 delta;
  369. protected:
  370. static void _bind_methods();
  371. public:
  372. void set_delta(const Vector2 &p_delta);
  373. Vector2 get_delta() const;
  374. virtual RequiredResult<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  375. virtual String as_text() const override;
  376. virtual String _to_string() override;
  377. InputEventType get_type() const final override { return InputEventType::PAN_GESTURE; }
  378. };
  379. class InputEventMIDI : public InputEvent {
  380. GDCLASS(InputEventMIDI, InputEvent);
  381. int channel = 0;
  382. MIDIMessage message = MIDIMessage::NONE;
  383. int pitch = 0;
  384. int velocity = 0;
  385. int instrument = 0;
  386. int pressure = 0;
  387. int controller_number = 0;
  388. int controller_value = 0;
  389. protected:
  390. static void _bind_methods();
  391. public:
  392. void set_channel(const int p_channel);
  393. int get_channel() const;
  394. void set_message(const MIDIMessage p_message);
  395. MIDIMessage get_message() const;
  396. void set_pitch(const int p_pitch);
  397. int get_pitch() const;
  398. void set_velocity(const int p_velocity);
  399. int get_velocity() const;
  400. void set_instrument(const int p_instrument);
  401. int get_instrument() const;
  402. void set_pressure(const int p_pressure);
  403. int get_pressure() const;
  404. void set_controller_number(const int p_controller_number);
  405. int get_controller_number() const;
  406. void set_controller_value(const int p_controller_value);
  407. int get_controller_value() const;
  408. virtual String as_text() const override;
  409. virtual String _to_string() override;
  410. InputEventType get_type() const final override { return InputEventType::MIDI; }
  411. };
  412. class InputEventShortcut : public InputEvent {
  413. GDCLASS(InputEventShortcut, InputEvent);
  414. Ref<Shortcut> shortcut;
  415. protected:
  416. static void _bind_methods();
  417. public:
  418. void set_shortcut(Ref<Shortcut> p_shortcut);
  419. Ref<Shortcut> get_shortcut();
  420. virtual String as_text() const override;
  421. virtual String _to_string() override;
  422. InputEventType get_type() const final override { return InputEventType::SHORTCUT; }
  423. InputEventShortcut();
  424. };