Debian Codegraph @

core/input/input_event.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* input_event.cpp */
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 #include "input_event.h"
32
33 #include "core/input/input_map.h"
34 #include "core/input/shortcut.h"
35 #include "core/os/keyboard.h"
36 #include "core/os/os.h"
37
38 void InputEvent::set_device(int p_device) {
39 device = p_device;
40 emit_changed();
41 }
42
43 int InputEvent::get_device() const {
44 return device;
45 }
46
47 bool InputEvent::is_action(const StringName &p_action, bool p_exact_match) const {
48 return InputMap::get_singleton()->event_is_action(Ref<InputEvent>(const_cast<InputEvent *>(this)), p_action, p_exact_match);
49 }
50
51 bool InputEvent::is_action_pressed(const StringName &p_action, bool p_allow_echo, bool p_exact_match) const {
52 bool pressed_state;
53 bool valid = InputMap::get_singleton()->event_get_action_status(Ref<InputEvent>(const_cast<InputEvent *>(this)), p_action, p_exact_match, &pressed_state, nullptr, nullptr);
54 return valid && pressed_state && (p_allow_echo || !is_echo());
55 }
56
57 bool InputEvent::is_action_released(const StringName &p_action, bool p_exact_match) const {
58 bool pressed_state;
59 bool valid = InputMap::get_singleton()->event_get_action_status(Ref<InputEvent>(const_cast<InputEvent *>(this)), p_action, p_exact_match, &pressed_state, nullptr, nullptr);
60 return valid && !pressed_state;
61 }
62
63 float InputEvent::get_action_strength(const StringName &p_action, bool p_exact_match) const {
64 float strength;
65 bool valid = InputMap::get_singleton()->event_get_action_status(Ref<InputEvent>(const_cast<InputEvent *>(this)), p_action, p_exact_match, nullptr, &strength, nullptr);
66 return valid ? strength : 0.0f;
67 }
68
69 float InputEvent::get_action_raw_strength(const StringName &p_action, bool p_exact_match) const {
70 float raw_strength;
71 bool valid = InputMap::get_singleton()->event_get_action_status(Ref<InputEvent>(const_cast<InputEvent *>(this)), p_action, p_exact_match, nullptr, nullptr, &raw_strength);
72 return valid ? raw_strength : 0.0f;
73 }
74
75 bool InputEvent::is_canceled() const {
76 return canceled;
77 }
78
79 bool InputEvent::is_pressed() const {
80 return pressed && !canceled;
81 }
82
83 bool InputEvent::is_released() const {
84 return !pressed && !canceled;
85 }
86
87 bool InputEvent::is_echo() const {
88 return false;
89 }
90
91 RequiredResult<InputEvent> InputEvent::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const {
92 return Ref<InputEvent>(const_cast<InputEvent *>(this));
93 }
94
95 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 {
96 return false;
97 }
98
99 bool InputEvent::is_match(const Ref<InputEvent> &p_event, bool p_exact_match) const {
100 return false;
101 }
102
103 bool InputEvent::is_action_type() const {
104 return false;
105 }
106
107 void InputEvent::_bind_methods() {
108 ClassDB::bind_method(D_METHOD("set_device", "device"), &InputEvent::set_device);
109 ClassDB::bind_method(D_METHOD("get_device"), &InputEvent::get_device);
110
111 ClassDB::bind_method(D_METHOD("is_action", "action", "exact_match"), &InputEvent::is_action, DEFVAL(false));
112 ClassDB::bind_method(D_METHOD("is_action_pressed", "action", "allow_echo", "exact_match"), &InputEvent::is_action_pressed, DEFVAL(false), DEFVAL(false));
113 ClassDB::bind_method(D_METHOD("is_action_released", "action", "exact_match"), &InputEvent::is_action_released, DEFVAL(false));
114 ClassDB::bind_method(D_METHOD("get_action_strength", "action", "exact_match"), &InputEvent::get_action_strength, DEFVAL(false));
115
116 ClassDB::bind_method(D_METHOD("is_canceled"), &InputEvent::is_canceled);
117 ClassDB::bind_method(D_METHOD("is_pressed"), &InputEvent::is_pressed);
118 ClassDB::bind_method(D_METHOD("is_released"), &InputEvent::is_released);
119 ClassDB::bind_method(D_METHOD("is_echo"), &InputEvent::is_echo);
120
121 ClassDB::bind_method(D_METHOD("as_text"), &InputEvent::as_text);
122
123 ClassDB::bind_method(D_METHOD("is_match", "event", "exact_match"), &InputEvent::is_match, DEFVAL(true));
124
125 ClassDB::bind_method(D_METHOD("is_action_type"), &InputEvent::is_action_type);
126
127 ClassDB::bind_method(D_METHOD("accumulate", "with_event"), &InputEvent::accumulate);
128
129 ClassDB::bind_method(D_METHOD("xformed_by", "xform", "local_ofs"), &InputEvent::xformed_by, DEFVAL(Vector2()));
130
131 ADD_PROPERTY(PropertyInfo(Variant::INT, "device"), "set_device", "get_device");
132
133 BIND_CONSTANT(DEVICE_ID_EMULATION);
134 }
135
136 ///////////////////////////////////
137
138 void InputEventFromWindow::_bind_methods() {
139 ClassDB::bind_method(D_METHOD("set_window_id", "id"), &InputEventFromWindow::set_window_id);
140 ClassDB::bind_method(D_METHOD("get_window_id"), &InputEventFromWindow::get_window_id);
141 ADD_PROPERTY(PropertyInfo(Variant::INT, "window_id"), "set_window_id", "get_window_id");
142 }
143
144 void InputEventFromWindow::set_window_id(int64_t p_id) {
145 window_id = p_id;
146 emit_changed();
147 }
148
149 int64_t InputEventFromWindow::get_window_id() const {
150 return window_id;
151 }
152
153 ///////////////////////////////////
154
155 void InputEventWithModifiers::set_command_or_control_autoremap(bool p_enabled) {
156 if (command_or_control_autoremap == p_enabled) {
157 return;
158 }
159 command_or_control_autoremap = p_enabled;
160 if (command_or_control_autoremap) {
161 if (OS::prefer_meta_over_ctrl()) {
162 ctrl_pressed = false;
163 meta_pressed = true;
164 } else {
165 ctrl_pressed = true;
166 meta_pressed = false;
167 }
168 } else {
169 ctrl_pressed = false;
170 meta_pressed = false;
171 }
172 notify_property_list_changed();
173 emit_changed();
174 }
175
176 bool InputEventWithModifiers::is_command_or_control_autoremap() const {
177 return command_or_control_autoremap;
178 }
179
180 bool InputEventWithModifiers::is_command_or_control_pressed() const {
181 if (OS::prefer_meta_over_ctrl()) {
182 return meta_pressed;
183 } else {
184 return ctrl_pressed;
185 }
186 }
187
188 void InputEventWithModifiers::set_shift_pressed(bool p_enabled) {
189 shift_pressed = p_enabled;
190 emit_changed();
191 }
192
193 bool InputEventWithModifiers::is_shift_pressed() const {
194 return shift_pressed;
195 }
196
197 void InputEventWithModifiers::set_alt_pressed(bool p_enabled) {
198 alt_pressed = p_enabled;
199 emit_changed();
200 }
201
202 bool InputEventWithModifiers::is_alt_pressed() const {
203 return alt_pressed;
204 }
205
206 void InputEventWithModifiers::set_ctrl_pressed(bool p_enabled) {
207 ERR_FAIL_COND_MSG(command_or_control_autoremap, "Command or Control autoremapping is enabled, cannot set Control directly!");
208 ctrl_pressed = p_enabled;
209 emit_changed();
210 }
211
212 bool InputEventWithModifiers::is_ctrl_pressed() const {
213 return ctrl_pressed;
214 }
215
216 void InputEventWithModifiers::set_meta_pressed(bool p_enabled) {
217 ERR_FAIL_COND_MSG(command_or_control_autoremap, "Command or Control autoremapping is enabled, cannot set Meta directly!");
218 meta_pressed = p_enabled;
219 emit_changed();
220 }
221
222 bool InputEventWithModifiers::is_meta_pressed() const {
223 return meta_pressed;
224 }
225
226 void InputEventWithModifiers::set_modifiers_from_event(const InputEventWithModifiers *event) {
227 set_alt_pressed(event->is_alt_pressed());
228 set_shift_pressed(event->is_shift_pressed());
229 set_ctrl_pressed(event->is_ctrl_pressed());
230 set_meta_pressed(event->is_meta_pressed());
231 }
232
233 BitField<KeyModifierMask> InputEventWithModifiers::get_modifiers_mask() const {
234 BitField<KeyModifierMask> mask = {};
235 if (is_ctrl_pressed()) {
236 mask.set_flag(KeyModifierMask::CTRL);
237 }
238 if (is_shift_pressed()) {
239 mask.set_flag(KeyModifierMask::SHIFT);
240 }
241 if (is_alt_pressed()) {
242 mask.set_flag(KeyModifierMask::ALT);
243 }
244 if (is_meta_pressed()) {
245 mask.set_flag(KeyModifierMask::META);
246 }
247 if (is_command_or_control_autoremap()) {
248 if (OS::prefer_meta_over_ctrl()) {
249 mask.set_flag(KeyModifierMask::META);
250 } else {
251 mask.set_flag(KeyModifierMask::CTRL);
252 }
253 }
254 return mask;
255 }
256
257 String InputEventWithModifiers::as_text() const {
258 Vector<String> mod_names;
259
260 if (is_ctrl_pressed()) {
261 mod_names.push_back(find_keycode_name(Key::CTRL));
262 }
263 if (is_alt_pressed()) {
264 mod_names.push_back(find_keycode_name(Key::ALT));
265 }
266 if (is_shift_pressed()) {
267 mod_names.push_back(find_keycode_name(Key::SHIFT));
268 }
269 if (is_meta_pressed()) {
270 mod_names.push_back(find_keycode_name(Key::META));
271 }
272
273 if (!mod_names.is_empty()) {
274 return String("+").join(mod_names);
275 } else {
276 return "";
277 }
278 }
279
280 String InputEventWithModifiers::_to_string() {
281 return as_text();
282 }
283
284 void InputEventWithModifiers::_bind_methods() {
285 ClassDB::bind_method(D_METHOD("set_command_or_control_autoremap", "enable"), &InputEventWithModifiers::set_command_or_control_autoremap);
286 ClassDB::bind_method(D_METHOD("is_command_or_control_autoremap"), &InputEventWithModifiers::is_command_or_control_autoremap);
287
288 ClassDB::bind_method(D_METHOD("is_command_or_control_pressed"), &InputEventWithModifiers::is_command_or_control_pressed);
289
290 ClassDB::bind_method(D_METHOD("set_alt_pressed", "pressed"), &InputEventWithModifiers::set_alt_pressed);
291 ClassDB::bind_method(D_METHOD("is_alt_pressed"), &InputEventWithModifiers::is_alt_pressed);
292
293 ClassDB::bind_method(D_METHOD("set_shift_pressed", "pressed"), &InputEventWithModifiers::set_shift_pressed);
294 ClassDB::bind_method(D_METHOD("is_shift_pressed"), &InputEventWithModifiers::is_shift_pressed);
295
296 ClassDB::bind_method(D_METHOD("set_ctrl_pressed", "pressed"), &InputEventWithModifiers::set_ctrl_pressed);
297 ClassDB::bind_method(D_METHOD("is_ctrl_pressed"), &InputEventWithModifiers::is_ctrl_pressed);
298
299 ClassDB::bind_method(D_METHOD("set_meta_pressed", "pressed"), &InputEventWithModifiers::set_meta_pressed);
300 ClassDB::bind_method(D_METHOD("is_meta_pressed"), &InputEventWithModifiers::is_meta_pressed);
301
302 ClassDB::bind_method(D_METHOD("get_modifiers_mask"), &InputEventWithModifiers::get_modifiers_mask);
303
304 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "command_or_control_autoremap"), "set_command_or_control_autoremap", "is_command_or_control_autoremap");
305 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "alt_pressed"), "set_alt_pressed", "is_alt_pressed");
306 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shift_pressed"), "set_shift_pressed", "is_shift_pressed");
307 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "ctrl_pressed"), "set_ctrl_pressed", "is_ctrl_pressed");
308 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "meta_pressed"), "set_meta_pressed", "is_meta_pressed");
309 }
310
311 void InputEventWithModifiers::_validate_property(PropertyInfo &p_property) const {
312 if (command_or_control_autoremap) {
313 // Cannot be used with Meta/Command or Control!
314 if (p_property.name == "meta_pressed") {
315 p_property.usage ^= PROPERTY_USAGE_STORAGE;
316 p_property.usage ^= PROPERTY_USAGE_EDITOR;
317 }
318 if (p_property.name == "ctrl_pressed") {
319 p_property.usage ^= PROPERTY_USAGE_STORAGE;
320 p_property.usage ^= PROPERTY_USAGE_EDITOR;
321 }
322 } else {
323 if (p_property.name == "command_or_control_autoremap") {
324 p_property.usage ^= PROPERTY_USAGE_STORAGE;
325 }
326 }
327 }
328
329 ///////////////////////////////////
330
331 void InputEventKey::set_pressed(bool p_pressed) {
332 pressed = p_pressed;
333 emit_changed();
334 }
335
336 void InputEventKey::set_keycode(Key p_keycode) {
337 keycode = p_keycode;
338 emit_changed();
339 }
340
341 Key InputEventKey::get_keycode() const {
342 return keycode;
343 }
344
345 void InputEventKey::set_key_label(Key p_key_label) {
346 key_label = p_key_label;
347 emit_changed();
348 }
349
350 Key InputEventKey::get_key_label() const {
351 return key_label;
352 }
353
354 void InputEventKey::set_physical_keycode(Key p_keycode) {
355 physical_keycode = p_keycode;
356 emit_changed();
357 }
358
359 Key InputEventKey::get_physical_keycode() const {
360 return physical_keycode;
361 }
362
363 void InputEventKey::set_unicode(char32_t p_unicode) {
364 unicode = p_unicode;
365 emit_changed();
366 }
367
368 char32_t InputEventKey::get_unicode() const {
369 return unicode;
370 }
371
372 void InputEventKey::set_location(KeyLocation p_key_location) {
373 location = p_key_location;
374 emit_changed();
375 }
376
377 KeyLocation InputEventKey::get_location() const {
378 return location;
379 }
380
381 void InputEventKey::set_echo(bool p_enable) {
382 echo = p_enable;
383 emit_changed();
384 }
385
386 bool InputEventKey::is_echo() const {
387 return echo;
388 }
389
390 Key InputEventKey::get_keycode_with_modifiers() const {
391 return keycode | get_modifiers_mask();
392 }
393
394 Key InputEventKey::get_physical_keycode_with_modifiers() const {
395 return physical_keycode | get_modifiers_mask();
396 }
397
398 Key InputEventKey::get_key_label_with_modifiers() const {
399 return key_label | get_modifiers_mask();
400 }
401
402 String InputEventKey::as_text_physical_keycode() const {
403 String kc;
404
405 if (physical_keycode != Key::NONE) {
406 kc = keycode_get_string(physical_keycode);
407 } else {
408 kc = "(" + RTR("unset") + ")";
409 }
410
411 if (kc.is_empty()) {
412 return kc;
413 }
414
415 String mods_text = InputEventWithModifiers::as_text();
416 return mods_text.is_empty() ? kc : mods_text + "+" + kc;
417 }
418
419 String InputEventKey::as_text_keycode() const {
420 String kc;
421
422 if (keycode != Key::NONE) {
423 kc = keycode_get_string(keycode);
424 } else {
425 kc = "(" + RTR("unset") + ")";
426 }
427
428 if (kc.is_empty()) {
429 return kc;
430 }
431
432 String mods_text = InputEventWithModifiers::as_text();
433 return mods_text.is_empty() ? kc : mods_text + "+" + kc;
434 }
435
436 String InputEventKey::as_text_key_label() const {
437 String kc;
438
439 if (key_label != Key::NONE) {
440 kc = keycode_get_string(key_label);
441 } else {
442 kc = "(" + RTR("unset") + ")";
443 }
444
445 if (kc.is_empty()) {
446 return kc;
447 }
448
449 String mods_text = InputEventWithModifiers::as_text();
450 return mods_text.is_empty() ? kc : mods_text + "+" + kc;
451 }
452
453 String InputEventKey::as_text_location() const {
454 String loc;
455
456 switch (location) {
457 case KeyLocation::LEFT:
458 loc = "left";
459 break;
460 case KeyLocation::RIGHT:
461 loc = "right";
462 break;
463 default:
464 break;
465 }
466
467 return loc;
468 }
469
470 String InputEventKey::as_text() const {
471 String kc;
472
473 if (keycode == Key::NONE && physical_keycode == Key::NONE && key_label != Key::NONE) {
474 kc = keycode_get_string(key_label) + " - Unicode";
475 } else if (keycode != Key::NONE) {
476 kc = keycode_get_string(keycode);
477 } else if (physical_keycode != Key::NONE) {
478 kc = keycode_get_string(physical_keycode);
479 if ((physical_keycode & Key::SPECIAL) != Key::SPECIAL) {
480 kc += " - " + RTR("Physical");
481 }
482 } else {
483 kc = "(" + RTR("unset") + ")";
484 }
485
486 if (kc.is_empty()) {
487 return kc;
488 }
489
490 String mods_text = InputEventWithModifiers::as_text();
491 return mods_text.is_empty() ? kc : mods_text + "+" + kc;
492 }
493
494 String InputEventKey::_to_string() {
495 String p = is_pressed() ? "true" : "false";
496 String e = is_echo() ? "true" : "false";
497
498 String kc = "";
499 String physical = "false";
500
501 String loc = as_text_location();
502 if (loc.is_empty()) {
503 loc = "unspecified";
504 }
505
506 if (keycode == Key::NONE && physical_keycode == Key::NONE && unicode != 0) {
507 kc = "U+" + String::num_uint64(unicode, 16) + " (" + String::chr(unicode) + ")";
508 } else if (keycode != Key::NONE) {
509 kc = itos((int64_t)keycode) + " (" + keycode_get_string(keycode) + ")";
510 } else if (physical_keycode != Key::NONE) {
511 kc = itos((int64_t)physical_keycode) + " (" + keycode_get_string(physical_keycode) + ")";
512 physical = "true";
513 } else {
514 kc = "(" + RTR("unset") + ")";
515 }
516
517 String mods = InputEventWithModifiers::as_text();
518 mods = mods.is_empty() ? "none" : mods;
519
520 return vformat("InputEventKey: keycode=%s, mods=%s, physical=%s, location=%s, pressed=%s, echo=%s", kc, mods, physical, loc, p, e);
521 }
522
523 Ref<InputEventKey> InputEventKey::create_reference(Key p_keycode, bool p_physical) {
524 Ref<InputEventKey> ie;
525 ie.instantiate();
526 if (p_physical) {
527 ie->set_physical_keycode(p_keycode & KeyModifierMask::CODE_MASK);
528 } else {
529 ie->set_keycode(p_keycode & KeyModifierMask::CODE_MASK);
530 }
531
532 char32_t ch = char32_t(p_keycode & KeyModifierMask::CODE_MASK);
533 if (ch < 0xd800 || (ch > 0xdfff && ch <= 0x10ffff)) {
534 ie->set_unicode(ch);
535 }
536
537 if ((p_keycode & KeyModifierMask::SHIFT) != Key::NONE) {
538 ie->set_shift_pressed(true);
539 }
540 if ((p_keycode & KeyModifierMask::ALT) != Key::NONE) {
541 ie->set_alt_pressed(true);
542 }
543 if ((p_keycode & KeyModifierMask::CMD_OR_CTRL) != Key::NONE) {
544 ie->set_command_or_control_autoremap(true);
545 if ((p_keycode & KeyModifierMask::CTRL) != Key::NONE || (p_keycode & KeyModifierMask::META) != Key::NONE) {
546 WARN_PRINT("Invalid Key Modifiers: Command or Control autoremapping is enabled, Meta and Control values are ignored!");
547 }
548 } else {
549 if ((p_keycode & KeyModifierMask::CTRL) != Key::NONE) {
550 ie->set_ctrl_pressed(true);
551 }
552 if ((p_keycode & KeyModifierMask::META) != Key::NONE) {
553 ie->set_meta_pressed(true);
554 }
555 }
556
557 return ie;
558 }
559
560 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 {
561 Ref<InputEventKey> key = p_event;
562 if (key.is_null()) {
563 return false;
564 }
565
566 bool match;
567 if (keycode == Key::NONE && physical_keycode == Key::NONE && key_label != Key::NONE) {
568 match = key_label == key->key_label;
569 } else if (keycode != Key::NONE) {
570 match = keycode == key->keycode;
571 } else if (physical_keycode != Key::NONE) {
572 match = physical_keycode == key->physical_keycode;
573 if (location != KeyLocation::UNSPECIFIED) {
574 match &= location == key->location;
575 }
576 } else {
577 match = false;
578 }
579
580 Key action_mask = (Key)(int64_t)get_modifiers_mask();
581 Key key_mask = (Key)(int64_t)key->get_modifiers_mask();
582 if (key->is_pressed()) {
583 match &= (action_mask & key_mask) == action_mask;
584 }
585 if (p_exact_match) {
586 match &= action_mask == key_mask;
587 }
588 if (match) {
589 bool key_pressed = key->is_pressed();
590 if (r_pressed != nullptr) {
591 *r_pressed = key_pressed;
592 }
593 float strength = key_pressed ? 1.0f : 0.0f;
594 if (r_strength != nullptr) {
595 *r_strength = strength;
596 }
597 if (r_raw_strength != nullptr) {
598 *r_raw_strength = strength;
599 }
600 }
601 return match;
602 }
603
604 bool InputEventKey::is_match(const Ref<InputEvent> &p_event, bool p_exact_match) const {
605 Ref<InputEventKey> key = p_event;
606 if (key.is_null()) {
607 return false;
608 }
609
610 if (keycode == Key::NONE && physical_keycode == Key::NONE && key_label != Key::NONE) {
611 return (key_label == key->key_label) &&
612 (!p_exact_match || get_modifiers_mask() == key->get_modifiers_mask());
613 } else if (keycode != Key::NONE) {
614 return (keycode == key->keycode) &&
615 (!p_exact_match || get_modifiers_mask() == key->get_modifiers_mask());
616 } else if (physical_keycode != Key::NONE) {
617 if (location != KeyLocation::UNSPECIFIED && location != key->location) {
618 return false;
619 }
620 return (physical_keycode == key->physical_keycode) &&
621 (!p_exact_match || get_modifiers_mask() == key->get_modifiers_mask());
622 } else {
623 return false;
624 }
625 }
626
627 void InputEventKey::_bind_methods() {
628 ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &InputEventKey::set_pressed);
629
630 ClassDB::bind_method(D_METHOD("set_keycode", "keycode"), &InputEventKey::set_keycode);
631 ClassDB::bind_method(D_METHOD("get_keycode"), &InputEventKey::get_keycode);
632
633 ClassDB::bind_method(D_METHOD("set_physical_keycode", "physical_keycode"), &InputEventKey::set_physical_keycode);
634 ClassDB::bind_method(D_METHOD("get_physical_keycode"), &InputEventKey::get_physical_keycode);
635
636 ClassDB::bind_method(D_METHOD("set_key_label", "key_label"), &InputEventKey::set_key_label);
637 ClassDB::bind_method(D_METHOD("get_key_label"), &InputEventKey::get_key_label);
638
639 ClassDB::bind_method(D_METHOD("set_unicode", "unicode"), &InputEventKey::set_unicode);
640 ClassDB::bind_method(D_METHOD("get_unicode"), &InputEventKey::get_unicode);
641
642 ClassDB::bind_method(D_METHOD("set_location", "location"), &InputEventKey::set_location);
643 ClassDB::bind_method(D_METHOD("get_location"), &InputEventKey::get_location);
644
645 ClassDB::bind_method(D_METHOD("set_echo", "echo"), &InputEventKey::set_echo);
646
647 ClassDB::bind_method(D_METHOD("get_keycode_with_modifiers"), &InputEventKey::get_keycode_with_modifiers);
648 ClassDB::bind_method(D_METHOD("get_physical_keycode_with_modifiers"), &InputEventKey::get_physical_keycode_with_modifiers);
649 ClassDB::bind_method(D_METHOD("get_key_label_with_modifiers"), &InputEventKey::get_key_label_with_modifiers);
650
651 ClassDB::bind_method(D_METHOD("as_text_keycode"), &InputEventKey::as_text_keycode);
652 ClassDB::bind_method(D_METHOD("as_text_physical_keycode"), &InputEventKey::as_text_physical_keycode);
653 ClassDB::bind_method(D_METHOD("as_text_key_label"), &InputEventKey::as_text_key_label);
654 ClassDB::bind_method(D_METHOD("as_text_location"), &InputEventKey::as_text_location);
655
656 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pressed"), "set_pressed", "is_pressed");
657 ADD_PROPERTY(PropertyInfo(Variant::INT, "keycode"), "set_keycode", "get_keycode");
658 ADD_PROPERTY(PropertyInfo(Variant::INT, "physical_keycode"), "set_physical_keycode", "get_physical_keycode");
659 ADD_PROPERTY(PropertyInfo(Variant::INT, "key_label"), "set_key_label", "get_key_label");
660 ADD_PROPERTY(PropertyInfo(Variant::INT, "unicode"), "set_unicode", "get_unicode");
661 ADD_PROPERTY(PropertyInfo(Variant::INT, "location", PROPERTY_HINT_ENUM, "Unspecified,Left,Right"), "set_location", "get_location");
662 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "echo"), "set_echo", "is_echo");
663 }
664
665 ///////////////////////////////////
666
667 void InputEventMouse::set_button_mask(BitField<MouseButtonMask> p_mask) {
668 button_mask = p_mask;
669 emit_changed();
670 }
671
672 BitField<MouseButtonMask> InputEventMouse::get_button_mask() const {
673 return button_mask;
674 }
675
676 void InputEventMouse::set_position(const Vector2 &p_pos) {
677 pos = p_pos;
678 }
679
680 Vector2 InputEventMouse::get_position() const {
681 return pos;
682 }
683
684 void InputEventMouse::set_global_position(const Vector2 &p_global_pos) {
685 global_pos = p_global_pos;
686 }
687
688 Vector2 InputEventMouse::get_global_position() const {
689 return global_pos;
690 }
691
692 void InputEventMouse::_bind_methods() {
693 ClassDB::bind_method(D_METHOD("set_button_mask", "button_mask"), &InputEventMouse::set_button_mask);
694 ClassDB::bind_method(D_METHOD("get_button_mask"), &InputEventMouse::get_button_mask);
695
696 ClassDB::bind_method(D_METHOD("set_position", "position"), &InputEventMouse::set_position);
697 ClassDB::bind_method(D_METHOD("get_position"), &InputEventMouse::get_position);
698
699 ClassDB::bind_method(D_METHOD("set_global_position", "global_position"), &InputEventMouse::set_global_position);
700 ClassDB::bind_method(D_METHOD("get_global_position"), &InputEventMouse::get_global_position);
701
702 ADD_PROPERTY(PropertyInfo(Variant::INT, "button_mask"), "set_button_mask", "get_button_mask");
703 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position", PROPERTY_HINT_NONE, "suffix:px"), "set_position", "get_position");
704 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "global_position", PROPERTY_HINT_NONE, "suffix:px"), "set_global_position", "get_global_position");
705 }
706
707 ///////////////////////////////////
708
709 void InputEventMouseButton::set_factor(float p_factor) {
710 factor = p_factor;
711 }
712
713 float InputEventMouseButton::get_factor() const {
714 return factor;
715 }
716
717 void InputEventMouseButton::set_button_index(MouseButton p_index) {
718 button_index = p_index;
719 emit_changed();
720 }
721
722 MouseButton InputEventMouseButton::get_button_index() const {
723 return button_index;
724 }
725
726 void InputEventMouseButton::set_pressed(bool p_pressed) {
727 pressed = p_pressed;
728 }
729
730 void InputEventMouseButton::set_canceled(bool p_canceled) {
731 canceled = p_canceled;
732 }
733
734 void InputEventMouseButton::set_double_click(bool p_double_click) {
735 double_click = p_double_click;
736 }
737
738 bool InputEventMouseButton::is_double_click() const {
739 return double_click;
740 }
741
742 RequiredResult<InputEvent> InputEventMouseButton::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const {
743 Vector2 g = get_global_position();
744 Vector2 l = p_xform.xform(get_position() + p_local_ofs);
745
746 Ref<InputEventMouseButton> mb;
747 mb.instantiate();
748
749 mb->set_device(get_device());
750 mb->set_window_id(get_window_id());
751 mb->set_modifiers_from_event(this);
752
753 mb->set_position(l);
754 mb->set_global_position(g);
755
756 mb->set_button_mask(get_button_mask());
757 mb->set_pressed(pressed);
758 mb->set_canceled(canceled);
759 mb->set_double_click(double_click);
760 mb->set_factor(factor);
761 mb->set_button_index(button_index);
762
763 mb->merge_meta_from(this);
764
765 return mb;
766 }
767
768 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 {
769 Ref<InputEventMouseButton> mb = p_event;
770 if (mb.is_null()) {
771 return false;
772 }
773
774 bool match = button_index == mb->button_index;
775 Key action_modifiers_mask = (Key)(int64_t)get_modifiers_mask();
776 Key button_modifiers_mask = (Key)(int64_t)mb->get_modifiers_mask();
777 if (mb->is_pressed()) {
778 match &= (action_modifiers_mask & button_modifiers_mask) == action_modifiers_mask;
779 }
780 if (p_exact_match) {
781 match &= action_modifiers_mask == button_modifiers_mask;
782 }
783 if (match) {
784 bool mb_pressed = mb->is_pressed();
785 if (r_pressed != nullptr) {
786 *r_pressed = mb_pressed;
787 }
788 float strength = mb_pressed ? 1.0f : 0.0f;
789 if (r_strength != nullptr) {
790 *r_strength = strength;
791 }
792 if (r_raw_strength != nullptr) {
793 *r_raw_strength = strength;
794 }
795 }
796
797 return match;
798 }
799
800 bool InputEventMouseButton::is_match(const Ref<InputEvent> &p_event, bool p_exact_match) const {
801 Ref<InputEventMouseButton> mb = p_event;
802 if (mb.is_null()) {
803 return false;
804 }
805
806 return button_index == mb->button_index &&
807 (!p_exact_match || get_modifiers_mask() == mb->get_modifiers_mask());
808 }
809
810 static const char *_mouse_button_descriptions[9] = {
811 TTRC("Left Mouse Button"),
812 TTRC("Right Mouse Button"),
813 TTRC("Middle Mouse Button"),
814 TTRC("Mouse Wheel Up"),
815 TTRC("Mouse Wheel Down"),
816 TTRC("Mouse Wheel Left"),
817 TTRC("Mouse Wheel Right"),
818 TTRC("Mouse Thumb Button 1"),
819 TTRC("Mouse Thumb Button 2")
820 };
821
822 String InputEventMouseButton::as_text() const {
823 // Modifiers
824 String mods_text = InputEventWithModifiers::as_text();
825 String full_string = mods_text.is_empty() ? "" : mods_text + "+";
826
827 // Button
828 MouseButton idx = get_button_index();
829 switch (idx) {
830 case MouseButton::LEFT:
831 case MouseButton::RIGHT:
832 case MouseButton::MIDDLE:
833 case MouseButton::WHEEL_UP:
834 case MouseButton::WHEEL_DOWN:
835 case MouseButton::WHEEL_LEFT:
836 case MouseButton::WHEEL_RIGHT:
837 case MouseButton::MB_XBUTTON1:
838 case MouseButton::MB_XBUTTON2:
839 full_string += RTR(_mouse_button_descriptions[(size_t)idx - 1]); // button index starts from 1, array index starts from 0, so subtract 1
840 break;
841 default:
842 full_string += RTR("Button") + " #" + itos((int64_t)idx);
843 break;
844 }
845
846 // Double Click
847 if (double_click) {
848 full_string += " (" + RTR("Double Click") + ")";
849 }
850
851 return full_string;
852 }
853
854 String InputEventMouseButton::_to_string() {
855 String p = is_pressed() ? "true" : "false";
856 String canceled_state = is_canceled() ? "true" : "false";
857 String d = double_click ? "true" : "false";
858
859 MouseButton idx = get_button_index();
860 String button_string = itos((int64_t)idx);
861
862 switch (idx) {
863 case MouseButton::LEFT:
864 case MouseButton::RIGHT:
865 case MouseButton::MIDDLE:
866 case MouseButton::WHEEL_UP:
867 case MouseButton::WHEEL_DOWN:
868 case MouseButton::WHEEL_LEFT:
869 case MouseButton::WHEEL_RIGHT:
870 case MouseButton::MB_XBUTTON1:
871 case MouseButton::MB_XBUTTON2:
872 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
873 break;
874 default:
875 break;
876 }
877
878 String mods = InputEventWithModifiers::as_text();
879 mods = mods.is_empty() ? "none" : mods;
880
881 // Work around the fact vformat can only take 5 substitutions but 6 need to be passed.
882 String index_and_mods = vformat("button_index=%s, mods=%s", button_index, mods);
883 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);
884 }
885
886 void InputEventMouseButton::_bind_methods() {
887 ClassDB::bind_method(D_METHOD("set_factor", "factor"), &InputEventMouseButton::set_factor);
888 ClassDB::bind_method(D_METHOD("get_factor"), &InputEventMouseButton::get_factor);
889
890 ClassDB::bind_method(D_METHOD("set_button_index", "button_index"), &InputEventMouseButton::set_button_index);
891 ClassDB::bind_method(D_METHOD("get_button_index"), &InputEventMouseButton::get_button_index);
892
893 ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &InputEventMouseButton::set_pressed);
894 ClassDB::bind_method(D_METHOD("set_canceled", "canceled"), &InputEventMouseButton::set_canceled);
895
896 ClassDB::bind_method(D_METHOD("set_double_click", "double_click"), &InputEventMouseButton::set_double_click);
897 ClassDB::bind_method(D_METHOD("is_double_click"), &InputEventMouseButton::is_double_click);
898
899 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "factor"), "set_factor", "get_factor");
900 ADD_PROPERTY(PropertyInfo(Variant::INT, "button_index"), "set_button_index", "get_button_index");
901 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "canceled"), "set_canceled", "is_canceled");
902 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pressed"), "set_pressed", "is_pressed");
903 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "double_click"), "set_double_click", "is_double_click");
904 }
905
906 ///////////////////////////////////
907
908 void InputEventMouseMotion::set_tilt(const Vector2 &p_tilt) {
909 tilt = p_tilt;
910 }
911
912 Vector2 InputEventMouseMotion::get_tilt() const {
913 return tilt;
914 }
915
916 void InputEventMouseMotion::set_pressure(float p_pressure) {
917 pressure = p_pressure;
918 }
919
920 float InputEventMouseMotion::get_pressure() const {
921 return pressure;
922 }
923
924 void InputEventMouseMotion::set_pen_inverted(bool p_inverted) {
925 pen_inverted = p_inverted;
926 }
927
928 bool InputEventMouseMotion::get_pen_inverted() const {
929 return pen_inverted;
930 }
931
932 void InputEventMouseMotion::set_relative(const Vector2 &p_relative) {
933 relative = p_relative;
934 }
935
936 Vector2 InputEventMouseMotion::get_relative() const {
937 return relative;
938 }
939
940 void InputEventMouseMotion::set_relative_screen_position(const Vector2 &p_relative) {
941 screen_relative = p_relative;
942 }
943
944 Vector2 InputEventMouseMotion::get_relative_screen_position() const {
945 return screen_relative;
946 }
947
948 void InputEventMouseMotion::set_velocity(const Vector2 &p_velocity) {
949 velocity = p_velocity;
950 }
951
952 Vector2 InputEventMouseMotion::get_velocity() const {
953 return velocity;
954 }
955
956 void InputEventMouseMotion::set_screen_velocity(const Vector2 &p_velocity) {
957 screen_velocity = p_velocity;
958 }
959
960 Vector2 InputEventMouseMotion::get_screen_velocity() const {
961 return screen_velocity;
962 }
963
964 RequiredResult<InputEvent> InputEventMouseMotion::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const {
965 Ref<InputEventMouseMotion> mm;
966 mm.instantiate();
967
968 mm->set_device(get_device());
969 mm->set_window_id(get_window_id());
970
971 mm->set_modifiers_from_event(this);
972
973 mm->set_position(p_xform.xform(get_position() + p_local_ofs));
974 mm->set_pressure(get_pressure());
975 mm->set_pen_inverted(get_pen_inverted());
976 mm->set_tilt(get_tilt());
977 mm->set_global_position(get_global_position());
978
979 mm->set_button_mask(get_button_mask());
980 mm->set_relative(p_xform.basis_xform(get_relative()));
981 mm->set_relative_screen_position(get_relative_screen_position());
982 mm->set_velocity(p_xform.basis_xform(get_velocity()));
983 mm->set_screen_velocity(get_screen_velocity());
984
985 mm->merge_meta_from(this);
986
987 return mm;
988 }
989
990 String InputEventMouseMotion::as_text() const {
991 return vformat(RTR("Mouse motion at position (%s) with velocity (%s)"), String(get_position()), String(get_velocity()));
992 }
993
994 String InputEventMouseMotion::_to_string() {
995 BitField<MouseButtonMask> mouse_button_mask = get_button_mask();
996 String button_mask_string = itos((int64_t)mouse_button_mask);
997
998 if (mouse_button_mask.has_flag(MouseButtonMask::LEFT)) {
999 button_mask_string += vformat(" (%s)", TTRGET(_mouse_button_descriptions[(size_t)MouseButton::LEFT - 1]));
1000 }
1001 if (mouse_button_mask.has_flag(MouseButtonMask::MIDDLE)) {
1002 button_mask_string += vformat(" (%s)", TTRGET(_mouse_button_descriptions[(size_t)MouseButton::MIDDLE - 1]));
1003 }
1004 if (mouse_button_mask.has_flag(MouseButtonMask::RIGHT)) {
1005 button_mask_string += vformat(" (%s)", TTRGET(_mouse_button_descriptions[(size_t)MouseButton::RIGHT - 1]));
1006 }
1007 if (mouse_button_mask.has_flag(MouseButtonMask::MB_XBUTTON1)) {
1008 button_mask_string += vformat(" (%s)", TTRGET(_mouse_button_descriptions[(size_t)MouseButton::MB_XBUTTON1 - 1]));
1009 }
1010 if (mouse_button_mask.has_flag(MouseButtonMask::MB_XBUTTON2)) {
1011 button_mask_string += vformat(" (%s)", TTRGET(_mouse_button_descriptions[(size_t)MouseButton::MB_XBUTTON2 - 1]));
1012 }
1013
1014 // Work around the fact vformat can only take 5 substitutions but 7 need to be passed.
1015 String mask_and_position_and_relative = vformat("button_mask=%s, position=(%s), relative=(%s)", button_mask_string, String(get_position()), String(get_relative()));
1016 return vformat("InputEventMouseMotion: %s, velocity=(%s), pressure=%.2f, tilt=(%s), pen_inverted=(%s)", mask_and_position_and_relative, String(get_velocity()), get_pressure(), String(get_tilt()), get_pen_inverted());
1017 }
1018
1019 bool InputEventMouseMotion::accumulate(const Ref<InputEvent> &p_event) {
1020 Ref<InputEventMouseMotion> motion = p_event;
1021 if (motion.is_null()) {
1022 return false;
1023 }
1024
1025 if (get_window_id() != motion->get_window_id()) {
1026 return false;
1027 }
1028
1029 if (is_canceled() != motion->is_canceled()) {
1030 return false;
1031 }
1032
1033 if (is_pressed() != motion->is_pressed()) {
1034 return false;
1035 }
1036
1037 if (get_button_mask() != motion->get_button_mask()) {
1038 return false;
1039 }
1040
1041 if (is_shift_pressed() != motion->is_shift_pressed()) {
1042 return false;
1043 }
1044
1045 if (is_ctrl_pressed() != motion->is_ctrl_pressed()) {
1046 return false;
1047 }
1048
1049 if (is_alt_pressed() != motion->is_alt_pressed()) {
1050 return false;
1051 }
1052
1053 if (is_meta_pressed() != motion->is_meta_pressed()) {
1054 return false;
1055 }
1056
1057 set_position(motion->get_position());
1058 set_global_position(motion->get_global_position());
1059 set_velocity(motion->get_velocity());
1060 set_screen_velocity(motion->get_screen_velocity());
1061 relative += motion->get_relative();
1062 screen_relative += motion->get_relative_screen_position();
1063
1064 return true;
1065 }
1066
1067 void InputEventMouseMotion::_bind_methods() {
1068 ClassDB::bind_method(D_METHOD("set_tilt", "tilt"), &InputEventMouseMotion::set_tilt);
1069 ClassDB::bind_method(D_METHOD("get_tilt"), &InputEventMouseMotion::get_tilt);
1070
1071 ClassDB::bind_method(D_METHOD("set_pressure", "pressure"), &InputEventMouseMotion::set_pressure);
1072 ClassDB::bind_method(D_METHOD("get_pressure"), &InputEventMouseMotion::get_pressure);
1073
1074 ClassDB::bind_method(D_METHOD("set_pen_inverted", "pen_inverted"), &InputEventMouseMotion::set_pen_inverted);
1075 ClassDB::bind_method(D_METHOD("get_pen_inverted"), &InputEventMouseMotion::get_pen_inverted);
1076
1077 ClassDB::bind_method(D_METHOD("set_relative", "relative"), &InputEventMouseMotion::set_relative);
1078 ClassDB::bind_method(D_METHOD("get_relative"), &InputEventMouseMotion::get_relative);
1079
1080 ClassDB::bind_method(D_METHOD("set_screen_relative", "relative"), &InputEventMouseMotion::set_relative_screen_position);
1081 ClassDB::bind_method(D_METHOD("get_screen_relative"), &InputEventMouseMotion::get_relative_screen_position);
1082
1083 ClassDB::bind_method(D_METHOD("set_velocity", "velocity"), &InputEventMouseMotion::set_velocity);
1084 ClassDB::bind_method(D_METHOD("get_velocity"), &InputEventMouseMotion::get_velocity);
1085
1086 ClassDB::bind_method(D_METHOD("set_screen_velocity", "velocity"), &InputEventMouseMotion::set_screen_velocity);
1087 ClassDB::bind_method(D_METHOD("get_screen_velocity"), &InputEventMouseMotion::get_screen_velocity);
1088
1089 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "tilt"), "set_tilt", "get_tilt");
1090 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "pressure"), "set_pressure", "get_pressure");
1091 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pen_inverted"), "set_pen_inverted", "get_pen_inverted");
1092 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "relative", PROPERTY_HINT_NONE, "suffix:px"), "set_relative", "get_relative");
1093 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "screen_relative", PROPERTY_HINT_NONE, "suffix:px"), "set_screen_relative", "get_screen_relative");
1094 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "velocity", PROPERTY_HINT_NONE, "suffix:px/s"), "set_velocity", "get_velocity");
1095 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "screen_velocity", PROPERTY_HINT_NONE, "suffix:px/s"), "set_screen_velocity", "get_screen_velocity");
1096 }
1097
1098 ///////////////////////////////////
1099
1100 void InputEventJoypadMotion::set_axis(JoyAxis p_axis) {
1101 ERR_FAIL_COND(p_axis < JoyAxis::INVALID || p_axis > JoyAxis::MAX);
1102
1103 axis = p_axis;
1104 emit_changed();
1105 }
1106
1107 JoyAxis InputEventJoypadMotion::get_axis() const {
1108 return axis;
1109 }
1110
1111 void InputEventJoypadMotion::set_axis_value(float p_value) {
1112 axis_value = p_value;
1113 pressed = Math::abs(axis_value) >= InputMap::DEFAULT_TOGGLE_DEADZONE;
1114 emit_changed();
1115 }
1116
1117 float InputEventJoypadMotion::get_axis_value() const {
1118 return axis_value;
1119 }
1120
1121 bool InputEventJoypadMotion::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 {
1122 Ref<InputEventJoypadMotion> jm = p_event;
1123 if (jm.is_null()) {
1124 return false;
1125 }
1126
1127 // Matches even if not in the same direction, but returns a "not pressed" event.
1128 bool match = axis == jm->axis;
1129 if (p_exact_match) {
1130 match &= (axis_value < 0) == (jm->axis_value < 0);
1131 }
1132 if (match) {
1133 float jm_abs_axis_value = Math::abs(jm->get_axis_value());
1134 bool same_direction = (((axis_value < 0) == (jm->axis_value < 0)) || jm->axis_value == 0);
1135 bool pressed_state = same_direction && jm_abs_axis_value >= p_deadzone;
1136 if (r_pressed != nullptr) {
1137 *r_pressed = pressed_state;
1138 }
1139 if (r_strength != nullptr) {
1140 if (pressed_state) {
1141 if (p_deadzone == 1.0f) {
1142 *r_strength = 1.0f;
1143 } else {
1144 *r_strength = CLAMP(Math::inverse_lerp(p_deadzone, 1.0f, jm_abs_axis_value), 0.0f, 1.0f);
1145 }
1146 } else {
1147 *r_strength = 0.0f;
1148 }
1149 }
1150 if (r_raw_strength != nullptr) {
1151 if (same_direction) { // NOT pressed, because we want to ignore the deadzone.
1152 *r_raw_strength = jm_abs_axis_value;
1153 } else {
1154 *r_raw_strength = 0.0f;
1155 }
1156 }
1157 }
1158 return match;
1159 }
1160
1161 bool InputEventJoypadMotion::is_match(const Ref<InputEvent> &p_event, bool p_exact_match) const {
1162 Ref<InputEventJoypadMotion> jm = p_event;
1163 if (jm.is_null()) {
1164 return false;
1165 }
1166
1167 return axis == jm->axis &&
1168 (!p_exact_match || ((axis_value < 0) == (jm->axis_value < 0)));
1169 }
1170
1171 static const char *_joy_axis_descriptions[(size_t)JoyAxis::MAX] = {
1172 TTRC("Left Stick X-Axis, Joystick 0 X-Axis"),
1173 TTRC("Left Stick Y-Axis, Joystick 0 Y-Axis"),
1174 TTRC("Right Stick X-Axis, Joystick 1 X-Axis"),
1175 TTRC("Right Stick Y-Axis, Joystick 1 Y-Axis"),
1176 TTRC("Joystick 2 X-Axis, Left Trigger, Sony L2, Xbox LT"),
1177 TTRC("Joystick 2 Y-Axis, Right Trigger, Sony R2, Xbox RT"),
1178 TTRC("Joystick 3 X-Axis"),
1179 TTRC("Joystick 3 Y-Axis"),
1180 TTRC("Joystick 4 X-Axis"),
1181 TTRC("Joystick 4 Y-Axis"),
1182 };
1183
1184 String InputEventJoypadMotion::as_text() const {
1185 String desc = axis < JoyAxis::MAX ? TTRGET(_joy_axis_descriptions[(size_t)axis]) : RTR("Unknown Joypad Axis");
1186
1187 return vformat(RTR("Joypad Motion on Axis %d (%s) with Value %.2f"), axis, desc, axis_value);
1188 }
1189
1190 String InputEventJoypadMotion::_to_string() {
1191 return vformat("InputEventJoypadMotion: axis=%d, axis_value=%.2f", axis, axis_value);
1192 }
1193
1194 Ref<InputEventJoypadMotion> InputEventJoypadMotion::create_reference(JoyAxis p_axis, float p_value, int p_device) {
1195 Ref<InputEventJoypadMotion> ie;
1196 ie.instantiate();
1197 ie->set_axis(p_axis);
1198 ie->set_axis_value(p_value);
1199 ie->set_device(p_device);
1200
1201 return ie;
1202 }
1203
1204 void InputEventJoypadMotion::_bind_methods() {
1205 ClassDB::bind_method(D_METHOD("set_axis", "axis"), &InputEventJoypadMotion::set_axis);
1206 ClassDB::bind_method(D_METHOD("get_axis"), &InputEventJoypadMotion::get_axis);
1207
1208 ClassDB::bind_method(D_METHOD("set_axis_value", "axis_value"), &InputEventJoypadMotion::set_axis_value);
1209 ClassDB::bind_method(D_METHOD("get_axis_value"), &InputEventJoypadMotion::get_axis_value);
1210
1211 ADD_PROPERTY(PropertyInfo(Variant::INT, "axis"), "set_axis", "get_axis");
1212 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "axis_value"), "set_axis_value", "get_axis_value");
1213 }
1214
1215 ///////////////////////////////////
1216
1217 void InputEventJoypadButton::set_button_index(JoyButton p_index) {
1218 button_index = p_index;
1219 emit_changed();
1220 }
1221
1222 JoyButton InputEventJoypadButton::get_button_index() const {
1223 return button_index;
1224 }
1225
1226 void InputEventJoypadButton::set_pressed(bool p_pressed) {
1227 pressed = p_pressed;
1228 }
1229
1230 void InputEventJoypadButton::set_pressure(float p_pressure) {
1231 pressure = p_pressure;
1232 }
1233
1234 float InputEventJoypadButton::get_pressure() const {
1235 return pressure;
1236 }
1237
1238 bool InputEventJoypadButton::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 {
1239 Ref<InputEventJoypadButton> jb = p_event;
1240 if (jb.is_null()) {
1241 return false;
1242 }
1243
1244 bool match = button_index == jb->button_index;
1245 if (match) {
1246 bool jb_pressed = jb->is_pressed();
1247 if (r_pressed != nullptr) {
1248 *r_pressed = jb_pressed;
1249 }
1250 float strength = jb_pressed ? 1.0f : 0.0f;
1251 if (r_strength != nullptr) {
1252 *r_strength = strength;
1253 }
1254 if (r_raw_strength != nullptr) {
1255 *r_raw_strength = strength;
1256 }
1257 }
1258
1259 return match;
1260 }
1261
1262 bool InputEventJoypadButton::is_match(const Ref<InputEvent> &p_event, bool p_exact_match) const {
1263 Ref<InputEventJoypadButton> button = p_event;
1264 if (button.is_null()) {
1265 return false;
1266 }
1267
1268 return button_index == button->button_index;
1269 }
1270
1271 static const char *_joy_button_descriptions[(size_t)JoyButton::SDL_MAX] = {
1272 TTRC("Bottom Action, Sony Cross, Xbox A, Nintendo B"),
1273 TTRC("Right Action, Sony Circle, Xbox B, Nintendo A"),
1274 TTRC("Left Action, Sony Square, Xbox X, Nintendo Y"),
1275 TTRC("Top Action, Sony Triangle, Xbox Y, Nintendo X"),
1276 TTRC("Back, Sony Select, Xbox Back, Nintendo -"),
1277 TTRC("Guide, Sony PS, Xbox Home"),
1278 TTRC("Start, Xbox Menu, Nintendo +"),
1279 TTRC("Left Stick, Sony L3, Xbox L/LS"),
1280 TTRC("Right Stick, Sony R3, Xbox R/RS"),
1281 TTRC("Left Shoulder, Sony L1, Xbox LB"),
1282 TTRC("Right Shoulder, Sony R1, Xbox RB"),
1283 TTRC("D-pad Up"),
1284 TTRC("D-pad Down"),
1285 TTRC("D-pad Left"),
1286 TTRC("D-pad Right"),
1287 TTRC("Xbox Share, PS5 Microphone, Nintendo Capture"),
1288 TTRC("Xbox Paddle 1"),
1289 TTRC("Xbox Paddle 2"),
1290 TTRC("Xbox Paddle 3"),
1291 TTRC("Xbox Paddle 4"),
1292 TTRC("PS4/5 Touchpad"),
1293 };
1294
1295 String InputEventJoypadButton::as_text() const {
1296 String text = vformat(RTR("Joypad Button %d"), (int64_t)button_index);
1297
1298 if (button_index > JoyButton::INVALID && button_index < JoyButton::SDL_MAX) {
1299 text += vformat(" (%s)", TTRGET(_joy_button_descriptions[(size_t)button_index]));
1300 }
1301
1302 if (pressure != 0) {
1303 text += ", " + RTR("Pressure:") + " " + String(Variant(pressure));
1304 }
1305
1306 return text;
1307 }
1308
1309 String InputEventJoypadButton::_to_string() {
1310 String p = is_pressed() ? "true" : "false";
1311 return vformat("InputEventJoypadButton: button_index=%d, pressed=%s, pressure=%.2f", button_index, p, pressure);
1312 }
1313
1314 Ref<InputEventJoypadButton> InputEventJoypadButton::create_reference(JoyButton p_btn_index, int p_device) {
1315 Ref<InputEventJoypadButton> ie;
1316 ie.instantiate();
1317 ie->set_button_index(p_btn_index);
1318 ie->set_device(p_device);
1319
1320 return ie;
1321 }
1322
1323 void InputEventJoypadButton::_bind_methods() {
1324 ClassDB::bind_method(D_METHOD("set_button_index", "button_index"), &InputEventJoypadButton::set_button_index);
1325 ClassDB::bind_method(D_METHOD("get_button_index"), &InputEventJoypadButton::get_button_index);
1326
1327 ClassDB::bind_method(D_METHOD("set_pressure", "pressure"), &InputEventJoypadButton::set_pressure);
1328 ClassDB::bind_method(D_METHOD("get_pressure"), &InputEventJoypadButton::get_pressure);
1329
1330 ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &InputEventJoypadButton::set_pressed);
1331
1332 ADD_PROPERTY(PropertyInfo(Variant::INT, "button_index"), "set_button_index", "get_button_index");
1333 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "pressure"), "set_pressure", "get_pressure");
1334 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pressed"), "set_pressed", "is_pressed");
1335 }
1336
1337 ///////////////////////////////////
1338
1339 void InputEventScreenTouch::set_index(int p_index) {
1340 index = p_index;
1341 }
1342
1343 int InputEventScreenTouch::get_index() const {
1344 return index;
1345 }
1346
1347 void InputEventScreenTouch::set_position(const Vector2 &p_pos) {
1348 pos = p_pos;
1349 }
1350
1351 Vector2 InputEventScreenTouch::get_position() const {
1352 return pos;
1353 }
1354
1355 void InputEventScreenTouch::set_pressed(bool p_pressed) {
1356 pressed = p_pressed;
1357 }
1358
1359 void InputEventScreenTouch::set_canceled(bool p_canceled) {
1360 canceled = p_canceled;
1361 }
1362
1363 void InputEventScreenTouch::set_double_tap(bool p_double_tap) {
1364 double_tap = p_double_tap;
1365 }
1366 bool InputEventScreenTouch::is_double_tap() const {
1367 return double_tap;
1368 }
1369
1370 RequiredResult<InputEvent> InputEventScreenTouch::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const {
1371 Ref<InputEventScreenTouch> st;
1372 st.instantiate();
1373 st->set_device(get_device());
1374 st->set_window_id(get_window_id());
1375 st->set_index(index);
1376 st->set_position(p_xform.xform(pos + p_local_ofs));
1377 st->set_pressed(pressed);
1378 st->set_canceled(canceled);
1379 st->set_double_tap(double_tap);
1380
1381 st->merge_meta_from(this);
1382
1383 return st;
1384 }
1385
1386 String InputEventScreenTouch::as_text() const {
1387 String status = canceled ? RTR("canceled") : (pressed ? RTR("touched") : RTR("released"));
1388
1389 return vformat(RTR("Screen %s at (%s) with %s touch points"), status, String(get_position()), itos(index));
1390 }
1391
1392 String InputEventScreenTouch::_to_string() {
1393 String p = pressed ? "true" : "false";
1394 String canceled_state = canceled ? "true" : "false";
1395 String double_tap_string = double_tap ? "true" : "false";
1396 return vformat("InputEventScreenTouch: index=%d, pressed=%s, canceled=%s, position=(%s), double_tap=%s", index, p, canceled_state, String(get_position()), double_tap_string);
1397 }
1398
1399 void InputEventScreenTouch::_bind_methods() {
1400 ClassDB::bind_method(D_METHOD("set_index", "index"), &InputEventScreenTouch::set_index);
1401 ClassDB::bind_method(D_METHOD("get_index"), &InputEventScreenTouch::get_index);
1402
1403 ClassDB::bind_method(D_METHOD("set_position", "position"), &InputEventScreenTouch::set_position);
1404 ClassDB::bind_method(D_METHOD("get_position"), &InputEventScreenTouch::get_position);
1405
1406 ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &InputEventScreenTouch::set_pressed);
1407 ClassDB::bind_method(D_METHOD("set_canceled", "canceled"), &InputEventScreenTouch::set_canceled);
1408
1409 ClassDB::bind_method(D_METHOD("set_double_tap", "double_tap"), &InputEventScreenTouch::set_double_tap);
1410 ClassDB::bind_method(D_METHOD("is_double_tap"), &InputEventScreenTouch::is_double_tap);
1411
1412 ADD_PROPERTY(PropertyInfo(Variant::INT, "index"), "set_index", "get_index");
1413 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position", PROPERTY_HINT_NONE, "suffix:px"), "set_position", "get_position");
1414 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "canceled"), "set_canceled", "is_canceled");
1415 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pressed"), "set_pressed", "is_pressed");
1416 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "double_tap"), "set_double_tap", "is_double_tap");
1417 }
1418
1419 ///////////////////////////////////
1420
1421 void InputEventScreenDrag::set_index(int p_index) {
1422 index = p_index;
1423 }
1424
1425 int InputEventScreenDrag::get_index() const {
1426 return index;
1427 }
1428
1429 void InputEventScreenDrag::set_tilt(const Vector2 &p_tilt) {
1430 tilt = p_tilt;
1431 }
1432
1433 Vector2 InputEventScreenDrag::get_tilt() const {
1434 return tilt;
1435 }
1436
1437 void InputEventScreenDrag::set_pressure(float p_pressure) {
1438 pressure = p_pressure;
1439 }
1440
1441 float InputEventScreenDrag::get_pressure() const {
1442 return pressure;
1443 }
1444
1445 void InputEventScreenDrag::set_pen_inverted(bool p_inverted) {
1446 pen_inverted = p_inverted;
1447 }
1448
1449 bool InputEventScreenDrag::get_pen_inverted() const {
1450 return pen_inverted;
1451 }
1452
1453 void InputEventScreenDrag::set_position(const Vector2 &p_pos) {
1454 pos = p_pos;
1455 }
1456
1457 Vector2 InputEventScreenDrag::get_position() const {
1458 return pos;
1459 }
1460
1461 void InputEventScreenDrag::set_relative(const Vector2 &p_relative) {
1462 relative = p_relative;
1463 }
1464
1465 Vector2 InputEventScreenDrag::get_relative() const {
1466 return relative;
1467 }
1468
1469 void InputEventScreenDrag::set_relative_screen_position(const Vector2 &p_relative) {
1470 screen_relative = p_relative;
1471 }
1472
1473 Vector2 InputEventScreenDrag::get_relative_screen_position() const {
1474 return screen_relative;
1475 }
1476
1477 void InputEventScreenDrag::set_velocity(const Vector2 &p_velocity) {
1478 velocity = p_velocity;
1479 }
1480
1481 Vector2 InputEventScreenDrag::get_velocity() const {
1482 return velocity;
1483 }
1484
1485 void InputEventScreenDrag::set_screen_velocity(const Vector2 &p_velocity) {
1486 screen_velocity = p_velocity;
1487 }
1488
1489 Vector2 InputEventScreenDrag::get_screen_velocity() const {
1490 return screen_velocity;
1491 }
1492
1493 RequiredResult<InputEvent> InputEventScreenDrag::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const {
1494 Ref<InputEventScreenDrag> sd;
1495
1496 sd.instantiate();
1497
1498 sd->set_device(get_device());
1499 sd->set_window_id(get_window_id());
1500
1501 sd->set_index(index);
1502 sd->set_pressure(get_pressure());
1503 sd->set_pen_inverted(get_pen_inverted());
1504 sd->set_tilt(get_tilt());
1505 sd->set_position(p_xform.xform(pos + p_local_ofs));
1506 sd->set_relative(p_xform.basis_xform(relative));
1507 sd->set_relative_screen_position(get_relative_screen_position());
1508 sd->set_velocity(p_xform.basis_xform(velocity));
1509 sd->set_screen_velocity(get_screen_velocity());
1510
1511 sd->merge_meta_from(this);
1512
1513 return sd;
1514 }
1515
1516 String InputEventScreenDrag::as_text() const {
1517 return vformat(RTR("Screen dragged with %s touch points at position (%s) with velocity of (%s)"), itos(index), String(get_position()), String(get_velocity()));
1518 }
1519
1520 String InputEventScreenDrag::_to_string() {
1521 return vformat("InputEventScreenDrag: index=%d, position=(%s), relative=(%s), velocity=(%s), pressure=%.2f, tilt=(%s), pen_inverted=(%s)", index, String(get_position()), String(get_relative()), String(get_velocity()), get_pressure(), String(get_tilt()), get_pen_inverted());
1522 }
1523
1524 bool InputEventScreenDrag::accumulate(const Ref<InputEvent> &p_event) {
1525 Ref<InputEventScreenDrag> drag = p_event;
1526 if (drag.is_null()) {
1527 return false;
1528 }
1529
1530 if (get_index() != drag->get_index()) {
1531 return false;
1532 }
1533
1534 set_position(drag->get_position());
1535 set_velocity(drag->get_velocity());
1536 set_screen_velocity(drag->get_screen_velocity());
1537 relative += drag->get_relative();
1538 screen_relative += drag->get_relative_screen_position();
1539
1540 return true;
1541 }
1542
1543 void InputEventScreenDrag::_bind_methods() {
1544 ClassDB::bind_method(D_METHOD("set_index", "index"), &InputEventScreenDrag::set_index);
1545 ClassDB::bind_method(D_METHOD("get_index"), &InputEventScreenDrag::get_index);
1546
1547 ClassDB::bind_method(D_METHOD("set_tilt", "tilt"), &InputEventScreenDrag::set_tilt);
1548 ClassDB::bind_method(D_METHOD("get_tilt"), &InputEventScreenDrag::get_tilt);
1549
1550 ClassDB::bind_method(D_METHOD("set_pressure", "pressure"), &InputEventScreenDrag::set_pressure);
1551 ClassDB::bind_method(D_METHOD("get_pressure"), &InputEventScreenDrag::get_pressure);
1552
1553 ClassDB::bind_method(D_METHOD("set_pen_inverted", "pen_inverted"), &InputEventScreenDrag::set_pen_inverted);
1554 ClassDB::bind_method(D_METHOD("get_pen_inverted"), &InputEventScreenDrag::get_pen_inverted);
1555
1556 ClassDB::bind_method(D_METHOD("set_position", "position"), &InputEventScreenDrag::set_position);
1557 ClassDB::bind_method(D_METHOD("get_position"), &InputEventScreenDrag::get_position);
1558
1559 ClassDB::bind_method(D_METHOD("set_relative", "relative"), &InputEventScreenDrag::set_relative);
1560 ClassDB::bind_method(D_METHOD("get_relative"), &InputEventScreenDrag::get_relative);
1561
1562 ClassDB::bind_method(D_METHOD("set_screen_relative", "relative"), &InputEventScreenDrag::set_relative_screen_position);
1563 ClassDB::bind_method(D_METHOD("get_screen_relative"), &InputEventScreenDrag::get_relative_screen_position);
1564
1565 ClassDB::bind_method(D_METHOD("set_velocity", "velocity"), &InputEventScreenDrag::set_velocity);
1566 ClassDB::bind_method(D_METHOD("get_velocity"), &InputEventScreenDrag::get_velocity);
1567
1568 ClassDB::bind_method(D_METHOD("set_screen_velocity", "velocity"), &InputEventScreenDrag::set_screen_velocity);
1569 ClassDB::bind_method(D_METHOD("get_screen_velocity"), &InputEventScreenDrag::get_screen_velocity);
1570
1571 ADD_PROPERTY(PropertyInfo(Variant::INT, "index"), "set_index", "get_index");
1572 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "tilt"), "set_tilt", "get_tilt");
1573 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "pressure"), "set_pressure", "get_pressure");
1574 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pen_inverted"), "set_pen_inverted", "get_pen_inverted");
1575 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position", PROPERTY_HINT_NONE, "suffix:px"), "set_position", "get_position");
1576 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "relative", PROPERTY_HINT_NONE, "suffix:px"), "set_relative", "get_relative");
1577 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "screen_relative", PROPERTY_HINT_NONE, "suffix:px"), "set_screen_relative", "get_screen_relative");
1578 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "velocity", PROPERTY_HINT_NONE, "suffix:px/s"), "set_velocity", "get_velocity");
1579 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "screen_velocity", PROPERTY_HINT_NONE, "suffix:px/s"), "set_screen_velocity", "get_screen_velocity");
1580 }
1581
1582 ///////////////////////////////////
1583
1584 void InputEventAction::set_action(const StringName &p_action) {
1585 action = p_action;
1586 }
1587
1588 StringName InputEventAction::get_action() const {
1589 return action;
1590 }
1591
1592 void InputEventAction::set_pressed(bool p_pressed) {
1593 pressed = p_pressed;
1594 }
1595
1596 void InputEventAction::set_strength(float p_strength) {
1597 strength = CLAMP(p_strength, 0.0f, 1.0f);
1598 }
1599
1600 float InputEventAction::get_strength() const {
1601 return strength;
1602 }
1603
1604 void InputEventAction::set_event_index(int p_index) {
1605 event_index = p_index;
1606 }
1607
1608 int InputEventAction::get_event_index() const {
1609 return event_index;
1610 }
1611
1612 bool InputEventAction::is_match(const Ref<InputEvent> &p_event, bool p_exact_match) const {
1613 if (p_event.is_null()) {
1614 return false;
1615 }
1616
1617 return p_event->is_action(action, p_exact_match);
1618 }
1619
1620 bool InputEventAction::is_action(const StringName &p_action) const {
1621 return action == p_action;
1622 }
1623
1624 bool InputEventAction::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 {
1625 Ref<InputEventAction> act = p_event;
1626 if (act.is_null()) {
1627 return false;
1628 }
1629
1630 bool match = action == act->action;
1631 if (match) {
1632 bool act_pressed = act->is_pressed();
1633 if (r_pressed != nullptr) {
1634 *r_pressed = act_pressed;
1635 }
1636 float act_strength = act_pressed ? 1.0f : 0.0f;
1637 if (r_strength != nullptr) {
1638 *r_strength = act_strength;
1639 }
1640 if (r_raw_strength != nullptr) {
1641 *r_raw_strength = act_strength;
1642 }
1643 }
1644 return match;
1645 }
1646
1647 String InputEventAction::as_text() const {
1648 const List<Ref<InputEvent>> *events = InputMap::get_singleton()->action_get_events(action);
1649 if (!events) {
1650 return String();
1651 }
1652
1653 for (const Ref<InputEvent> &E : *events) {
1654 if (E.is_valid()) {
1655 return E->as_text();
1656 }
1657 }
1658
1659 return String();
1660 }
1661
1662 String InputEventAction::_to_string() {
1663 String p = is_pressed() ? "true" : "false";
1664 return vformat("InputEventAction: action=\"%s\", pressed=%s", action, p);
1665 }
1666
1667 void InputEventAction::_bind_methods() {
1668 ClassDB::bind_method(D_METHOD("set_action", "action"), &InputEventAction::set_action);
1669 ClassDB::bind_method(D_METHOD("get_action"), &InputEventAction::get_action);
1670
1671 ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &InputEventAction::set_pressed);
1672
1673 ClassDB::bind_method(D_METHOD("set_strength", "strength"), &InputEventAction::set_strength);
1674 ClassDB::bind_method(D_METHOD("get_strength"), &InputEventAction::get_strength);
1675
1676 ClassDB::bind_method(D_METHOD("set_event_index", "index"), &InputEventAction::set_event_index);
1677 ClassDB::bind_method(D_METHOD("get_event_index"), &InputEventAction::get_event_index);
1678
1679 ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "action", PROPERTY_HINT_INPUT_NAME, "show_builtin,loose_mode"), "set_action", "get_action");
1680 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pressed"), "set_pressed", "is_pressed");
1681 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "strength", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_strength", "get_strength");
1682 ADD_PROPERTY(PropertyInfo(Variant::INT, "event_index", PROPERTY_HINT_RANGE, "-1,31,1"), "set_event_index", "get_event_index"); // The max value equals to Input::MAX_EVENT - 1.
1683 }
1684
1685 ///////////////////////////////////
1686
1687 void InputEventGesture::set_position(const Vector2 &p_pos) {
1688 pos = p_pos;
1689 }
1690
1691 void InputEventGesture::_bind_methods() {
1692 ClassDB::bind_method(D_METHOD("set_position", "position"), &InputEventGesture::set_position);
1693 ClassDB::bind_method(D_METHOD("get_position"), &InputEventGesture::get_position);
1694
1695 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position", PROPERTY_HINT_NONE, "suffix:px"), "set_position", "get_position");
1696 }
1697
1698 Vector2 InputEventGesture::get_position() const {
1699 return pos;
1700 }
1701
1702 ///////////////////////////////////
1703
1704 void InputEventMagnifyGesture::set_factor(real_t p_factor) {
1705 factor = p_factor;
1706 }
1707
1708 real_t InputEventMagnifyGesture::get_factor() const {
1709 return factor;
1710 }
1711
1712 RequiredResult<InputEvent> InputEventMagnifyGesture::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const {
1713 Ref<InputEventMagnifyGesture> ev;
1714 ev.instantiate();
1715
1716 ev->set_device(get_device());
1717 ev->set_window_id(get_window_id());
1718
1719 ev->set_modifiers_from_event(this);
1720
1721 ev->set_position(p_xform.xform(get_position() + p_local_ofs));
1722 ev->set_factor(get_factor());
1723
1724 ev->merge_meta_from(this);
1725
1726 return ev;
1727 }
1728
1729 String InputEventMagnifyGesture::as_text() const {
1730 return vformat(RTR("Magnify Gesture at (%s) with factor %s"), String(get_position()), rtos(get_factor()));
1731 }
1732
1733 String InputEventMagnifyGesture::_to_string() {
1734 return vformat("InputEventMagnifyGesture: factor=%.2f, position=(%s)", factor, String(get_position()));
1735 }
1736
1737 void InputEventMagnifyGesture::_bind_methods() {
1738 ClassDB::bind_method(D_METHOD("set_factor", "factor"), &InputEventMagnifyGesture::set_factor);
1739 ClassDB::bind_method(D_METHOD("get_factor"), &InputEventMagnifyGesture::get_factor);
1740
1741 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "factor"), "set_factor", "get_factor");
1742 }
1743
1744 ///////////////////////////////////
1745
1746 void InputEventPanGesture::set_delta(const Vector2 &p_delta) {
1747 delta = p_delta;
1748 }
1749
1750 Vector2 InputEventPanGesture::get_delta() const {
1751 return delta;
1752 }
1753
1754 RequiredResult<InputEvent> InputEventPanGesture::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const {
1755 Ref<InputEventPanGesture> ev;
1756 ev.instantiate();
1757
1758 ev->set_device(get_device());
1759 ev->set_window_id(get_window_id());
1760
1761 ev->set_modifiers_from_event(this);
1762
1763 ev->set_position(p_xform.xform(get_position() + p_local_ofs));
1764 ev->set_delta(get_delta());
1765
1766 ev->merge_meta_from(this);
1767
1768 return ev;
1769 }
1770
1771 String InputEventPanGesture::as_text() const {
1772 return vformat(RTR("Pan Gesture at (%s) with delta (%s)"), String(get_position()), String(get_delta()));
1773 }
1774
1775 String InputEventPanGesture::_to_string() {
1776 return vformat("InputEventPanGesture: delta=(%s), position=(%s)", String(get_delta()), String(get_position()));
1777 }
1778
1779 void InputEventPanGesture::_bind_methods() {
1780 ClassDB::bind_method(D_METHOD("set_delta", "delta"), &InputEventPanGesture::set_delta);
1781 ClassDB::bind_method(D_METHOD("get_delta"), &InputEventPanGesture::get_delta);
1782
1783 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "delta"), "set_delta", "get_delta");
1784 }
1785
1786 ///////////////////////////////////
1787
1788 void InputEventMIDI::set_channel(const int p_channel) {
1789 channel = p_channel;
1790 }
1791
1792 int InputEventMIDI::get_channel() const {
1793 return channel;
1794 }
1795
1796 void InputEventMIDI::set_message(const MIDIMessage p_message) {
1797 message = p_message;
1798 }
1799
1800 MIDIMessage InputEventMIDI::get_message() const {
1801 return message;
1802 }
1803
1804 void InputEventMIDI::set_pitch(const int p_pitch) {
1805 pitch = p_pitch;
1806 }
1807
1808 int InputEventMIDI::get_pitch() const {
1809 return pitch;
1810 }
1811
1812 void InputEventMIDI::set_velocity(const int p_velocity) {
1813 velocity = p_velocity;
1814 }
1815
1816 int InputEventMIDI::get_velocity() const {
1817 return velocity;
1818 }
1819
1820 void InputEventMIDI::set_instrument(const int p_instrument) {
1821 instrument = p_instrument;
1822 }
1823
1824 int InputEventMIDI::get_instrument() const {
1825 return instrument;
1826 }
1827
1828 void InputEventMIDI::set_pressure(const int p_pressure) {
1829 pressure = p_pressure;
1830 }
1831
1832 int InputEventMIDI::get_pressure() const {
1833 return pressure;
1834 }
1835
1836 void InputEventMIDI::set_controller_number(const int p_controller_number) {
1837 controller_number = p_controller_number;
1838 }
1839
1840 int InputEventMIDI::get_controller_number() const {
1841 return controller_number;
1842 }
1843
1844 void InputEventMIDI::set_controller_value(const int p_controller_value) {
1845 controller_value = p_controller_value;
1846 }
1847
1848 int InputEventMIDI::get_controller_value() const {
1849 return controller_value;
1850 }
1851
1852 String InputEventMIDI::as_text() const {
1853 return vformat(RTR("MIDI Input on Channel=%s Message=%s"), itos(channel), itos((int64_t)message));
1854 }
1855
1856 String InputEventMIDI::_to_string() {
1857 String ret;
1858 switch (message) {
1859 case MIDIMessage::NOTE_ON:
1860 ret = vformat("Note On: channel=%d, pitch=%d, velocity=%d", channel, pitch, velocity);
1861 break;
1862 case MIDIMessage::NOTE_OFF:
1863 ret = vformat("Note Off: channel=%d, pitch=%d, velocity=%d", channel, pitch, velocity);
1864 break;
1865 case MIDIMessage::PITCH_BEND:
1866 ret = vformat("Pitch Bend: channel=%d, pitch=%d", channel, pitch);
1867 break;
1868 case MIDIMessage::CHANNEL_PRESSURE:
1869 ret = vformat("Channel Pressure: channel=%d, pressure=%d", channel, pressure);
1870 break;
1871 case MIDIMessage::CONTROL_CHANGE:
1872 ret = vformat("Control Change: channel=%d, controller_number=%d, controller_value=%d", channel, controller_number, controller_value);
1873 break;
1874 default:
1875 ret = vformat("channel=%d, message=%d, pitch=%d, velocity=%d, pressure=%d, controller_number=%d, controller_value=%d, instrument=%d", channel, message, pitch, velocity, pressure, controller_number, controller_value, instrument);
1876 }
1877 return "InputEventMIDI: " + ret;
1878 }
1879
1880 void InputEventMIDI::_bind_methods() {
1881 ClassDB::bind_method(D_METHOD("set_channel", "channel"), &InputEventMIDI::set_channel);
1882 ClassDB::bind_method(D_METHOD("get_channel"), &InputEventMIDI::get_channel);
1883 ClassDB::bind_method(D_METHOD("set_message", "message"), &InputEventMIDI::set_message);
1884 ClassDB::bind_method(D_METHOD("get_message"), &InputEventMIDI::get_message);
1885 ClassDB::bind_method(D_METHOD("set_pitch", "pitch"), &InputEventMIDI::set_pitch);
1886 ClassDB::bind_method(D_METHOD("get_pitch"), &InputEventMIDI::get_pitch);
1887 ClassDB::bind_method(D_METHOD("set_velocity", "velocity"), &InputEventMIDI::set_velocity);
1888 ClassDB::bind_method(D_METHOD("get_velocity"), &InputEventMIDI::get_velocity);
1889 ClassDB::bind_method(D_METHOD("set_instrument", "instrument"), &InputEventMIDI::set_instrument);
1890 ClassDB::bind_method(D_METHOD("get_instrument"), &InputEventMIDI::get_instrument);
1891 ClassDB::bind_method(D_METHOD("set_pressure", "pressure"), &InputEventMIDI::set_pressure);
1892 ClassDB::bind_method(D_METHOD("get_pressure"), &InputEventMIDI::get_pressure);
1893 ClassDB::bind_method(D_METHOD("set_controller_number", "controller_number"), &InputEventMIDI::set_controller_number);
1894 ClassDB::bind_method(D_METHOD("get_controller_number"), &InputEventMIDI::get_controller_number);
1895 ClassDB::bind_method(D_METHOD("set_controller_value", "controller_value"), &InputEventMIDI::set_controller_value);
1896 ClassDB::bind_method(D_METHOD("get_controller_value"), &InputEventMIDI::get_controller_value);
1897
1898 ADD_PROPERTY(PropertyInfo(Variant::INT, "channel"), "set_channel", "get_channel");
1899 ADD_PROPERTY(PropertyInfo(Variant::INT, "message"), "set_message", "get_message");
1900 ADD_PROPERTY(PropertyInfo(Variant::INT, "pitch"), "set_pitch", "get_pitch");
1901 ADD_PROPERTY(PropertyInfo(Variant::INT, "velocity"), "set_velocity", "get_velocity");
1902 ADD_PROPERTY(PropertyInfo(Variant::INT, "instrument"), "set_instrument", "get_instrument");
1903 ADD_PROPERTY(PropertyInfo(Variant::INT, "pressure"), "set_pressure", "get_pressure");
1904 ADD_PROPERTY(PropertyInfo(Variant::INT, "controller_number"), "set_controller_number", "get_controller_number");
1905 ADD_PROPERTY(PropertyInfo(Variant::INT, "controller_value"), "set_controller_value", "get_controller_value");
1906 }
1907
1908 ///////////////////////////////////
1909
1910 void InputEventShortcut::set_shortcut(Ref<Shortcut> p_shortcut) {
1911 shortcut = p_shortcut;
1912 emit_changed();
1913 }
1914
1915 Ref<Shortcut> InputEventShortcut::get_shortcut() {
1916 return shortcut;
1917 }
1918
1919 void InputEventShortcut::_bind_methods() {
1920 ClassDB::bind_method(D_METHOD("set_shortcut", "shortcut"), &InputEventShortcut::set_shortcut);
1921 ClassDB::bind_method(D_METHOD("get_shortcut"), &InputEventShortcut::get_shortcut);
1922
1923 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shortcut", PROPERTY_HINT_RESOURCE_TYPE, "Shortcut"), "set_shortcut", "get_shortcut");
1924 }
1925
1926 String InputEventShortcut::as_text() const {
1927 ERR_FAIL_COND_V(shortcut.is_null(), "None");
1928
1929 return vformat(RTR("Input Event with Shortcut=%s"), shortcut->get_as_text());
1930 }
1931
1932 String InputEventShortcut::_to_string() {
1933 ERR_FAIL_COND_V(shortcut.is_null(), "None");
1934
1935 return vformat("InputEventShortcut: shortcut=%s", shortcut->get_as_text());
1936 }
1937
1938 InputEventShortcut::InputEventShortcut() {
1939 pressed = true;
1940 }