input_event.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. /*************************************************************************/
  2. /* input_event.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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/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_TOUCH_MOUSE;
  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 store_command = true;
  86. bool shift_pressed = false;
  87. bool alt_pressed = false;
  88. #ifdef APPLE_STYLE_KEYS
  89. union {
  90. bool command_pressed;
  91. bool meta_pressed = false; //< windows/mac key
  92. };
  93. bool ctrl_pressed = false;
  94. #else
  95. union {
  96. bool command_pressed; //< windows/mac key
  97. bool ctrl_pressed = false;
  98. };
  99. bool meta_pressed = false; //< windows/mac key
  100. #endif
  101. protected:
  102. static void _bind_methods();
  103. void _validate_property(PropertyInfo &p_property) const;
  104. public:
  105. void set_store_command(bool p_enabled);
  106. bool is_storing_command() const;
  107. void set_shift_pressed(bool p_pressed);
  108. bool is_shift_pressed() const;
  109. void set_alt_pressed(bool p_pressed);
  110. bool is_alt_pressed() const;
  111. void set_ctrl_pressed(bool p_pressed);
  112. bool is_ctrl_pressed() const;
  113. void set_meta_pressed(bool p_pressed);
  114. bool is_meta_pressed() const;
  115. void set_command_pressed(bool p_pressed);
  116. bool is_command_pressed() const;
  117. void set_modifiers_from_event(const InputEventWithModifiers *event);
  118. Key get_modifiers_mask() const;
  119. virtual String as_text() const override;
  120. virtual String to_string() override;
  121. InputEventWithModifiers() {}
  122. };
  123. class InputEventKey : public InputEventWithModifiers {
  124. GDCLASS(InputEventKey, InputEventWithModifiers);
  125. bool pressed = false; /// otherwise release
  126. Key keycode = Key::NONE; // Key enum, without modifier masks.
  127. Key physical_keycode = Key::NONE;
  128. uint32_t unicode = 0; ///unicode
  129. bool echo = false; /// true if this is an echo key
  130. protected:
  131. static void _bind_methods();
  132. public:
  133. void set_pressed(bool p_pressed);
  134. virtual bool is_pressed() const override;
  135. void set_keycode(Key p_keycode);
  136. Key get_keycode() const;
  137. void set_physical_keycode(Key p_keycode);
  138. Key get_physical_keycode() const;
  139. void set_unicode(char32_t p_unicode);
  140. char32_t get_unicode() const;
  141. void set_echo(bool p_enable);
  142. virtual bool is_echo() const override;
  143. Key get_keycode_with_modifiers() const;
  144. Key get_physical_keycode_with_modifiers() const;
  145. 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;
  146. virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const override;
  147. virtual bool is_action_type() const override { return true; }
  148. virtual String as_text() const override;
  149. virtual String to_string() override;
  150. static Ref<InputEventKey> create_reference(Key p_keycode_with_modifier_masks);
  151. InputEventKey() {}
  152. };
  153. class InputEventMouse : public InputEventWithModifiers {
  154. GDCLASS(InputEventMouse, InputEventWithModifiers);
  155. MouseButton button_mask = MouseButton::NONE;
  156. Vector2 pos;
  157. Vector2 global_pos;
  158. protected:
  159. static void _bind_methods();
  160. public:
  161. void set_button_mask(MouseButton p_mask);
  162. MouseButton get_button_mask() const;
  163. void set_position(const Vector2 &p_pos);
  164. Vector2 get_position() const;
  165. void set_global_position(const Vector2 &p_global_pos);
  166. Vector2 get_global_position() const;
  167. InputEventMouse() {}
  168. };
  169. class InputEventMouseButton : public InputEventMouse {
  170. GDCLASS(InputEventMouseButton, InputEventMouse);
  171. float factor = 1;
  172. MouseButton button_index = MouseButton::NONE;
  173. bool pressed = false; //otherwise released
  174. bool double_click = false; //last even less than double click time
  175. protected:
  176. static void _bind_methods();
  177. public:
  178. void set_factor(float p_factor);
  179. float get_factor() const;
  180. void set_button_index(MouseButton p_index);
  181. MouseButton get_button_index() const;
  182. void set_pressed(bool p_pressed);
  183. virtual bool is_pressed() const override;
  184. void set_double_click(bool p_double_click);
  185. bool is_double_click() const;
  186. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  187. 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;
  188. virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const override;
  189. virtual bool is_action_type() const override { return true; }
  190. virtual String as_text() const override;
  191. virtual String to_string() override;
  192. InputEventMouseButton() {}
  193. };
  194. class InputEventMouseMotion : public InputEventMouse {
  195. GDCLASS(InputEventMouseMotion, InputEventMouse);
  196. Vector2 tilt;
  197. float pressure = 0;
  198. Vector2 relative;
  199. Vector2 velocity;
  200. bool pen_inverted = false;
  201. protected:
  202. static void _bind_methods();
  203. public:
  204. void set_tilt(const Vector2 &p_tilt);
  205. Vector2 get_tilt() const;
  206. void set_pressure(float p_pressure);
  207. float get_pressure() const;
  208. void set_pen_inverted(bool p_inverted);
  209. bool get_pen_inverted() const;
  210. void set_relative(const Vector2 &p_relative);
  211. Vector2 get_relative() const;
  212. void set_velocity(const Vector2 &p_velocity);
  213. Vector2 get_velocity() const;
  214. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  215. virtual String as_text() const override;
  216. virtual String to_string() override;
  217. virtual bool accumulate(const Ref<InputEvent> &p_event) override;
  218. InputEventMouseMotion() {}
  219. };
  220. class InputEventJoypadMotion : public InputEvent {
  221. GDCLASS(InputEventJoypadMotion, InputEvent);
  222. JoyAxis axis = (JoyAxis)0; ///< Joypad axis
  223. float axis_value = 0; ///< -1 to 1
  224. protected:
  225. static void _bind_methods();
  226. public:
  227. void set_axis(JoyAxis p_axis);
  228. JoyAxis get_axis() const;
  229. void set_axis_value(float p_value);
  230. float get_axis_value() const;
  231. virtual bool is_pressed() const override;
  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. InputEventJoypadMotion() {}
  238. };
  239. class InputEventJoypadButton : public InputEvent {
  240. GDCLASS(InputEventJoypadButton, InputEvent);
  241. JoyButton button_index = (JoyButton)0;
  242. bool pressed = false;
  243. float pressure = 0; //0 to 1
  244. protected:
  245. static void _bind_methods();
  246. public:
  247. void set_button_index(JoyButton p_index);
  248. JoyButton get_button_index() const;
  249. void set_pressed(bool p_pressed);
  250. virtual bool is_pressed() const override;
  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. static Ref<InputEventJoypadButton> create_reference(JoyButton p_btn_index);
  259. InputEventJoypadButton() {}
  260. };
  261. class InputEventScreenTouch : public InputEventFromWindow {
  262. GDCLASS(InputEventScreenTouch, InputEventFromWindow);
  263. int index = 0;
  264. Vector2 pos;
  265. bool pressed = false;
  266. protected:
  267. static void _bind_methods();
  268. public:
  269. void set_index(int p_index);
  270. int get_index() const;
  271. void set_position(const Vector2 &p_pos);
  272. Vector2 get_position() const;
  273. void set_pressed(bool p_pressed);
  274. virtual bool is_pressed() const override;
  275. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  276. virtual String as_text() const override;
  277. virtual String to_string() override;
  278. InputEventScreenTouch() {}
  279. };
  280. class InputEventScreenDrag : public InputEventFromWindow {
  281. GDCLASS(InputEventScreenDrag, InputEventFromWindow);
  282. int index = 0;
  283. Vector2 pos;
  284. Vector2 relative;
  285. Vector2 velocity;
  286. protected:
  287. static void _bind_methods();
  288. public:
  289. void set_index(int p_index);
  290. int get_index() const;
  291. void set_position(const Vector2 &p_pos);
  292. Vector2 get_position() const;
  293. void set_relative(const Vector2 &p_relative);
  294. Vector2 get_relative() const;
  295. void set_velocity(const Vector2 &p_velocity);
  296. Vector2 get_velocity() const;
  297. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  298. virtual String as_text() const override;
  299. virtual String to_string() override;
  300. virtual bool accumulate(const Ref<InputEvent> &p_event) override;
  301. InputEventScreenDrag() {}
  302. };
  303. class InputEventAction : public InputEvent {
  304. GDCLASS(InputEventAction, InputEvent);
  305. StringName action;
  306. bool pressed = false;
  307. float strength = 1.0f;
  308. protected:
  309. static void _bind_methods();
  310. public:
  311. void set_action(const StringName &p_action);
  312. StringName get_action() const;
  313. void set_pressed(bool p_pressed);
  314. virtual bool is_pressed() const override;
  315. void set_strength(float p_strength);
  316. float get_strength() const;
  317. virtual bool is_action(const StringName &p_action) const;
  318. 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;
  319. virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const override;
  320. virtual bool is_action_type() const override { return true; }
  321. virtual String as_text() const override;
  322. virtual String to_string() override;
  323. InputEventAction() {}
  324. };
  325. class InputEventGesture : public InputEventWithModifiers {
  326. GDCLASS(InputEventGesture, InputEventWithModifiers);
  327. Vector2 pos;
  328. protected:
  329. static void _bind_methods();
  330. public:
  331. void set_position(const Vector2 &p_pos);
  332. Vector2 get_position() const;
  333. };
  334. class InputEventMagnifyGesture : public InputEventGesture {
  335. GDCLASS(InputEventMagnifyGesture, InputEventGesture);
  336. real_t factor = 1.0;
  337. protected:
  338. static void _bind_methods();
  339. public:
  340. void set_factor(real_t p_factor);
  341. real_t get_factor() const;
  342. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  343. virtual String as_text() const override;
  344. virtual String to_string() override;
  345. InputEventMagnifyGesture() {}
  346. };
  347. class InputEventPanGesture : public InputEventGesture {
  348. GDCLASS(InputEventPanGesture, InputEventGesture);
  349. Vector2 delta;
  350. protected:
  351. static void _bind_methods();
  352. public:
  353. void set_delta(const Vector2 &p_delta);
  354. Vector2 get_delta() const;
  355. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  356. virtual String as_text() const override;
  357. virtual String to_string() override;
  358. InputEventPanGesture() {}
  359. };
  360. class InputEventMIDI : public InputEvent {
  361. GDCLASS(InputEventMIDI, InputEvent);
  362. int channel = 0;
  363. MIDIMessage message = MIDIMessage::NONE;
  364. int pitch = 0;
  365. int velocity = 0;
  366. int instrument = 0;
  367. int pressure = 0;
  368. int controller_number = 0;
  369. int controller_value = 0;
  370. protected:
  371. static void _bind_methods();
  372. public:
  373. void set_channel(const int p_channel);
  374. int get_channel() const;
  375. void set_message(const MIDIMessage p_message);
  376. MIDIMessage get_message() const;
  377. void set_pitch(const int p_pitch);
  378. int get_pitch() const;
  379. void set_velocity(const int p_velocity);
  380. int get_velocity() const;
  381. void set_instrument(const int p_instrument);
  382. int get_instrument() const;
  383. void set_pressure(const int p_pressure);
  384. int get_pressure() const;
  385. void set_controller_number(const int p_controller_number);
  386. int get_controller_number() const;
  387. void set_controller_value(const int p_controller_value);
  388. int get_controller_value() const;
  389. virtual String as_text() const override;
  390. virtual String to_string() override;
  391. InputEventMIDI() {}
  392. };
  393. class InputEventShortcut : public InputEvent {
  394. GDCLASS(InputEventShortcut, InputEvent);
  395. Ref<Shortcut> shortcut;
  396. protected:
  397. static void _bind_methods();
  398. public:
  399. void set_shortcut(Ref<Shortcut> p_shortcut);
  400. Ref<Shortcut> get_shortcut();
  401. virtual bool is_pressed() const override;
  402. virtual String as_text() const override;
  403. virtual String to_string() override;
  404. };
  405. #endif // INPUT_EVENT_H