input_default.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /**************************************************************************/
  2. /* input_default.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_DEFAULT_H
  31. #define INPUT_DEFAULT_H
  32. #include "core/os/input.h"
  33. class InputDefault : public Input {
  34. GDCLASS(InputDefault, Input);
  35. _THREAD_SAFE_CLASS_
  36. int mouse_button_mask;
  37. Set<int> physical_keys_pressed;
  38. Set<int> keys_pressed;
  39. Set<int> joy_buttons_pressed;
  40. Map<int, float> _joy_axis;
  41. //Map<StringName,int> custom_action_press;
  42. Vector3 gravity;
  43. Vector3 accelerometer;
  44. Vector3 magnetometer;
  45. Vector3 gyroscope;
  46. Vector2 mouse_pos;
  47. MainLoop *main_loop;
  48. bool legacy_just_pressed_behavior = false;
  49. struct Action {
  50. uint64_t pressed_physics_frame = UINT64_MAX;
  51. uint64_t pressed_idle_frame = UINT64_MAX;
  52. uint64_t released_physics_frame = UINT64_MAX;
  53. uint64_t released_idle_frame = UINT64_MAX;
  54. bool pressed = false;
  55. bool exact = true;
  56. float strength = 0.0f;
  57. float raw_strength = 0.0f;
  58. };
  59. Map<StringName, Action> action_state;
  60. bool emulate_touch_from_mouse;
  61. bool emulate_mouse_from_touch;
  62. int mouse_from_touch_index;
  63. struct SpeedTrack {
  64. uint64_t last_tick;
  65. Vector2 speed;
  66. Vector2 accum;
  67. float accum_t;
  68. float min_ref_frame;
  69. float max_ref_frame;
  70. void update(const Vector2 &p_delta_p);
  71. void reset();
  72. SpeedTrack();
  73. };
  74. struct Joypad {
  75. StringName name;
  76. StringName uid;
  77. bool connected;
  78. bool last_buttons[JOY_BUTTON_MAX + 12]; //apparently SDL specifies 35 possible buttons on android
  79. float last_axis[JOY_AXIS_MAX];
  80. int last_hat;
  81. int mapping;
  82. int hat_current;
  83. Joypad() {
  84. for (int i = 0; i < JOY_AXIS_MAX; i++) {
  85. last_axis[i] = 0.0f;
  86. }
  87. for (int i = 0; i < JOY_BUTTON_MAX + 12; i++) {
  88. last_buttons[i] = false;
  89. }
  90. connected = false;
  91. last_hat = HAT_MASK_CENTER;
  92. mapping = -1;
  93. hat_current = 0;
  94. }
  95. };
  96. SpeedTrack mouse_speed_track;
  97. Map<int, SpeedTrack> touch_speed_track;
  98. Map<int, Joypad> joy_names;
  99. int fallback_mapping;
  100. CursorShape default_shape;
  101. public:
  102. enum HatMask {
  103. HAT_MASK_CENTER = 0,
  104. HAT_MASK_UP = 1,
  105. HAT_MASK_RIGHT = 2,
  106. HAT_MASK_DOWN = 4,
  107. HAT_MASK_LEFT = 8,
  108. };
  109. enum HatDir {
  110. HAT_UP,
  111. HAT_RIGHT,
  112. HAT_DOWN,
  113. HAT_LEFT,
  114. HAT_MAX,
  115. };
  116. enum {
  117. JOYPADS_MAX = 16,
  118. };
  119. private:
  120. enum JoyType {
  121. TYPE_BUTTON,
  122. TYPE_AXIS,
  123. TYPE_HAT,
  124. TYPE_MAX,
  125. };
  126. enum JoyAxisRange {
  127. NEGATIVE_HALF_AXIS = -1,
  128. FULL_AXIS = 0,
  129. POSITIVE_HALF_AXIS = 1
  130. };
  131. struct JoyEvent {
  132. int type;
  133. int index;
  134. float value;
  135. };
  136. struct JoyBinding {
  137. JoyType inputType;
  138. union {
  139. int button;
  140. struct {
  141. int axis;
  142. JoyAxisRange range;
  143. bool invert;
  144. } axis;
  145. struct {
  146. int hat;
  147. HatMask hat_mask;
  148. } hat;
  149. } input;
  150. JoyType outputType;
  151. union {
  152. JoystickList button;
  153. struct {
  154. JoystickList axis;
  155. JoyAxisRange range;
  156. } axis;
  157. } output;
  158. };
  159. struct JoyDeviceMapping {
  160. String uid;
  161. String name;
  162. Vector<JoyBinding> bindings;
  163. };
  164. Vector<JoyDeviceMapping> map_db;
  165. Set<uint32_t> ignored_device_ids;
  166. JoyEvent _get_mapped_button_event(const JoyDeviceMapping &mapping, int p_button);
  167. JoyEvent _get_mapped_axis_event(const JoyDeviceMapping &mapping, int p_axis, float p_value);
  168. void _get_mapped_hat_events(const JoyDeviceMapping &mapping, int p_hat, JoyEvent r_events[HAT_MAX]);
  169. JoystickList _get_output_button(String output);
  170. JoystickList _get_output_axis(String output);
  171. void _button_event(int p_device, int p_index, bool p_pressed);
  172. void _axis_event(int p_device, int p_axis, float p_value);
  173. void _parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_emulated);
  174. List<Ref<InputEvent>> buffered_events;
  175. bool use_input_buffering;
  176. bool use_accumulated_input;
  177. #ifdef DEBUG_ENABLED
  178. Set<Ref<InputEvent>> frame_parsed_events;
  179. uint64_t last_parsed_frame = UINT64_MAX;
  180. #endif
  181. protected:
  182. struct VibrationInfo {
  183. float weak_magnitude;
  184. float strong_magnitude;
  185. float duration; // Duration in seconds
  186. uint64_t timestamp;
  187. };
  188. Map<int, VibrationInfo> joy_vibration;
  189. public:
  190. virtual bool is_key_pressed(int p_scancode) const;
  191. virtual bool is_physical_key_pressed(int p_scancode) const;
  192. virtual bool is_mouse_button_pressed(int p_button) const;
  193. virtual bool is_joy_button_pressed(int p_device, int p_button) const;
  194. virtual bool is_action_pressed(const StringName &p_action, bool p_exact = false) const;
  195. virtual bool is_action_just_pressed(const StringName &p_action, bool p_exact = false) const;
  196. virtual bool is_action_just_released(const StringName &p_action, bool p_exact = false) const;
  197. virtual float get_action_strength(const StringName &p_action, bool p_exact = false) const;
  198. virtual float get_action_raw_strength(const StringName &p_action, bool p_exact = false) const;
  199. virtual float get_joy_axis(int p_device, int p_axis) const;
  200. String get_joy_name(int p_idx);
  201. virtual Array get_connected_joypads();
  202. virtual Vector2 get_joy_vibration_strength(int p_device);
  203. virtual float get_joy_vibration_duration(int p_device);
  204. virtual uint64_t get_joy_vibration_timestamp(int p_device);
  205. void joy_connection_changed(int p_idx, bool p_connected, String p_name, String p_guid = "");
  206. virtual Vector3 get_gravity() const;
  207. virtual Vector3 get_accelerometer() const;
  208. virtual Vector3 get_magnetometer() const;
  209. virtual Vector3 get_gyroscope() const;
  210. virtual Point2 get_mouse_position() const;
  211. virtual Point2 get_last_mouse_speed();
  212. virtual int get_mouse_button_mask() const;
  213. virtual void warp_mouse_position(const Vector2 &p_to);
  214. virtual Point2i warp_mouse_motion(const Ref<InputEventMouseMotion> &p_motion, const Rect2 &p_rect);
  215. virtual void parse_input_event(const Ref<InputEvent> &p_event);
  216. virtual void set_gravity(const Vector3 &p_gravity);
  217. virtual void set_accelerometer(const Vector3 &p_accel);
  218. virtual void set_magnetometer(const Vector3 &p_magnetometer);
  219. virtual void set_gyroscope(const Vector3 &p_gyroscope);
  220. void set_joy_axis(int p_device, int p_axis, float p_value);
  221. virtual void start_joy_vibration(int p_device, float p_weak_magnitude, float p_strong_magnitude, float p_duration = 0);
  222. virtual void stop_joy_vibration(int p_device);
  223. virtual void vibrate_handheld(int p_duration_ms = 500);
  224. void set_main_loop(MainLoop *p_main_loop);
  225. void set_mouse_position(const Point2 &p_posf);
  226. void action_press(const StringName &p_action, float p_strength = 1.f);
  227. void action_release(const StringName &p_action);
  228. void iteration(float p_step);
  229. void set_emulate_touch_from_mouse(bool p_emulate);
  230. virtual bool is_emulating_touch_from_mouse() const;
  231. void ensure_touch_mouse_raised();
  232. void set_emulate_mouse_from_touch(bool p_emulate);
  233. virtual bool is_emulating_mouse_from_touch() const;
  234. virtual CursorShape get_default_cursor_shape() const;
  235. virtual void set_default_cursor_shape(CursorShape p_shape);
  236. virtual CursorShape get_current_cursor_shape() const;
  237. virtual void set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape = Input::CURSOR_ARROW, const Vector2 &p_hotspot = Vector2());
  238. void parse_mapping(String p_mapping);
  239. void joy_button(int p_device, int p_button, bool p_pressed);
  240. void joy_axis(int p_device, int p_axis, float p_value);
  241. void joy_hat(int p_device, int p_val);
  242. virtual void add_joy_mapping(String p_mapping, bool p_update_existing = false);
  243. virtual void remove_joy_mapping(String p_guid);
  244. virtual bool is_joy_known(int p_device);
  245. virtual String get_joy_guid(int p_device) const;
  246. bool should_ignore_device(int p_vendor_id, int p_product_id) const;
  247. virtual String get_joy_button_string(int p_button);
  248. virtual String get_joy_axis_string(int p_axis);
  249. virtual int get_joy_axis_index_from_string(String p_axis);
  250. virtual int get_joy_button_index_from_string(String p_button);
  251. int get_unused_joy_id();
  252. bool is_joy_mapped(int p_device);
  253. String get_joy_guid_remapped(int p_device) const;
  254. void set_fallback_mapping(String p_guid);
  255. virtual void flush_buffered_events();
  256. virtual bool is_using_input_buffering();
  257. virtual void set_use_input_buffering(bool p_enable);
  258. virtual bool is_using_accumulated_input();
  259. virtual void set_use_accumulated_input(bool p_enable);
  260. virtual void release_pressed_events();
  261. InputDefault();
  262. };
  263. #endif // INPUT_DEFAULT_H