input_default.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*************************************************************************/
  2. /* input_default.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 VibrationInfo {
  58. float weak_magnitude;
  59. float strong_magnitude;
  60. float duration; // Duration in seconds
  61. uint64_t timestamp;
  62. };
  63. Map<int, VibrationInfo> joy_vibration;
  64. struct SpeedTrack {
  65. uint64_t last_tick;
  66. Vector2 speed;
  67. Vector2 accum;
  68. float accum_t;
  69. float min_ref_frame;
  70. float max_ref_frame;
  71. void update(const Vector2 &p_delta_p);
  72. void reset();
  73. SpeedTrack();
  74. };
  75. struct Joypad {
  76. StringName name;
  77. StringName uid;
  78. bool connected;
  79. bool last_buttons[JOY_BUTTON_MAX + 19]; //apparently SDL specifies 35 possible buttons on android
  80. float last_axis[JOY_AXIS_MAX];
  81. float filter;
  82. int last_hat;
  83. int mapping;
  84. int hat_current;
  85. Joypad() {
  86. for (int i = 0; i < JOY_AXIS_MAX; i++) {
  87. last_axis[i] = 0.0f;
  88. }
  89. for (int i = 0; i < JOY_BUTTON_MAX + 19; i++) {
  90. last_buttons[i] = false;
  91. }
  92. connected = false;
  93. last_hat = HAT_MASK_CENTER;
  94. filter = 0.01f;
  95. mapping = -1;
  96. hat_current = 0;
  97. }
  98. };
  99. SpeedTrack mouse_speed_track;
  100. Map<int, SpeedTrack> touch_speed_track;
  101. Map<int, Joypad> joy_names;
  102. int fallback_mapping;
  103. CursorShape default_shape;
  104. public:
  105. enum HatMask {
  106. HAT_MASK_CENTER = 0,
  107. HAT_MASK_UP = 1,
  108. HAT_MASK_RIGHT = 2,
  109. HAT_MASK_DOWN = 4,
  110. HAT_MASK_LEFT = 8,
  111. };
  112. enum HatDir {
  113. HAT_UP,
  114. HAT_RIGHT,
  115. HAT_DOWN,
  116. HAT_LEFT,
  117. HAT_MAX,
  118. };
  119. enum {
  120. JOYPADS_MAX = 16,
  121. };
  122. struct JoyAxis {
  123. int min;
  124. float value;
  125. };
  126. private:
  127. enum JoyType {
  128. TYPE_BUTTON,
  129. TYPE_AXIS,
  130. TYPE_HAT,
  131. TYPE_MAX,
  132. };
  133. struct JoyEvent {
  134. int type;
  135. int index;
  136. int value;
  137. };
  138. struct JoyDeviceMapping {
  139. String uid;
  140. String name;
  141. Map<int, JoyEvent> buttons;
  142. Map<int, JoyEvent> axis;
  143. JoyEvent hat[HAT_MAX];
  144. };
  145. JoyEvent hat_map_default[HAT_MAX];
  146. Vector<JoyDeviceMapping> map_db;
  147. JoyEvent _find_to_event(String p_to);
  148. void _button_event(int p_device, int p_index, bool p_pressed);
  149. void _axis_event(int p_device, int p_axis, float p_value);
  150. float _handle_deadzone(int p_device, int p_axis, float p_value);
  151. void _parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_emulated);
  152. public:
  153. virtual bool is_key_pressed(int p_scancode) const;
  154. virtual bool is_mouse_button_pressed(int p_button) const;
  155. virtual bool is_joy_button_pressed(int p_device, int p_button) const;
  156. virtual bool is_action_pressed(const StringName &p_action) const;
  157. virtual bool is_action_just_pressed(const StringName &p_action) const;
  158. virtual bool is_action_just_released(const StringName &p_action) const;
  159. virtual float get_action_strength(const StringName &p_action) const;
  160. virtual float get_joy_axis(int p_device, int p_axis) const;
  161. String get_joy_name(int p_idx);
  162. virtual Array get_connected_joypads();
  163. virtual Vector2 get_joy_vibration_strength(int p_device);
  164. virtual float get_joy_vibration_duration(int p_device);
  165. virtual uint64_t get_joy_vibration_timestamp(int p_device);
  166. void joy_connection_changed(int p_idx, bool p_connected, String p_name, String p_guid = "");
  167. void parse_joypad_mapping(String p_mapping, bool p_update_existing);
  168. virtual Vector3 get_gravity() const;
  169. virtual Vector3 get_accelerometer() const;
  170. virtual Vector3 get_magnetometer() const;
  171. virtual Vector3 get_gyroscope() const;
  172. virtual Point2 get_mouse_position() const;
  173. virtual Point2 get_last_mouse_speed() const;
  174. virtual int get_mouse_button_mask() const;
  175. virtual void warp_mouse_position(const Vector2 &p_to);
  176. virtual Point2i warp_mouse_motion(const Ref<InputEventMouseMotion> &p_motion, const Rect2 &p_rect);
  177. virtual void parse_input_event(const Ref<InputEvent> &p_event);
  178. void set_gravity(const Vector3 &p_gravity);
  179. void set_accelerometer(const Vector3 &p_accel);
  180. void set_magnetometer(const Vector3 &p_magnetometer);
  181. void set_gyroscope(const Vector3 &p_gyroscope);
  182. void set_joy_axis(int p_device, int p_axis, float p_value);
  183. virtual void start_joy_vibration(int p_device, float p_weak_magnitude, float p_strong_magnitude, float p_duration = 0);
  184. virtual void stop_joy_vibration(int p_device);
  185. void set_main_loop(MainLoop *p_main_loop);
  186. void set_mouse_position(const Point2 &p_posf);
  187. void action_press(const StringName &p_action, float p_strength = 1.f);
  188. void action_release(const StringName &p_action);
  189. void iteration(float p_step);
  190. void set_emulate_touch_from_mouse(bool p_emulate);
  191. virtual bool is_emulating_touch_from_mouse() const;
  192. void ensure_touch_mouse_raised();
  193. void set_emulate_mouse_from_touch(bool p_emulate);
  194. virtual bool is_emulating_mouse_from_touch() const;
  195. virtual CursorShape get_default_cursor_shape();
  196. virtual void set_default_cursor_shape(CursorShape p_shape);
  197. virtual void set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape = Input::CURSOR_ARROW, const Vector2 &p_hotspot = Vector2());
  198. virtual void set_mouse_in_window(bool p_in_window);
  199. void parse_mapping(String p_mapping);
  200. void joy_button(int p_device, int p_button, bool p_pressed);
  201. void joy_axis(int p_device, int p_axis, const JoyAxis &p_value);
  202. void joy_hat(int p_device, int p_val);
  203. virtual void add_joy_mapping(String p_mapping, bool p_update_existing = false);
  204. virtual void remove_joy_mapping(String p_guid);
  205. virtual bool is_joy_known(int p_device);
  206. virtual String get_joy_guid(int p_device) const;
  207. virtual String get_joy_button_string(int p_button);
  208. virtual String get_joy_axis_string(int p_axis);
  209. virtual int get_joy_axis_index_from_string(String p_axis);
  210. virtual int get_joy_button_index_from_string(String p_button);
  211. int get_unused_joy_id();
  212. bool is_joy_mapped(int p_device);
  213. String get_joy_guid_remapped(int p_device) const;
  214. void set_fallback_mapping(String p_guid);
  215. InputDefault();
  216. };
  217. #endif // INPUT_DEFAULT_H