input_event.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) 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. virtual void _validate_property(PropertyInfo &property) const override;
  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_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) 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_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) 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 speed;
  200. protected:
  201. static void _bind_methods();
  202. public:
  203. void set_tilt(const Vector2 &p_tilt);
  204. Vector2 get_tilt() const;
  205. void set_pressure(float p_pressure);
  206. float get_pressure() const;
  207. void set_relative(const Vector2 &p_relative);
  208. Vector2 get_relative() const;
  209. void set_speed(const Vector2 &p_speed);
  210. Vector2 get_speed() const;
  211. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  212. virtual String as_text() const override;
  213. virtual String to_string() override;
  214. virtual bool accumulate(const Ref<InputEvent> &p_event) override;
  215. InputEventMouseMotion() {}
  216. };
  217. class InputEventJoypadMotion : public InputEvent {
  218. GDCLASS(InputEventJoypadMotion, InputEvent);
  219. JoyAxis axis = (JoyAxis)0; ///< Joypad axis
  220. float axis_value = 0; ///< -1 to 1
  221. protected:
  222. static void _bind_methods();
  223. public:
  224. void set_axis(JoyAxis p_axis);
  225. JoyAxis get_axis() const;
  226. void set_axis_value(float p_value);
  227. float get_axis_value() const;
  228. virtual bool is_pressed() const override;
  229. virtual bool action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const override;
  230. virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const override;
  231. virtual bool is_action_type() const override { return true; }
  232. virtual String as_text() const override;
  233. virtual String to_string() override;
  234. InputEventJoypadMotion() {}
  235. };
  236. class InputEventJoypadButton : public InputEvent {
  237. GDCLASS(InputEventJoypadButton, InputEvent);
  238. JoyButton button_index = (JoyButton)0;
  239. bool pressed = false;
  240. float pressure = 0; //0 to 1
  241. protected:
  242. static void _bind_methods();
  243. public:
  244. void set_button_index(JoyButton p_index);
  245. JoyButton get_button_index() const;
  246. void set_pressed(bool p_pressed);
  247. virtual bool is_pressed() const override;
  248. void set_pressure(float p_pressure);
  249. float get_pressure() const;
  250. virtual bool action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const override;
  251. virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const override;
  252. virtual bool is_action_type() const override { return true; }
  253. virtual String as_text() const override;
  254. virtual String to_string() override;
  255. static Ref<InputEventJoypadButton> create_reference(JoyButton p_btn_index);
  256. InputEventJoypadButton() {}
  257. };
  258. class InputEventScreenTouch : public InputEventFromWindow {
  259. GDCLASS(InputEventScreenTouch, InputEventFromWindow);
  260. int index = 0;
  261. Vector2 pos;
  262. bool pressed = false;
  263. protected:
  264. static void _bind_methods();
  265. public:
  266. void set_index(int p_index);
  267. int get_index() const;
  268. void set_position(const Vector2 &p_pos);
  269. Vector2 get_position() const;
  270. void set_pressed(bool p_pressed);
  271. virtual bool is_pressed() const override;
  272. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  273. virtual String as_text() const override;
  274. virtual String to_string() override;
  275. InputEventScreenTouch() {}
  276. };
  277. class InputEventScreenDrag : public InputEventFromWindow {
  278. GDCLASS(InputEventScreenDrag, InputEventFromWindow);
  279. int index = 0;
  280. Vector2 pos;
  281. Vector2 relative;
  282. Vector2 speed;
  283. protected:
  284. static void _bind_methods();
  285. public:
  286. void set_index(int p_index);
  287. int get_index() const;
  288. void set_position(const Vector2 &p_pos);
  289. Vector2 get_position() const;
  290. void set_relative(const Vector2 &p_relative);
  291. Vector2 get_relative() const;
  292. void set_speed(const Vector2 &p_speed);
  293. Vector2 get_speed() const;
  294. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  295. virtual String as_text() const override;
  296. virtual String to_string() override;
  297. virtual bool accumulate(const Ref<InputEvent> &p_event) override;
  298. InputEventScreenDrag() {}
  299. };
  300. class InputEventAction : public InputEvent {
  301. GDCLASS(InputEventAction, InputEvent);
  302. StringName action;
  303. bool pressed = false;
  304. float strength = 1.0f;
  305. protected:
  306. static void _bind_methods();
  307. public:
  308. void set_action(const StringName &p_action);
  309. StringName get_action() const;
  310. void set_pressed(bool p_pressed);
  311. virtual bool is_pressed() const override;
  312. void set_strength(float p_strength);
  313. float get_strength() const;
  314. virtual bool is_action(const StringName &p_action) const;
  315. virtual bool action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const override;
  316. virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const override;
  317. virtual bool is_action_type() const override { return true; }
  318. virtual String as_text() const override;
  319. virtual String to_string() override;
  320. InputEventAction() {}
  321. };
  322. class InputEventGesture : public InputEventWithModifiers {
  323. GDCLASS(InputEventGesture, InputEventWithModifiers);
  324. Vector2 pos;
  325. protected:
  326. static void _bind_methods();
  327. public:
  328. void set_position(const Vector2 &p_pos);
  329. Vector2 get_position() const;
  330. };
  331. class InputEventMagnifyGesture : public InputEventGesture {
  332. GDCLASS(InputEventMagnifyGesture, InputEventGesture);
  333. real_t factor = 1.0;
  334. protected:
  335. static void _bind_methods();
  336. public:
  337. void set_factor(real_t p_factor);
  338. real_t get_factor() const;
  339. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  340. virtual String as_text() const override;
  341. virtual String to_string() override;
  342. InputEventMagnifyGesture() {}
  343. };
  344. class InputEventPanGesture : public InputEventGesture {
  345. GDCLASS(InputEventPanGesture, InputEventGesture);
  346. Vector2 delta;
  347. protected:
  348. static void _bind_methods();
  349. public:
  350. void set_delta(const Vector2 &p_delta);
  351. Vector2 get_delta() const;
  352. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  353. virtual String as_text() const override;
  354. virtual String to_string() override;
  355. InputEventPanGesture() {}
  356. };
  357. class InputEventMIDI : public InputEvent {
  358. GDCLASS(InputEventMIDI, InputEvent);
  359. int channel = 0;
  360. MIDIMessage message = MIDIMessage::NONE;
  361. int pitch = 0;
  362. int velocity = 0;
  363. int instrument = 0;
  364. int pressure = 0;
  365. int controller_number = 0;
  366. int controller_value = 0;
  367. protected:
  368. static void _bind_methods();
  369. public:
  370. void set_channel(const int p_channel);
  371. int get_channel() const;
  372. void set_message(const MIDIMessage p_message);
  373. MIDIMessage get_message() const;
  374. void set_pitch(const int p_pitch);
  375. int get_pitch() const;
  376. void set_velocity(const int p_velocity);
  377. int get_velocity() const;
  378. void set_instrument(const int p_instrument);
  379. int get_instrument() const;
  380. void set_pressure(const int p_pressure);
  381. int get_pressure() const;
  382. void set_controller_number(const int p_controller_number);
  383. int get_controller_number() const;
  384. void set_controller_value(const int p_controller_value);
  385. int get_controller_value() const;
  386. virtual String as_text() const override;
  387. virtual String to_string() override;
  388. InputEventMIDI() {}
  389. };
  390. class InputEventShortcut : public InputEvent {
  391. GDCLASS(InputEventShortcut, InputEvent);
  392. Ref<Shortcut> shortcut;
  393. protected:
  394. static void _bind_methods();
  395. public:
  396. void set_shortcut(Ref<Shortcut> p_shortcut);
  397. Ref<Shortcut> get_shortcut();
  398. virtual bool is_pressed() const override;
  399. virtual String as_text() const override;
  400. virtual String to_string() override;
  401. };
  402. #endif // INPUT_EVENT_H