input_event.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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 const int DEVICE_ID_EMULATION;
  55. static const int DEVICE_ID_INTERNAL;
  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. InputEvent() {}
  74. };
  75. class InputEventFromWindow : public InputEvent {
  76. GDCLASS(InputEventFromWindow, InputEvent);
  77. int64_t window_id = 0;
  78. protected:
  79. static void _bind_methods();
  80. public:
  81. void set_window_id(int64_t p_id);
  82. int64_t get_window_id() const;
  83. InputEventFromWindow() {}
  84. };
  85. class InputEventWithModifiers : public InputEventFromWindow {
  86. GDCLASS(InputEventWithModifiers, InputEventFromWindow);
  87. bool command_or_control_autoremap = false;
  88. bool shift_pressed = false;
  89. bool alt_pressed = false;
  90. bool meta_pressed = false; // "Command" on macOS, "Meta/Win" key on other platforms.
  91. bool ctrl_pressed = false;
  92. protected:
  93. static void _bind_methods();
  94. void _validate_property(PropertyInfo &p_property) const;
  95. public:
  96. void set_command_or_control_autoremap(bool p_enabled);
  97. bool is_command_or_control_autoremap() const;
  98. bool is_command_or_control_pressed() const;
  99. void set_shift_pressed(bool p_pressed);
  100. bool is_shift_pressed() const;
  101. void set_alt_pressed(bool p_pressed);
  102. bool is_alt_pressed() const;
  103. void set_ctrl_pressed(bool p_pressed);
  104. bool is_ctrl_pressed() const;
  105. void set_meta_pressed(bool p_pressed);
  106. bool is_meta_pressed() const;
  107. void set_modifiers_from_event(const InputEventWithModifiers *event);
  108. BitField<KeyModifierMask> get_modifiers_mask() const;
  109. virtual String as_text() const override;
  110. virtual String to_string() override;
  111. InputEventWithModifiers() {}
  112. };
  113. class InputEventKey : public InputEventWithModifiers {
  114. GDCLASS(InputEventKey, InputEventWithModifiers);
  115. Key keycode = Key::NONE; // Key enum, without modifier masks.
  116. Key physical_keycode = Key::NONE;
  117. Key key_label = Key::NONE;
  118. uint32_t unicode = 0; ///unicode
  119. KeyLocation location = KeyLocation::UNSPECIFIED;
  120. bool echo = false; /// true if this is an echo key
  121. protected:
  122. static void _bind_methods();
  123. public:
  124. void set_pressed(bool p_pressed);
  125. void set_keycode(Key p_keycode);
  126. Key get_keycode() const;
  127. void set_physical_keycode(Key p_keycode);
  128. Key get_physical_keycode() const;
  129. void set_key_label(Key p_key_label);
  130. Key get_key_label() const;
  131. void set_unicode(char32_t p_unicode);
  132. char32_t get_unicode() const;
  133. void set_location(KeyLocation p_key_location);
  134. KeyLocation get_location() const;
  135. void set_echo(bool p_enable);
  136. virtual bool is_echo() const override;
  137. Key get_keycode_with_modifiers() const;
  138. Key get_physical_keycode_with_modifiers() const;
  139. Key get_key_label_with_modifiers() const;
  140. 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;
  141. virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const override;
  142. virtual bool is_action_type() const override { return true; }
  143. virtual String as_text_physical_keycode() const;
  144. virtual String as_text_keycode() const;
  145. virtual String as_text_key_label() const;
  146. virtual String as_text_location() const;
  147. virtual String as_text() const override;
  148. virtual String to_string() override;
  149. static Ref<InputEventKey> create_reference(Key p_keycode_with_modifier_masks, bool p_physical = false);
  150. InputEventKey() {}
  151. };
  152. class InputEventMouse : public InputEventWithModifiers {
  153. GDCLASS(InputEventMouse, InputEventWithModifiers);
  154. BitField<MouseButtonMask> button_mask = MouseButtonMask::NONE;
  155. Vector2 pos;
  156. Vector2 global_pos;
  157. protected:
  158. static void _bind_methods();
  159. public:
  160. void set_button_mask(BitField<MouseButtonMask> p_mask);
  161. BitField<MouseButtonMask> 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 = MouseButton::NONE;
  172. bool double_click = false; //last even less than double click time
  173. protected:
  174. static void _bind_methods();
  175. public:
  176. void set_factor(float p_factor);
  177. float get_factor() const;
  178. void set_button_index(MouseButton p_index);
  179. MouseButton get_button_index() const;
  180. void set_pressed(bool p_pressed);
  181. void set_canceled(bool p_canceled);
  182. void set_double_click(bool p_double_click);
  183. bool is_double_click() const;
  184. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  185. 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;
  186. virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const override;
  187. virtual bool is_action_type() const override { return true; }
  188. virtual String as_text() const override;
  189. virtual String to_string() override;
  190. InputEventMouseButton() {}
  191. };
  192. class InputEventMouseMotion : public InputEventMouse {
  193. GDCLASS(InputEventMouseMotion, InputEventMouse);
  194. Vector2 tilt;
  195. float pressure = 0;
  196. Vector2 relative;
  197. Vector2 screen_relative;
  198. Vector2 velocity;
  199. Vector2 screen_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_relative_screen_position(const Vector2 &p_relative);
  213. Vector2 get_relative_screen_position() const;
  214. void set_velocity(const Vector2 &p_velocity);
  215. Vector2 get_velocity() const;
  216. void set_screen_velocity(const Vector2 &p_velocity);
  217. Vector2 get_screen_velocity() const;
  218. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  219. virtual String as_text() const override;
  220. virtual String to_string() override;
  221. virtual bool accumulate(const Ref<InputEvent> &p_event) override;
  222. InputEventMouseMotion() {}
  223. };
  224. class InputEventJoypadMotion : public InputEvent {
  225. GDCLASS(InputEventJoypadMotion, InputEvent);
  226. JoyAxis axis = (JoyAxis)0; ///< Joypad axis
  227. float axis_value = 0; ///< -1 to 1
  228. protected:
  229. static void _bind_methods();
  230. public:
  231. void set_axis(JoyAxis p_axis);
  232. JoyAxis get_axis() const;
  233. void set_axis_value(float p_value);
  234. float get_axis_value() const;
  235. 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;
  236. virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const override;
  237. virtual bool is_action_type() const override { return true; }
  238. virtual String as_text() const override;
  239. virtual String to_string() override;
  240. static Ref<InputEventJoypadMotion> create_reference(JoyAxis p_axis, float p_value);
  241. InputEventJoypadMotion() {}
  242. };
  243. class InputEventJoypadButton : public InputEvent {
  244. GDCLASS(InputEventJoypadButton, InputEvent);
  245. JoyButton button_index = (JoyButton)0;
  246. float pressure = 0; //0 to 1
  247. protected:
  248. static void _bind_methods();
  249. public:
  250. void set_button_index(JoyButton p_index);
  251. JoyButton get_button_index() const;
  252. void set_pressed(bool p_pressed);
  253. void set_pressure(float p_pressure);
  254. float get_pressure() const;
  255. 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;
  256. virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const override;
  257. virtual bool is_action_type() const override { return true; }
  258. virtual String as_text() const override;
  259. virtual String to_string() override;
  260. static Ref<InputEventJoypadButton> create_reference(JoyButton p_btn_index);
  261. InputEventJoypadButton() {}
  262. };
  263. class InputEventScreenTouch : public InputEventFromWindow {
  264. GDCLASS(InputEventScreenTouch, InputEventFromWindow);
  265. int index = 0;
  266. Vector2 pos;
  267. bool double_tap = false;
  268. protected:
  269. static void _bind_methods();
  270. public:
  271. void set_index(int p_index);
  272. int get_index() const;
  273. void set_position(const Vector2 &p_pos);
  274. Vector2 get_position() const;
  275. void set_pressed(bool p_pressed);
  276. void set_canceled(bool p_canceled);
  277. void set_double_tap(bool p_double_tap);
  278. bool is_double_tap() const;
  279. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  280. virtual String as_text() const override;
  281. virtual String to_string() override;
  282. InputEventScreenTouch() {}
  283. };
  284. class InputEventScreenDrag : public InputEventFromWindow {
  285. GDCLASS(InputEventScreenDrag, InputEventFromWindow);
  286. int index = 0;
  287. Vector2 pos;
  288. Vector2 relative;
  289. Vector2 screen_relative;
  290. Vector2 velocity;
  291. Vector2 screen_velocity;
  292. Vector2 tilt;
  293. float pressure = 0;
  294. bool pen_inverted = false;
  295. protected:
  296. static void _bind_methods();
  297. public:
  298. void set_index(int p_index);
  299. int get_index() const;
  300. void set_tilt(const Vector2 &p_tilt);
  301. Vector2 get_tilt() const;
  302. void set_pressure(float p_pressure);
  303. float get_pressure() const;
  304. void set_pen_inverted(bool p_inverted);
  305. bool get_pen_inverted() const;
  306. void set_position(const Vector2 &p_pos);
  307. Vector2 get_position() const;
  308. void set_relative(const Vector2 &p_relative);
  309. Vector2 get_relative() const;
  310. void set_relative_screen_position(const Vector2 &p_relative);
  311. Vector2 get_relative_screen_position() const;
  312. void set_velocity(const Vector2 &p_velocity);
  313. Vector2 get_velocity() const;
  314. void set_screen_velocity(const Vector2 &p_velocity);
  315. Vector2 get_screen_velocity() const;
  316. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  317. virtual String as_text() const override;
  318. virtual String to_string() override;
  319. virtual bool accumulate(const Ref<InputEvent> &p_event) override;
  320. InputEventScreenDrag() {}
  321. };
  322. class InputEventAction : public InputEvent {
  323. GDCLASS(InputEventAction, InputEvent);
  324. StringName action;
  325. float strength = 1.0f;
  326. int event_index = -1;
  327. protected:
  328. static void _bind_methods();
  329. public:
  330. void set_action(const StringName &p_action);
  331. StringName get_action() const;
  332. void set_pressed(bool p_pressed);
  333. void set_strength(float p_strength);
  334. float get_strength() const;
  335. void set_event_index(int p_index);
  336. int get_event_index() const;
  337. virtual bool is_action(const StringName &p_action) const;
  338. 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;
  339. virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const override;
  340. virtual bool is_action_type() const override { return true; }
  341. virtual String as_text() const override;
  342. virtual String to_string() override;
  343. InputEventAction() {}
  344. };
  345. class InputEventGesture : public InputEventWithModifiers {
  346. GDCLASS(InputEventGesture, InputEventWithModifiers);
  347. Vector2 pos;
  348. protected:
  349. static void _bind_methods();
  350. public:
  351. void set_position(const Vector2 &p_pos);
  352. Vector2 get_position() const;
  353. };
  354. class InputEventMagnifyGesture : public InputEventGesture {
  355. GDCLASS(InputEventMagnifyGesture, InputEventGesture);
  356. real_t factor = 1.0;
  357. protected:
  358. static void _bind_methods();
  359. public:
  360. void set_factor(real_t p_factor);
  361. real_t get_factor() const;
  362. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  363. virtual String as_text() const override;
  364. virtual String to_string() override;
  365. InputEventMagnifyGesture() {}
  366. };
  367. class InputEventPanGesture : public InputEventGesture {
  368. GDCLASS(InputEventPanGesture, InputEventGesture);
  369. Vector2 delta;
  370. protected:
  371. static void _bind_methods();
  372. public:
  373. void set_delta(const Vector2 &p_delta);
  374. Vector2 get_delta() const;
  375. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  376. virtual String as_text() const override;
  377. virtual String to_string() override;
  378. InputEventPanGesture() {}
  379. };
  380. class InputEventMIDI : public InputEvent {
  381. GDCLASS(InputEventMIDI, InputEvent);
  382. int channel = 0;
  383. MIDIMessage message = MIDIMessage::NONE;
  384. int pitch = 0;
  385. int velocity = 0;
  386. int instrument = 0;
  387. int pressure = 0;
  388. int controller_number = 0;
  389. int controller_value = 0;
  390. protected:
  391. static void _bind_methods();
  392. public:
  393. void set_channel(const int p_channel);
  394. int get_channel() const;
  395. void set_message(const MIDIMessage p_message);
  396. MIDIMessage get_message() const;
  397. void set_pitch(const int p_pitch);
  398. int get_pitch() const;
  399. void set_velocity(const int p_velocity);
  400. int get_velocity() const;
  401. void set_instrument(const int p_instrument);
  402. int get_instrument() const;
  403. void set_pressure(const int p_pressure);
  404. int get_pressure() const;
  405. void set_controller_number(const int p_controller_number);
  406. int get_controller_number() const;
  407. void set_controller_value(const int p_controller_value);
  408. int get_controller_value() const;
  409. virtual String as_text() const override;
  410. virtual String to_string() override;
  411. InputEventMIDI() {}
  412. };
  413. class InputEventShortcut : public InputEvent {
  414. GDCLASS(InputEventShortcut, InputEvent);
  415. Ref<Shortcut> shortcut;
  416. protected:
  417. static void _bind_methods();
  418. public:
  419. void set_shortcut(Ref<Shortcut> p_shortcut);
  420. Ref<Shortcut> get_shortcut();
  421. virtual String as_text() const override;
  422. virtual String to_string() override;
  423. InputEventShortcut();
  424. };