input.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /**************************************************************************/
  2. /* input.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_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. #include "core/templates/rb_set.h"
  37. #include "core/variant/typed_array.h"
  38. class Input : public Object {
  39. GDCLASS(Input, Object);
  40. _THREAD_SAFE_CLASS_
  41. static Input *singleton;
  42. static constexpr uint64_t MAX_EVENT = 32;
  43. public:
  44. // Keep synced with "DisplayServer::MouseMode" enum.
  45. enum MouseMode {
  46. MOUSE_MODE_VISIBLE,
  47. MOUSE_MODE_HIDDEN,
  48. MOUSE_MODE_CAPTURED,
  49. MOUSE_MODE_CONFINED,
  50. MOUSE_MODE_CONFINED_HIDDEN,
  51. MOUSE_MODE_MAX,
  52. };
  53. #undef CursorShape
  54. enum CursorShape {
  55. CURSOR_ARROW,
  56. CURSOR_IBEAM,
  57. CURSOR_POINTING_HAND,
  58. CURSOR_CROSS,
  59. CURSOR_WAIT,
  60. CURSOR_BUSY,
  61. CURSOR_DRAG,
  62. CURSOR_CAN_DROP,
  63. CURSOR_FORBIDDEN,
  64. CURSOR_VSIZE,
  65. CURSOR_HSIZE,
  66. CURSOR_BDIAGSIZE,
  67. CURSOR_FDIAGSIZE,
  68. CURSOR_MOVE,
  69. CURSOR_VSPLIT,
  70. CURSOR_HSPLIT,
  71. CURSOR_HELP,
  72. CURSOR_MAX
  73. };
  74. enum {
  75. JOYPADS_MAX = 16,
  76. };
  77. typedef void (*EventDispatchFunc)(const Ref<InputEvent> &p_event);
  78. private:
  79. BitField<MouseButtonMask> mouse_button_mask;
  80. RBSet<Key> key_label_pressed;
  81. RBSet<Key> physical_keys_pressed;
  82. RBSet<Key> keys_pressed;
  83. RBSet<JoyButton> joy_buttons_pressed;
  84. RBMap<JoyAxis, float> _joy_axis;
  85. //RBMap<StringName,int> custom_action_press;
  86. bool gravity_enabled = false;
  87. Vector3 gravity;
  88. bool accelerometer_enabled = false;
  89. Vector3 accelerometer;
  90. bool magnetometer_enabled = false;
  91. Vector3 magnetometer;
  92. bool gyroscope_enabled = false;
  93. Vector3 gyroscope;
  94. Vector2 mouse_pos;
  95. int64_t mouse_window = 0;
  96. bool legacy_just_pressed_behavior = false;
  97. bool disable_input = false;
  98. struct ActionState {
  99. uint64_t pressed_physics_frame = UINT64_MAX;
  100. uint64_t pressed_process_frame = UINT64_MAX;
  101. uint64_t released_physics_frame = UINT64_MAX;
  102. uint64_t released_process_frame = UINT64_MAX;
  103. bool exact = true;
  104. struct DeviceState {
  105. bool pressed[MAX_EVENT] = { false };
  106. float strength[MAX_EVENT] = { 0.0 };
  107. float raw_strength[MAX_EVENT] = { 0.0 };
  108. };
  109. bool api_pressed = false;
  110. float api_strength = 0.0;
  111. HashMap<int, DeviceState> device_states;
  112. // Cache.
  113. struct ActionStateCache {
  114. bool pressed = false;
  115. float strength = false;
  116. float raw_strength = false;
  117. } cache;
  118. };
  119. HashMap<StringName, ActionState> action_states;
  120. bool emulate_touch_from_mouse = false;
  121. bool emulate_mouse_from_touch = false;
  122. bool agile_input_event_flushing = false;
  123. bool use_accumulated_input = true;
  124. int mouse_from_touch_index = -1;
  125. struct VibrationInfo {
  126. float weak_magnitude;
  127. float strong_magnitude;
  128. float duration; // Duration in seconds
  129. uint64_t timestamp;
  130. };
  131. HashMap<int, VibrationInfo> joy_vibration;
  132. struct VelocityTrack {
  133. uint64_t last_tick = 0;
  134. Vector2 velocity;
  135. Vector2 screen_velocity;
  136. Vector2 accum;
  137. Vector2 screen_accum;
  138. float accum_t = 0.0f;
  139. float min_ref_frame;
  140. float max_ref_frame;
  141. void update(const Vector2 &p_delta_p, const Vector2 &p_screen_delta_p);
  142. void reset();
  143. VelocityTrack();
  144. };
  145. struct Joypad {
  146. StringName name;
  147. StringName uid;
  148. bool connected = false;
  149. bool last_buttons[(size_t)JoyButton::MAX] = { false };
  150. float last_axis[(size_t)JoyAxis::MAX] = { 0.0f };
  151. HatMask last_hat = HatMask::CENTER;
  152. int mapping = -1;
  153. int hat_current = 0;
  154. Dictionary info;
  155. };
  156. VelocityTrack mouse_velocity_track;
  157. HashMap<int, VelocityTrack> touch_velocity_track;
  158. HashMap<int, Joypad> joy_names;
  159. HashSet<uint32_t> ignored_device_ids;
  160. int fallback_mapping = -1; // Index of the guid in map_db.
  161. CursorShape default_shape = CURSOR_ARROW;
  162. enum JoyType {
  163. TYPE_BUTTON,
  164. TYPE_AXIS,
  165. TYPE_HAT,
  166. TYPE_MAX,
  167. };
  168. enum JoyAxisRange {
  169. NEGATIVE_HALF_AXIS = -1,
  170. FULL_AXIS = 0,
  171. POSITIVE_HALF_AXIS = 1
  172. };
  173. struct JoyEvent {
  174. int type = TYPE_MAX;
  175. int index = -1; // Can be either JoyAxis or JoyButton.
  176. float value = 0.f;
  177. };
  178. struct JoyBinding {
  179. JoyType inputType;
  180. union {
  181. JoyButton button;
  182. struct {
  183. JoyAxis axis;
  184. JoyAxisRange range;
  185. bool invert;
  186. } axis;
  187. struct {
  188. HatDir hat;
  189. HatMask hat_mask;
  190. } hat;
  191. } input;
  192. JoyType outputType;
  193. union {
  194. JoyButton button;
  195. struct {
  196. JoyAxis axis;
  197. JoyAxisRange range;
  198. } axis;
  199. } output;
  200. };
  201. struct JoyDeviceMapping {
  202. String uid;
  203. String name;
  204. Vector<JoyBinding> bindings;
  205. };
  206. Vector<JoyDeviceMapping> map_db;
  207. void _set_joypad_mapping(Joypad &p_js, int p_map_index);
  208. JoyEvent _get_mapped_button_event(const JoyDeviceMapping &mapping, JoyButton p_button);
  209. JoyEvent _get_mapped_axis_event(const JoyDeviceMapping &mapping, JoyAxis p_axis, float p_value, JoyAxisRange &r_range);
  210. void _get_mapped_hat_events(const JoyDeviceMapping &mapping, HatDir p_hat, JoyEvent r_events[(size_t)HatDir::MAX]);
  211. JoyButton _get_output_button(const String &output);
  212. JoyAxis _get_output_axis(const String &output);
  213. void _button_event(int p_device, JoyButton p_index, bool p_pressed);
  214. void _axis_event(int p_device, JoyAxis p_axis, float p_value);
  215. void _update_action_cache(const StringName &p_action_name, ActionState &r_action_state);
  216. void _parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_emulated);
  217. List<Ref<InputEvent>> buffered_events;
  218. #ifdef DEBUG_ENABLED
  219. HashSet<Ref<InputEvent>> frame_parsed_events;
  220. uint64_t last_parsed_frame = UINT64_MAX;
  221. #endif
  222. friend class DisplayServer;
  223. static void (*set_mouse_mode_func)(MouseMode);
  224. static MouseMode (*get_mouse_mode_func)();
  225. static void (*set_mouse_mode_override_func)(MouseMode);
  226. static MouseMode (*get_mouse_mode_override_func)();
  227. static void (*set_mouse_mode_override_enabled_func)(bool);
  228. static bool (*is_mouse_mode_override_enabled_func)();
  229. static void (*warp_mouse_func)(const Vector2 &p_position);
  230. static CursorShape (*get_current_cursor_shape_func)();
  231. static void (*set_custom_mouse_cursor_func)(const Ref<Resource> &, CursorShape, const Vector2 &);
  232. EventDispatchFunc event_dispatch_function = nullptr;
  233. #ifndef DISABLE_DEPRECATED
  234. void _vibrate_handheld_bind_compat_91143(int p_duration_ms = 500);
  235. static void _bind_compatibility_methods();
  236. #endif // DISABLE_DEPRECATED
  237. protected:
  238. static void _bind_methods();
  239. public:
  240. void set_mouse_mode(MouseMode p_mode);
  241. MouseMode get_mouse_mode() const;
  242. void set_mouse_mode_override(MouseMode p_mode);
  243. MouseMode get_mouse_mode_override() const;
  244. void set_mouse_mode_override_enabled(bool p_override_enabled);
  245. bool is_mouse_mode_override_enabled();
  246. #ifdef TOOLS_ENABLED
  247. void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const override;
  248. #endif
  249. static Input *get_singleton();
  250. bool is_anything_pressed() const;
  251. bool is_anything_pressed_except_mouse() const;
  252. bool is_key_pressed(Key p_keycode) const;
  253. bool is_physical_key_pressed(Key p_keycode) const;
  254. bool is_key_label_pressed(Key p_keycode) const;
  255. bool is_mouse_button_pressed(MouseButton p_button) const;
  256. bool is_joy_button_pressed(int p_device, JoyButton p_button) const;
  257. bool is_action_pressed(const StringName &p_action, bool p_exact = false) const;
  258. bool is_action_just_pressed(const StringName &p_action, bool p_exact = false) const;
  259. bool is_action_just_released(const StringName &p_action, bool p_exact = false) const;
  260. float get_action_strength(const StringName &p_action, bool p_exact = false) const;
  261. float get_action_raw_strength(const StringName &p_action, bool p_exact = false) const;
  262. float get_axis(const StringName &p_negative_action, const StringName &p_positive_action) const;
  263. 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;
  264. float get_joy_axis(int p_device, JoyAxis p_axis) const;
  265. String get_joy_name(int p_idx);
  266. TypedArray<int> get_connected_joypads();
  267. Vector2 get_joy_vibration_strength(int p_device);
  268. float get_joy_vibration_duration(int p_device);
  269. uint64_t get_joy_vibration_timestamp(int p_device);
  270. void joy_connection_changed(int p_idx, bool p_connected, const String &p_name, const String &p_guid = "", const Dictionary &p_joypad_info = Dictionary());
  271. Vector3 get_gravity() const;
  272. Vector3 get_accelerometer() const;
  273. Vector3 get_magnetometer() const;
  274. Vector3 get_gyroscope() const;
  275. Point2 get_mouse_position() const;
  276. Vector2 get_last_mouse_velocity();
  277. Vector2 get_last_mouse_screen_velocity();
  278. BitField<MouseButtonMask> get_mouse_button_mask() const;
  279. void warp_mouse(const Vector2 &p_position);
  280. Point2 warp_mouse_motion(const Ref<InputEventMouseMotion> &p_motion, const Rect2 &p_rect);
  281. void parse_input_event(const Ref<InputEvent> &p_event);
  282. void set_gravity(const Vector3 &p_gravity);
  283. void set_accelerometer(const Vector3 &p_accel);
  284. void set_magnetometer(const Vector3 &p_magnetometer);
  285. void set_gyroscope(const Vector3 &p_gyroscope);
  286. void set_joy_axis(int p_device, JoyAxis p_axis, float p_value);
  287. void start_joy_vibration(int p_device, float p_weak_magnitude, float p_strong_magnitude, float p_duration = 0);
  288. void stop_joy_vibration(int p_device);
  289. void vibrate_handheld(int p_duration_ms = 500, float p_amplitude = -1.0);
  290. void set_mouse_position(const Point2 &p_posf);
  291. void action_press(const StringName &p_action, float p_strength = 1.f);
  292. void action_release(const StringName &p_action);
  293. void set_emulate_touch_from_mouse(bool p_emulate);
  294. bool is_emulating_touch_from_mouse() const;
  295. void ensure_touch_mouse_raised();
  296. void set_emulate_mouse_from_touch(bool p_emulate);
  297. bool is_emulating_mouse_from_touch() const;
  298. CursorShape get_default_cursor_shape() const;
  299. void set_default_cursor_shape(CursorShape p_shape);
  300. CursorShape get_current_cursor_shape() const;
  301. void set_custom_mouse_cursor(const Ref<Resource> &p_cursor, CursorShape p_shape = Input::CURSOR_ARROW, const Vector2 &p_hotspot = Vector2());
  302. void parse_mapping(const String &p_mapping);
  303. void joy_button(int p_device, JoyButton p_button, bool p_pressed);
  304. void joy_axis(int p_device, JoyAxis p_axis, float p_value);
  305. void joy_hat(int p_device, BitField<HatMask> p_val);
  306. void add_joy_mapping(const String &p_mapping, bool p_update_existing = false);
  307. void remove_joy_mapping(const String &p_guid);
  308. int get_unused_joy_id();
  309. bool is_joy_known(int p_device);
  310. String get_joy_guid(int p_device) const;
  311. bool should_ignore_device(int p_vendor_id, int p_product_id) const;
  312. Dictionary get_joy_info(int p_device) const;
  313. void set_fallback_mapping(const String &p_guid);
  314. #ifdef DEBUG_ENABLED
  315. void flush_frame_parsed_events();
  316. #endif
  317. void flush_buffered_events();
  318. bool is_agile_input_event_flushing();
  319. void set_agile_input_event_flushing(bool p_enable);
  320. void set_use_accumulated_input(bool p_enable);
  321. bool is_using_accumulated_input();
  322. void release_pressed_events();
  323. void set_event_dispatch_function(EventDispatchFunc p_function);
  324. void set_disable_input(bool p_disable);
  325. bool is_input_disabled() const;
  326. Input();
  327. ~Input();
  328. };
  329. VARIANT_ENUM_CAST(Input::MouseMode);
  330. VARIANT_ENUM_CAST(Input::CursorShape);
  331. #endif // INPUT_H