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