input.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*************************************************************************/
  2. /* input.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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_H
  31. #define INPUT_H
  32. #include "core/input/input_event.h"
  33. #include "core/object/object.h"
  34. #include "core/os/keyboard.h"
  35. #include "core/os/thread_safe.h"
  36. class Input : public Object {
  37. GDCLASS(Input, Object);
  38. _THREAD_SAFE_CLASS_
  39. static Input *singleton;
  40. public:
  41. enum MouseMode {
  42. MOUSE_MODE_VISIBLE,
  43. MOUSE_MODE_HIDDEN,
  44. MOUSE_MODE_CAPTURED,
  45. MOUSE_MODE_CONFINED,
  46. MOUSE_MODE_CONFINED_HIDDEN,
  47. };
  48. #undef CursorShape
  49. enum CursorShape {
  50. CURSOR_ARROW,
  51. CURSOR_IBEAM,
  52. CURSOR_POINTING_HAND,
  53. CURSOR_CROSS,
  54. CURSOR_WAIT,
  55. CURSOR_BUSY,
  56. CURSOR_DRAG,
  57. CURSOR_CAN_DROP,
  58. CURSOR_FORBIDDEN,
  59. CURSOR_VSIZE,
  60. CURSOR_HSIZE,
  61. CURSOR_BDIAGSIZE,
  62. CURSOR_FDIAGSIZE,
  63. CURSOR_MOVE,
  64. CURSOR_VSPLIT,
  65. CURSOR_HSPLIT,
  66. CURSOR_HELP,
  67. CURSOR_MAX
  68. };
  69. enum {
  70. JOYPADS_MAX = 16,
  71. };
  72. typedef void (*EventDispatchFunc)(const Ref<InputEvent> &p_event);
  73. private:
  74. MouseButton mouse_button_mask = MouseButton::NONE;
  75. Set<Key> physical_keys_pressed;
  76. Set<Key> keys_pressed;
  77. Set<JoyButton> joy_buttons_pressed;
  78. Map<JoyAxis, float> _joy_axis;
  79. //Map<StringName,int> custom_action_press;
  80. Vector3 gravity;
  81. Vector3 accelerometer;
  82. Vector3 magnetometer;
  83. Vector3 gyroscope;
  84. Vector2 mouse_pos;
  85. int64_t mouse_window = 0;
  86. struct Action {
  87. uint64_t physics_frame;
  88. uint64_t process_frame;
  89. bool pressed;
  90. bool exact;
  91. float strength;
  92. float raw_strength;
  93. };
  94. Map<StringName, Action> action_state;
  95. bool emulate_touch_from_mouse = false;
  96. bool emulate_mouse_from_touch = false;
  97. bool use_input_buffering = false;
  98. bool use_accumulated_input = false;
  99. int mouse_from_touch_index = -1;
  100. struct VelocityTrack {
  101. uint64_t last_tick = 0;
  102. Vector2 velocity;
  103. Vector2 accum;
  104. float accum_t = 0.0f;
  105. float min_ref_frame;
  106. float max_ref_frame;
  107. void update(const Vector2 &p_delta_p);
  108. void reset();
  109. VelocityTrack();
  110. };
  111. struct Joypad {
  112. StringName name;
  113. StringName uid;
  114. bool connected = false;
  115. bool last_buttons[(size_t)JoyButton::MAX] = { false };
  116. float last_axis[(size_t)JoyAxis::MAX] = { 0.0f };
  117. HatMask last_hat = HatMask::CENTER;
  118. int mapping = -1;
  119. int hat_current = 0;
  120. };
  121. VelocityTrack mouse_velocity_track;
  122. Map<int, VelocityTrack> touch_velocity_track;
  123. Map<int, Joypad> joy_names;
  124. int fallback_mapping = -1;
  125. CursorShape default_shape = CURSOR_ARROW;
  126. enum JoyType {
  127. TYPE_BUTTON,
  128. TYPE_AXIS,
  129. TYPE_HAT,
  130. TYPE_MAX,
  131. };
  132. enum JoyAxisRange {
  133. NEGATIVE_HALF_AXIS = -1,
  134. FULL_AXIS = 0,
  135. POSITIVE_HALF_AXIS = 1
  136. };
  137. struct JoyEvent {
  138. int type = TYPE_MAX;
  139. int index = -1; // Can be either JoyAxis or JoyButton.
  140. float value = 0.f;
  141. };
  142. struct JoyBinding {
  143. JoyType inputType;
  144. union {
  145. JoyButton button;
  146. struct {
  147. JoyAxis axis;
  148. JoyAxisRange range;
  149. bool invert;
  150. } axis;
  151. struct {
  152. HatDir hat;
  153. HatMask hat_mask;
  154. } hat;
  155. } input;
  156. JoyType outputType;
  157. union {
  158. JoyButton button;
  159. struct {
  160. JoyAxis axis;
  161. JoyAxisRange range;
  162. } axis;
  163. } output;
  164. };
  165. struct JoyDeviceMapping {
  166. String uid;
  167. String name;
  168. Vector<JoyBinding> bindings;
  169. };
  170. Vector<JoyDeviceMapping> map_db;
  171. JoyEvent _get_mapped_button_event(const JoyDeviceMapping &mapping, JoyButton p_button);
  172. JoyEvent _get_mapped_axis_event(const JoyDeviceMapping &mapping, JoyAxis p_axis, float p_value);
  173. void _get_mapped_hat_events(const JoyDeviceMapping &mapping, HatDir p_hat, JoyEvent r_events[(size_t)HatDir::MAX]);
  174. JoyButton _get_output_button(String output);
  175. JoyAxis _get_output_axis(String output);
  176. void _button_event(int p_device, JoyButton p_index, bool p_pressed);
  177. void _axis_event(int p_device, JoyAxis p_axis, float p_value);
  178. void _parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_emulated);
  179. List<Ref<InputEvent>> buffered_events;
  180. friend class DisplayServer;
  181. static void (*set_mouse_mode_func)(MouseMode);
  182. static MouseMode (*get_mouse_mode_func)();
  183. static void (*warp_mouse_func)(const Vector2 &p_position);
  184. static CursorShape (*get_current_cursor_shape_func)();
  185. static void (*set_custom_mouse_cursor_func)(const Ref<Resource> &, CursorShape, const Vector2 &);
  186. EventDispatchFunc event_dispatch_function = nullptr;
  187. protected:
  188. struct VibrationInfo {
  189. float weak_magnitude;
  190. float strong_magnitude;
  191. float duration; // Duration in seconds
  192. uint64_t timestamp;
  193. };
  194. Map<int, VibrationInfo> joy_vibration;
  195. static void _bind_methods();
  196. public:
  197. void set_mouse_mode(MouseMode p_mode);
  198. MouseMode get_mouse_mode() const;
  199. void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const override;
  200. static Input *get_singleton();
  201. bool is_anything_pressed() const;
  202. bool is_key_pressed(Key p_keycode) const;
  203. bool is_physical_key_pressed(Key p_keycode) const;
  204. bool is_mouse_button_pressed(MouseButton p_button) const;
  205. bool is_joy_button_pressed(int p_device, JoyButton p_button) const;
  206. bool is_action_pressed(const StringName &p_action, bool p_exact = false) const;
  207. bool is_action_just_pressed(const StringName &p_action, bool p_exact = false) const;
  208. bool is_action_just_released(const StringName &p_action, bool p_exact = false) const;
  209. float get_action_strength(const StringName &p_action, bool p_exact = false) const;
  210. float get_action_raw_strength(const StringName &p_action, bool p_exact = false) const;
  211. float get_axis(const StringName &p_negative_action, const StringName &p_positive_action) const;
  212. Vector2 get_vector(const StringName &p_negative_x, const StringName &p_positive_x, const StringName &p_negative_y, const StringName &p_positive_y, float p_deadzone = -1.0f) const;
  213. float get_joy_axis(int p_device, JoyAxis p_axis) const;
  214. String get_joy_name(int p_idx);
  215. Array get_connected_joypads();
  216. Vector2 get_joy_vibration_strength(int p_device);
  217. float get_joy_vibration_duration(int p_device);
  218. uint64_t get_joy_vibration_timestamp(int p_device);
  219. void joy_connection_changed(int p_idx, bool p_connected, String p_name, String p_guid = "");
  220. Vector3 get_gravity() const;
  221. Vector3 get_accelerometer() const;
  222. Vector3 get_magnetometer() const;
  223. Vector3 get_gyroscope() const;
  224. Point2 get_mouse_position() const;
  225. Vector2 get_last_mouse_velocity();
  226. MouseButton get_mouse_button_mask() const;
  227. void warp_mouse(const Vector2 &p_position);
  228. Point2i warp_mouse_motion(const Ref<InputEventMouseMotion> &p_motion, const Rect2 &p_rect);
  229. void parse_input_event(const Ref<InputEvent> &p_event);
  230. void set_gravity(const Vector3 &p_gravity);
  231. void set_accelerometer(const Vector3 &p_accel);
  232. void set_magnetometer(const Vector3 &p_magnetometer);
  233. void set_gyroscope(const Vector3 &p_gyroscope);
  234. void set_joy_axis(int p_device, JoyAxis p_axis, float p_value);
  235. void start_joy_vibration(int p_device, float p_weak_magnitude, float p_strong_magnitude, float p_duration = 0);
  236. void stop_joy_vibration(int p_device);
  237. void vibrate_handheld(int p_duration_ms = 500);
  238. void set_mouse_position(const Point2 &p_posf);
  239. void action_press(const StringName &p_action, float p_strength = 1.f);
  240. void action_release(const StringName &p_action);
  241. void iteration(float p_step);
  242. void set_emulate_touch_from_mouse(bool p_emulate);
  243. bool is_emulating_touch_from_mouse() const;
  244. void ensure_touch_mouse_raised();
  245. void set_emulate_mouse_from_touch(bool p_emulate);
  246. bool is_emulating_mouse_from_touch() const;
  247. CursorShape get_default_cursor_shape() const;
  248. void set_default_cursor_shape(CursorShape p_shape);
  249. CursorShape get_current_cursor_shape() const;
  250. void set_custom_mouse_cursor(const Ref<Resource> &p_cursor, CursorShape p_shape = Input::CURSOR_ARROW, const Vector2 &p_hotspot = Vector2());
  251. void parse_mapping(String p_mapping);
  252. void joy_button(int p_device, JoyButton p_button, bool p_pressed);
  253. void joy_axis(int p_device, JoyAxis p_axis, float p_value);
  254. void joy_hat(int p_device, HatMask p_val);
  255. void add_joy_mapping(String p_mapping, bool p_update_existing = false);
  256. void remove_joy_mapping(String p_guid);
  257. int get_unused_joy_id();
  258. bool is_joy_known(int p_device);
  259. String get_joy_guid(int p_device) const;
  260. void set_fallback_mapping(String p_guid);
  261. void flush_buffered_events();
  262. bool is_using_input_buffering();
  263. void set_use_input_buffering(bool p_enable);
  264. void set_use_accumulated_input(bool p_enable);
  265. void release_pressed_events();
  266. void set_event_dispatch_function(EventDispatchFunc p_function);
  267. Input();
  268. ~Input();
  269. };
  270. VARIANT_ENUM_CAST(Input::MouseMode);
  271. VARIANT_ENUM_CAST(Input::CursorShape);
  272. #endif // INPUT_H