forked from godotengine/godot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput_event.cpp
1920 lines (1528 loc) · 64.5 KB
/
input_event.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**************************************************************************/
/* input_event.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "input_event.h"
#include "core/input/input_map.h"
#include "core/input/shortcut.h"
#include "core/os/keyboard.h"
#include "core/os/os.h"
const int InputEvent::DEVICE_ID_EMULATION = -1;
const int InputEvent::DEVICE_ID_INTERNAL = -2;
void InputEvent::set_device(int p_device) {
device = p_device;
emit_changed();
}
int InputEvent::get_device() const {
return device;
}
bool InputEvent::is_action(const StringName &p_action, bool p_exact_match) const {
return InputMap::get_singleton()->event_is_action(Ref<InputEvent>(const_cast<InputEvent *>(this)), p_action, p_exact_match);
}
bool InputEvent::is_action_pressed(const StringName &p_action, bool p_allow_echo, bool p_exact_match) const {
bool pressed_state;
bool valid = InputMap::get_singleton()->event_get_action_status(Ref<InputEvent>(const_cast<InputEvent *>(this)), p_action, p_exact_match, &pressed_state, nullptr, nullptr);
return valid && pressed_state && (p_allow_echo || !is_echo());
}
bool InputEvent::is_action_released(const StringName &p_action, bool p_exact_match) const {
bool pressed_state;
bool valid = InputMap::get_singleton()->event_get_action_status(Ref<InputEvent>(const_cast<InputEvent *>(this)), p_action, p_exact_match, &pressed_state, nullptr, nullptr);
return valid && !pressed_state;
}
float InputEvent::get_action_strength(const StringName &p_action, bool p_exact_match) const {
float strength;
bool valid = InputMap::get_singleton()->event_get_action_status(Ref<InputEvent>(const_cast<InputEvent *>(this)), p_action, p_exact_match, nullptr, &strength, nullptr);
return valid ? strength : 0.0f;
}
float InputEvent::get_action_raw_strength(const StringName &p_action, bool p_exact_match) const {
float raw_strength;
bool valid = InputMap::get_singleton()->event_get_action_status(Ref<InputEvent>(const_cast<InputEvent *>(this)), p_action, p_exact_match, nullptr, nullptr, &raw_strength);
return valid ? raw_strength : 0.0f;
}
bool InputEvent::is_canceled() const {
return canceled;
}
bool InputEvent::is_pressed() const {
return pressed && !canceled;
}
bool InputEvent::is_released() const {
return !pressed && !canceled;
}
bool InputEvent::is_echo() const {
return false;
}
Ref<InputEvent> InputEvent::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const {
return Ref<InputEvent>(const_cast<InputEvent *>(this));
}
bool InputEvent::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 {
return false;
}
bool InputEvent::is_match(const Ref<InputEvent> &p_event, bool p_exact_match) const {
return false;
}
bool InputEvent::is_action_type() const {
return false;
}
void InputEvent::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_device", "device"), &InputEvent::set_device);
ClassDB::bind_method(D_METHOD("get_device"), &InputEvent::get_device);
ClassDB::bind_method(D_METHOD("is_action", "action", "exact_match"), &InputEvent::is_action, DEFVAL(false));
ClassDB::bind_method(D_METHOD("is_action_pressed", "action", "allow_echo", "exact_match"), &InputEvent::is_action_pressed, DEFVAL(false), DEFVAL(false));
ClassDB::bind_method(D_METHOD("is_action_released", "action", "exact_match"), &InputEvent::is_action_released, DEFVAL(false));
ClassDB::bind_method(D_METHOD("get_action_strength", "action", "exact_match"), &InputEvent::get_action_strength, DEFVAL(false));
ClassDB::bind_method(D_METHOD("is_canceled"), &InputEvent::is_canceled);
ClassDB::bind_method(D_METHOD("is_pressed"), &InputEvent::is_pressed);
ClassDB::bind_method(D_METHOD("is_released"), &InputEvent::is_released);
ClassDB::bind_method(D_METHOD("is_echo"), &InputEvent::is_echo);
ClassDB::bind_method(D_METHOD("as_text"), &InputEvent::as_text);
ClassDB::bind_method(D_METHOD("is_match", "event", "exact_match"), &InputEvent::is_match, DEFVAL(true));
ClassDB::bind_method(D_METHOD("is_action_type"), &InputEvent::is_action_type);
ClassDB::bind_method(D_METHOD("accumulate", "with_event"), &InputEvent::accumulate);
ClassDB::bind_method(D_METHOD("xformed_by", "xform", "local_ofs"), &InputEvent::xformed_by, DEFVAL(Vector2()));
ADD_PROPERTY(PropertyInfo(Variant::INT, "device"), "set_device", "get_device");
BIND_CONSTANT(DEVICE_ID_EMULATION);
}
///////////////////////////////////
void InputEventFromWindow::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_window_id", "id"), &InputEventFromWindow::set_window_id);
ClassDB::bind_method(D_METHOD("get_window_id"), &InputEventFromWindow::get_window_id);
ADD_PROPERTY(PropertyInfo(Variant::INT, "window_id"), "set_window_id", "get_window_id");
}
void InputEventFromWindow::set_window_id(int64_t p_id) {
window_id = p_id;
emit_changed();
}
int64_t InputEventFromWindow::get_window_id() const {
return window_id;
}
///////////////////////////////////
void InputEventWithModifiers::set_command_or_control_autoremap(bool p_enabled) {
command_or_control_autoremap = p_enabled;
if (command_or_control_autoremap) {
if (OS::get_singleton()->has_feature("macos") || OS::get_singleton()->has_feature("web_macos") || OS::get_singleton()->has_feature("web_ios")) {
ctrl_pressed = false;
meta_pressed = true;
} else {
ctrl_pressed = true;
meta_pressed = false;
}
} else {
ctrl_pressed = false;
meta_pressed = false;
}
emit_changed();
}
bool InputEventWithModifiers::is_command_or_control_autoremap() const {
return command_or_control_autoremap;
}
bool InputEventWithModifiers::is_command_or_control_pressed() const {
if (OS::get_singleton()->has_feature("macos") || OS::get_singleton()->has_feature("web_macos") || OS::get_singleton()->has_feature("web_ios")) {
return meta_pressed;
} else {
return ctrl_pressed;
}
}
void InputEventWithModifiers::set_shift_pressed(bool p_enabled) {
shift_pressed = p_enabled;
emit_changed();
}
bool InputEventWithModifiers::is_shift_pressed() const {
return shift_pressed;
}
void InputEventWithModifiers::set_alt_pressed(bool p_enabled) {
alt_pressed = p_enabled;
emit_changed();
}
bool InputEventWithModifiers::is_alt_pressed() const {
return alt_pressed;
}
void InputEventWithModifiers::set_ctrl_pressed(bool p_enabled) {
ERR_FAIL_COND_MSG(command_or_control_autoremap, "Command or Control autoremapping is enabled, cannot set Control directly!");
ctrl_pressed = p_enabled;
emit_changed();
}
bool InputEventWithModifiers::is_ctrl_pressed() const {
return ctrl_pressed;
}
void InputEventWithModifiers::set_meta_pressed(bool p_enabled) {
ERR_FAIL_COND_MSG(command_or_control_autoremap, "Command or Control autoremapping is enabled, cannot set Meta directly!");
meta_pressed = p_enabled;
emit_changed();
}
bool InputEventWithModifiers::is_meta_pressed() const {
return meta_pressed;
}
void InputEventWithModifiers::set_modifiers_from_event(const InputEventWithModifiers *event) {
set_alt_pressed(event->is_alt_pressed());
set_shift_pressed(event->is_shift_pressed());
set_ctrl_pressed(event->is_ctrl_pressed());
set_meta_pressed(event->is_meta_pressed());
}
BitField<KeyModifierMask> InputEventWithModifiers::get_modifiers_mask() const {
BitField<KeyModifierMask> mask;
if (is_ctrl_pressed()) {
mask.set_flag(KeyModifierMask::CTRL);
}
if (is_shift_pressed()) {
mask.set_flag(KeyModifierMask::SHIFT);
}
if (is_alt_pressed()) {
mask.set_flag(KeyModifierMask::ALT);
}
if (is_meta_pressed()) {
mask.set_flag(KeyModifierMask::META);
}
if (is_command_or_control_autoremap()) {
if (OS::get_singleton()->has_feature("macos") || OS::get_singleton()->has_feature("web_macos") || OS::get_singleton()->has_feature("web_ios")) {
mask.set_flag(KeyModifierMask::META);
} else {
mask.set_flag(KeyModifierMask::CTRL);
}
}
return mask;
}
String InputEventWithModifiers::as_text() const {
Vector<String> mod_names;
if (is_ctrl_pressed()) {
mod_names.push_back(find_keycode_name(Key::CTRL));
}
if (is_shift_pressed()) {
mod_names.push_back(find_keycode_name(Key::SHIFT));
}
if (is_alt_pressed()) {
mod_names.push_back(find_keycode_name(Key::ALT));
}
if (is_meta_pressed()) {
mod_names.push_back(find_keycode_name(Key::META));
}
if (!mod_names.is_empty()) {
return String("+").join(mod_names);
} else {
return "";
}
}
String InputEventWithModifiers::to_string() {
return as_text();
}
void InputEventWithModifiers::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_command_or_control_autoremap", "enable"), &InputEventWithModifiers::set_command_or_control_autoremap);
ClassDB::bind_method(D_METHOD("is_command_or_control_autoremap"), &InputEventWithModifiers::is_command_or_control_autoremap);
ClassDB::bind_method(D_METHOD("is_command_or_control_pressed"), &InputEventWithModifiers::is_command_or_control_pressed);
ClassDB::bind_method(D_METHOD("set_alt_pressed", "pressed"), &InputEventWithModifiers::set_alt_pressed);
ClassDB::bind_method(D_METHOD("is_alt_pressed"), &InputEventWithModifiers::is_alt_pressed);
ClassDB::bind_method(D_METHOD("set_shift_pressed", "pressed"), &InputEventWithModifiers::set_shift_pressed);
ClassDB::bind_method(D_METHOD("is_shift_pressed"), &InputEventWithModifiers::is_shift_pressed);
ClassDB::bind_method(D_METHOD("set_ctrl_pressed", "pressed"), &InputEventWithModifiers::set_ctrl_pressed);
ClassDB::bind_method(D_METHOD("is_ctrl_pressed"), &InputEventWithModifiers::is_ctrl_pressed);
ClassDB::bind_method(D_METHOD("set_meta_pressed", "pressed"), &InputEventWithModifiers::set_meta_pressed);
ClassDB::bind_method(D_METHOD("is_meta_pressed"), &InputEventWithModifiers::is_meta_pressed);
ClassDB::bind_method(D_METHOD("get_modifiers_mask"), &InputEventWithModifiers::get_modifiers_mask);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "command_or_control_autoremap"), "set_command_or_control_autoremap", "is_command_or_control_autoremap");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "alt_pressed"), "set_alt_pressed", "is_alt_pressed");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shift_pressed"), "set_shift_pressed", "is_shift_pressed");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "ctrl_pressed"), "set_ctrl_pressed", "is_ctrl_pressed");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "meta_pressed"), "set_meta_pressed", "is_meta_pressed");
}
void InputEventWithModifiers::_validate_property(PropertyInfo &p_property) const {
if (command_or_control_autoremap) {
// Cannot be used with Meta/Command or Control!
if (p_property.name == "meta_pressed") {
p_property.usage ^= PROPERTY_USAGE_STORAGE;
}
if (p_property.name == "ctrl_pressed") {
p_property.usage ^= PROPERTY_USAGE_STORAGE;
}
} else {
if (p_property.name == "command_or_control_autoremap") {
p_property.usage ^= PROPERTY_USAGE_STORAGE;
}
}
}
///////////////////////////////////
void InputEventKey::set_pressed(bool p_pressed) {
pressed = p_pressed;
emit_changed();
}
void InputEventKey::set_keycode(Key p_keycode) {
keycode = p_keycode;
emit_changed();
}
Key InputEventKey::get_keycode() const {
return keycode;
}
void InputEventKey::set_key_label(Key p_key_label) {
key_label = p_key_label;
emit_changed();
}
Key InputEventKey::get_key_label() const {
return key_label;
}
void InputEventKey::set_physical_keycode(Key p_keycode) {
physical_keycode = p_keycode;
emit_changed();
}
Key InputEventKey::get_physical_keycode() const {
return physical_keycode;
}
void InputEventKey::set_unicode(char32_t p_unicode) {
unicode = p_unicode;
emit_changed();
}
char32_t InputEventKey::get_unicode() const {
return unicode;
}
void InputEventKey::set_location(KeyLocation p_key_location) {
location = p_key_location;
emit_changed();
}
KeyLocation InputEventKey::get_location() const {
return location;
}
void InputEventKey::set_echo(bool p_enable) {
echo = p_enable;
emit_changed();
}
bool InputEventKey::is_echo() const {
return echo;
}
Key InputEventKey::get_keycode_with_modifiers() const {
return keycode | (int64_t)get_modifiers_mask();
}
Key InputEventKey::get_physical_keycode_with_modifiers() const {
return physical_keycode | (int64_t)get_modifiers_mask();
}
Key InputEventKey::get_key_label_with_modifiers() const {
return key_label | get_modifiers_mask();
}
String InputEventKey::as_text_physical_keycode() const {
String kc;
if (physical_keycode != Key::NONE) {
kc = keycode_get_string(physical_keycode);
} else {
kc = "(" + RTR("Unset") + ")";
}
if (kc.is_empty()) {
return kc;
}
String mods_text = InputEventWithModifiers::as_text();
return mods_text.is_empty() ? kc : mods_text + "+" + kc;
}
String InputEventKey::as_text_keycode() const {
String kc;
if (keycode != Key::NONE) {
kc = keycode_get_string(keycode);
} else {
kc = "(" + RTR("Unset") + ")";
}
if (kc.is_empty()) {
return kc;
}
String mods_text = InputEventWithModifiers::as_text();
return mods_text.is_empty() ? kc : mods_text + "+" + kc;
}
String InputEventKey::as_text_key_label() const {
String kc;
if (key_label != Key::NONE) {
kc = keycode_get_string(key_label);
} else {
kc = "(" + RTR("Unset") + ")";
}
if (kc.is_empty()) {
return kc;
}
String mods_text = InputEventWithModifiers::as_text();
return mods_text.is_empty() ? kc : mods_text + "+" + kc;
}
String InputEventKey::as_text_location() const {
String loc;
switch (location) {
case KeyLocation::LEFT:
loc = "left";
break;
case KeyLocation::RIGHT:
loc = "right";
break;
default:
break;
}
return loc;
}
String InputEventKey::as_text() const {
String kc;
if (keycode == Key::NONE && physical_keycode == Key::NONE && key_label != Key::NONE) {
kc = keycode_get_string(key_label) + " (Unicode)";
} else if (keycode != Key::NONE) {
kc = keycode_get_string(keycode);
} else if (physical_keycode != Key::NONE) {
kc = keycode_get_string(physical_keycode) + " (" + RTR("Physical") + ")";
} else {
kc = "(" + RTR("Unset") + ")";
}
if (kc.is_empty()) {
return kc;
}
String mods_text = InputEventWithModifiers::as_text();
return mods_text.is_empty() ? kc : mods_text + "+" + kc;
}
String InputEventKey::to_string() {
String p = is_pressed() ? "true" : "false";
String e = is_echo() ? "true" : "false";
String kc = "";
String physical = "false";
String loc = as_text_location();
if (loc.is_empty()) {
loc = "unspecified";
}
if (keycode == Key::NONE && physical_keycode == Key::NONE && unicode != 0) {
kc = "U+" + String::num_uint64(unicode, 16) + " (" + String::chr(unicode) + ")";
} else if (keycode != Key::NONE) {
kc = itos((int64_t)keycode) + " (" + keycode_get_string(keycode) + ")";
} else if (physical_keycode != Key::NONE) {
kc = itos((int64_t)physical_keycode) + " (" + keycode_get_string(physical_keycode) + ")";
physical = "true";
} else {
kc = "(" + RTR("Unset") + ")";
}
String mods = InputEventWithModifiers::as_text();
mods = mods.is_empty() ? "none" : mods;
return vformat("InputEventKey: keycode=%s, mods=%s, physical=%s, location=%s, pressed=%s, echo=%s", kc, mods, physical, loc, p, e);
}
Ref<InputEventKey> InputEventKey::create_reference(Key p_keycode, bool p_physical) {
Ref<InputEventKey> ie;
ie.instantiate();
if (p_physical) {
ie->set_physical_keycode(p_keycode & KeyModifierMask::CODE_MASK);
} else {
ie->set_keycode(p_keycode & KeyModifierMask::CODE_MASK);
}
char32_t ch = char32_t(p_keycode & KeyModifierMask::CODE_MASK);
if (ch < 0xd800 || (ch > 0xdfff && ch <= 0x10ffff)) {
ie->set_unicode(ch);
}
if ((p_keycode & KeyModifierMask::SHIFT) != Key::NONE) {
ie->set_shift_pressed(true);
}
if ((p_keycode & KeyModifierMask::ALT) != Key::NONE) {
ie->set_alt_pressed(true);
}
if ((p_keycode & KeyModifierMask::CMD_OR_CTRL) != Key::NONE) {
ie->set_command_or_control_autoremap(true);
if ((p_keycode & KeyModifierMask::CTRL) != Key::NONE || (p_keycode & KeyModifierMask::META) != Key::NONE) {
WARN_PRINT("Invalid Key Modifiers: Command or Control autoremapping is enabled, Meta and Control values are ignored!");
}
} else {
if ((p_keycode & KeyModifierMask::CTRL) != Key::NONE) {
ie->set_ctrl_pressed(true);
}
if ((p_keycode & KeyModifierMask::META) != Key::NONE) {
ie->set_meta_pressed(true);
}
}
return ie;
}
bool InputEventKey::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 {
Ref<InputEventKey> key = p_event;
if (key.is_null()) {
return false;
}
bool match;
if (keycode == Key::NONE && physical_keycode == Key::NONE && key_label != Key::NONE) {
match = key_label == key->key_label;
} else if (keycode != Key::NONE) {
match = keycode == key->keycode;
} else if (physical_keycode != Key::NONE) {
match = physical_keycode == key->physical_keycode;
if (location != KeyLocation::UNSPECIFIED) {
match &= location == key->location;
}
} else {
match = false;
}
Key action_mask = (Key)(int64_t)get_modifiers_mask();
Key key_mask = (Key)(int64_t)key->get_modifiers_mask();
if (key->is_pressed()) {
match &= (action_mask & key_mask) == action_mask;
}
if (p_exact_match) {
match &= action_mask == key_mask;
}
if (match) {
bool key_pressed = key->is_pressed();
if (r_pressed != nullptr) {
*r_pressed = key_pressed;
}
float strength = key_pressed ? 1.0f : 0.0f;
if (r_strength != nullptr) {
*r_strength = strength;
}
if (r_raw_strength != nullptr) {
*r_raw_strength = strength;
}
}
return match;
}
bool InputEventKey::is_match(const Ref<InputEvent> &p_event, bool p_exact_match) const {
Ref<InputEventKey> key = p_event;
if (key.is_null()) {
return false;
}
if (keycode == Key::NONE && physical_keycode == Key::NONE && key_label != Key::NONE) {
return (key_label == key->key_label) &&
(!p_exact_match || get_modifiers_mask() == key->get_modifiers_mask());
} else if (keycode != Key::NONE) {
return (keycode == key->keycode) &&
(!p_exact_match || get_modifiers_mask() == key->get_modifiers_mask());
} else if (physical_keycode != Key::NONE) {
if (location != KeyLocation::UNSPECIFIED && location != key->location) {
return false;
}
return (physical_keycode == key->physical_keycode) &&
(!p_exact_match || get_modifiers_mask() == key->get_modifiers_mask());
} else {
return false;
}
}
void InputEventKey::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &InputEventKey::set_pressed);
ClassDB::bind_method(D_METHOD("set_keycode", "keycode"), &InputEventKey::set_keycode);
ClassDB::bind_method(D_METHOD("get_keycode"), &InputEventKey::get_keycode);
ClassDB::bind_method(D_METHOD("set_physical_keycode", "physical_keycode"), &InputEventKey::set_physical_keycode);
ClassDB::bind_method(D_METHOD("get_physical_keycode"), &InputEventKey::get_physical_keycode);
ClassDB::bind_method(D_METHOD("set_key_label", "key_label"), &InputEventKey::set_key_label);
ClassDB::bind_method(D_METHOD("get_key_label"), &InputEventKey::get_key_label);
ClassDB::bind_method(D_METHOD("set_unicode", "unicode"), &InputEventKey::set_unicode);
ClassDB::bind_method(D_METHOD("get_unicode"), &InputEventKey::get_unicode);
ClassDB::bind_method(D_METHOD("set_location", "location"), &InputEventKey::set_location);
ClassDB::bind_method(D_METHOD("get_location"), &InputEventKey::get_location);
ClassDB::bind_method(D_METHOD("set_echo", "echo"), &InputEventKey::set_echo);
ClassDB::bind_method(D_METHOD("get_keycode_with_modifiers"), &InputEventKey::get_keycode_with_modifiers);
ClassDB::bind_method(D_METHOD("get_physical_keycode_with_modifiers"), &InputEventKey::get_physical_keycode_with_modifiers);
ClassDB::bind_method(D_METHOD("get_key_label_with_modifiers"), &InputEventKey::get_key_label_with_modifiers);
ClassDB::bind_method(D_METHOD("as_text_keycode"), &InputEventKey::as_text_keycode);
ClassDB::bind_method(D_METHOD("as_text_physical_keycode"), &InputEventKey::as_text_physical_keycode);
ClassDB::bind_method(D_METHOD("as_text_key_label"), &InputEventKey::as_text_key_label);
ClassDB::bind_method(D_METHOD("as_text_location"), &InputEventKey::as_text_location);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pressed"), "set_pressed", "is_pressed");
ADD_PROPERTY(PropertyInfo(Variant::INT, "keycode"), "set_keycode", "get_keycode");
ADD_PROPERTY(PropertyInfo(Variant::INT, "physical_keycode"), "set_physical_keycode", "get_physical_keycode");
ADD_PROPERTY(PropertyInfo(Variant::INT, "key_label"), "set_key_label", "get_key_label");
ADD_PROPERTY(PropertyInfo(Variant::INT, "unicode"), "set_unicode", "get_unicode");
ADD_PROPERTY(PropertyInfo(Variant::INT, "location", PROPERTY_HINT_ENUM, "Unspecified,Left,Right"), "set_location", "get_location");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "echo"), "set_echo", "is_echo");
}
///////////////////////////////////
void InputEventMouse::set_button_mask(BitField<MouseButtonMask> p_mask) {
button_mask = p_mask;
emit_changed();
}
BitField<MouseButtonMask> InputEventMouse::get_button_mask() const {
return button_mask;
}
void InputEventMouse::set_position(const Vector2 &p_pos) {
pos = p_pos;
}
Vector2 InputEventMouse::get_position() const {
return pos;
}
void InputEventMouse::set_global_position(const Vector2 &p_global_pos) {
global_pos = p_global_pos;
}
Vector2 InputEventMouse::get_global_position() const {
return global_pos;
}
void InputEventMouse::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_button_mask", "button_mask"), &InputEventMouse::set_button_mask);
ClassDB::bind_method(D_METHOD("get_button_mask"), &InputEventMouse::get_button_mask);
ClassDB::bind_method(D_METHOD("set_position", "position"), &InputEventMouse::set_position);
ClassDB::bind_method(D_METHOD("get_position"), &InputEventMouse::get_position);
ClassDB::bind_method(D_METHOD("set_global_position", "global_position"), &InputEventMouse::set_global_position);
ClassDB::bind_method(D_METHOD("get_global_position"), &InputEventMouse::get_global_position);
ADD_PROPERTY(PropertyInfo(Variant::INT, "button_mask"), "set_button_mask", "get_button_mask");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position", PROPERTY_HINT_NONE, "suffix:px"), "set_position", "get_position");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "global_position", PROPERTY_HINT_NONE, "suffix:px"), "set_global_position", "get_global_position");
}
///////////////////////////////////
void InputEventMouseButton::set_factor(float p_factor) {
factor = p_factor;
}
float InputEventMouseButton::get_factor() const {
return factor;
}
void InputEventMouseButton::set_button_index(MouseButton p_index) {
button_index = p_index;
emit_changed();
}
MouseButton InputEventMouseButton::get_button_index() const {
return button_index;
}
void InputEventMouseButton::set_pressed(bool p_pressed) {
pressed = p_pressed;
}
void InputEventMouseButton::set_canceled(bool p_canceled) {
canceled = p_canceled;
}
void InputEventMouseButton::set_double_click(bool p_double_click) {
double_click = p_double_click;
}
bool InputEventMouseButton::is_double_click() const {
return double_click;
}
Ref<InputEvent> InputEventMouseButton::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const {
Vector2 g = get_global_position();
Vector2 l = p_xform.xform(get_position() + p_local_ofs);
Ref<InputEventMouseButton> mb;
mb.instantiate();
mb->set_device(get_device());
mb->set_window_id(get_window_id());
mb->set_modifiers_from_event(this);
mb->set_position(l);
mb->set_global_position(g);
mb->set_button_mask(get_button_mask());
mb->set_pressed(pressed);
mb->set_canceled(canceled);
mb->set_double_click(double_click);
mb->set_factor(factor);
mb->set_button_index(button_index);
return mb;
}
bool InputEventMouseButton::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 {
Ref<InputEventMouseButton> mb = p_event;
if (mb.is_null()) {
return false;
}
bool match = button_index == mb->button_index;
Key action_modifiers_mask = (Key)(int64_t)get_modifiers_mask();
Key button_modifiers_mask = (Key)(int64_t)mb->get_modifiers_mask();
if (mb->is_pressed()) {
match &= (action_modifiers_mask & button_modifiers_mask) == action_modifiers_mask;
}
if (p_exact_match) {
match &= action_modifiers_mask == button_modifiers_mask;
}
if (match) {
bool mb_pressed = mb->is_pressed();
if (r_pressed != nullptr) {
*r_pressed = mb_pressed;
}
float strength = mb_pressed ? 1.0f : 0.0f;
if (r_strength != nullptr) {
*r_strength = strength;
}
if (r_raw_strength != nullptr) {
*r_raw_strength = strength;
}
}
return match;
}
bool InputEventMouseButton::is_match(const Ref<InputEvent> &p_event, bool p_exact_match) const {
Ref<InputEventMouseButton> mb = p_event;
if (mb.is_null()) {
return false;
}
return button_index == mb->button_index &&
(!p_exact_match || get_modifiers_mask() == mb->get_modifiers_mask());
}
static const char *_mouse_button_descriptions[9] = {
TTRC("Left Mouse Button"),
TTRC("Right Mouse Button"),
TTRC("Middle Mouse Button"),
TTRC("Mouse Wheel Up"),
TTRC("Mouse Wheel Down"),
TTRC("Mouse Wheel Left"),
TTRC("Mouse Wheel Right"),
TTRC("Mouse Thumb Button 1"),
TTRC("Mouse Thumb Button 2")
};
String InputEventMouseButton::as_text() const {
// Modifiers
String mods_text = InputEventWithModifiers::as_text();
String full_string = mods_text.is_empty() ? "" : mods_text + "+";
// Button
MouseButton idx = get_button_index();
switch (idx) {
case MouseButton::LEFT:
case MouseButton::RIGHT:
case MouseButton::MIDDLE:
case MouseButton::WHEEL_UP:
case MouseButton::WHEEL_DOWN:
case MouseButton::WHEEL_LEFT:
case MouseButton::WHEEL_RIGHT:
case MouseButton::MB_XBUTTON1:
case MouseButton::MB_XBUTTON2:
full_string += RTR(_mouse_button_descriptions[(size_t)idx - 1]); // button index starts from 1, array index starts from 0, so subtract 1
break;
default:
full_string += RTR("Button") + " #" + itos((int64_t)idx);
break;
}
// Double Click
if (double_click) {
full_string += " (" + RTR("Double Click") + ")";
}
return full_string;
}
String InputEventMouseButton::to_string() {
String p = is_pressed() ? "true" : "false";
String canceled_state = is_canceled() ? "true" : "false";
String d = double_click ? "true" : "false";
MouseButton idx = get_button_index();
String button_string = itos((int64_t)idx);
switch (idx) {
case MouseButton::LEFT:
case MouseButton::RIGHT:
case MouseButton::MIDDLE:
case MouseButton::WHEEL_UP:
case MouseButton::WHEEL_DOWN:
case MouseButton::WHEEL_LEFT:
case MouseButton::WHEEL_RIGHT:
case MouseButton::MB_XBUTTON1:
case MouseButton::MB_XBUTTON2:
button_string += vformat(" (%s)", TTRGET(_mouse_button_descriptions[(size_t)idx - 1])); // button index starts from 1, array index starts from 0, so subtract 1
break;
default:
break;
}
String mods = InputEventWithModifiers::as_text();
mods = mods.is_empty() ? "none" : mods;
// Work around the fact vformat can only take 5 substitutions but 6 need to be passed.
String index_and_mods = vformat("button_index=%s, mods=%s", button_index, mods);
return vformat("InputEventMouseButton: %s, pressed=%s, canceled=%s, position=(%s), button_mask=%d, double_click=%s", index_and_mods, p, canceled_state, String(get_position()), get_button_mask(), d);
}
void InputEventMouseButton::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_factor", "factor"), &InputEventMouseButton::set_factor);
ClassDB::bind_method(D_METHOD("get_factor"), &InputEventMouseButton::get_factor);
ClassDB::bind_method(D_METHOD("set_button_index", "button_index"), &InputEventMouseButton::set_button_index);
ClassDB::bind_method(D_METHOD("get_button_index"), &InputEventMouseButton::get_button_index);
ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &InputEventMouseButton::set_pressed);
ClassDB::bind_method(D_METHOD("set_canceled", "canceled"), &InputEventMouseButton::set_canceled);
ClassDB::bind_method(D_METHOD("set_double_click", "double_click"), &InputEventMouseButton::set_double_click);
ClassDB::bind_method(D_METHOD("is_double_click"), &InputEventMouseButton::is_double_click);
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "factor"), "set_factor", "get_factor");
ADD_PROPERTY(PropertyInfo(Variant::INT, "button_index"), "set_button_index", "get_button_index");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "canceled"), "set_canceled", "is_canceled");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pressed"), "set_pressed", "is_pressed");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "double_click"), "set_double_click", "is_double_click");
}
///////////////////////////////////
void InputEventMouseMotion::set_tilt(const Vector2 &p_tilt) {
tilt = p_tilt;
}
Vector2 InputEventMouseMotion::get_tilt() const {
return tilt;
}
void InputEventMouseMotion::set_pressure(float p_pressure) {
pressure = p_pressure;
}
float InputEventMouseMotion::get_pressure() const {
return pressure;
}
void InputEventMouseMotion::set_pen_inverted(bool p_inverted) {
pen_inverted = p_inverted;
}
bool InputEventMouseMotion::get_pen_inverted() const {
return pen_inverted;
}
void InputEventMouseMotion::set_relative(const Vector2 &p_relative) {
relative = p_relative;
}
Vector2 InputEventMouseMotion::get_relative() const {
return relative;
}
void InputEventMouseMotion::set_relative_screen_position(const Vector2 &p_relative) {
screen_relative = p_relative;
}
Vector2 InputEventMouseMotion::get_relative_screen_position() const {
return screen_relative;
}
void InputEventMouseMotion::set_velocity(const Vector2 &p_velocity) {
velocity = p_velocity;
}
Vector2 InputEventMouseMotion::get_velocity() const {
return velocity;
}
void InputEventMouseMotion::set_screen_velocity(const Vector2 &p_velocity) {
screen_velocity = p_velocity;
}
Vector2 InputEventMouseMotion::get_screen_velocity() const {
return screen_velocity;
}
Ref<InputEvent> InputEventMouseMotion::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const {
Ref<InputEventMouseMotion> mm;
mm.instantiate();
mm->set_device(get_device());
mm->set_window_id(get_window_id());
mm->set_modifiers_from_event(this);
mm->set_position(p_xform.xform(get_position() + p_local_ofs));
mm->set_pressure(get_pressure());
mm->set_pen_inverted(get_pen_inverted());
mm->set_tilt(get_tilt());
mm->set_global_position(get_global_position());
mm->set_button_mask(get_button_mask());
mm->set_relative(p_xform.basis_xform(get_relative()));
mm->set_relative_screen_position(get_relative_screen_position());
mm->set_velocity(p_xform.basis_xform(get_velocity()));
mm->set_screen_velocity(get_screen_velocity());
return mm;
}
String InputEventMouseMotion::as_text() const {
return vformat(RTR("Mouse motion at position (%s) with velocity (%s)"), String(get_position()), String(get_velocity()));
}
String InputEventMouseMotion::to_string() {
BitField<MouseButtonMask> mouse_button_mask = get_button_mask();
String button_mask_string = itos((int64_t)mouse_button_mask);
if (mouse_button_mask.has_flag(MouseButtonMask::LEFT)) {
button_mask_string += vformat(" (%s)", TTRGET(_mouse_button_descriptions[(size_t)MouseButton::LEFT - 1]));
}
if (mouse_button_mask.has_flag(MouseButtonMask::MIDDLE)) {
button_mask_string += vformat(" (%s)", TTRGET(_mouse_button_descriptions[(size_t)MouseButton::MIDDLE - 1]));
}
if (mouse_button_mask.has_flag(MouseButtonMask::RIGHT)) {
button_mask_string += vformat(" (%s)", TTRGET(_mouse_button_descriptions[(size_t)MouseButton::RIGHT - 1]));
}
if (mouse_button_mask.has_flag(MouseButtonMask::MB_XBUTTON1)) {
button_mask_string += vformat(" (%s)", TTRGET(_mouse_button_descriptions[(size_t)MouseButton::MB_XBUTTON1 - 1]));
}
if (mouse_button_mask.has_flag(MouseButtonMask::MB_XBUTTON2)) {