input_default.h 9.7 KB

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