Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/input/input_event.h
21081 views
1
/**************************************************************************/
2
/* input_event.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_enums.h"
34
#include "core/io/resource.h"
35
#include "core/math/transform_2d.h"
36
#include "core/os/keyboard.h"
37
#include "core/string/ustring.h"
38
#include "core/typedefs.h"
39
40
/**
41
* Input Event classes. These are used in the main loop.
42
* The events are pretty obvious.
43
*/
44
45
class Shortcut;
46
47
/**
48
* Input Modifier Status
49
* for keyboard/mouse events.
50
*/
51
52
class InputEvent : public Resource {
53
GDCLASS(InputEvent, Resource);
54
55
int device = 0;
56
57
protected:
58
bool canceled = false;
59
bool pressed = false;
60
61
static void _bind_methods();
62
63
public:
64
static constexpr int DEVICE_ID_EMULATION = -1;
65
static constexpr int DEVICE_ID_INTERNAL = -2;
66
67
void set_device(int p_device);
68
int get_device() const;
69
70
bool is_action(const StringName &p_action, bool p_exact_match = false) const;
71
bool is_action_pressed(const StringName &p_action, bool p_allow_echo = false, bool p_exact_match = false) const;
72
bool is_action_released(const StringName &p_action, bool p_exact_match = false) const;
73
float get_action_strength(const StringName &p_action, bool p_exact_match = false) const;
74
float get_action_raw_strength(const StringName &p_action, bool p_exact_match = false) const;
75
76
bool is_canceled() const;
77
bool is_pressed() const;
78
bool is_released() const;
79
virtual bool is_echo() const;
80
81
virtual String as_text() const = 0;
82
83
virtual RequiredResult<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const;
84
85
virtual bool action_match(const Ref<InputEvent> &p_event, bool p_exact_match, float p_deadzone, bool *r_pressed, float *r_strength, float *r_raw_strength) const;
86
virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const;
87
88
virtual bool is_action_type() const;
89
90
virtual bool accumulate(const Ref<InputEvent> &p_event) { return false; }
91
92
virtual InputEventType get_type() const { return InputEventType::INVALID; }
93
};
94
95
class InputEventFromWindow : public InputEvent {
96
GDCLASS(InputEventFromWindow, InputEvent);
97
98
int64_t window_id = 0;
99
100
protected:
101
static void _bind_methods();
102
103
public:
104
void set_window_id(int64_t p_id);
105
int64_t get_window_id() const;
106
};
107
108
class InputEventWithModifiers : public InputEventFromWindow {
109
GDCLASS(InputEventWithModifiers, InputEventFromWindow);
110
111
bool command_or_control_autoremap = false;
112
113
bool shift_pressed = false;
114
bool alt_pressed = false;
115
bool meta_pressed = false; // "Command" on macOS, "Meta/Win" key on other platforms.
116
bool ctrl_pressed = false;
117
118
protected:
119
static void _bind_methods();
120
void _validate_property(PropertyInfo &p_property) const;
121
122
public:
123
void set_command_or_control_autoremap(bool p_enabled);
124
bool is_command_or_control_autoremap() const;
125
126
bool is_command_or_control_pressed() const;
127
128
void set_shift_pressed(bool p_pressed);
129
bool is_shift_pressed() const;
130
131
void set_alt_pressed(bool p_pressed);
132
bool is_alt_pressed() const;
133
134
void set_ctrl_pressed(bool p_pressed);
135
bool is_ctrl_pressed() const;
136
137
void set_meta_pressed(bool p_pressed);
138
bool is_meta_pressed() const;
139
140
void set_modifiers_from_event(const InputEventWithModifiers *event);
141
142
BitField<KeyModifierMask> get_modifiers_mask() const;
143
144
virtual String as_text() const override;
145
virtual String _to_string() override;
146
};
147
148
class InputEventKey : public InputEventWithModifiers {
149
GDCLASS(InputEventKey, InputEventWithModifiers);
150
151
Key keycode = Key::NONE; // Key enum, without modifier masks.
152
Key physical_keycode = Key::NONE;
153
Key key_label = Key::NONE;
154
uint32_t unicode = 0; ///unicode
155
KeyLocation location = KeyLocation::UNSPECIFIED;
156
157
bool echo = false; /// true if this is an echo key
158
159
protected:
160
static void _bind_methods();
161
162
public:
163
void set_pressed(bool p_pressed);
164
165
void set_keycode(Key p_keycode);
166
Key get_keycode() const;
167
168
void set_physical_keycode(Key p_keycode);
169
Key get_physical_keycode() const;
170
171
void set_key_label(Key p_key_label);
172
Key get_key_label() const;
173
174
void set_unicode(char32_t p_unicode);
175
char32_t get_unicode() const;
176
177
void set_location(KeyLocation p_key_location);
178
KeyLocation get_location() const;
179
180
void set_echo(bool p_enable);
181
virtual bool is_echo() const override;
182
183
Key get_keycode_with_modifiers() const;
184
Key get_physical_keycode_with_modifiers() const;
185
Key get_key_label_with_modifiers() const;
186
187
virtual bool action_match(const Ref<InputEvent> &p_event, bool p_exact_match, float p_deadzone, bool *r_pressed, float *r_strength, float *r_raw_strength) const override;
188
virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const override;
189
190
virtual bool is_action_type() const override { return true; }
191
192
virtual String as_text_physical_keycode() const;
193
virtual String as_text_keycode() const;
194
virtual String as_text_key_label() const;
195
virtual String as_text_location() const;
196
virtual String as_text() const override;
197
virtual String _to_string() override;
198
199
static Ref<InputEventKey> create_reference(Key p_keycode_with_modifier_masks, bool p_physical = false);
200
201
InputEventType get_type() const final override { return InputEventType::KEY; }
202
};
203
204
class InputEventMouse : public InputEventWithModifiers {
205
GDCLASS(InputEventMouse, InputEventWithModifiers);
206
207
BitField<MouseButtonMask> button_mask = MouseButtonMask::NONE;
208
209
Vector2 pos;
210
Vector2 global_pos;
211
212
protected:
213
static void _bind_methods();
214
215
public:
216
void set_button_mask(BitField<MouseButtonMask> p_mask);
217
BitField<MouseButtonMask> get_button_mask() const;
218
219
void set_position(const Vector2 &p_pos);
220
Vector2 get_position() const;
221
222
void set_global_position(const Vector2 &p_global_pos);
223
Vector2 get_global_position() const;
224
};
225
226
class InputEventMouseButton : public InputEventMouse {
227
GDCLASS(InputEventMouseButton, InputEventMouse);
228
229
float factor = 1;
230
MouseButton button_index = MouseButton::NONE;
231
bool double_click = false; //last even less than double click time
232
233
protected:
234
static void _bind_methods();
235
236
public:
237
void set_factor(float p_factor);
238
float get_factor() const;
239
240
void set_button_index(MouseButton p_index);
241
MouseButton get_button_index() const;
242
243
void set_pressed(bool p_pressed);
244
void set_canceled(bool p_canceled);
245
246
void set_double_click(bool p_double_click);
247
bool is_double_click() const;
248
249
virtual RequiredResult<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
250
251
virtual bool action_match(const Ref<InputEvent> &p_event, bool p_exact_match, float p_deadzone, bool *r_pressed, float *r_strength, float *r_raw_strength) const override;
252
virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const override;
253
254
virtual bool is_action_type() const override { return true; }
255
virtual String as_text() const override;
256
virtual String _to_string() override;
257
258
InputEventType get_type() const final override { return InputEventType::MOUSE_BUTTON; }
259
};
260
261
class InputEventMouseMotion : public InputEventMouse {
262
GDCLASS(InputEventMouseMotion, InputEventMouse);
263
264
Vector2 tilt;
265
float pressure = 0;
266
Vector2 relative;
267
Vector2 screen_relative;
268
Vector2 velocity;
269
Vector2 screen_velocity;
270
bool pen_inverted = false;
271
272
protected:
273
static void _bind_methods();
274
275
public:
276
void set_tilt(const Vector2 &p_tilt);
277
Vector2 get_tilt() const;
278
279
void set_pressure(float p_pressure);
280
float get_pressure() const;
281
282
void set_pen_inverted(bool p_inverted);
283
bool get_pen_inverted() const;
284
285
void set_relative(const Vector2 &p_relative);
286
Vector2 get_relative() const;
287
288
void set_relative_screen_position(const Vector2 &p_relative);
289
Vector2 get_relative_screen_position() const;
290
291
void set_velocity(const Vector2 &p_velocity);
292
Vector2 get_velocity() const;
293
294
void set_screen_velocity(const Vector2 &p_velocity);
295
Vector2 get_screen_velocity() const;
296
297
virtual RequiredResult<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
298
virtual String as_text() const override;
299
virtual String _to_string() override;
300
301
virtual bool accumulate(const Ref<InputEvent> &p_event) override;
302
303
InputEventType get_type() const final override { return InputEventType::MOUSE_MOTION; }
304
};
305
306
class InputEventJoypadMotion : public InputEvent {
307
GDCLASS(InputEventJoypadMotion, InputEvent);
308
JoyAxis axis = (JoyAxis)0; ///< Joypad axis
309
float axis_value = 0; ///< -1 to 1
310
311
protected:
312
static void _bind_methods();
313
314
public:
315
void set_axis(JoyAxis p_axis);
316
JoyAxis get_axis() const;
317
318
void set_axis_value(float p_value);
319
float get_axis_value() const;
320
321
virtual bool action_match(const Ref<InputEvent> &p_event, bool p_exact_match, float p_deadzone, bool *r_pressed, float *r_strength, float *r_raw_strength) const override;
322
virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const override;
323
324
virtual bool is_action_type() const override { return true; }
325
virtual String as_text() const override;
326
virtual String _to_string() override;
327
328
// The default device ID is `InputMap::ALL_DEVICES`.
329
static Ref<InputEventJoypadMotion> create_reference(JoyAxis p_axis, float p_value, int p_device = -1);
330
331
InputEventType get_type() const final override { return InputEventType::JOY_MOTION; }
332
};
333
334
class InputEventJoypadButton : public InputEvent {
335
GDCLASS(InputEventJoypadButton, InputEvent);
336
337
JoyButton button_index = (JoyButton)0;
338
float pressure = 0; //0 to 1
339
protected:
340
static void _bind_methods();
341
342
public:
343
void set_button_index(JoyButton p_index);
344
JoyButton get_button_index() const;
345
346
void set_pressed(bool p_pressed);
347
348
void set_pressure(float p_pressure);
349
float get_pressure() const;
350
351
virtual bool action_match(const Ref<InputEvent> &p_event, bool p_exact_match, float p_deadzone, bool *r_pressed, float *r_strength, float *r_raw_strength) const override;
352
virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const override;
353
354
virtual bool is_action_type() const override { return true; }
355
356
virtual String as_text() const override;
357
virtual String _to_string() override;
358
359
// The default device ID is `InputMap::ALL_DEVICES`.
360
static Ref<InputEventJoypadButton> create_reference(JoyButton p_btn_index, int p_device = -1);
361
362
InputEventType get_type() const final override { return InputEventType::JOY_BUTTON; }
363
};
364
365
class InputEventScreenTouch : public InputEventFromWindow {
366
GDCLASS(InputEventScreenTouch, InputEventFromWindow);
367
int index = 0;
368
Vector2 pos;
369
bool double_tap = false;
370
371
protected:
372
static void _bind_methods();
373
374
public:
375
void set_index(int p_index);
376
int get_index() const;
377
378
void set_position(const Vector2 &p_pos);
379
Vector2 get_position() const;
380
381
void set_pressed(bool p_pressed);
382
void set_canceled(bool p_canceled);
383
384
void set_double_tap(bool p_double_tap);
385
bool is_double_tap() const;
386
387
virtual RequiredResult<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
388
virtual String as_text() const override;
389
virtual String _to_string() override;
390
391
InputEventType get_type() const final override { return InputEventType::SCREEN_TOUCH; }
392
};
393
394
class InputEventScreenDrag : public InputEventFromWindow {
395
GDCLASS(InputEventScreenDrag, InputEventFromWindow);
396
int index = 0;
397
Vector2 pos;
398
Vector2 relative;
399
Vector2 screen_relative;
400
Vector2 velocity;
401
Vector2 screen_velocity;
402
Vector2 tilt;
403
float pressure = 0;
404
bool pen_inverted = false;
405
406
protected:
407
static void _bind_methods();
408
409
public:
410
void set_index(int p_index);
411
int get_index() const;
412
413
void set_tilt(const Vector2 &p_tilt);
414
Vector2 get_tilt() const;
415
416
void set_pressure(float p_pressure);
417
float get_pressure() const;
418
419
void set_pen_inverted(bool p_inverted);
420
bool get_pen_inverted() const;
421
422
void set_position(const Vector2 &p_pos);
423
Vector2 get_position() const;
424
425
void set_relative(const Vector2 &p_relative);
426
Vector2 get_relative() const;
427
428
void set_relative_screen_position(const Vector2 &p_relative);
429
Vector2 get_relative_screen_position() const;
430
431
void set_velocity(const Vector2 &p_velocity);
432
Vector2 get_velocity() const;
433
434
void set_screen_velocity(const Vector2 &p_velocity);
435
Vector2 get_screen_velocity() const;
436
437
virtual RequiredResult<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
438
virtual String as_text() const override;
439
virtual String _to_string() override;
440
441
virtual bool accumulate(const Ref<InputEvent> &p_event) override;
442
443
InputEventType get_type() const final override { return InputEventType::SCREEN_DRAG; }
444
};
445
446
class InputEventAction : public InputEvent {
447
GDCLASS(InputEventAction, InputEvent);
448
449
StringName action;
450
float strength = 1.0f;
451
int event_index = -1;
452
453
protected:
454
static void _bind_methods();
455
456
public:
457
void set_action(const StringName &p_action);
458
StringName get_action() const;
459
460
void set_pressed(bool p_pressed);
461
462
void set_strength(float p_strength);
463
float get_strength() const;
464
465
void set_event_index(int p_index);
466
int get_event_index() const;
467
468
virtual bool is_action(const StringName &p_action) const;
469
470
virtual bool action_match(const Ref<InputEvent> &p_event, bool p_exact_match, float p_deadzone, bool *r_pressed, float *r_strength, float *r_raw_strength) const override;
471
virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const override;
472
473
virtual bool is_action_type() const override { return true; }
474
475
virtual String as_text() const override;
476
virtual String _to_string() override;
477
478
InputEventType get_type() const final override { return InputEventType::ACTION; }
479
};
480
481
class InputEventGesture : public InputEventWithModifiers {
482
GDCLASS(InputEventGesture, InputEventWithModifiers);
483
484
Vector2 pos;
485
486
protected:
487
static void _bind_methods();
488
489
public:
490
void set_position(const Vector2 &p_pos);
491
Vector2 get_position() const;
492
};
493
494
class InputEventMagnifyGesture : public InputEventGesture {
495
GDCLASS(InputEventMagnifyGesture, InputEventGesture);
496
real_t factor = 1.0;
497
498
protected:
499
static void _bind_methods();
500
501
public:
502
void set_factor(real_t p_factor);
503
real_t get_factor() const;
504
505
virtual RequiredResult<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
506
virtual String as_text() const override;
507
virtual String _to_string() override;
508
509
InputEventType get_type() const final override { return InputEventType::MAGNIFY_GESTURE; }
510
};
511
512
class InputEventPanGesture : public InputEventGesture {
513
GDCLASS(InputEventPanGesture, InputEventGesture);
514
Vector2 delta;
515
516
protected:
517
static void _bind_methods();
518
519
public:
520
void set_delta(const Vector2 &p_delta);
521
Vector2 get_delta() const;
522
523
virtual RequiredResult<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
524
virtual String as_text() const override;
525
virtual String _to_string() override;
526
527
InputEventType get_type() const final override { return InputEventType::PAN_GESTURE; }
528
};
529
530
class InputEventMIDI : public InputEvent {
531
GDCLASS(InputEventMIDI, InputEvent);
532
533
int channel = 0;
534
MIDIMessage message = MIDIMessage::NONE;
535
int pitch = 0;
536
int velocity = 0;
537
int instrument = 0;
538
int pressure = 0;
539
int controller_number = 0;
540
int controller_value = 0;
541
542
protected:
543
static void _bind_methods();
544
545
public:
546
void set_channel(const int p_channel);
547
int get_channel() const;
548
549
void set_message(const MIDIMessage p_message);
550
MIDIMessage get_message() const;
551
552
void set_pitch(const int p_pitch);
553
int get_pitch() const;
554
555
void set_velocity(const int p_velocity);
556
int get_velocity() const;
557
558
void set_instrument(const int p_instrument);
559
int get_instrument() const;
560
561
void set_pressure(const int p_pressure);
562
int get_pressure() const;
563
564
void set_controller_number(const int p_controller_number);
565
int get_controller_number() const;
566
567
void set_controller_value(const int p_controller_value);
568
int get_controller_value() const;
569
570
virtual String as_text() const override;
571
virtual String _to_string() override;
572
573
InputEventType get_type() const final override { return InputEventType::MIDI; }
574
};
575
576
class InputEventShortcut : public InputEvent {
577
GDCLASS(InputEventShortcut, InputEvent);
578
579
Ref<Shortcut> shortcut;
580
581
protected:
582
static void _bind_methods();
583
584
public:
585
void set_shortcut(Ref<Shortcut> p_shortcut);
586
Ref<Shortcut> get_shortcut();
587
588
virtual String as_text() const override;
589
virtual String _to_string() override;
590
591
InputEventType get_type() const final override { return InputEventType::SHORTCUT; }
592
593
InputEventShortcut();
594
};
595
596