input.h 13 KB

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