input.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*************************************************************************/
  2. /* input.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_H
  31. #define INPUT_H
  32. #include "core/object.h"
  33. #include "core/os/main_loop.h"
  34. #include "core/os/thread_safe.h"
  35. class Input : public Object {
  36. GDCLASS(Input, Object);
  37. _THREAD_SAFE_CLASS_
  38. static Input *singleton;
  39. public:
  40. enum MouseMode {
  41. MOUSE_MODE_VISIBLE,
  42. MOUSE_MODE_HIDDEN,
  43. MOUSE_MODE_CAPTURED,
  44. MOUSE_MODE_CONFINED
  45. };
  46. #undef CursorShape
  47. enum CursorShape {
  48. CURSOR_ARROW,
  49. CURSOR_IBEAM,
  50. CURSOR_POINTING_HAND,
  51. CURSOR_CROSS,
  52. CURSOR_WAIT,
  53. CURSOR_BUSY,
  54. CURSOR_DRAG,
  55. CURSOR_CAN_DROP,
  56. CURSOR_FORBIDDEN,
  57. CURSOR_VSIZE,
  58. CURSOR_HSIZE,
  59. CURSOR_BDIAGSIZE,
  60. CURSOR_FDIAGSIZE,
  61. CURSOR_MOVE,
  62. CURSOR_VSPLIT,
  63. CURSOR_HSPLIT,
  64. CURSOR_HELP,
  65. CURSOR_MAX
  66. };
  67. enum HatMask {
  68. HAT_MASK_CENTER = 0,
  69. HAT_MASK_UP = 1,
  70. HAT_MASK_RIGHT = 2,
  71. HAT_MASK_DOWN = 4,
  72. HAT_MASK_LEFT = 8,
  73. };
  74. enum HatDir {
  75. HAT_UP,
  76. HAT_RIGHT,
  77. HAT_DOWN,
  78. HAT_LEFT,
  79. HAT_MAX,
  80. };
  81. enum {
  82. JOYPADS_MAX = 16,
  83. };
  84. struct JoyAxis {
  85. int min;
  86. float value;
  87. };
  88. private:
  89. int mouse_button_mask;
  90. Set<int> keys_pressed;
  91. Set<int> joy_buttons_pressed;
  92. Map<int, float> _joy_axis;
  93. //Map<StringName,int> custom_action_press;
  94. Vector3 gravity;
  95. Vector3 accelerometer;
  96. Vector3 magnetometer;
  97. Vector3 gyroscope;
  98. Vector2 mouse_pos;
  99. int64_t mouse_window;
  100. MainLoop *main_loop;
  101. struct Action {
  102. uint64_t physics_frame;
  103. uint64_t idle_frame;
  104. bool pressed;
  105. float strength;
  106. };
  107. Map<StringName, Action> action_state;
  108. bool emulate_touch_from_mouse;
  109. bool emulate_mouse_from_touch;
  110. int mouse_from_touch_index;
  111. struct VibrationInfo {
  112. float weak_magnitude;
  113. float strong_magnitude;
  114. float duration; // Duration in seconds
  115. uint64_t timestamp;
  116. };
  117. Map<int, VibrationInfo> joy_vibration;
  118. struct SpeedTrack {
  119. uint64_t last_tick;
  120. Vector2 speed;
  121. Vector2 accum;
  122. float accum_t;
  123. float min_ref_frame;
  124. float max_ref_frame;
  125. void update(const Vector2 &p_delta_p);
  126. void reset();
  127. SpeedTrack();
  128. };
  129. struct Joypad {
  130. StringName name;
  131. StringName uid;
  132. bool connected;
  133. bool last_buttons[JOY_BUTTON_MAX + 19]; //apparently SDL specifies 35 possible buttons on android
  134. float last_axis[JOY_AXIS_MAX];
  135. float filter;
  136. int last_hat;
  137. int mapping;
  138. int hat_current;
  139. Joypad() {
  140. for (int i = 0; i < JOY_AXIS_MAX; i++) {
  141. last_axis[i] = 0.0f;
  142. }
  143. for (int i = 0; i < JOY_BUTTON_MAX + 19; i++) {
  144. last_buttons[i] = false;
  145. }
  146. connected = false;
  147. last_hat = HAT_MASK_CENTER;
  148. filter = 0.01f;
  149. mapping = -1;
  150. hat_current = 0;
  151. }
  152. };
  153. SpeedTrack mouse_speed_track;
  154. Map<int, SpeedTrack> touch_speed_track;
  155. Map<int, Joypad> joy_names;
  156. int fallback_mapping;
  157. CursorShape default_shape;
  158. enum JoyType {
  159. TYPE_BUTTON,
  160. TYPE_AXIS,
  161. TYPE_HAT,
  162. TYPE_MAX,
  163. };
  164. struct JoyEvent {
  165. int type;
  166. int index;
  167. int value;
  168. };
  169. struct JoyDeviceMapping {
  170. String uid;
  171. String name;
  172. Map<int, JoyEvent> buttons;
  173. Map<int, JoyEvent> axis;
  174. JoyEvent hat[HAT_MAX];
  175. };
  176. JoyEvent hat_map_default[HAT_MAX];
  177. Vector<JoyDeviceMapping> map_db;
  178. JoyEvent _find_to_event(String p_to);
  179. void _button_event(int p_device, int p_index, bool p_pressed);
  180. void _axis_event(int p_device, int p_axis, float p_value);
  181. float _handle_deadzone(int p_device, int p_axis, float p_value);
  182. void _parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_emulated);
  183. List<Ref<InputEvent>> accumulated_events;
  184. bool use_accumulated_input;
  185. friend class DisplayServer;
  186. static void (*set_mouse_mode_func)(MouseMode);
  187. static MouseMode (*get_mouse_mode_func)();
  188. static void (*warp_mouse_func)(const Vector2 &p_to_pos);
  189. static CursorShape (*get_current_cursor_shape_func)();
  190. static void (*set_custom_mouse_cursor_func)(const RES &, CursorShape, const Vector2 &);
  191. protected:
  192. static void _bind_methods();
  193. public:
  194. void set_mouse_mode(MouseMode p_mode);
  195. MouseMode get_mouse_mode() const;
  196. void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const;
  197. static Input *get_singleton();
  198. bool is_key_pressed(int p_keycode) const;
  199. bool is_mouse_button_pressed(int p_button) const;
  200. bool is_joy_button_pressed(int p_device, int p_button) const;
  201. bool is_action_pressed(const StringName &p_action) const;
  202. bool is_action_just_pressed(const StringName &p_action) const;
  203. bool is_action_just_released(const StringName &p_action) const;
  204. float get_action_strength(const StringName &p_action) const;
  205. float get_joy_axis(int p_device, int p_axis) const;
  206. String get_joy_name(int p_idx);
  207. Array get_connected_joypads();
  208. Vector2 get_joy_vibration_strength(int p_device);
  209. float get_joy_vibration_duration(int p_device);
  210. uint64_t get_joy_vibration_timestamp(int p_device);
  211. void joy_connection_changed(int p_idx, bool p_connected, String p_name, String p_guid = "");
  212. void parse_joypad_mapping(String p_mapping, bool p_update_existing);
  213. Vector3 get_gravity() const;
  214. Vector3 get_accelerometer() const;
  215. Vector3 get_magnetometer() const;
  216. Vector3 get_gyroscope() const;
  217. Point2 get_mouse_position() const;
  218. Point2 get_last_mouse_speed() const;
  219. int get_mouse_button_mask() const;
  220. void warp_mouse_position(const Vector2 &p_to);
  221. Point2i warp_mouse_motion(const Ref<InputEventMouseMotion> &p_motion, const Rect2 &p_rect);
  222. void parse_drop_files(const Vector<String> &p_files);
  223. void parse_notification(int p_notification);
  224. void parse_input_event(const Ref<InputEvent> &p_event);
  225. void set_gravity(const Vector3 &p_gravity);
  226. void set_accelerometer(const Vector3 &p_accel);
  227. void set_magnetometer(const Vector3 &p_magnetometer);
  228. void set_gyroscope(const Vector3 &p_gyroscope);
  229. void set_joy_axis(int p_device, int p_axis, float p_value);
  230. void start_joy_vibration(int p_device, float p_weak_magnitude, float p_strong_magnitude, float p_duration = 0);
  231. void stop_joy_vibration(int p_device);
  232. void vibrate_handheld(int p_duration_ms = 500);
  233. void set_main_loop(MainLoop *p_main_loop);
  234. void set_mouse_position(const Point2 &p_posf);
  235. void action_press(const StringName &p_action, float p_strength = 1.f);
  236. void action_release(const StringName &p_action);
  237. void iteration(float p_step);
  238. void set_emulate_touch_from_mouse(bool p_emulate);
  239. bool is_emulating_touch_from_mouse() const;
  240. void ensure_touch_mouse_raised();
  241. void set_emulate_mouse_from_touch(bool p_emulate);
  242. bool is_emulating_mouse_from_touch() const;
  243. CursorShape get_default_cursor_shape() const;
  244. void set_default_cursor_shape(CursorShape p_shape);
  245. CursorShape get_current_cursor_shape() const;
  246. void set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape = Input::CURSOR_ARROW, const Vector2 &p_hotspot = Vector2());
  247. void parse_mapping(String p_mapping);
  248. void joy_button(int p_device, int p_button, bool p_pressed);
  249. void joy_axis(int p_device, int p_axis, const JoyAxis &p_value);
  250. void joy_hat(int p_device, int p_val);
  251. void add_joy_mapping(String p_mapping, bool p_update_existing = false);
  252. void remove_joy_mapping(String p_guid);
  253. String get_joy_button_string(int p_button);
  254. String get_joy_axis_string(int p_axis);
  255. int get_joy_axis_index_from_string(String p_axis);
  256. int get_joy_button_index_from_string(String p_button);
  257. int get_unused_joy_id();
  258. bool is_joy_known(int p_device);
  259. String get_joy_guid(int p_device) const;
  260. void set_fallback_mapping(String p_guid);
  261. void accumulate_input_event(const Ref<InputEvent> &p_event);
  262. void flush_accumulated_events();
  263. void set_use_accumulated_input(bool p_enable);
  264. void release_pressed_events();
  265. Input();
  266. };
  267. VARIANT_ENUM_CAST(Input::MouseMode);
  268. VARIANT_ENUM_CAST(Input::CursorShape);
  269. #endif // INPUT_H