input_event.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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. #ifndef INPUT_EVENT_H
  31. #define INPUT_EVENT_H
  32. #include "core/input/input_enums.h"
  33. #include "core/io/resource.h"
  34. #include "core/math/transform_2d.h"
  35. #include "core/os/keyboard.h"
  36. #include "core/string/ustring.h"
  37. #include "core/typedefs.h"
  38. /**
  39. * Input Event classes. These are used in the main loop.
  40. * The events are pretty obvious.
  41. */
  42. class Shortcut;
  43. /**
  44. * Input Modifier Status
  45. * for keyboard/mouse events.
  46. */
  47. class InputEvent : public Resource {
  48. GDCLASS(InputEvent, Resource);
  49. int device = 0;
  50. protected:
  51. bool canceled = false;
  52. bool pressed = false;
  53. static void _bind_methods();
  54. public:
  55. static const int DEVICE_ID_EMULATION;
  56. static const int DEVICE_ID_INTERNAL;
  57. void set_device(int p_device);
  58. int get_device() const;
  59. bool is_action(const StringName &p_action, bool p_exact_match = false) const;
  60. bool is_action_pressed(const StringName &p_action, bool p_allow_echo = false, bool p_exact_match = false) const;
  61. bool is_action_released(const StringName &p_action, bool p_exact_match = false) const;
  62. float get_action_strength(const StringName &p_action, bool p_exact_match = false) const;
  63. float get_action_raw_strength(const StringName &p_action, bool p_exact_match = false) const;
  64. bool is_canceled() const;
  65. bool is_pressed() const;
  66. bool is_released() const;
  67. virtual bool is_echo() const;
  68. virtual String as_text() const = 0;
  69. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const;
  70. 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;
  71. virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const;
  72. virtual bool is_action_type() const;
  73. virtual bool accumulate(const Ref<InputEvent> &p_event) { return false; }
  74. InputEvent() {}
  75. };
  76. class InputEventFromWindow : public InputEvent {
  77. GDCLASS(InputEventFromWindow, InputEvent);
  78. int64_t window_id = 0;
  79. protected:
  80. static void _bind_methods();
  81. public:
  82. void set_window_id(int64_t p_id);
  83. int64_t get_window_id() const;
  84. InputEventFromWindow() {}
  85. };
  86. class InputEventWithModifiers : public InputEventFromWindow {
  87. GDCLASS(InputEventWithModifiers, InputEventFromWindow);
  88. bool command_or_control_autoremap = false;
  89. bool shift_pressed = false;
  90. bool alt_pressed = false;
  91. bool meta_pressed = false; // "Command" on macOS, "Meta/Win" key on other platforms.
  92. bool ctrl_pressed = false;
  93. protected:
  94. static void _bind_methods();
  95. void _validate_property(PropertyInfo &p_property) const;
  96. public:
  97. void set_command_or_control_autoremap(bool p_enabled);
  98. bool is_command_or_control_autoremap() const;
  99. bool is_command_or_control_pressed() const;
  100. void set_shift_pressed(bool p_pressed);
  101. bool is_shift_pressed() const;
  102. void set_alt_pressed(bool p_pressed);
  103. bool is_alt_pressed() const;
  104. void set_ctrl_pressed(bool p_pressed);
  105. bool is_ctrl_pressed() const;
  106. void set_meta_pressed(bool p_pressed);
  107. bool is_meta_pressed() const;
  108. void set_modifiers_from_event(const InputEventWithModifiers *event);
  109. BitField<KeyModifierMask> get_modifiers_mask() const;
  110. virtual String as_text() const override;
  111. virtual String to_string() override;
  112. InputEventWithModifiers() {}
  113. };
  114. class InputEventKey : public InputEventWithModifiers {
  115. GDCLASS(InputEventKey, InputEventWithModifiers);
  116. Key keycode = Key::NONE; // Key enum, without modifier masks.
  117. Key physical_keycode = Key::NONE;
  118. Key key_label = Key::NONE;
  119. uint32_t unicode = 0; ///unicode
  120. bool echo = false; /// true if this is an echo key
  121. protected:
  122. static void _bind_methods();
  123. public:
  124. void set_pressed(bool p_pressed);
  125. void set_keycode(Key p_keycode);
  126. Key get_keycode() const;
  127. void set_physical_keycode(Key p_keycode);
  128. Key get_physical_keycode() const;
  129. void set_key_label(Key p_key_label);
  130. Key get_key_label() const;
  131. void set_unicode(char32_t p_unicode);
  132. char32_t get_unicode() 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() const override;
  145. virtual String to_string() override;
  146. static Ref<InputEventKey> create_reference(Key p_keycode_with_modifier_masks, bool p_physical = false);
  147. InputEventKey() {}
  148. };
  149. class InputEventMouse : public InputEventWithModifiers {
  150. GDCLASS(InputEventMouse, InputEventWithModifiers);
  151. BitField<MouseButtonMask> button_mask;
  152. Vector2 pos;
  153. Vector2 global_pos;
  154. protected:
  155. static void _bind_methods();
  156. public:
  157. void set_button_mask(BitField<MouseButtonMask> p_mask);
  158. BitField<MouseButtonMask> get_button_mask() const;
  159. void set_position(const Vector2 &p_pos);
  160. Vector2 get_position() const;
  161. void set_global_position(const Vector2 &p_global_pos);
  162. Vector2 get_global_position() const;
  163. InputEventMouse() {}
  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 Ref<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. InputEventMouseButton() {}
  188. };
  189. class InputEventMouseMotion : public InputEventMouse {
  190. GDCLASS(InputEventMouseMotion, InputEventMouse);
  191. Vector2 tilt;
  192. float pressure = 0;
  193. Vector2 relative;
  194. Vector2 velocity;
  195. bool pen_inverted = false;
  196. protected:
  197. static void _bind_methods();
  198. public:
  199. void set_tilt(const Vector2 &p_tilt);
  200. Vector2 get_tilt() const;
  201. void set_pressure(float p_pressure);
  202. float get_pressure() const;
  203. void set_pen_inverted(bool p_inverted);
  204. bool get_pen_inverted() const;
  205. void set_relative(const Vector2 &p_relative);
  206. Vector2 get_relative() const;
  207. void set_velocity(const Vector2 &p_velocity);
  208. Vector2 get_velocity() const;
  209. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  210. virtual String as_text() const override;
  211. virtual String to_string() override;
  212. virtual bool accumulate(const Ref<InputEvent> &p_event) override;
  213. InputEventMouseMotion() {}
  214. };
  215. class InputEventJoypadMotion : public InputEvent {
  216. GDCLASS(InputEventJoypadMotion, InputEvent);
  217. JoyAxis axis = (JoyAxis)0; ///< Joypad axis
  218. float axis_value = 0; ///< -1 to 1
  219. protected:
  220. static void _bind_methods();
  221. public:
  222. void set_axis(JoyAxis p_axis);
  223. JoyAxis get_axis() const;
  224. void set_axis_value(float p_value);
  225. float get_axis_value() const;
  226. 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;
  227. virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const override;
  228. virtual bool is_action_type() const override { return true; }
  229. virtual String as_text() const override;
  230. virtual String to_string() override;
  231. static Ref<InputEventJoypadMotion> create_reference(JoyAxis p_axis, float p_value);
  232. InputEventJoypadMotion() {}
  233. };
  234. class InputEventJoypadButton : public InputEvent {
  235. GDCLASS(InputEventJoypadButton, InputEvent);
  236. JoyButton button_index = (JoyButton)0;
  237. float pressure = 0; //0 to 1
  238. protected:
  239. static void _bind_methods();
  240. public:
  241. void set_button_index(JoyButton p_index);
  242. JoyButton get_button_index() const;
  243. void set_pressed(bool p_pressed);
  244. void set_pressure(float p_pressure);
  245. float get_pressure() const;
  246. 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;
  247. virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const override;
  248. virtual bool is_action_type() const override { return true; }
  249. virtual String as_text() const override;
  250. virtual String to_string() override;
  251. static Ref<InputEventJoypadButton> create_reference(JoyButton p_btn_index);
  252. InputEventJoypadButton() {}
  253. };
  254. class InputEventScreenTouch : public InputEventFromWindow {
  255. GDCLASS(InputEventScreenTouch, InputEventFromWindow);
  256. int index = 0;
  257. Vector2 pos;
  258. bool double_tap = false;
  259. protected:
  260. static void _bind_methods();
  261. public:
  262. void set_index(int p_index);
  263. int get_index() const;
  264. void set_position(const Vector2 &p_pos);
  265. Vector2 get_position() const;
  266. void set_pressed(bool p_pressed);
  267. void set_canceled(bool p_canceled);
  268. void set_double_tap(bool p_double_tap);
  269. bool is_double_tap() const;
  270. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  271. virtual String as_text() const override;
  272. virtual String to_string() override;
  273. InputEventScreenTouch() {}
  274. };
  275. class InputEventScreenDrag : public InputEventFromWindow {
  276. GDCLASS(InputEventScreenDrag, InputEventFromWindow);
  277. int index = 0;
  278. Vector2 pos;
  279. Vector2 relative;
  280. Vector2 velocity;
  281. Vector2 tilt;
  282. float pressure = 0;
  283. bool pen_inverted = false;
  284. protected:
  285. static void _bind_methods();
  286. public:
  287. void set_index(int p_index);
  288. int get_index() const;
  289. void set_tilt(const Vector2 &p_tilt);
  290. Vector2 get_tilt() const;
  291. void set_pressure(float p_pressure);
  292. float get_pressure() const;
  293. void set_pen_inverted(bool p_inverted);
  294. bool get_pen_inverted() const;
  295. void set_position(const Vector2 &p_pos);
  296. Vector2 get_position() const;
  297. void set_relative(const Vector2 &p_relative);
  298. Vector2 get_relative() const;
  299. void set_velocity(const Vector2 &p_velocity);
  300. Vector2 get_velocity() const;
  301. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  302. virtual String as_text() const override;
  303. virtual String to_string() override;
  304. virtual bool accumulate(const Ref<InputEvent> &p_event) override;
  305. InputEventScreenDrag() {}
  306. };
  307. class InputEventAction : public InputEvent {
  308. GDCLASS(InputEventAction, InputEvent);
  309. StringName action;
  310. float strength = 1.0f;
  311. protected:
  312. static void _bind_methods();
  313. public:
  314. void set_action(const StringName &p_action);
  315. StringName get_action() const;
  316. void set_pressed(bool p_pressed);
  317. void set_strength(float p_strength);
  318. float get_strength() const;
  319. virtual bool is_action(const StringName &p_action) const;
  320. 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;
  321. virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const override;
  322. virtual bool is_action_type() const override { return true; }
  323. virtual String as_text() const override;
  324. virtual String to_string() override;
  325. InputEventAction() {}
  326. };
  327. class InputEventGesture : public InputEventWithModifiers {
  328. GDCLASS(InputEventGesture, InputEventWithModifiers);
  329. Vector2 pos;
  330. protected:
  331. static void _bind_methods();
  332. public:
  333. void set_position(const Vector2 &p_pos);
  334. Vector2 get_position() const;
  335. };
  336. class InputEventMagnifyGesture : public InputEventGesture {
  337. GDCLASS(InputEventMagnifyGesture, InputEventGesture);
  338. real_t factor = 1.0;
  339. protected:
  340. static void _bind_methods();
  341. public:
  342. void set_factor(real_t p_factor);
  343. real_t get_factor() const;
  344. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  345. virtual String as_text() const override;
  346. virtual String to_string() override;
  347. InputEventMagnifyGesture() {}
  348. };
  349. class InputEventPanGesture : public InputEventGesture {
  350. GDCLASS(InputEventPanGesture, InputEventGesture);
  351. Vector2 delta;
  352. protected:
  353. static void _bind_methods();
  354. public:
  355. void set_delta(const Vector2 &p_delta);
  356. Vector2 get_delta() const;
  357. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  358. virtual String as_text() const override;
  359. virtual String to_string() override;
  360. InputEventPanGesture() {}
  361. };
  362. class InputEventMIDI : public InputEvent {
  363. GDCLASS(InputEventMIDI, InputEvent);
  364. int channel = 0;
  365. MIDIMessage message = MIDIMessage::NONE;
  366. int pitch = 0;
  367. int velocity = 0;
  368. int instrument = 0;
  369. int pressure = 0;
  370. int controller_number = 0;
  371. int controller_value = 0;
  372. protected:
  373. static void _bind_methods();
  374. public:
  375. void set_channel(const int p_channel);
  376. int get_channel() const;
  377. void set_message(const MIDIMessage p_message);
  378. MIDIMessage get_message() const;
  379. void set_pitch(const int p_pitch);
  380. int get_pitch() const;
  381. void set_velocity(const int p_velocity);
  382. int get_velocity() const;
  383. void set_instrument(const int p_instrument);
  384. int get_instrument() const;
  385. void set_pressure(const int p_pressure);
  386. int get_pressure() const;
  387. void set_controller_number(const int p_controller_number);
  388. int get_controller_number() const;
  389. void set_controller_value(const int p_controller_value);
  390. int get_controller_value() const;
  391. virtual String as_text() const override;
  392. virtual String to_string() override;
  393. InputEventMIDI() {}
  394. };
  395. class InputEventShortcut : public InputEvent {
  396. GDCLASS(InputEventShortcut, InputEvent);
  397. Ref<Shortcut> shortcut;
  398. protected:
  399. static void _bind_methods();
  400. public:
  401. void set_shortcut(Ref<Shortcut> p_shortcut);
  402. Ref<Shortcut> get_shortcut();
  403. virtual String as_text() const override;
  404. virtual String to_string() override;
  405. };
  406. #endif // INPUT_EVENT_H