input.h 12 KB

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