30
30
31
31
#include " input_event.h"
32
32
33
+ #include " core/config/project_settings.h"
34
+ #include " core/input/input.h"
33
35
#include " core/input/input_map.h"
34
36
#include " core/input/shortcut.h"
35
37
#include " core/os/keyboard.h"
@@ -47,29 +49,98 @@ int InputEvent::get_device() const {
47
49
return device;
48
50
}
49
51
52
+ void InputEvent::set_player (PlayerId p_player) {
53
+ player = p_player;
54
+ emit_changed ();
55
+ }
56
+
57
+ PlayerId InputEvent::get_player () const {
58
+ return player;
59
+ }
60
+
61
+ void InputEvent::set_player_from_device () {
62
+ // ProjectSettings *ps = ProjectSettings::get_singleton();
63
+ Ref<InputEvent> event = Ref<InputEvent>(this );
64
+
65
+ // Keyboard events.
66
+
67
+ Ref<InputEventKey> k = event;
68
+ if (k.is_valid ()) {
69
+ player = (PlayerId)(GLOBAL_GET (" input/keyboard_player_id_override" ).operator int ());
70
+ return ;
71
+ }
72
+
73
+ // Mouse events.
74
+
75
+ Ref<InputEventMouseButton> mb = event;
76
+ Ref<InputEventMouseMotion> mm = event;
77
+ if (mb.is_valid () || mm.is_valid ()) {
78
+ player = (PlayerId)(GLOBAL_GET (" input/mouse_player_id_override" ).operator int ());
79
+ return ;
80
+ }
81
+
82
+ // Joypad events.
83
+
84
+ Ref<InputEventJoypadButton> jb = event;
85
+ Ref<InputEventJoypadMotion> jm = event;
86
+ if (jb.is_valid () || jm.is_valid ()) {
87
+ Input *input = Input::get_singleton ();
88
+ HashMap<int , Input::Joypad>::Iterator E = input->_get_joy_names ().find (device);
89
+
90
+ if (!E) {
91
+ player = PlayerId::P1;
92
+ return ;
93
+ }
94
+
95
+ player = E->value .player_id ;
96
+ }
97
+
98
+ // Touch events.
99
+
100
+ Ref<InputEventScreenTouch> st = event;
101
+ Ref<InputEventScreenDrag> sd = event;
102
+ Ref<InputEventGesture> ge = event;
103
+ if (st.is_valid () || sd.is_valid () || ge.is_valid ()) {
104
+ player = (PlayerId)(GLOBAL_GET (" input/touch_player_id_override" ).operator int ());
105
+ return ;
106
+ }
107
+ }
108
+
50
109
bool InputEvent::is_action (const StringName &p_action, bool p_exact_match) const {
51
110
return InputMap::get_singleton ()->event_is_action (Ref<InputEvent>(const_cast <InputEvent *>(this )), p_action, p_exact_match);
52
111
}
53
112
54
- bool InputEvent::is_action_pressed (const StringName &p_action, bool p_allow_echo, bool p_exact_match) const {
113
+ bool InputEvent::is_action_pressed (const StringName &p_action, bool p_allow_echo, bool p_exact_match, PlayerId p_player_id) const {
114
+ if (player != p_player_id) {
115
+ return false ;
116
+ }
55
117
bool pressed_state;
56
118
bool valid = InputMap::get_singleton ()->event_get_action_status (Ref<InputEvent>(const_cast <InputEvent *>(this )), p_action, p_exact_match, &pressed_state, nullptr , nullptr );
57
119
return valid && pressed_state && (p_allow_echo || !is_echo ());
58
120
}
59
121
60
- bool InputEvent::is_action_released (const StringName &p_action, bool p_exact_match) const {
122
+ bool InputEvent::is_action_released (const StringName &p_action, bool p_exact_match, PlayerId p_player_id) const {
123
+ if (player != p_player_id) {
124
+ return false ;
125
+ }
61
126
bool pressed_state;
62
127
bool valid = InputMap::get_singleton ()->event_get_action_status (Ref<InputEvent>(const_cast <InputEvent *>(this )), p_action, p_exact_match, &pressed_state, nullptr , nullptr );
63
128
return valid && !pressed_state;
64
129
}
65
130
66
- float InputEvent::get_action_strength (const StringName &p_action, bool p_exact_match) const {
131
+ float InputEvent::get_action_strength (const StringName &p_action, bool p_exact_match, PlayerId p_player_id) const {
132
+ if (player != p_player_id) {
133
+ return 0 .0f ;
134
+ }
67
135
float strength;
68
136
bool valid = InputMap::get_singleton ()->event_get_action_status (Ref<InputEvent>(const_cast <InputEvent *>(this )), p_action, p_exact_match, nullptr , &strength, nullptr );
69
137
return valid ? strength : 0 .0f ;
70
138
}
71
139
72
- float InputEvent::get_action_raw_strength (const StringName &p_action, bool p_exact_match) const {
140
+ float InputEvent::get_action_raw_strength (const StringName &p_action, bool p_exact_match, PlayerId p_player_id) const {
141
+ if (player != p_player_id) {
142
+ return 0 .0f ;
143
+ }
73
144
float raw_strength;
74
145
bool valid = InputMap::get_singleton ()->event_get_action_status (Ref<InputEvent>(const_cast <InputEvent *>(this )), p_action, p_exact_match, nullptr , nullptr , &raw_strength);
75
146
return valid ? raw_strength : 0 .0f ;
@@ -111,10 +182,14 @@ void InputEvent::_bind_methods() {
111
182
ClassDB::bind_method (D_METHOD (" set_device" , " device" ), &InputEvent::set_device);
112
183
ClassDB::bind_method (D_METHOD (" get_device" ), &InputEvent::get_device);
113
184
185
+ ClassDB::bind_method (D_METHOD (" set_player" , " player" ), &InputEvent::set_player);
186
+ ClassDB::bind_method (D_METHOD (" get_player" ), &InputEvent::get_player);
187
+ ClassDB::bind_method (D_METHOD (" set_player_from_device" ), &InputEvent::set_player_from_device);
188
+
114
189
ClassDB::bind_method (D_METHOD (" is_action" , " action" , " exact_match" ), &InputEvent::is_action, DEFVAL (false ));
115
- ClassDB::bind_method (D_METHOD (" is_action_pressed" , " action" , " allow_echo" , " exact_match" ), &InputEvent::is_action_pressed, DEFVAL (false ), DEFVAL (false ));
116
- ClassDB::bind_method (D_METHOD (" is_action_released" , " action" , " exact_match" ), &InputEvent::is_action_released, DEFVAL (false ));
117
- ClassDB::bind_method (D_METHOD (" get_action_strength" , " action" , " exact_match" ), &InputEvent::get_action_strength, DEFVAL (false ));
190
+ ClassDB::bind_method (D_METHOD (" is_action_pressed" , " action" , " allow_echo" , " exact_match" , " player_id " ), &InputEvent::is_action_pressed, DEFVAL (false ), DEFVAL (false ), DEFVAL (PlayerId::P1 ));
191
+ ClassDB::bind_method (D_METHOD (" is_action_released" , " action" , " exact_match" , " player_id " ), &InputEvent::is_action_released, DEFVAL (false ), DEFVAL (PlayerId::P1 ));
192
+ ClassDB::bind_method (D_METHOD (" get_action_strength" , " action" , " exact_match" , " player_id " ), &InputEvent::get_action_strength, DEFVAL (false ), DEFVAL (PlayerId::P1 ));
118
193
119
194
ClassDB::bind_method (D_METHOD (" is_canceled" ), &InputEvent::is_canceled);
120
195
ClassDB::bind_method (D_METHOD (" is_pressed" ), &InputEvent::is_pressed);
@@ -132,6 +207,7 @@ void InputEvent::_bind_methods() {
132
207
ClassDB::bind_method (D_METHOD (" xformed_by" , " xform" , " local_ofs" ), &InputEvent::xformed_by, DEFVAL (Vector2 ()));
133
208
134
209
ADD_PROPERTY (PropertyInfo (Variant::INT, " device" ), " set_device" , " get_device" );
210
+ ADD_PROPERTY (PropertyInfo (Variant::INT, " player" ), " set_player" , " get_player" );
135
211
136
212
BIND_CONSTANT (DEVICE_ID_EMULATION);
137
213
}
@@ -741,6 +817,7 @@ Ref<InputEvent> InputEventMouseButton::xformed_by(const Transform2D &p_xform, co
741
817
mb.instantiate ();
742
818
743
819
mb->set_device (get_device ());
820
+ mb->set_player_from_device ();
744
821
mb->set_window_id (get_window_id ());
745
822
mb->set_modifiers_from_event (this );
746
823
@@ -960,6 +1037,7 @@ Ref<InputEvent> InputEventMouseMotion::xformed_by(const Transform2D &p_xform, co
960
1037
mm.instantiate ();
961
1038
962
1039
mm->set_device (get_device ());
1040
+ mm->set_player_from_device ();
963
1041
mm->set_window_id (get_window_id ());
964
1042
965
1043
mm->set_modifiers_from_event (this );
@@ -1363,6 +1441,7 @@ Ref<InputEvent> InputEventScreenTouch::xformed_by(const Transform2D &p_xform, co
1363
1441
Ref<InputEventScreenTouch> st;
1364
1442
st.instantiate ();
1365
1443
st->set_device (get_device ());
1444
+ st->set_player_from_device ();
1366
1445
st->set_window_id (get_window_id ());
1367
1446
st->set_index (index );
1368
1447
st->set_position (p_xform.xform (pos + p_local_ofs));
@@ -1488,6 +1567,7 @@ Ref<InputEvent> InputEventScreenDrag::xformed_by(const Transform2D &p_xform, con
1488
1567
sd.instantiate ();
1489
1568
1490
1569
sd->set_device (get_device ());
1570
+ sd->set_player_from_device ();
1491
1571
sd->set_window_id (get_window_id ());
1492
1572
1493
1573
sd->set_index (index );
@@ -1706,6 +1786,7 @@ Ref<InputEvent> InputEventMagnifyGesture::xformed_by(const Transform2D &p_xform,
1706
1786
ev.instantiate ();
1707
1787
1708
1788
ev->set_device (get_device ());
1789
+ ev->set_player_from_device ();
1709
1790
ev->set_window_id (get_window_id ());
1710
1791
1711
1792
ev->set_modifiers_from_event (this );
@@ -1748,6 +1829,7 @@ Ref<InputEvent> InputEventPanGesture::xformed_by(const Transform2D &p_xform, con
1748
1829
ev.instantiate ();
1749
1830
1750
1831
ev->set_device (get_device ());
1832
+ ev->set_player_from_device ();
1751
1833
ev->set_window_id (get_window_id ());
1752
1834
1753
1835
ev->set_modifiers_from_event (this );
0 commit comments