input.h 11 KB

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