input_event.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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. bool canceled = false;
  52. bool pressed = false;
  53. static void _bind_methods();
  54. public:
  55. static const int DEVICE_ID_EMULATION;
  56. static const int DEVICE_ID_INTERNAL;
  57. void set_device(int p_device);
  58. int get_device() const;
  59. bool is_action(const StringName &p_action, bool p_exact_match = false) const;
  60. bool is_action_pressed(const StringName &p_action, bool p_allow_echo = false, bool p_exact_match = false) const;
  61. bool is_action_released(const StringName &p_action, bool p_exact_match = false) const;
  62. float get_action_strength(const StringName &p_action, bool p_exact_match = false) const;
  63. float get_action_raw_strength(const StringName &p_action, bool p_exact_match = false) const;
  64. bool is_canceled() const;
  65. bool is_pressed() const;
  66. bool is_released() const;
  67. virtual bool is_echo() const;
  68. virtual String as_text() const = 0;
  69. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const;
  70. 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;
  71. virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const;
  72. virtual bool is_action_type() const;
  73. virtual bool accumulate(const Ref<InputEvent> &p_event) { return false; }
  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. InputEventKey() {}
  152. };
  153. class InputEventMouse : public InputEventWithModifiers {
  154. GDCLASS(InputEventMouse, InputEventWithModifiers);
  155. BitField<MouseButtonMask> button_mask;
  156. Vector2 pos;
  157. Vector2 global_pos;
  158. protected:
  159. static void _bind_methods();
  160. public:
  161. void set_button_mask(BitField<MouseButtonMask> p_mask);
  162. BitField<MouseButtonMask> 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 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. void set_canceled(bool p_canceled);
  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_exact_match, float p_deadzone, bool *r_pressed, float *r_strength, float *r_raw_strength) 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 screen_relative;
  199. Vector2 velocity;
  200. Vector2 screen_velocity;
  201. bool pen_inverted = false;
  202. protected:
  203. static void _bind_methods();
  204. public:
  205. void set_tilt(const Vector2 &p_tilt);
  206. Vector2 get_tilt() const;
  207. void set_pressure(float p_pressure);
  208. float get_pressure() const;
  209. void set_pen_inverted(bool p_inverted);
  210. bool get_pen_inverted() const;
  211. void set_relative(const Vector2 &p_relative);
  212. Vector2 get_relative() const;
  213. void set_relative_screen_position(const Vector2 &p_relative);
  214. Vector2 get_relative_screen_position() const;
  215. void set_velocity(const Vector2 &p_velocity);
  216. Vector2 get_velocity() const;
  217. void set_screen_velocity(const Vector2 &p_velocity);
  218. Vector2 get_screen_velocity() const;
  219. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  220. virtual String as_text() const override;
  221. virtual String to_string() override;
  222. virtual bool accumulate(const Ref<InputEvent> &p_event) override;
  223. InputEventMouseMotion() {}
  224. };
  225. class InputEventJoypadMotion : public InputEvent {
  226. GDCLASS(InputEventJoypadMotion, InputEvent);
  227. JoyAxis axis = (JoyAxis)0; ///< Joypad axis
  228. float axis_value = 0; ///< -1 to 1
  229. protected:
  230. static void _bind_methods();
  231. public:
  232. void set_axis(JoyAxis p_axis);
  233. JoyAxis get_axis() const;
  234. void set_axis_value(float p_value);
  235. float get_axis_value() const;
  236. 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;
  237. virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const override;
  238. virtual bool is_action_type() const override { return true; }
  239. virtual String as_text() const override;
  240. virtual String to_string() override;
  241. static Ref<InputEventJoypadMotion> create_reference(JoyAxis p_axis, float p_value);
  242. InputEventJoypadMotion() {}
  243. };
  244. class InputEventJoypadButton : public InputEvent {
  245. GDCLASS(InputEventJoypadButton, InputEvent);
  246. JoyButton button_index = (JoyButton)0;
  247. float pressure = 0; //0 to 1
  248. protected:
  249. static void _bind_methods();
  250. public:
  251. void set_button_index(JoyButton p_index);
  252. JoyButton get_button_index() const;
  253. void set_pressed(bool p_pressed);
  254. void set_pressure(float p_pressure);
  255. float get_pressure() const;
  256. 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;
  257. virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const override;
  258. virtual bool is_action_type() const override { return true; }
  259. virtual String as_text() const override;
  260. virtual String to_string() override;
  261. static Ref<InputEventJoypadButton> create_reference(JoyButton p_btn_index);
  262. InputEventJoypadButton() {}
  263. };
  264. class InputEventScreenTouch : public InputEventFromWindow {
  265. GDCLASS(InputEventScreenTouch, InputEventFromWindow);
  266. int index = 0;
  267. Vector2 pos;
  268. bool double_tap = false;
  269. protected:
  270. static void _bind_methods();
  271. public:
  272. void set_index(int p_index);
  273. int get_index() const;
  274. void set_position(const Vector2 &p_pos);
  275. Vector2 get_position() const;
  276. void set_pressed(bool p_pressed);
  277. void set_canceled(bool p_canceled);
  278. void set_double_tap(bool p_double_tap);
  279. bool is_double_tap() const;
  280. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  281. virtual String as_text() const override;
  282. virtual String to_string() override;
  283. InputEventScreenTouch() {}
  284. };
  285. class InputEventScreenDrag : public InputEventFromWindow {
  286. GDCLASS(InputEventScreenDrag, InputEventFromWindow);
  287. int index = 0;
  288. Vector2 pos;
  289. Vector2 relative;
  290. Vector2 screen_relative;
  291. Vector2 velocity;
  292. Vector2 screen_velocity;
  293. Vector2 tilt;
  294. float pressure = 0;
  295. bool pen_inverted = false;
  296. protected:
  297. static void _bind_methods();
  298. public:
  299. void set_index(int p_index);
  300. int get_index() const;
  301. void set_tilt(const Vector2 &p_tilt);
  302. Vector2 get_tilt() const;
  303. void set_pressure(float p_pressure);
  304. float get_pressure() const;
  305. void set_pen_inverted(bool p_inverted);
  306. bool get_pen_inverted() const;
  307. void set_position(const Vector2 &p_pos);
  308. Vector2 get_position() const;
  309. void set_relative(const Vector2 &p_relative);
  310. Vector2 get_relative() const;
  311. void set_relative_screen_position(const Vector2 &p_relative);
  312. Vector2 get_relative_screen_position() const;
  313. void set_velocity(const Vector2 &p_velocity);
  314. Vector2 get_velocity() const;
  315. void set_screen_velocity(const Vector2 &p_velocity);
  316. Vector2 get_screen_velocity() const;
  317. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  318. virtual String as_text() const override;
  319. virtual String to_string() override;
  320. virtual bool accumulate(const Ref<InputEvent> &p_event) override;
  321. InputEventScreenDrag() {}
  322. };
  323. class InputEventAction : public InputEvent {
  324. GDCLASS(InputEventAction, InputEvent);
  325. StringName action;
  326. float strength = 1.0f;
  327. int event_index = -1;
  328. protected:
  329. static void _bind_methods();
  330. public:
  331. void set_action(const StringName &p_action);
  332. StringName get_action() const;
  333. void set_pressed(bool p_pressed);
  334. void set_strength(float p_strength);
  335. float get_strength() const;
  336. void set_event_index(int p_index);
  337. int get_event_index() const;
  338. virtual bool is_action(const StringName &p_action) const;
  339. 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;
  340. virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const override;
  341. virtual bool is_action_type() const override { return true; }
  342. virtual String as_text() const override;
  343. virtual String to_string() override;
  344. InputEventAction() {}
  345. };
  346. class InputEventGesture : public InputEventWithModifiers {
  347. GDCLASS(InputEventGesture, InputEventWithModifiers);
  348. Vector2 pos;
  349. protected:
  350. static void _bind_methods();
  351. public:
  352. void set_position(const Vector2 &p_pos);
  353. Vector2 get_position() const;
  354. };
  355. class InputEventMagnifyGesture : public InputEventGesture {
  356. GDCLASS(InputEventMagnifyGesture, InputEventGesture);
  357. real_t factor = 1.0;
  358. protected:
  359. static void _bind_methods();
  360. public:
  361. void set_factor(real_t p_factor);
  362. real_t get_factor() const;
  363. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  364. virtual String as_text() const override;
  365. virtual String to_string() override;
  366. InputEventMagnifyGesture() {}
  367. };
  368. class InputEventPanGesture : public InputEventGesture {
  369. GDCLASS(InputEventPanGesture, InputEventGesture);
  370. Vector2 delta;
  371. protected:
  372. static void _bind_methods();
  373. public:
  374. void set_delta(const Vector2 &p_delta);
  375. Vector2 get_delta() const;
  376. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  377. virtual String as_text() const override;
  378. virtual String to_string() override;
  379. InputEventPanGesture() {}
  380. };
  381. class InputEventMIDI : public InputEvent {
  382. GDCLASS(InputEventMIDI, InputEvent);
  383. int channel = 0;
  384. MIDIMessage message = MIDIMessage::NONE;
  385. int pitch = 0;
  386. int velocity = 0;
  387. int instrument = 0;
  388. int pressure = 0;
  389. int controller_number = 0;
  390. int controller_value = 0;
  391. protected:
  392. static void _bind_methods();
  393. public:
  394. void set_channel(const int p_channel);
  395. int get_channel() const;
  396. void set_message(const MIDIMessage p_message);
  397. MIDIMessage get_message() const;
  398. void set_pitch(const int p_pitch);
  399. int get_pitch() const;
  400. void set_velocity(const int p_velocity);
  401. int get_velocity() const;
  402. void set_instrument(const int p_instrument);
  403. int get_instrument() const;
  404. void set_pressure(const int p_pressure);
  405. int get_pressure() const;
  406. void set_controller_number(const int p_controller_number);
  407. int get_controller_number() const;
  408. void set_controller_value(const int p_controller_value);
  409. int get_controller_value() const;
  410. virtual String as_text() const override;
  411. virtual String to_string() override;
  412. InputEventMIDI() {}
  413. };
  414. class InputEventShortcut : public InputEvent {
  415. GDCLASS(InputEventShortcut, InputEvent);
  416. Ref<Shortcut> shortcut;
  417. protected:
  418. static void _bind_methods();
  419. public:
  420. void set_shortcut(Ref<Shortcut> p_shortcut);
  421. Ref<Shortcut> get_shortcut();
  422. virtual String as_text() const override;
  423. virtual String to_string() override;
  424. InputEventShortcut();
  425. };
  426. #endif // INPUT_EVENT_H