| 1 | /**************************************************************************/ |
| 2 | /* input.h */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 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 | |
| 31 | #ifndef INPUT_H |
| 32 | #define INPUT_H |
| 33 | |
| 34 | #include "core/input/input_event.h" |
| 35 | #include "core/object/object.h" |
| 36 | #include "core/os/keyboard.h" |
| 37 | #include "core/os/thread_safe.h" |
| 38 | #include "core/templates/rb_set.h" |
| 39 | #include "core/variant/typed_array.h" |
| 40 | |
| 41 | class Input : public Object { |
| 42 | GDCLASS(Input, Object); |
| 43 | _THREAD_SAFE_CLASS_ |
| 44 | |
| 45 | static Input *singleton; |
| 46 | |
| 47 | public: |
| 48 | enum MouseMode { |
| 49 | MOUSE_MODE_VISIBLE, |
| 50 | MOUSE_MODE_HIDDEN, |
| 51 | MOUSE_MODE_CAPTURED, |
| 52 | MOUSE_MODE_CONFINED, |
| 53 | MOUSE_MODE_CONFINED_HIDDEN, |
| 54 | }; |
| 55 | |
| 56 | #undef CursorShape |
| 57 | enum CursorShape { |
| 58 | CURSOR_ARROW, |
| 59 | CURSOR_IBEAM, |
| 60 | CURSOR_POINTING_HAND, |
| 61 | CURSOR_CROSS, |
| 62 | CURSOR_WAIT, |
| 63 | CURSOR_BUSY, |
| 64 | CURSOR_DRAG, |
| 65 | CURSOR_CAN_DROP, |
| 66 | CURSOR_FORBIDDEN, |
| 67 | CURSOR_VSIZE, |
| 68 | CURSOR_HSIZE, |
| 69 | CURSOR_BDIAGSIZE, |
| 70 | CURSOR_FDIAGSIZE, |
| 71 | CURSOR_MOVE, |
| 72 | CURSOR_VSPLIT, |
| 73 | CURSOR_HSPLIT, |
| 74 | CURSOR_HELP, |
| 75 | CURSOR_MAX |
| 76 | }; |
| 77 | |
| 78 | enum { |
| 79 | JOYPADS_MAX = 16, |
| 80 | }; |
| 81 | |
| 82 | typedef void (*EventDispatchFunc)(const Ref<InputEvent> &p_event); |
| 83 | |
| 84 | private: |
| 85 | BitField<MouseButtonMask> mouse_button_mask; |
| 86 | |
| 87 | RBSet<Key> key_label_pressed; |
| 88 | RBSet<Key> physical_keys_pressed; |
| 89 | RBSet<Key> keys_pressed; |
| 90 | RBSet<JoyButton> joy_buttons_pressed; |
| 91 | RBMap<JoyAxis, float> _joy_axis; |
| 92 | //RBMap<StringName,int> custom_action_press; |
| 93 | Vector3 gravity; |
| 94 | Vector3 accelerometer; |
| 95 | Vector3 magnetometer; |
| 96 | Vector3 gyroscope; |
| 97 | Vector2 mouse_pos; |
| 98 | int64_t mouse_window = 0; |
| 99 | bool legacy_just_pressed_behavior = false; |
| 100 | |
| 101 | struct Action { |
| 102 | uint64_t pressed_physics_frame = UINT64_MAX; |
| 103 | uint64_t pressed_process_frame = UINT64_MAX; |
| 104 | uint64_t released_physics_frame = UINT64_MAX; |
| 105 | uint64_t released_process_frame = UINT64_MAX; |
| 106 | int pressed = 0; |
| 107 | bool axis_pressed = false; |
| 108 | bool exact = true; |
| 109 | float strength = 0.0f; |
| 110 | float raw_strength = 0.0f; |
| 111 | }; |
| 112 | |
| 113 | HashMap<StringName, Action> action_state; |
| 114 | |
| 115 | bool emulate_touch_from_mouse = false; |
| 116 | bool emulate_mouse_from_touch = false; |
| 117 | bool use_input_buffering = false; |
| 118 | bool use_accumulated_input = true; |
| 119 | |
| 120 | int mouse_from_touch_index = -1; |
| 121 | |
| 122 | struct VibrationInfo { |
| 123 | float weak_magnitude; |
| 124 | float strong_magnitude; |
| 125 | float duration; // Duration in seconds |
| 126 | uint64_t timestamp; |
| 127 | }; |
| 128 | |
| 129 | HashMap<int, VibrationInfo> joy_vibration; |
| 130 | |
| 131 | struct VelocityTrack { |
| 132 | uint64_t last_tick = 0; |
| 133 | Vector2 velocity; |
| 134 | Vector2 accum; |
| 135 | float accum_t = 0.0f; |
| 136 | float min_ref_frame; |
| 137 | float max_ref_frame; |
| 138 | |
| 139 | void update(const Vector2 &p_delta_p); |
| 140 | void reset(); |
| 141 | VelocityTrack(); |
| 142 | }; |
| 143 | |
| 144 | struct Joypad { |
| 145 | StringName name; |
| 146 | StringName uid; |
| 147 | bool connected = false; |
| 148 | bool last_buttons[(size_t)JoyButton::MAX] = { false }; |
| 149 | float last_axis[(size_t)JoyAxis::MAX] = { 0.0f }; |
| 150 | HatMask last_hat = HatMask::CENTER; |
| 151 | int mapping = -1; |
| 152 | int hat_current = 0; |
| 153 | Dictionary info; |
| 154 | }; |
| 155 | |
| 156 | VelocityTrack mouse_velocity_track; |
| 157 | HashMap<int, VelocityTrack> touch_velocity_track; |
| 158 | HashMap<int, Joypad> joy_names; |
| 159 | |
| 160 | HashSet<uint32_t> ignored_device_ids; |
| 161 | |
| 162 | int fallback_mapping = -1; |
| 163 | |
| 164 | CursorShape default_shape = CURSOR_ARROW; |
| 165 | |
| 166 | enum JoyType { |
| 167 | TYPE_BUTTON, |
| 168 | TYPE_AXIS, |
| 169 | TYPE_HAT, |
| 170 | TYPE_MAX, |
| 171 | }; |
| 172 | |
| 173 | enum JoyAxisRange { |
| 174 | NEGATIVE_HALF_AXIS = -1, |
| 175 | FULL_AXIS = 0, |
| 176 | POSITIVE_HALF_AXIS = 1 |
| 177 | }; |
| 178 | |
| 179 | struct JoyEvent { |
| 180 | int type = TYPE_MAX; |
| 181 | int index = -1; // Can be either JoyAxis or JoyButton. |
| 182 | float value = 0.f; |
| 183 | }; |
| 184 | |
| 185 | struct JoyBinding { |
| 186 | JoyType inputType; |
| 187 | union { |
| 188 | JoyButton button; |
| 189 | |
| 190 | struct { |
| 191 | JoyAxis axis; |
| 192 | JoyAxisRange range; |
| 193 | bool invert; |
| 194 | } axis; |
| 195 | |
| 196 | struct { |
| 197 | HatDir hat; |
| 198 | HatMask hat_mask; |
| 199 | } hat; |
| 200 | |
| 201 | } input; |
| 202 | |
| 203 | JoyType outputType; |
| 204 | union { |
| 205 | JoyButton button; |
| 206 | |
| 207 | struct { |
| 208 | JoyAxis axis; |
| 209 | JoyAxisRange range; |
| 210 | } axis; |
| 211 | |
| 212 | } output; |
| 213 | }; |
| 214 | |
| 215 | struct JoyDeviceMapping { |
| 216 | String uid; |
| 217 | String name; |
| 218 | Vector<JoyBinding> bindings; |
| 219 | }; |
| 220 | |
| 221 | Vector<JoyDeviceMapping> map_db; |
| 222 | |
| 223 | JoyEvent _get_mapped_button_event(const JoyDeviceMapping &mapping, JoyButton p_button); |
| 224 | JoyEvent _get_mapped_axis_event(const JoyDeviceMapping &mapping, JoyAxis p_axis, float p_value); |
| 225 | void _get_mapped_hat_events(const JoyDeviceMapping &mapping, HatDir p_hat, JoyEvent r_events[(size_t)HatDir::MAX]); |
| 226 | JoyButton _get_output_button(String output); |
| 227 | JoyAxis _get_output_axis(String output); |
| 228 | void _button_event(int p_device, JoyButton p_index, bool p_pressed); |
| 229 | void _axis_event(int p_device, JoyAxis p_axis, float p_value); |
| 230 | |
| 231 | void _parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_emulated); |
| 232 | |
| 233 | List<Ref<InputEvent>> buffered_events; |
| 234 | #ifdef DEBUG_ENABLED |
| 235 | HashSet<Ref<InputEvent>> frame_parsed_events; |
| 236 | uint64_t last_parsed_frame = UINT64_MAX; |
| 237 | #endif |
| 238 | |
| 239 | friend class DisplayServer; |
| 240 | |
| 241 | static void (*set_mouse_mode_func)(MouseMode); |
| 242 | static MouseMode (*get_mouse_mode_func)(); |
| 243 | static void (*warp_mouse_func)(const Vector2 &p_position); |
| 244 | |
| 245 | static CursorShape (*get_current_cursor_shape_func)(); |
| 246 | static void (*set_custom_mouse_cursor_func)(const Ref<Resource> &, CursorShape, const Vector2 &); |
| 247 | |
| 248 | EventDispatchFunc event_dispatch_function = nullptr; |
| 249 | |
| 250 | protected: |
| 251 | static void _bind_methods(); |
| 252 | |
| 253 | public: |
| 254 | void set_mouse_mode(MouseMode p_mode); |
| 255 | MouseMode get_mouse_mode() const; |
| 256 | void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const override; |
| 257 | |
| 258 | static Input *get_singleton(); |
| 259 | |
| 260 | bool is_anything_pressed() const; |
| 261 | bool is_key_pressed(Key p_keycode) const; |
| 262 | bool is_physical_key_pressed(Key p_keycode) const; |
| 263 | bool is_key_label_pressed(Key p_keycode) const; |
| 264 | bool is_mouse_button_pressed(MouseButton p_button) const; |
| 265 | bool is_joy_button_pressed(int p_device, JoyButton p_button) const; |
| 266 | bool is_action_pressed(const StringName &p_action, bool p_exact = false) const; |
| 267 | bool is_action_just_pressed(const StringName &p_action, bool p_exact = false) const; |
| 268 | bool is_action_just_released(const StringName &p_action, bool p_exact = false) const; |
| 269 | float get_action_strength(const StringName &p_action, bool p_exact = false) const; |
| 270 | float get_action_raw_strength(const StringName &p_action, bool p_exact = false) const; |
| 271 | |
| 272 | float get_axis(const StringName &p_negative_action, const StringName &p_positive_action) const; |
| 273 | Vector2 get_vector(const StringName &p_negative_x, const StringName &p_positive_x, const StringName &p_negative_y, const StringName &p_positive_y, float p_deadzone = -1.0f) const; |
| 274 | |
| 275 | float get_joy_axis(int p_device, JoyAxis p_axis) const; |
| 276 | String get_joy_name(int p_idx); |
| 277 | TypedArray<int> get_connected_joypads(); |
| 278 | Vector2 get_joy_vibration_strength(int p_device); |
| 279 | float get_joy_vibration_duration(int p_device); |
| 280 | uint64_t get_joy_vibration_timestamp(int p_device); |
| 281 | void joy_connection_changed(int p_idx, bool p_connected, String p_name, String p_guid = "" , Dictionary p_joypad_info = Dictionary()); |
| 282 | |
| 283 | Vector3 get_gravity() const; |
| 284 | Vector3 get_accelerometer() const; |
| 285 | Vector3 get_magnetometer() const; |
| 286 | Vector3 get_gyroscope() const; |
| 287 | |
| 288 | Point2 get_mouse_position() const; |
| 289 | Vector2 get_last_mouse_velocity(); |
| 290 | BitField<MouseButtonMask> get_mouse_button_mask() const; |
| 291 | |
| 292 | void warp_mouse(const Vector2 &p_position); |
| 293 | Point2i warp_mouse_motion(const Ref<InputEventMouseMotion> &p_motion, const Rect2 &p_rect); |
| 294 | |
| 295 | void parse_input_event(const Ref<InputEvent> &p_event); |
| 296 | |
| 297 | void set_gravity(const Vector3 &p_gravity); |
| 298 | void set_accelerometer(const Vector3 &p_accel); |
| 299 | void set_magnetometer(const Vector3 &p_magnetometer); |
| 300 | void set_gyroscope(const Vector3 &p_gyroscope); |
| 301 | void set_joy_axis(int p_device, JoyAxis p_axis, float p_value); |
| 302 | |
| 303 | void start_joy_vibration(int p_device, float p_weak_magnitude, float p_strong_magnitude, float p_duration = 0); |
| 304 | void stop_joy_vibration(int p_device); |
| 305 | void vibrate_handheld(int p_duration_ms = 500); |
| 306 | |
| 307 | void set_mouse_position(const Point2 &p_posf); |
| 308 | |
| 309 | void action_press(const StringName &p_action, float p_strength = 1.f); |
| 310 | void action_release(const StringName &p_action); |
| 311 | |
| 312 | void set_emulate_touch_from_mouse(bool p_emulate); |
| 313 | bool is_emulating_touch_from_mouse() const; |
| 314 | void ensure_touch_mouse_raised(); |
| 315 | |
| 316 | void set_emulate_mouse_from_touch(bool p_emulate); |
| 317 | bool is_emulating_mouse_from_touch() const; |
| 318 | |
| 319 | CursorShape get_default_cursor_shape() const; |
| 320 | void set_default_cursor_shape(CursorShape p_shape); |
| 321 | CursorShape get_current_cursor_shape() const; |
| 322 | void set_custom_mouse_cursor(const Ref<Resource> &p_cursor, CursorShape p_shape = Input::CURSOR_ARROW, const Vector2 &p_hotspot = Vector2()); |
| 323 | |
| 324 | void parse_mapping(String p_mapping); |
| 325 | void joy_button(int p_device, JoyButton p_button, bool p_pressed); |
| 326 | void joy_axis(int p_device, JoyAxis p_axis, float p_value); |
| 327 | void joy_hat(int p_device, BitField<HatMask> p_val); |
| 328 | |
| 329 | void add_joy_mapping(String p_mapping, bool p_update_existing = false); |
| 330 | void remove_joy_mapping(String p_guid); |
| 331 | |
| 332 | int get_unused_joy_id(); |
| 333 | |
| 334 | bool is_joy_known(int p_device); |
| 335 | String get_joy_guid(int p_device) const; |
| 336 | bool should_ignore_device(int p_vendor_id, int p_product_id) const; |
| 337 | Dictionary get_joy_info(int p_device) const; |
| 338 | void set_fallback_mapping(String p_guid); |
| 339 | |
| 340 | void flush_buffered_events(); |
| 341 | bool is_using_input_buffering(); |
| 342 | void set_use_input_buffering(bool p_enable); |
| 343 | void set_use_accumulated_input(bool p_enable); |
| 344 | bool is_using_accumulated_input(); |
| 345 | |
| 346 | void release_pressed_events(); |
| 347 | |
| 348 | void set_event_dispatch_function(EventDispatchFunc p_function); |
| 349 | |
| 350 | Input(); |
| 351 | ~Input(); |
| 352 | }; |
| 353 | |
| 354 | VARIANT_ENUM_CAST(Input::MouseMode); |
| 355 | VARIANT_ENUM_CAST(Input::CursorShape); |
| 356 | |
| 357 | #endif // INPUT_H |
| 358 | |