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