Debian Codegraph @

core/input/input.h

raw symbols SCIP
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 #pragma once
32
33 #include "core/input/input_event.h"
34 #include "core/object/object.h"
35 #include "core/os/keyboard.h"
36 #include "core/os/thread_safe.h"
37 #include "core/templates/rb_map.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 inline Input *singleton = nullptr;
46
47 static constexpr uint64_t MAX_EVENT = 32;
48
49 public:
50 // Keep synced with "DisplayServer::MouseMode" enum.
51 enum MouseMode {
52 MOUSE_MODE_VISIBLE,
53 MOUSE_MODE_HIDDEN,
54 MOUSE_MODE_CAPTURED,
55 MOUSE_MODE_CONFINED,
56 MOUSE_MODE_CONFINED_HIDDEN,
57 MOUSE_MODE_MAX,
58 };
59
60 #undef CursorShape
61 enum CursorShape {
62 CURSOR_ARROW,
63 CURSOR_IBEAM,
64 CURSOR_POINTING_HAND,
65 CURSOR_CROSS,
66 CURSOR_WAIT,
67 CURSOR_BUSY,
68 CURSOR_DRAG,
69 CURSOR_CAN_DROP,
70 CURSOR_FORBIDDEN,
71 CURSOR_VSIZE,
72 CURSOR_HSIZE,
73 CURSOR_BDIAGSIZE,
74 CURSOR_FDIAGSIZE,
75 CURSOR_MOVE,
76 CURSOR_VSPLIT,
77 CURSOR_HSPLIT,
78 CURSOR_HELP,
79 CURSOR_MAX
80 };
81
82 class JoypadFeatures {
83 public:
84 virtual ~JoypadFeatures() {}
85
86 virtual bool has_joy_light() const { return false; }
87 virtual void set_joy_light(const Color &p_color) {}
88 };
89
90 static constexpr int32_t JOYPADS_MAX = 16;
91
92 typedef void (*EventDispatchFunc)(const Ref<InputEvent> &p_event);
93
94 private:
95 BitField<MouseButtonMask> mouse_button_mask = MouseButtonMask::NONE;
96
97 RBSet<Key> key_label_pressed;
98 RBSet<Key> physical_keys_pressed;
99 RBSet<Key> keys_pressed;
100 RBSet<JoyButton> joy_buttons_pressed;
101 RBMap<JoyAxis, float> _joy_axis;
102 //RBMap<StringName,int> custom_action_press;
103 bool gravity_enabled = false;
104 Vector3 gravity;
105 bool accelerometer_enabled = false;
106 Vector3 accelerometer;
107 bool magnetometer_enabled = false;
108 Vector3 magnetometer;
109 bool gyroscope_enabled = false;
110 Vector3 gyroscope;
111 Vector2 mouse_pos;
112 int64_t mouse_window = 0;
113 bool legacy_just_pressed_behavior = false;
114 bool disable_input = false;
115
116 struct ActionState {
117 uint64_t pressed_physics_frame = UINT64_MAX;
118 uint64_t pressed_process_frame = UINT64_MAX;
119 uint64_t released_physics_frame = UINT64_MAX;
120 uint64_t released_process_frame = UINT64_MAX;
121 ObjectID pressed_event_id;
122 ObjectID released_event_id;
123 bool exact = true;
124
125 struct DeviceState {
126 bool pressed[MAX_EVENT] = { false };
127 float strength[MAX_EVENT] = { 0.0 };
128 float raw_strength[MAX_EVENT] = { 0.0 };
129 };
130 bool api_pressed = false;
131 float api_strength = 0.0;
132 HashMap<int, DeviceState> device_states;
133
134 // Cache.
135 struct ActionStateCache {
136 bool pressed = false;
137 float strength = false;
138 float raw_strength = false;
139 } cache;
140 };
141
142 HashMap<StringName, ActionState> action_states;
143
144 bool emulate_touch_from_mouse = false;
145 bool emulate_mouse_from_touch = false;
146 bool agile_input_event_flushing = false;
147 bool use_accumulated_input = true;
148
149 int mouse_from_touch_index = -1;
150
151 struct VibrationInfo {
152 float weak_magnitude;
153 float strong_magnitude;
154 float duration; // Duration in seconds
155 uint64_t timestamp;
156 };
157
158 HashMap<int, VibrationInfo> joy_vibration;
159
160 struct VelocityTrack {
161 uint64_t last_tick = 0;
162 Vector2 velocity;
163 Vector2 screen_velocity;
164 Vector2 accum;
165 Vector2 screen_accum;
166 float accum_t = 0.0f;
167 float min_ref_frame;
168 float max_ref_frame;
169
170 void update(const Vector2 &p_delta_p, const Vector2 &p_screen_delta_p);
171 void reset();
172 VelocityTrack();
173 };
174
175 struct Joypad {
176 StringName name;
177 StringName uid;
178 bool connected = false;
179 bool is_known = false;
180 bool last_buttons[(size_t)JoyButton::MAX] = { false };
181 float last_axis[(size_t)JoyAxis::MAX] = { 0.0f };
182 HatMask last_hat = HatMask::CENTER;
183 int mapping = -1;
184 int hat_current = 0;
185 Dictionary info;
186 bool has_light = false;
187 Input::JoypadFeatures *features = nullptr;
188 };
189
190 VelocityTrack mouse_velocity_track;
191 HashMap<int, VelocityTrack> touch_velocity_track;
192 HashMap<int, Joypad> joy_names;
193
194 HashSet<uint32_t> ignored_device_ids;
195
196 int fallback_mapping = -1; // Index of the guid in map_db.
197
198 CursorShape default_shape = CURSOR_ARROW;
199
200 enum JoyType {
201 TYPE_BUTTON,
202 TYPE_AXIS,
203 TYPE_HAT,
204 TYPE_MAX,
205 };
206
207 enum JoyAxisRange {
208 NEGATIVE_HALF_AXIS = -1,
209 FULL_AXIS = 0,
210 POSITIVE_HALF_AXIS = 1
211 };
212
213 struct JoyEvent {
214 int type = TYPE_MAX;
215 int index = -1; // Can be either JoyAxis or JoyButton.
216 float value = 0.f;
217 };
218
219 struct JoyBinding {
220 JoyType inputType;
221 union {
222 JoyButton button;
223
224 struct {
225 JoyAxis axis;
226 JoyAxisRange range;
227 bool invert;
228 } axis;
229
230 struct {
231 HatDir hat;
232 HatMask hat_mask;
233 } hat;
234
235 } input;
236
237 JoyType outputType;
238 union {
239 JoyButton button;
240
241 struct {
242 JoyAxis axis;
243 JoyAxisRange range;
244 } axis;
245
246 } output;
247 };
248
249 struct JoyDeviceMapping {
250 String uid;
251 String name;
252 Vector<JoyBinding> bindings;
253 };
254
255 Vector<JoyDeviceMapping> map_db;
256
257 void _set_joypad_mapping(Joypad &p_js, int p_map_index);
258
259 JoyEvent _get_mapped_button_event(const JoyDeviceMapping &mapping, JoyButton p_button);
260 JoyEvent _get_mapped_axis_event(const JoyDeviceMapping &mapping, JoyAxis p_axis, float p_value, JoyAxisRange &r_range);
261 void _get_mapped_hat_events(const JoyDeviceMapping &mapping, HatDir p_hat, JoyEvent r_events[(size_t)HatDir::MAX]);
262 JoyButton _get_output_button(const String &output);
263 JoyAxis _get_output_axis(const String &output);
264 void _button_event(int p_device, JoyButton p_index, bool p_pressed);
265 void _axis_event(int p_device, JoyAxis p_axis, float p_value);
266 void _update_action_cache(const StringName &p_action_name, ActionState &r_action_state);
267 void _update_joypad_features(int p_device);
268
269 void _parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_emulated);
270
271 List<Ref<InputEvent>> buffered_events;
272 #ifdef DEBUG_ENABLED
273 HashSet<Ref<InputEvent>> frame_parsed_events;
274 uint64_t last_parsed_frame = UINT64_MAX;
275 #endif
276
277 friend class DisplayServer;
278
279 static void (*set_mouse_mode_func)(MouseMode);
280 static MouseMode (*get_mouse_mode_func)();
281 static void (*set_mouse_mode_override_func)(MouseMode);
282 static MouseMode (*get_mouse_mode_override_func)();
283 static void (*set_mouse_mode_override_enabled_func)(bool);
284 static bool (*is_mouse_mode_override_enabled_func)();
285 static void (*warp_mouse_func)(const Vector2 &p_position);
286
287 static CursorShape (*get_current_cursor_shape_func)();
288 static void (*set_custom_mouse_cursor_func)(const Ref<Resource> &, CursorShape, const Vector2 &);
289
290 EventDispatchFunc event_dispatch_function = nullptr;
291
292 #ifndef DISABLE_DEPRECATED
293 void _vibrate_handheld_bind_compat_91143(int p_duration_ms = 500);
294 static void _bind_compatibility_methods();
295 #endif // DISABLE_DEPRECATED
296
297 protected:
298 static void _bind_methods();
299
300 public:
301 void set_mouse_mode(MouseMode p_mode);
302 MouseMode get_mouse_mode() const;
303 void set_mouse_mode_override(MouseMode p_mode);
304 MouseMode get_mouse_mode_override() const;
305 void set_mouse_mode_override_enabled(bool p_override_enabled);
306 bool is_mouse_mode_override_enabled();
307
308 #ifdef TOOLS_ENABLED
309 void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const override;
310 #endif
311
312 static Input *get_singleton();
313
314 bool is_anything_pressed() const;
315 bool is_anything_pressed_except_mouse() const;
316 bool is_key_pressed(Key p_keycode) const;
317 bool is_physical_key_pressed(Key p_keycode) const;
318 bool is_key_label_pressed(Key p_keycode) const;
319 bool is_mouse_button_pressed(MouseButton p_button) const;
320 bool is_joy_button_pressed(int p_device, JoyButton p_button) const;
321 bool is_action_pressed(const StringName &p_action, bool p_exact = false) const;
322 bool is_action_just_pressed(const StringName &p_action, bool p_exact = false) const;
323 bool is_action_just_released(const StringName &p_action, bool p_exact = false) const;
324 bool is_action_just_pressed_by_event(const StringName &p_action, RequiredParam<InputEvent> rp_event, bool p_exact = false) const;
325 bool is_action_just_released_by_event(const StringName &p_action, RequiredParam<InputEvent> rp_event, bool p_exact = false) const;
326 float get_action_strength(const StringName &p_action, bool p_exact = false) const;
327 float get_action_raw_strength(const StringName &p_action, bool p_exact = false) const;
328
329 float get_axis(const StringName &p_negative_action, const StringName &p_positive_action) const;
330 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;
331
332 float get_joy_axis(int p_device, JoyAxis p_axis) const;
333 String get_joy_name(int p_idx);
334 TypedArray<int> get_connected_joypads();
335 Vector2 get_joy_vibration_strength(int p_device);
336 float get_joy_vibration_duration(int p_device);
337 uint64_t get_joy_vibration_timestamp(int p_device);
338 void joy_connection_changed(int p_idx, bool p_connected, const String &p_name, const String &p_guid = "", const Dictionary &p_joypad_info = Dictionary());
339
340 Vector3 get_gravity() const;
341 Vector3 get_accelerometer() const;
342 Vector3 get_magnetometer() const;
343 Vector3 get_gyroscope() const;
344
345 Point2 get_mouse_position() const;
346 Vector2 get_last_mouse_velocity();
347 Vector2 get_last_mouse_screen_velocity();
348 BitField<MouseButtonMask> get_mouse_button_mask() const;
349
350 void warp_mouse(const Vector2 &p_position);
351 Point2 warp_mouse_motion(const Ref<InputEventMouseMotion> &p_motion, const Rect2 &p_rect);
352
353 void parse_input_event(RequiredParam<InputEvent> rp_event);
354
355 void set_gravity(const Vector3 &p_gravity);
356 void set_accelerometer(const Vector3 &p_accel);
357 void set_magnetometer(const Vector3 &p_magnetometer);
358 void set_gyroscope(const Vector3 &p_gyroscope);
359 void set_joy_axis(int p_device, JoyAxis p_axis, float p_value);
360
361 void set_joy_features(int p_device, JoypadFeatures *p_features);
362
363 void set_joy_light(int p_device, const Color &p_color);
364 bool has_joy_light(int p_device) const;
365
366 void start_joy_vibration(int p_device, float p_weak_magnitude, float p_strong_magnitude, float p_duration = 0);
367 void stop_joy_vibration(int p_device);
368 void vibrate_handheld(int p_duration_ms = 500, float p_amplitude = -1.0);
369
370 void set_mouse_position(const Point2 &p_posf);
371
372 void action_press(const StringName &p_action, float p_strength = 1.f);
373 void action_release(const StringName &p_action);
374
375 void set_emulate_touch_from_mouse(bool p_emulate);
376 bool is_emulating_touch_from_mouse() const;
377 void ensure_touch_mouse_raised();
378
379 void set_emulate_mouse_from_touch(bool p_emulate);
380 bool is_emulating_mouse_from_touch() const;
381
382 CursorShape get_default_cursor_shape() const;
383 void set_default_cursor_shape(CursorShape p_shape);
384 CursorShape get_current_cursor_shape() const;
385 void set_custom_mouse_cursor(const Ref<Resource> &p_cursor, CursorShape p_shape = Input::CURSOR_ARROW, const Vector2 &p_hotspot = Vector2());
386
387 void parse_mapping(const String &p_mapping);
388 void joy_button(int p_device, JoyButton p_button, bool p_pressed);
389 void joy_axis(int p_device, JoyAxis p_axis, float p_value);
390 void joy_hat(int p_device, BitField<HatMask> p_val);
391
392 void add_joy_mapping(const String &p_mapping, bool p_update_existing = false);
393 void remove_joy_mapping(const String &p_guid);
394
395 int get_unused_joy_id();
396
397 bool is_joy_known(int p_device);
398 String get_joy_guid(int p_device) const;
399 bool should_ignore_device(int p_vendor_id, int p_product_id) const;
400 Dictionary get_joy_info(int p_device) const;
401 void set_fallback_mapping(const String &p_guid);
402
403 #ifdef DEBUG_ENABLED
404 void flush_frame_parsed_events();
405 #endif
406 void flush_buffered_events();
407 bool is_agile_input_event_flushing();
408 void set_agile_input_event_flushing(bool p_enable);
409 void set_use_accumulated_input(bool p_enable);
410 bool is_using_accumulated_input();
411
412 void release_pressed_events();
413
414 void set_event_dispatch_function(EventDispatchFunc p_function);
415
416 void set_disable_input(bool p_disable);
417 bool is_input_disabled() const;
418
419 Input();
420 ~Input();
421 };
422
423 VARIANT_ENUM_CAST(Input::MouseMode);
424 VARIANT_ENUM_CAST(Input::CursorShape);