input_default.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*************************************************************************/
  2. /* input_default.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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> keys_pressed;
  38. Set<int> joy_buttons_pressed;
  39. Map<int, float> _joy_axis;
  40. //Map<StringName,int> custom_action_press;
  41. Vector3 gravity;
  42. Vector3 accelerometer;
  43. Vector3 magnetometer;
  44. Vector3 gyroscope;
  45. Vector2 mouse_pos;
  46. MainLoop *main_loop;
  47. struct Action {
  48. uint64_t physics_frame;
  49. uint64_t idle_frame;
  50. bool pressed;
  51. float strength;
  52. };
  53. Map<StringName, Action> action_state;
  54. bool emulate_touch_from_mouse;
  55. bool emulate_mouse_from_touch;
  56. int mouse_from_touch_index;
  57. struct SpeedTrack {
  58. uint64_t last_tick;
  59. Vector2 speed;
  60. Vector2 accum;
  61. float accum_t;
  62. float min_ref_frame;
  63. float max_ref_frame;
  64. void update(const Vector2 &p_delta_p);
  65. void reset();
  66. SpeedTrack();
  67. };
  68. struct Joypad {
  69. StringName name;
  70. StringName uid;
  71. bool connected;
  72. bool last_buttons[JOY_BUTTON_MAX + 19]; //apparently SDL specifies 35 possible buttons on android
  73. float last_axis[JOY_AXIS_MAX];
  74. float filter;
  75. int last_hat;
  76. int mapping;
  77. int hat_current;
  78. Joypad() {
  79. for (int i = 0; i < JOY_AXIS_MAX; i++) {
  80. last_axis[i] = 0.0f;
  81. }
  82. for (int i = 0; i < JOY_BUTTON_MAX + 19; i++) {
  83. last_buttons[i] = false;
  84. }
  85. connected = false;
  86. last_hat = HAT_MASK_CENTER;
  87. filter = 0.01f;
  88. mapping = -1;
  89. hat_current = 0;
  90. }
  91. };
  92. SpeedTrack mouse_speed_track;
  93. Map<int, SpeedTrack> touch_speed_track;
  94. Map<int, Joypad> joy_names;
  95. int fallback_mapping;
  96. CursorShape default_shape;
  97. public:
  98. enum HatMask {
  99. HAT_MASK_CENTER = 0,
  100. HAT_MASK_UP = 1,
  101. HAT_MASK_RIGHT = 2,
  102. HAT_MASK_DOWN = 4,
  103. HAT_MASK_LEFT = 8,
  104. };
  105. enum HatDir {
  106. HAT_UP,
  107. HAT_RIGHT,
  108. HAT_DOWN,
  109. HAT_LEFT,
  110. HAT_MAX,
  111. };
  112. enum {
  113. JOYPADS_MAX = 16,
  114. };
  115. struct JoyAxis {
  116. int min;
  117. float value;
  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. JoyEvent _get_mapped_button_event(const JoyDeviceMapping &mapping, int p_button);
  166. JoyEvent _get_mapped_axis_event(const JoyDeviceMapping &mapping, int p_axis, const JoyAxis &p_value);
  167. void _get_mapped_hat_events(const JoyDeviceMapping &mapping, int p_hat, JoyEvent r_events[HAT_MAX]);
  168. JoystickList _get_output_button(String output);
  169. JoystickList _get_output_axis(String output);
  170. void _button_event(int p_device, int p_index, bool p_pressed);
  171. void _axis_event(int p_device, int p_axis, float p_value);
  172. float _handle_deadzone(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> > accumulated_events;
  175. bool use_accumulated_input;
  176. protected:
  177. struct VibrationInfo {
  178. float weak_magnitude;
  179. float strong_magnitude;
  180. float duration; // Duration in seconds
  181. uint64_t timestamp;
  182. };
  183. Map<int, VibrationInfo> joy_vibration;
  184. public:
  185. virtual bool is_key_pressed(int p_scancode) const;
  186. virtual bool is_mouse_button_pressed(int p_button) const;
  187. virtual bool is_joy_button_pressed(int p_device, int p_button) const;
  188. virtual bool is_action_pressed(const StringName &p_action) const;
  189. virtual bool is_action_just_pressed(const StringName &p_action) const;
  190. virtual bool is_action_just_released(const StringName &p_action) const;
  191. virtual float get_action_strength(const StringName &p_action) const;
  192. virtual float get_joy_axis(int p_device, int p_axis) const;
  193. String get_joy_name(int p_idx);
  194. virtual Array get_connected_joypads();
  195. virtual Vector2 get_joy_vibration_strength(int p_device);
  196. virtual float get_joy_vibration_duration(int p_device);
  197. virtual uint64_t get_joy_vibration_timestamp(int p_device);
  198. void joy_connection_changed(int p_idx, bool p_connected, String p_name, String p_guid = "");
  199. void parse_joypad_mapping(String p_mapping, bool p_update_existing);
  200. virtual Vector3 get_gravity() const;
  201. virtual Vector3 get_accelerometer() const;
  202. virtual Vector3 get_magnetometer() const;
  203. virtual Vector3 get_gyroscope() const;
  204. virtual Point2 get_mouse_position() const;
  205. virtual Point2 get_last_mouse_speed() const;
  206. virtual int get_mouse_button_mask() const;
  207. virtual void warp_mouse_position(const Vector2 &p_to);
  208. virtual Point2i warp_mouse_motion(const Ref<InputEventMouseMotion> &p_motion, const Rect2 &p_rect);
  209. virtual void parse_input_event(const Ref<InputEvent> &p_event);
  210. void set_gravity(const Vector3 &p_gravity);
  211. void set_accelerometer(const Vector3 &p_accel);
  212. void set_magnetometer(const Vector3 &p_magnetometer);
  213. void set_gyroscope(const Vector3 &p_gyroscope);
  214. void set_joy_axis(int p_device, int p_axis, float p_value);
  215. virtual void start_joy_vibration(int p_device, float p_weak_magnitude, float p_strong_magnitude, float p_duration = 0);
  216. virtual void stop_joy_vibration(int p_device);
  217. virtual void vibrate_handheld(int p_duration_ms = 500);
  218. void set_main_loop(MainLoop *p_main_loop);
  219. void set_mouse_position(const Point2 &p_posf);
  220. void action_press(const StringName &p_action, float p_strength = 1.f);
  221. void action_release(const StringName &p_action);
  222. void iteration(float p_step);
  223. void set_emulate_touch_from_mouse(bool p_emulate);
  224. virtual bool is_emulating_touch_from_mouse() const;
  225. void ensure_touch_mouse_raised();
  226. void set_emulate_mouse_from_touch(bool p_emulate);
  227. virtual bool is_emulating_mouse_from_touch() const;
  228. virtual CursorShape get_default_cursor_shape() const;
  229. virtual void set_default_cursor_shape(CursorShape p_shape);
  230. virtual CursorShape get_current_cursor_shape() const;
  231. virtual void set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape = Input::CURSOR_ARROW, const Vector2 &p_hotspot = Vector2());
  232. void parse_mapping(String p_mapping);
  233. void joy_button(int p_device, int p_button, bool p_pressed);
  234. void joy_axis(int p_device, int p_axis, const JoyAxis &p_value);
  235. void joy_hat(int p_device, int p_val);
  236. virtual void add_joy_mapping(String p_mapping, bool p_update_existing = false);
  237. virtual void remove_joy_mapping(String p_guid);
  238. virtual bool is_joy_known(int p_device);
  239. virtual String get_joy_guid(int p_device) const;
  240. virtual String get_joy_button_string(int p_button);
  241. virtual String get_joy_axis_string(int p_axis);
  242. virtual int get_joy_axis_index_from_string(String p_axis);
  243. virtual int get_joy_button_index_from_string(String p_button);
  244. int get_unused_joy_id();
  245. bool is_joy_mapped(int p_device);
  246. String get_joy_guid_remapped(int p_device) const;
  247. void set_fallback_mapping(String p_guid);
  248. virtual void accumulate_input_event(const Ref<InputEvent> &p_event);
  249. virtual void flush_accumulated_events();
  250. virtual void set_use_accumulated_input(bool p_enable);
  251. virtual void release_pressed_events();
  252. InputDefault();
  253. };
  254. #endif // INPUT_DEFAULT_H