input.h 14 KB

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