input_event.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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. static void _bind_methods();
  52. public:
  53. static const int DEVICE_ID_EMULATION;
  54. static const int DEVICE_ID_INTERNAL;
  55. void set_device(int p_device);
  56. int get_device() const;
  57. bool is_action(const StringName &p_action, bool p_exact_match = false) const;
  58. bool is_action_pressed(const StringName &p_action, bool p_allow_echo = false, bool p_exact_match = false) const;
  59. bool is_action_released(const StringName &p_action, bool p_exact_match = false) const;
  60. float get_action_strength(const StringName &p_action, bool p_exact_match = false) const;
  61. float get_action_raw_strength(const StringName &p_action, bool p_exact_match = false) const;
  62. // To be removed someday, since they do not make sense for all events
  63. virtual bool is_pressed() const;
  64. virtual bool is_echo() const;
  65. virtual String as_text() const = 0;
  66. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const;
  67. 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;
  68. virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const;
  69. virtual bool is_action_type() const;
  70. virtual bool accumulate(const Ref<InputEvent> &p_event) { return false; }
  71. InputEvent() {}
  72. };
  73. class InputEventFromWindow : public InputEvent {
  74. GDCLASS(InputEventFromWindow, InputEvent);
  75. int64_t window_id = 0;
  76. protected:
  77. static void _bind_methods();
  78. public:
  79. void set_window_id(int64_t p_id);
  80. int64_t get_window_id() const;
  81. InputEventFromWindow() {}
  82. };
  83. class InputEventWithModifiers : public InputEventFromWindow {
  84. GDCLASS(InputEventWithModifiers, InputEventFromWindow);
  85. bool command_or_control_autoremap = false;
  86. bool shift_pressed = false;
  87. bool alt_pressed = false;
  88. bool meta_pressed = false; // "Command" on macOS, "Meta/Win" key on other platforms.
  89. bool ctrl_pressed = false;
  90. protected:
  91. static void _bind_methods();
  92. void _validate_property(PropertyInfo &p_property) const;
  93. public:
  94. void set_command_or_control_autoremap(bool p_enabled);
  95. bool is_command_or_control_autoremap() const;
  96. bool is_command_or_control_pressed() const;
  97. void set_shift_pressed(bool p_pressed);
  98. bool is_shift_pressed() const;
  99. void set_alt_pressed(bool p_pressed);
  100. bool is_alt_pressed() const;
  101. void set_ctrl_pressed(bool p_pressed);
  102. bool is_ctrl_pressed() const;
  103. void set_meta_pressed(bool p_pressed);
  104. bool is_meta_pressed() const;
  105. void set_modifiers_from_event(const InputEventWithModifiers *event);
  106. BitField<KeyModifierMask> get_modifiers_mask() const;
  107. virtual String as_text() const override;
  108. virtual String to_string() override;
  109. InputEventWithModifiers() {}
  110. };
  111. class InputEventKey : public InputEventWithModifiers {
  112. GDCLASS(InputEventKey, InputEventWithModifiers);
  113. bool pressed = false; /// otherwise release
  114. Key keycode = Key::NONE; // Key enum, without modifier masks.
  115. Key physical_keycode = Key::NONE;
  116. Key key_label = Key::NONE;
  117. uint32_t unicode = 0; ///unicode
  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. virtual bool is_pressed() const override;
  124. void set_keycode(Key p_keycode);
  125. Key get_keycode() const;
  126. void set_physical_keycode(Key p_keycode);
  127. Key get_physical_keycode() const;
  128. void set_key_label(Key p_key_label);
  129. Key get_key_label() const;
  130. void set_unicode(char32_t p_unicode);
  131. char32_t get_unicode() const;
  132. void set_echo(bool p_enable);
  133. virtual bool is_echo() const override;
  134. Key get_keycode_with_modifiers() const;
  135. Key get_physical_keycode_with_modifiers() const;
  136. Key get_key_label_with_modifiers() const;
  137. 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;
  138. virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const override;
  139. virtual bool is_action_type() const override { return true; }
  140. virtual String as_text_physical_keycode() const;
  141. virtual String as_text_keycode() const;
  142. virtual String as_text_key_label() const;
  143. virtual String as_text() const override;
  144. virtual String to_string() override;
  145. static Ref<InputEventKey> create_reference(Key p_keycode_with_modifier_masks, bool p_physical = false);
  146. InputEventKey() {}
  147. };
  148. class InputEventMouse : public InputEventWithModifiers {
  149. GDCLASS(InputEventMouse, InputEventWithModifiers);
  150. BitField<MouseButtonMask> button_mask;
  151. Vector2 pos;
  152. Vector2 global_pos;
  153. protected:
  154. static void _bind_methods();
  155. public:
  156. void set_button_mask(BitField<MouseButtonMask> p_mask);
  157. BitField<MouseButtonMask> get_button_mask() const;
  158. void set_position(const Vector2 &p_pos);
  159. Vector2 get_position() const;
  160. void set_global_position(const Vector2 &p_global_pos);
  161. Vector2 get_global_position() const;
  162. InputEventMouse() {}
  163. };
  164. class InputEventMouseButton : public InputEventMouse {
  165. GDCLASS(InputEventMouseButton, InputEventMouse);
  166. float factor = 1;
  167. MouseButton button_index = MouseButton::NONE;
  168. bool pressed = false; //otherwise released
  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. virtual bool is_pressed() const override;
  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 is_pressed() const override;
  227. 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;
  228. virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const override;
  229. virtual bool is_action_type() const override { return true; }
  230. virtual String as_text() const override;
  231. virtual String to_string() override;
  232. InputEventJoypadMotion() {}
  233. };
  234. class InputEventJoypadButton : public InputEvent {
  235. GDCLASS(InputEventJoypadButton, InputEvent);
  236. JoyButton button_index = (JoyButton)0;
  237. bool pressed = false;
  238. float pressure = 0; //0 to 1
  239. protected:
  240. static void _bind_methods();
  241. public:
  242. void set_button_index(JoyButton p_index);
  243. JoyButton get_button_index() const;
  244. void set_pressed(bool p_pressed);
  245. virtual bool is_pressed() const override;
  246. void set_pressure(float p_pressure);
  247. float get_pressure() const;
  248. 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;
  249. virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const override;
  250. virtual bool is_action_type() const override { return true; }
  251. virtual String as_text() const override;
  252. virtual String to_string() override;
  253. static Ref<InputEventJoypadButton> create_reference(JoyButton p_btn_index);
  254. InputEventJoypadButton() {}
  255. };
  256. class InputEventScreenTouch : public InputEventFromWindow {
  257. GDCLASS(InputEventScreenTouch, InputEventFromWindow);
  258. int index = 0;
  259. Vector2 pos;
  260. bool pressed = false;
  261. bool double_tap = false;
  262. protected:
  263. static void _bind_methods();
  264. public:
  265. void set_index(int p_index);
  266. int get_index() const;
  267. void set_position(const Vector2 &p_pos);
  268. Vector2 get_position() const;
  269. void set_pressed(bool p_pressed);
  270. virtual bool is_pressed() const override;
  271. void set_double_tap(bool p_double_tap);
  272. bool is_double_tap() const;
  273. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  274. virtual String as_text() const override;
  275. virtual String to_string() override;
  276. InputEventScreenTouch() {}
  277. };
  278. class InputEventScreenDrag : public InputEventFromWindow {
  279. GDCLASS(InputEventScreenDrag, InputEventFromWindow);
  280. int index = 0;
  281. Vector2 pos;
  282. Vector2 relative;
  283. Vector2 velocity;
  284. Vector2 tilt;
  285. float pressure = 0;
  286. bool pen_inverted = false;
  287. protected:
  288. static void _bind_methods();
  289. public:
  290. void set_index(int p_index);
  291. int get_index() const;
  292. void set_tilt(const Vector2 &p_tilt);
  293. Vector2 get_tilt() const;
  294. void set_pressure(float p_pressure);
  295. float get_pressure() const;
  296. void set_pen_inverted(bool p_inverted);
  297. bool get_pen_inverted() const;
  298. void set_position(const Vector2 &p_pos);
  299. Vector2 get_position() const;
  300. void set_relative(const Vector2 &p_relative);
  301. Vector2 get_relative() const;
  302. void set_velocity(const Vector2 &p_velocity);
  303. Vector2 get_velocity() const;
  304. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  305. virtual String as_text() const override;
  306. virtual String to_string() override;
  307. virtual bool accumulate(const Ref<InputEvent> &p_event) override;
  308. InputEventScreenDrag() {}
  309. };
  310. class InputEventAction : public InputEvent {
  311. GDCLASS(InputEventAction, InputEvent);
  312. StringName action;
  313. bool pressed = false;
  314. float strength = 1.0f;
  315. protected:
  316. static void _bind_methods();
  317. public:
  318. void set_action(const StringName &p_action);
  319. StringName get_action() const;
  320. void set_pressed(bool p_pressed);
  321. virtual bool is_pressed() const override;
  322. void set_strength(float p_strength);
  323. float get_strength() const;
  324. virtual bool is_action(const StringName &p_action) const;
  325. 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;
  326. virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const override;
  327. virtual bool is_action_type() const override { return true; }
  328. virtual String as_text() const override;
  329. virtual String to_string() override;
  330. InputEventAction() {}
  331. };
  332. class InputEventGesture : public InputEventWithModifiers {
  333. GDCLASS(InputEventGesture, InputEventWithModifiers);
  334. Vector2 pos;
  335. protected:
  336. static void _bind_methods();
  337. public:
  338. void set_position(const Vector2 &p_pos);
  339. Vector2 get_position() const;
  340. };
  341. class InputEventMagnifyGesture : public InputEventGesture {
  342. GDCLASS(InputEventMagnifyGesture, InputEventGesture);
  343. real_t factor = 1.0;
  344. protected:
  345. static void _bind_methods();
  346. public:
  347. void set_factor(real_t p_factor);
  348. real_t get_factor() const;
  349. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  350. virtual String as_text() const override;
  351. virtual String to_string() override;
  352. InputEventMagnifyGesture() {}
  353. };
  354. class InputEventPanGesture : public InputEventGesture {
  355. GDCLASS(InputEventPanGesture, InputEventGesture);
  356. Vector2 delta;
  357. protected:
  358. static void _bind_methods();
  359. public:
  360. void set_delta(const Vector2 &p_delta);
  361. Vector2 get_delta() const;
  362. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  363. virtual String as_text() const override;
  364. virtual String to_string() override;
  365. InputEventPanGesture() {}
  366. };
  367. class InputEventMIDI : public InputEvent {
  368. GDCLASS(InputEventMIDI, InputEvent);
  369. int channel = 0;
  370. MIDIMessage message = MIDIMessage::NONE;
  371. int pitch = 0;
  372. int velocity = 0;
  373. int instrument = 0;
  374. int pressure = 0;
  375. int controller_number = 0;
  376. int controller_value = 0;
  377. protected:
  378. static void _bind_methods();
  379. public:
  380. void set_channel(const int p_channel);
  381. int get_channel() const;
  382. void set_message(const MIDIMessage p_message);
  383. MIDIMessage get_message() const;
  384. void set_pitch(const int p_pitch);
  385. int get_pitch() const;
  386. void set_velocity(const int p_velocity);
  387. int get_velocity() const;
  388. void set_instrument(const int p_instrument);
  389. int get_instrument() const;
  390. void set_pressure(const int p_pressure);
  391. int get_pressure() const;
  392. void set_controller_number(const int p_controller_number);
  393. int get_controller_number() const;
  394. void set_controller_value(const int p_controller_value);
  395. int get_controller_value() const;
  396. virtual String as_text() const override;
  397. virtual String to_string() override;
  398. InputEventMIDI() {}
  399. };
  400. class InputEventShortcut : public InputEvent {
  401. GDCLASS(InputEventShortcut, InputEvent);
  402. Ref<Shortcut> shortcut;
  403. protected:
  404. static void _bind_methods();
  405. public:
  406. void set_shortcut(Ref<Shortcut> p_shortcut);
  407. Ref<Shortcut> get_shortcut();
  408. virtual bool is_pressed() const override;
  409. virtual String as_text() const override;
  410. virtual String to_string() override;
  411. };
  412. #endif // INPUT_EVENT_H