input_event.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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. #pragma once
  31. #include "core/input/input_enums.h"
  32. #include "core/io/resource.h"
  33. #include "core/math/transform_2d.h"
  34. #include "core/os/keyboard.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. bool canceled = false;
  51. bool pressed = false;
  52. static void _bind_methods();
  53. public:
  54. static constexpr int DEVICE_ID_EMULATION = -1;
  55. static constexpr int DEVICE_ID_INTERNAL = -2;
  56. void set_device(int p_device);
  57. int get_device() const;
  58. bool is_action(const StringName &p_action, bool p_exact_match = false) const;
  59. bool is_action_pressed(const StringName &p_action, bool p_allow_echo = false, bool p_exact_match = false) const;
  60. bool is_action_released(const StringName &p_action, bool p_exact_match = false) const;
  61. float get_action_strength(const StringName &p_action, bool p_exact_match = false) const;
  62. float get_action_raw_strength(const StringName &p_action, bool p_exact_match = false) const;
  63. bool is_canceled() const;
  64. bool is_pressed() const;
  65. bool is_released() const;
  66. virtual bool is_echo() const;
  67. virtual String as_text() const = 0;
  68. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const;
  69. 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;
  70. virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const;
  71. virtual bool is_action_type() const;
  72. virtual bool accumulate(const Ref<InputEvent> &p_event) { return false; }
  73. virtual InputEventType get_type() const { return InputEventType::INVALID; }
  74. InputEvent() {}
  75. };
  76. class InputEventFromWindow : public InputEvent {
  77. GDCLASS(InputEventFromWindow, InputEvent);
  78. int64_t window_id = 0;
  79. protected:
  80. static void _bind_methods();
  81. public:
  82. void set_window_id(int64_t p_id);
  83. int64_t get_window_id() const;
  84. InputEventFromWindow() {}
  85. };
  86. class InputEventWithModifiers : public InputEventFromWindow {
  87. GDCLASS(InputEventWithModifiers, InputEventFromWindow);
  88. bool command_or_control_autoremap = false;
  89. bool shift_pressed = false;
  90. bool alt_pressed = false;
  91. bool meta_pressed = false; // "Command" on macOS, "Meta/Win" key on other platforms.
  92. bool ctrl_pressed = false;
  93. protected:
  94. static void _bind_methods();
  95. void _validate_property(PropertyInfo &p_property) const;
  96. public:
  97. void set_command_or_control_autoremap(bool p_enabled);
  98. bool is_command_or_control_autoremap() const;
  99. bool is_command_or_control_pressed() const;
  100. void set_shift_pressed(bool p_pressed);
  101. bool is_shift_pressed() const;
  102. void set_alt_pressed(bool p_pressed);
  103. bool is_alt_pressed() const;
  104. void set_ctrl_pressed(bool p_pressed);
  105. bool is_ctrl_pressed() const;
  106. void set_meta_pressed(bool p_pressed);
  107. bool is_meta_pressed() const;
  108. void set_modifiers_from_event(const InputEventWithModifiers *event);
  109. BitField<KeyModifierMask> get_modifiers_mask() const;
  110. virtual String as_text() const override;
  111. virtual String to_string() override;
  112. InputEventWithModifiers() {}
  113. };
  114. class InputEventKey : public InputEventWithModifiers {
  115. GDCLASS(InputEventKey, InputEventWithModifiers);
  116. Key keycode = Key::NONE; // Key enum, without modifier masks.
  117. Key physical_keycode = Key::NONE;
  118. Key key_label = Key::NONE;
  119. uint32_t unicode = 0; ///unicode
  120. KeyLocation location = KeyLocation::UNSPECIFIED;
  121. bool echo = false; /// true if this is an echo key
  122. protected:
  123. static void _bind_methods();
  124. public:
  125. void set_pressed(bool p_pressed);
  126. void set_keycode(Key p_keycode);
  127. Key get_keycode() const;
  128. void set_physical_keycode(Key p_keycode);
  129. Key get_physical_keycode() const;
  130. void set_key_label(Key p_key_label);
  131. Key get_key_label() const;
  132. void set_unicode(char32_t p_unicode);
  133. char32_t get_unicode() const;
  134. void set_location(KeyLocation p_key_location);
  135. KeyLocation get_location() const;
  136. void set_echo(bool p_enable);
  137. virtual bool is_echo() const override;
  138. Key get_keycode_with_modifiers() const;
  139. Key get_physical_keycode_with_modifiers() const;
  140. Key get_key_label_with_modifiers() const;
  141. 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;
  142. virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const override;
  143. virtual bool is_action_type() const override { return true; }
  144. virtual String as_text_physical_keycode() const;
  145. virtual String as_text_keycode() const;
  146. virtual String as_text_key_label() const;
  147. virtual String as_text_location() const;
  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, bool p_physical = false);
  151. InputEventType get_type() const final override { return InputEventType::KEY; }
  152. InputEventKey() {}
  153. };
  154. class InputEventMouse : public InputEventWithModifiers {
  155. GDCLASS(InputEventMouse, InputEventWithModifiers);
  156. BitField<MouseButtonMask> button_mask = MouseButtonMask::NONE;
  157. Vector2 pos;
  158. Vector2 global_pos;
  159. protected:
  160. static void _bind_methods();
  161. public:
  162. void set_button_mask(BitField<MouseButtonMask> p_mask);
  163. BitField<MouseButtonMask> get_button_mask() const;
  164. void set_position(const Vector2 &p_pos);
  165. Vector2 get_position() const;
  166. void set_global_position(const Vector2 &p_global_pos);
  167. Vector2 get_global_position() const;
  168. InputEventMouse() {}
  169. };
  170. class InputEventMouseButton : public InputEventMouse {
  171. GDCLASS(InputEventMouseButton, InputEventMouse);
  172. float factor = 1;
  173. MouseButton button_index = MouseButton::NONE;
  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. void set_canceled(bool p_canceled);
  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. InputEventType get_type() const final override { return InputEventType::MOUSE_BUTTON; }
  193. InputEventMouseButton() {}
  194. };
  195. class InputEventMouseMotion : public InputEventMouse {
  196. GDCLASS(InputEventMouseMotion, InputEventMouse);
  197. Vector2 tilt;
  198. float pressure = 0;
  199. Vector2 relative;
  200. Vector2 screen_relative;
  201. Vector2 velocity;
  202. Vector2 screen_velocity;
  203. bool pen_inverted = false;
  204. protected:
  205. static void _bind_methods();
  206. public:
  207. void set_tilt(const Vector2 &p_tilt);
  208. Vector2 get_tilt() const;
  209. void set_pressure(float p_pressure);
  210. float get_pressure() const;
  211. void set_pen_inverted(bool p_inverted);
  212. bool get_pen_inverted() const;
  213. void set_relative(const Vector2 &p_relative);
  214. Vector2 get_relative() const;
  215. void set_relative_screen_position(const Vector2 &p_relative);
  216. Vector2 get_relative_screen_position() const;
  217. void set_velocity(const Vector2 &p_velocity);
  218. Vector2 get_velocity() const;
  219. void set_screen_velocity(const Vector2 &p_velocity);
  220. Vector2 get_screen_velocity() const;
  221. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  222. virtual String as_text() const override;
  223. virtual String to_string() override;
  224. virtual bool accumulate(const Ref<InputEvent> &p_event) override;
  225. InputEventType get_type() const final override { return InputEventType::MOUSE_MOTION; }
  226. InputEventMouseMotion() {}
  227. };
  228. class InputEventJoypadMotion : public InputEvent {
  229. GDCLASS(InputEventJoypadMotion, InputEvent);
  230. JoyAxis axis = (JoyAxis)0; ///< Joypad axis
  231. float axis_value = 0; ///< -1 to 1
  232. protected:
  233. static void _bind_methods();
  234. public:
  235. void set_axis(JoyAxis p_axis);
  236. JoyAxis get_axis() const;
  237. void set_axis_value(float p_value);
  238. float get_axis_value() const;
  239. 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;
  240. virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const override;
  241. virtual bool is_action_type() const override { return true; }
  242. virtual String as_text() const override;
  243. virtual String to_string() override;
  244. static Ref<InputEventJoypadMotion> create_reference(JoyAxis p_axis, float p_value);
  245. InputEventType get_type() const final override { return InputEventType::JOY_MOTION; }
  246. InputEventJoypadMotion() {}
  247. };
  248. class InputEventJoypadButton : public InputEvent {
  249. GDCLASS(InputEventJoypadButton, InputEvent);
  250. JoyButton button_index = (JoyButton)0;
  251. float pressure = 0; //0 to 1
  252. protected:
  253. static void _bind_methods();
  254. public:
  255. void set_button_index(JoyButton p_index);
  256. JoyButton get_button_index() const;
  257. void set_pressed(bool p_pressed);
  258. void set_pressure(float p_pressure);
  259. float get_pressure() const;
  260. 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;
  261. virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const override;
  262. virtual bool is_action_type() const override { return true; }
  263. virtual String as_text() const override;
  264. virtual String to_string() override;
  265. static Ref<InputEventJoypadButton> create_reference(JoyButton p_btn_index);
  266. InputEventType get_type() const final override { return InputEventType::JOY_BUTTON; }
  267. InputEventJoypadButton() {}
  268. };
  269. class InputEventScreenTouch : public InputEventFromWindow {
  270. GDCLASS(InputEventScreenTouch, InputEventFromWindow);
  271. int index = 0;
  272. Vector2 pos;
  273. bool double_tap = false;
  274. protected:
  275. static void _bind_methods();
  276. public:
  277. void set_index(int p_index);
  278. int get_index() const;
  279. void set_position(const Vector2 &p_pos);
  280. Vector2 get_position() const;
  281. void set_pressed(bool p_pressed);
  282. void set_canceled(bool p_canceled);
  283. void set_double_tap(bool p_double_tap);
  284. bool is_double_tap() const;
  285. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  286. virtual String as_text() const override;
  287. virtual String to_string() override;
  288. InputEventType get_type() const final override { return InputEventType::SCREEN_TOUCH; }
  289. InputEventScreenTouch() {}
  290. };
  291. class InputEventScreenDrag : public InputEventFromWindow {
  292. GDCLASS(InputEventScreenDrag, InputEventFromWindow);
  293. int index = 0;
  294. Vector2 pos;
  295. Vector2 relative;
  296. Vector2 screen_relative;
  297. Vector2 velocity;
  298. Vector2 screen_velocity;
  299. Vector2 tilt;
  300. float pressure = 0;
  301. bool pen_inverted = false;
  302. protected:
  303. static void _bind_methods();
  304. public:
  305. void set_index(int p_index);
  306. int get_index() const;
  307. void set_tilt(const Vector2 &p_tilt);
  308. Vector2 get_tilt() const;
  309. void set_pressure(float p_pressure);
  310. float get_pressure() const;
  311. void set_pen_inverted(bool p_inverted);
  312. bool get_pen_inverted() const;
  313. void set_position(const Vector2 &p_pos);
  314. Vector2 get_position() const;
  315. void set_relative(const Vector2 &p_relative);
  316. Vector2 get_relative() const;
  317. void set_relative_screen_position(const Vector2 &p_relative);
  318. Vector2 get_relative_screen_position() const;
  319. void set_velocity(const Vector2 &p_velocity);
  320. Vector2 get_velocity() const;
  321. void set_screen_velocity(const Vector2 &p_velocity);
  322. Vector2 get_screen_velocity() const;
  323. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  324. virtual String as_text() const override;
  325. virtual String to_string() override;
  326. virtual bool accumulate(const Ref<InputEvent> &p_event) override;
  327. InputEventType get_type() const final override { return InputEventType::SCREEN_DRAG; }
  328. InputEventScreenDrag() {}
  329. };
  330. class InputEventAction : public InputEvent {
  331. GDCLASS(InputEventAction, InputEvent);
  332. StringName action;
  333. float strength = 1.0f;
  334. int event_index = -1;
  335. protected:
  336. static void _bind_methods();
  337. public:
  338. void set_action(const StringName &p_action);
  339. StringName get_action() const;
  340. void set_pressed(bool p_pressed);
  341. void set_strength(float p_strength);
  342. float get_strength() const;
  343. void set_event_index(int p_index);
  344. int get_event_index() const;
  345. virtual bool is_action(const StringName &p_action) const;
  346. 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;
  347. virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const override;
  348. virtual bool is_action_type() const override { return true; }
  349. virtual String as_text() const override;
  350. virtual String to_string() override;
  351. InputEventType get_type() const final override { return InputEventType::ACTION; }
  352. InputEventAction() {}
  353. };
  354. class InputEventGesture : public InputEventWithModifiers {
  355. GDCLASS(InputEventGesture, InputEventWithModifiers);
  356. Vector2 pos;
  357. protected:
  358. static void _bind_methods();
  359. public:
  360. void set_position(const Vector2 &p_pos);
  361. Vector2 get_position() const;
  362. };
  363. class InputEventMagnifyGesture : public InputEventGesture {
  364. GDCLASS(InputEventMagnifyGesture, InputEventGesture);
  365. real_t factor = 1.0;
  366. protected:
  367. static void _bind_methods();
  368. public:
  369. void set_factor(real_t p_factor);
  370. real_t get_factor() const;
  371. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  372. virtual String as_text() const override;
  373. virtual String to_string() override;
  374. InputEventType get_type() const final override { return InputEventType::MAGNIFY_GESTURE; }
  375. InputEventMagnifyGesture() {}
  376. };
  377. class InputEventPanGesture : public InputEventGesture {
  378. GDCLASS(InputEventPanGesture, InputEventGesture);
  379. Vector2 delta;
  380. protected:
  381. static void _bind_methods();
  382. public:
  383. void set_delta(const Vector2 &p_delta);
  384. Vector2 get_delta() const;
  385. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  386. virtual String as_text() const override;
  387. virtual String to_string() override;
  388. InputEventType get_type() const final override { return InputEventType::PAN_GESTURE; }
  389. InputEventPanGesture() {}
  390. };
  391. class InputEventMIDI : public InputEvent {
  392. GDCLASS(InputEventMIDI, InputEvent);
  393. int channel = 0;
  394. MIDIMessage message = MIDIMessage::NONE;
  395. int pitch = 0;
  396. int velocity = 0;
  397. int instrument = 0;
  398. int pressure = 0;
  399. int controller_number = 0;
  400. int controller_value = 0;
  401. protected:
  402. static void _bind_methods();
  403. public:
  404. void set_channel(const int p_channel);
  405. int get_channel() const;
  406. void set_message(const MIDIMessage p_message);
  407. MIDIMessage get_message() const;
  408. void set_pitch(const int p_pitch);
  409. int get_pitch() const;
  410. void set_velocity(const int p_velocity);
  411. int get_velocity() const;
  412. void set_instrument(const int p_instrument);
  413. int get_instrument() const;
  414. void set_pressure(const int p_pressure);
  415. int get_pressure() const;
  416. void set_controller_number(const int p_controller_number);
  417. int get_controller_number() const;
  418. void set_controller_value(const int p_controller_value);
  419. int get_controller_value() const;
  420. virtual String as_text() const override;
  421. virtual String to_string() override;
  422. InputEventType get_type() const final override { return InputEventType::MIDI; }
  423. InputEventMIDI() {}
  424. };
  425. class InputEventShortcut : public InputEvent {
  426. GDCLASS(InputEventShortcut, InputEvent);
  427. Ref<Shortcut> shortcut;
  428. protected:
  429. static void _bind_methods();
  430. public:
  431. void set_shortcut(Ref<Shortcut> p_shortcut);
  432. Ref<Shortcut> get_shortcut();
  433. virtual String as_text() const override;
  434. virtual String to_string() override;
  435. InputEventType get_type() const final override { return InputEventType::SHORTCUT; }
  436. InputEventShortcut();
  437. };