input_event.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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/io/resource.h"
  33. #include "core/math/transform_2d.h"
  34. #include "core/string/ustring.h"
  35. #include "core/typedefs.h"
  36. /**
  37. * Input Event classes. These are used in the main loop.
  38. * The events are pretty obvious.
  39. */
  40. enum MouseButton {
  41. MOUSE_BUTTON_LEFT = 1,
  42. MOUSE_BUTTON_RIGHT = 2,
  43. MOUSE_BUTTON_MIDDLE = 3,
  44. MOUSE_BUTTON_WHEEL_UP = 4,
  45. MOUSE_BUTTON_WHEEL_DOWN = 5,
  46. MOUSE_BUTTON_WHEEL_LEFT = 6,
  47. MOUSE_BUTTON_WHEEL_RIGHT = 7,
  48. MOUSE_BUTTON_XBUTTON1 = 8,
  49. MOUSE_BUTTON_XBUTTON2 = 9,
  50. MOUSE_BUTTON_MASK_LEFT = (1 << (MOUSE_BUTTON_LEFT - 1)),
  51. MOUSE_BUTTON_MASK_RIGHT = (1 << (MOUSE_BUTTON_RIGHT - 1)),
  52. MOUSE_BUTTON_MASK_MIDDLE = (1 << (MOUSE_BUTTON_MIDDLE - 1)),
  53. MOUSE_BUTTON_MASK_XBUTTON1 = (1 << (MOUSE_BUTTON_XBUTTON1 - 1)),
  54. MOUSE_BUTTON_MASK_XBUTTON2 = (1 << (MOUSE_BUTTON_XBUTTON2 - 1))
  55. };
  56. enum JoyButton {
  57. JOY_BUTTON_INVALID = -1,
  58. JOY_BUTTON_A = 0,
  59. JOY_BUTTON_B = 1,
  60. JOY_BUTTON_X = 2,
  61. JOY_BUTTON_Y = 3,
  62. JOY_BUTTON_BACK = 4,
  63. JOY_BUTTON_GUIDE = 5,
  64. JOY_BUTTON_START = 6,
  65. JOY_BUTTON_LEFT_STICK = 7,
  66. JOY_BUTTON_RIGHT_STICK = 8,
  67. JOY_BUTTON_LEFT_SHOULDER = 9,
  68. JOY_BUTTON_RIGHT_SHOULDER = 10,
  69. JOY_BUTTON_DPAD_UP = 11,
  70. JOY_BUTTON_DPAD_DOWN = 12,
  71. JOY_BUTTON_DPAD_LEFT = 13,
  72. JOY_BUTTON_DPAD_RIGHT = 14,
  73. JOY_BUTTON_MISC1 = 15,
  74. JOY_BUTTON_PADDLE1 = 16,
  75. JOY_BUTTON_PADDLE2 = 17,
  76. JOY_BUTTON_PADDLE3 = 18,
  77. JOY_BUTTON_PADDLE4 = 19,
  78. JOY_BUTTON_TOUCHPAD = 20,
  79. JOY_BUTTON_SDL_MAX = 21,
  80. JOY_BUTTON_MAX = 36, // Android supports up to 36 buttons.
  81. };
  82. enum JoyAxis {
  83. JOY_AXIS_INVALID = -1,
  84. JOY_AXIS_LEFT_X = 0,
  85. JOY_AXIS_LEFT_Y = 1,
  86. JOY_AXIS_RIGHT_X = 2,
  87. JOY_AXIS_RIGHT_Y = 3,
  88. JOY_AXIS_TRIGGER_LEFT = 4,
  89. JOY_AXIS_TRIGGER_RIGHT = 5,
  90. JOY_AXIS_SDL_MAX = 6,
  91. JOY_AXIS_MAX = 10, // OpenVR supports up to 5 Joysticks making a total of 10 axes.
  92. };
  93. enum MIDIMessage {
  94. MIDI_MESSAGE_NOTE_OFF = 0x8,
  95. MIDI_MESSAGE_NOTE_ON = 0x9,
  96. MIDI_MESSAGE_AFTERTOUCH = 0xA,
  97. MIDI_MESSAGE_CONTROL_CHANGE = 0xB,
  98. MIDI_MESSAGE_PROGRAM_CHANGE = 0xC,
  99. MIDI_MESSAGE_CHANNEL_PRESSURE = 0xD,
  100. MIDI_MESSAGE_PITCH_BEND = 0xE,
  101. };
  102. /**
  103. * Input Modifier Status
  104. * for keyboard/mouse events.
  105. */
  106. class InputEvent : public Resource {
  107. GDCLASS(InputEvent, Resource);
  108. int device = 0;
  109. protected:
  110. static void _bind_methods();
  111. public:
  112. static const int DEVICE_ID_TOUCH_MOUSE;
  113. static const int DEVICE_ID_INTERNAL;
  114. void set_device(int p_device);
  115. int get_device() const;
  116. bool is_action(const StringName &p_action, bool p_exact_match = false) const;
  117. bool is_action_pressed(const StringName &p_action, bool p_allow_echo = false, bool p_exact_match = false) const;
  118. bool is_action_released(const StringName &p_action, bool p_exact_match = false) const;
  119. float get_action_strength(const StringName &p_action, bool p_exact_match = false) const;
  120. float get_action_raw_strength(const StringName &p_action, bool p_exact_match = false) const;
  121. // To be removed someday, since they do not make sense for all events
  122. virtual bool is_pressed() const;
  123. virtual bool is_echo() const;
  124. virtual String as_text() const = 0;
  125. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const;
  126. virtual bool action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const;
  127. virtual bool shortcut_match(const Ref<InputEvent> &p_event) const;
  128. virtual bool is_action_type() const;
  129. virtual bool accumulate(const Ref<InputEvent> &p_event) { return false; }
  130. InputEvent() {}
  131. };
  132. class InputEventFromWindow : public InputEvent {
  133. GDCLASS(InputEventFromWindow, InputEvent);
  134. int64_t window_id = 0;
  135. protected:
  136. static void _bind_methods();
  137. public:
  138. void set_window_id(int64_t p_id);
  139. int64_t get_window_id() const;
  140. InputEventFromWindow() {}
  141. };
  142. class InputEventWithModifiers : public InputEventFromWindow {
  143. GDCLASS(InputEventWithModifiers, InputEventFromWindow);
  144. bool store_command = true;
  145. bool shift = false;
  146. bool alt = false;
  147. #ifdef APPLE_STYLE_KEYS
  148. union {
  149. bool command;
  150. bool meta = false; //< windows/mac key
  151. };
  152. bool control = false;
  153. #else
  154. union {
  155. bool command; //< windows/mac key
  156. bool control = false;
  157. };
  158. bool meta = false; //< windows/mac key
  159. #endif
  160. protected:
  161. static void _bind_methods();
  162. virtual void _validate_property(PropertyInfo &property) const override;
  163. public:
  164. void set_store_command(bool p_enabled);
  165. bool is_storing_command() const;
  166. void set_shift(bool p_enabled);
  167. bool get_shift() const;
  168. void set_alt(bool p_enabled);
  169. bool get_alt() const;
  170. void set_control(bool p_enabled);
  171. bool get_control() const;
  172. void set_metakey(bool p_enabled);
  173. bool get_metakey() const;
  174. void set_command(bool p_enabled);
  175. bool get_command() const;
  176. void set_modifiers_from_event(const InputEventWithModifiers *event);
  177. virtual String as_text() const override;
  178. virtual String to_string() override;
  179. InputEventWithModifiers() {}
  180. };
  181. class InputEventKey : public InputEventWithModifiers {
  182. GDCLASS(InputEventKey, InputEventWithModifiers);
  183. bool pressed = false; /// otherwise release
  184. uint32_t keycode = 0; ///< check keyboard.h , KeyCode enum, without modifier masks
  185. uint32_t physical_keycode = 0;
  186. uint32_t unicode = 0; ///unicode
  187. bool echo = false; /// true if this is an echo key
  188. protected:
  189. static void _bind_methods();
  190. public:
  191. void set_pressed(bool p_pressed);
  192. virtual bool is_pressed() const override;
  193. void set_keycode(uint32_t p_keycode);
  194. uint32_t get_keycode() const;
  195. void set_physical_keycode(uint32_t p_keycode);
  196. uint32_t get_physical_keycode() const;
  197. void set_unicode(uint32_t p_unicode);
  198. uint32_t get_unicode() const;
  199. void set_echo(bool p_enable);
  200. virtual bool is_echo() const override;
  201. uint32_t get_keycode_with_modifiers() const;
  202. uint32_t get_physical_keycode_with_modifiers() const;
  203. virtual bool action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const override;
  204. virtual bool shortcut_match(const Ref<InputEvent> &p_event) const override;
  205. virtual bool is_action_type() const override { return true; }
  206. virtual String as_text() const override;
  207. virtual String to_string() override;
  208. static Ref<InputEventKey> create_reference(uint32_t p_keycode_with_modifier_masks);
  209. InputEventKey() {}
  210. };
  211. class InputEventMouse : public InputEventWithModifiers {
  212. GDCLASS(InputEventMouse, InputEventWithModifiers);
  213. int button_mask = 0;
  214. Vector2 pos;
  215. Vector2 global_pos;
  216. protected:
  217. static void _bind_methods();
  218. public:
  219. void set_button_mask(int p_mask);
  220. int get_button_mask() const;
  221. void set_position(const Vector2 &p_pos);
  222. Vector2 get_position() const;
  223. void set_global_position(const Vector2 &p_global_pos);
  224. Vector2 get_global_position() const;
  225. InputEventMouse() {}
  226. };
  227. class InputEventMouseButton : public InputEventMouse {
  228. GDCLASS(InputEventMouseButton, InputEventMouse);
  229. float factor = 1;
  230. int button_index = 0;
  231. bool pressed = false; //otherwise released
  232. bool doubleclick = false; //last even less than doubleclick time
  233. protected:
  234. static void _bind_methods();
  235. public:
  236. void set_factor(float p_factor);
  237. float get_factor() const;
  238. void set_button_index(int p_index);
  239. int get_button_index() const;
  240. void set_pressed(bool p_pressed);
  241. virtual bool is_pressed() const override;
  242. void set_doubleclick(bool p_doubleclick);
  243. bool is_doubleclick() const;
  244. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  245. virtual bool action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const override;
  246. virtual bool is_action_type() const override { return true; }
  247. virtual String as_text() const override;
  248. virtual String to_string() override;
  249. InputEventMouseButton() {}
  250. };
  251. class InputEventMouseMotion : public InputEventMouse {
  252. GDCLASS(InputEventMouseMotion, InputEventMouse);
  253. Vector2 tilt;
  254. float pressure = 0;
  255. Vector2 relative;
  256. Vector2 speed;
  257. protected:
  258. static void _bind_methods();
  259. public:
  260. void set_tilt(const Vector2 &p_tilt);
  261. Vector2 get_tilt() const;
  262. void set_pressure(float p_pressure);
  263. float get_pressure() const;
  264. void set_relative(const Vector2 &p_relative);
  265. Vector2 get_relative() const;
  266. void set_speed(const Vector2 &p_speed);
  267. Vector2 get_speed() const;
  268. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  269. virtual String as_text() const override;
  270. virtual String to_string() override;
  271. virtual bool accumulate(const Ref<InputEvent> &p_event) override;
  272. InputEventMouseMotion() {}
  273. };
  274. class InputEventJoypadMotion : public InputEvent {
  275. GDCLASS(InputEventJoypadMotion, InputEvent);
  276. int axis = 0; ///< Joypad axis
  277. float axis_value = 0; ///< -1 to 1
  278. protected:
  279. static void _bind_methods();
  280. public:
  281. void set_axis(int p_axis);
  282. int get_axis() const;
  283. void set_axis_value(float p_value);
  284. float get_axis_value() const;
  285. virtual bool is_pressed() const override;
  286. virtual bool action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const override;
  287. virtual bool is_action_type() const override { return true; }
  288. virtual String as_text() const override;
  289. virtual String to_string() override;
  290. InputEventJoypadMotion() {}
  291. };
  292. class InputEventJoypadButton : public InputEvent {
  293. GDCLASS(InputEventJoypadButton, InputEvent);
  294. int button_index = 0;
  295. bool pressed = false;
  296. float pressure = 0; //0 to 1
  297. protected:
  298. static void _bind_methods();
  299. public:
  300. void set_button_index(int p_index);
  301. int get_button_index() const;
  302. void set_pressed(bool p_pressed);
  303. virtual bool is_pressed() const override;
  304. void set_pressure(float p_pressure);
  305. float get_pressure() const;
  306. virtual bool action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const override;
  307. virtual bool shortcut_match(const Ref<InputEvent> &p_event) const override;
  308. virtual bool is_action_type() const override { return true; }
  309. virtual String as_text() const override;
  310. virtual String to_string() override;
  311. static Ref<InputEventJoypadButton> create_reference(int p_btn_index);
  312. InputEventJoypadButton() {}
  313. };
  314. class InputEventScreenTouch : public InputEventFromWindow {
  315. GDCLASS(InputEventScreenTouch, InputEventFromWindow);
  316. int index = 0;
  317. Vector2 pos;
  318. bool pressed = false;
  319. protected:
  320. static void _bind_methods();
  321. public:
  322. void set_index(int p_index);
  323. int get_index() const;
  324. void set_position(const Vector2 &p_pos);
  325. Vector2 get_position() const;
  326. void set_pressed(bool p_pressed);
  327. virtual bool is_pressed() const override;
  328. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  329. virtual String as_text() const override;
  330. virtual String to_string() override;
  331. InputEventScreenTouch() {}
  332. };
  333. class InputEventScreenDrag : public InputEventFromWindow {
  334. GDCLASS(InputEventScreenDrag, InputEventFromWindow);
  335. int index = 0;
  336. Vector2 pos;
  337. Vector2 relative;
  338. Vector2 speed;
  339. protected:
  340. static void _bind_methods();
  341. public:
  342. void set_index(int p_index);
  343. int get_index() const;
  344. void set_position(const Vector2 &p_pos);
  345. Vector2 get_position() const;
  346. void set_relative(const Vector2 &p_relative);
  347. Vector2 get_relative() const;
  348. void set_speed(const Vector2 &p_speed);
  349. Vector2 get_speed() 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. InputEventScreenDrag() {}
  354. };
  355. class InputEventAction : public InputEvent {
  356. GDCLASS(InputEventAction, InputEvent);
  357. StringName action;
  358. bool pressed = false;
  359. float strength = 1.0f;
  360. protected:
  361. static void _bind_methods();
  362. public:
  363. void set_action(const StringName &p_action);
  364. StringName get_action() const;
  365. void set_pressed(bool p_pressed);
  366. virtual bool is_pressed() const override;
  367. void set_strength(float p_strength);
  368. float get_strength() const;
  369. virtual bool is_action(const StringName &p_action) const;
  370. virtual bool action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const override;
  371. virtual bool shortcut_match(const Ref<InputEvent> &p_event) const override;
  372. virtual bool is_action_type() const override { return true; }
  373. virtual String as_text() const override;
  374. virtual String to_string() override;
  375. InputEventAction() {}
  376. };
  377. class InputEventGesture : public InputEventWithModifiers {
  378. GDCLASS(InputEventGesture, InputEventWithModifiers);
  379. Vector2 pos;
  380. protected:
  381. static void _bind_methods();
  382. public:
  383. void set_position(const Vector2 &p_pos);
  384. Vector2 get_position() const;
  385. };
  386. class InputEventMagnifyGesture : public InputEventGesture {
  387. GDCLASS(InputEventMagnifyGesture, InputEventGesture);
  388. real_t factor = 1.0;
  389. protected:
  390. static void _bind_methods();
  391. public:
  392. void set_factor(real_t p_factor);
  393. real_t get_factor() const;
  394. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  395. virtual String as_text() const override;
  396. virtual String to_string() override;
  397. InputEventMagnifyGesture() {}
  398. };
  399. class InputEventPanGesture : public InputEventGesture {
  400. GDCLASS(InputEventPanGesture, InputEventGesture);
  401. Vector2 delta;
  402. protected:
  403. static void _bind_methods();
  404. public:
  405. void set_delta(const Vector2 &p_delta);
  406. Vector2 get_delta() const;
  407. virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
  408. virtual String as_text() const override;
  409. virtual String to_string() override;
  410. InputEventPanGesture() {}
  411. };
  412. class InputEventMIDI : public InputEvent {
  413. GDCLASS(InputEventMIDI, InputEvent);
  414. int channel = 0;
  415. int message = 0;
  416. int pitch = 0;
  417. int velocity = 0;
  418. int instrument = 0;
  419. int pressure = 0;
  420. int controller_number = 0;
  421. int controller_value = 0;
  422. protected:
  423. static void _bind_methods();
  424. public:
  425. void set_channel(const int p_channel);
  426. int get_channel() const;
  427. void set_message(const int p_message);
  428. int get_message() const;
  429. void set_pitch(const int p_pitch);
  430. int get_pitch() const;
  431. void set_velocity(const int p_velocity);
  432. int get_velocity() const;
  433. void set_instrument(const int p_instrument);
  434. int get_instrument() const;
  435. void set_pressure(const int p_pressure);
  436. int get_pressure() const;
  437. void set_controller_number(const int p_controller_number);
  438. int get_controller_number() const;
  439. void set_controller_value(const int p_controller_value);
  440. int get_controller_value() const;
  441. virtual String as_text() const override;
  442. virtual String to_string() override;
  443. InputEventMIDI() {}
  444. };
  445. #endif // INPUT_EVENT_H