input_event.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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_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 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. Key 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. uint32_t unicode = 0; ///unicode
  117. bool echo = false; /// true if this is an echo key
  118. protected:
  119. static void _bind_methods();
  120. public:
  121. void set_pressed(bool p_pressed);
  122. virtual bool is_pressed() const override;
  123. void set_keycode(Key p_keycode);
  124. Key get_keycode() const;
  125. void set_physical_keycode(Key p_keycode);
  126. Key get_physical_keycode() const;
  127. void set_unicode(char32_t p_unicode);
  128. char32_t get_unicode() const;
  129. void set_echo(bool p_enable);
  130. virtual bool is_echo() const override;
  131. Key get_keycode_with_modifiers() const;
  132. Key get_physical_keycode_with_modifiers() const;
  133. 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;
  134. virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const override;
  135. virtual bool is_action_type() const override { return true; }
  136. virtual String as_text() const override;
  137. virtual String to_string() override;
  138. static Ref<InputEventKey> create_reference(Key p_keycode_with_modifier_masks);
  139. InputEventKey() {}
  140. };
  141. class InputEventMouse : public InputEventWithModifiers {
  142. GDCLASS(InputEventMouse, InputEventWithModifiers);
  143. MouseButton button_mask = MouseButton::NONE;
  144. Vector2 pos;
  145. Vector2 global_pos;
  146. protected:
  147. static void _bind_methods();
  148. public:
  149. void set_button_mask(MouseButton p_mask);
  150. MouseButton get_button_mask() const;
  151. void set_position(const Vector2 &p_pos);
  152. Vector2 get_position() const;
  153. void set_global_position(const Vector2 &p_global_pos);
  154. Vector2 get_global_position() const;
  155. InputEventMouse() {}
  156. };
  157. class InputEventMouseButton : public InputEventMouse {
  158. GDCLASS(InputEventMouseButton, InputEventMouse);
  159. float factor = 1;
  160. MouseButton button_index = MouseButton::NONE;
  161. bool pressed = false; //otherwise released
  162. bool double_click = false; //last even less than double click time
  163. protected:
  164. static void _bind_methods();
  165. public:
  166. void set_factor(float p_factor);
  167. float get_factor() const;
  168. void set_button_index(MouseButton p_index);
  169. MouseButton get_button_index() const;
  170. void set_pressed(bool p_pressed);
  171. virtual bool is_pressed() const override;
  172. void set_double_click(bool p_double_click);
  173. bool is_double_click() const;
  174. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  175. 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;
  176. virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const override;
  177. virtual bool is_action_type() const override { return true; }
  178. virtual String as_text() const override;
  179. virtual String to_string() override;
  180. InputEventMouseButton() {}
  181. };
  182. class InputEventMouseMotion : public InputEventMouse {
  183. GDCLASS(InputEventMouseMotion, InputEventMouse);
  184. Vector2 tilt;
  185. float pressure = 0;
  186. Vector2 relative;
  187. Vector2 velocity;
  188. bool pen_inverted = false;
  189. protected:
  190. static void _bind_methods();
  191. public:
  192. void set_tilt(const Vector2 &p_tilt);
  193. Vector2 get_tilt() const;
  194. void set_pressure(float p_pressure);
  195. float get_pressure() const;
  196. void set_pen_inverted(bool p_inverted);
  197. bool get_pen_inverted() const;
  198. void set_relative(const Vector2 &p_relative);
  199. Vector2 get_relative() const;
  200. void set_velocity(const Vector2 &p_velocity);
  201. Vector2 get_velocity() const;
  202. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  203. virtual String as_text() const override;
  204. virtual String to_string() override;
  205. virtual bool accumulate(const Ref<InputEvent> &p_event) override;
  206. InputEventMouseMotion() {}
  207. };
  208. class InputEventJoypadMotion : public InputEvent {
  209. GDCLASS(InputEventJoypadMotion, InputEvent);
  210. JoyAxis axis = (JoyAxis)0; ///< Joypad axis
  211. float axis_value = 0; ///< -1 to 1
  212. protected:
  213. static void _bind_methods();
  214. public:
  215. void set_axis(JoyAxis p_axis);
  216. JoyAxis get_axis() const;
  217. void set_axis_value(float p_value);
  218. float get_axis_value() const;
  219. virtual bool is_pressed() const override;
  220. 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;
  221. virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const override;
  222. virtual bool is_action_type() const override { return true; }
  223. virtual String as_text() const override;
  224. virtual String to_string() override;
  225. InputEventJoypadMotion() {}
  226. };
  227. class InputEventJoypadButton : public InputEvent {
  228. GDCLASS(InputEventJoypadButton, InputEvent);
  229. JoyButton button_index = (JoyButton)0;
  230. bool pressed = false;
  231. float pressure = 0; //0 to 1
  232. protected:
  233. static void _bind_methods();
  234. public:
  235. void set_button_index(JoyButton p_index);
  236. JoyButton get_button_index() const;
  237. void set_pressed(bool p_pressed);
  238. virtual bool is_pressed() const override;
  239. void set_pressure(float p_pressure);
  240. float get_pressure() const;
  241. 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;
  242. virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const override;
  243. virtual bool is_action_type() const override { return true; }
  244. virtual String as_text() const override;
  245. virtual String to_string() override;
  246. static Ref<InputEventJoypadButton> create_reference(JoyButton p_btn_index);
  247. InputEventJoypadButton() {}
  248. };
  249. class InputEventScreenTouch : public InputEventFromWindow {
  250. GDCLASS(InputEventScreenTouch, InputEventFromWindow);
  251. int index = 0;
  252. Vector2 pos;
  253. bool pressed = false;
  254. bool double_tap = false;
  255. protected:
  256. static void _bind_methods();
  257. public:
  258. void set_index(int p_index);
  259. int get_index() const;
  260. void set_position(const Vector2 &p_pos);
  261. Vector2 get_position() const;
  262. void set_pressed(bool p_pressed);
  263. virtual bool is_pressed() const override;
  264. void set_double_tap(bool p_double_tap);
  265. bool is_double_tap() const;
  266. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  267. virtual String as_text() const override;
  268. virtual String to_string() override;
  269. InputEventScreenTouch() {}
  270. };
  271. class InputEventScreenDrag : public InputEventFromWindow {
  272. GDCLASS(InputEventScreenDrag, InputEventFromWindow);
  273. int index = 0;
  274. Vector2 pos;
  275. Vector2 relative;
  276. Vector2 velocity;
  277. Vector2 tilt;
  278. float pressure = 0;
  279. bool pen_inverted = false;
  280. protected:
  281. static void _bind_methods();
  282. public:
  283. void set_index(int p_index);
  284. int get_index() const;
  285. void set_tilt(const Vector2 &p_tilt);
  286. Vector2 get_tilt() const;
  287. void set_pressure(float p_pressure);
  288. float get_pressure() const;
  289. void set_pen_inverted(bool p_inverted);
  290. bool get_pen_inverted() 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