input_default.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 "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. };
  52. Map<StringName, Action> action_state;
  53. bool emulate_touch;
  54. bool emulate_mouse_from_touch;
  55. int mouse_from_touch_index;
  56. struct VibrationInfo {
  57. float weak_magnitude;
  58. float strong_magnitude;
  59. float duration; // Duration in seconds
  60. uint64_t timestamp;
  61. };
  62. Map<int, VibrationInfo> joy_vibration;
  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 + 19]; //apparently SDL specifies 35 possible buttons on android
  79. float last_axis[JOY_AXIS_MAX];
  80. float filter;
  81. int last_hat;
  82. int mapping;
  83. int hat_current;
  84. Joypad() {
  85. for (int i = 0; i < JOY_AXIS_MAX; i++) {
  86. last_axis[i] = 0.0f;
  87. }
  88. for (int i = 0; i < JOY_BUTTON_MAX + 19; i++) {
  89. last_buttons[i] = false;
  90. }
  91. connected = false;
  92. last_hat = HAT_MASK_CENTER;
  93. filter = 0.01f;
  94. mapping = -1;
  95. }
  96. };
  97. SpeedTrack mouse_speed_track;
  98. Map<int, Joypad> joy_names;
  99. int fallback_mapping;
  100. RES custom_cursor;
  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. struct JoyAxis {
  120. int min;
  121. float value;
  122. };
  123. private:
  124. enum JoyType {
  125. TYPE_BUTTON,
  126. TYPE_AXIS,
  127. TYPE_HAT,
  128. TYPE_MAX,
  129. };
  130. struct JoyEvent {
  131. int type;
  132. int index;
  133. int value;
  134. };
  135. struct JoyDeviceMapping {
  136. String uid;
  137. String name;
  138. Map<int, JoyEvent> buttons;
  139. Map<int, JoyEvent> axis;
  140. JoyEvent hat[HAT_MAX];
  141. };
  142. JoyEvent hat_map_default[HAT_MAX];
  143. Vector<JoyDeviceMapping> map_db;
  144. JoyEvent _find_to_event(String p_to);
  145. void _button_event(int p_device, int p_index, bool p_pressed);
  146. void _axis_event(int p_device, int p_axis, float p_value);
  147. float _handle_deadzone(int p_device, int p_axis, float p_value);
  148. void _parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_emulated);
  149. public:
  150. virtual bool is_key_pressed(int p_scancode) const;
  151. virtual bool is_mouse_button_pressed(int p_button) const;
  152. virtual bool is_joy_button_pressed(int p_device, int p_button) const;
  153. virtual bool is_action_pressed(const StringName &p_action) const;
  154. virtual bool is_action_just_pressed(const StringName &p_action) const;
  155. virtual bool is_action_just_released(const StringName &p_action) const;
  156. virtual float get_joy_axis(int p_device, int p_axis) const;
  157. String get_joy_name(int p_idx);
  158. virtual Array get_connected_joypads();
  159. virtual Vector2 get_joy_vibration_strength(int p_device);
  160. virtual float get_joy_vibration_duration(int p_device);
  161. virtual uint64_t get_joy_vibration_timestamp(int p_device);
  162. void joy_connection_changed(int p_idx, bool p_connected, String p_name, String p_guid = "");
  163. void parse_joypad_mapping(String p_mapping, bool p_update_existing);
  164. virtual Vector3 get_gravity() const;
  165. virtual Vector3 get_accelerometer() const;
  166. virtual Vector3 get_magnetometer() const;
  167. virtual Vector3 get_gyroscope() const;
  168. virtual Point2 get_mouse_position() const;
  169. virtual Point2 get_last_mouse_speed() const;
  170. virtual int get_mouse_button_mask() const;
  171. virtual void warp_mouse_position(const Vector2 &p_to);
  172. virtual Point2i warp_mouse_motion(const Ref<InputEventMouseMotion> &p_motion, const Rect2 &p_rect);
  173. virtual void parse_input_event(const Ref<InputEvent> &p_event);
  174. void set_gravity(const Vector3 &p_gravity);
  175. void set_accelerometer(const Vector3 &p_accel);
  176. void set_magnetometer(const Vector3 &p_magnetometer);
  177. void set_gyroscope(const Vector3 &p_gyroscope);
  178. void set_joy_axis(int p_device, int p_axis, float p_value);
  179. virtual void start_joy_vibration(int p_device, float p_weak_magnitude, float p_strong_magnitude, float p_duration = 0);
  180. virtual void stop_joy_vibration(int p_device);
  181. void set_main_loop(MainLoop *p_main_loop);
  182. void set_mouse_position(const Point2 &p_posf);
  183. void action_press(const StringName &p_action);
  184. void action_release(const StringName &p_action);
  185. void iteration(float p_step);
  186. void set_emulate_touch(bool p_emulate);
  187. virtual bool is_emulating_touchscreen() const;
  188. void ensure_touch_mouse_raised();
  189. void set_emulate_mouse_from_touch(bool p_emulate);
  190. virtual bool is_emulating_mouse_from_touch() const;
  191. virtual void set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape = Input::CURSOR_ARROW, const Vector2 &p_hotspot = Vector2());
  192. virtual void set_mouse_in_window(bool p_in_window);
  193. void parse_mapping(String p_mapping);
  194. void joy_button(int p_device, int p_button, bool p_pressed);
  195. void joy_axis(int p_device, int p_axis, const JoyAxis &p_value);
  196. void joy_hat(int p_device, int p_val);
  197. virtual void add_joy_mapping(String p_mapping, bool p_update_existing = false);
  198. virtual void remove_joy_mapping(String p_guid);
  199. virtual bool is_joy_known(int p_device);
  200. virtual String get_joy_guid(int p_device) const;
  201. virtual String get_joy_button_string(int p_button);
  202. virtual String get_joy_axis_string(int p_axis);
  203. virtual int get_joy_axis_index_from_string(String p_axis);
  204. virtual int get_joy_button_index_from_string(String p_button);
  205. int get_unused_joy_id();
  206. bool is_joy_mapped(int p_device);
  207. String get_joy_guid_remapped(int p_device) const;
  208. void set_fallback_mapping(String p_guid);
  209. InputDefault();
  210. };
  211. #endif // INPUT_DEFAULT_H