input_event.h 16 KB

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