Skip to content

Commit 6c5bf42

Browse files
committed
Readd and improve methods to get the name of gamepad inputs
1 parent 4248411 commit 6c5bf42

3 files changed

Lines changed: 299 additions & 11 deletions

File tree

core/input/input.cpp

Lines changed: 232 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,138 @@ static const char *_joy_axes[(size_t)JoyAxis::SDL_MAX] = {
7373
"righttrigger",
7474
};
7575

76+
static const char *_joy_button_names_unknown[(size_t)JoyButton::SDL_MAX] = {
77+
"Bottom Action",
78+
"Right Action",
79+
"Left Action",
80+
"Top Action",
81+
"Select",
82+
"Guide",
83+
"Start",
84+
"Left Stick",
85+
"Right Stick",
86+
"Left Shoulder",
87+
"Right Shoulder",
88+
"D-pad Up",
89+
"D-pad Down",
90+
"D-pad Left",
91+
"D-pad Right",
92+
"Misc",
93+
"Paddle 1",
94+
"Paddle 2",
95+
"Paddle 3",
96+
"Paddle 4",
97+
"Touchpad",
98+
};
99+
100+
static const char *_joy_button_names_nintendo_generic[(size_t)JoyButton::SDL_MAX] = {
101+
"B",
102+
"A",
103+
"Y",
104+
"X",
105+
"-",
106+
"",
107+
"+",
108+
"",
109+
"",
110+
"L",
111+
"R",
112+
"",
113+
"",
114+
"",
115+
"",
116+
"Capture",
117+
"",
118+
"",
119+
"",
120+
"",
121+
"",
122+
};
123+
124+
static const char *_joy_button_names_playstation_generic[(size_t)JoyButton::SDL_MAX] = {
125+
"Cross",
126+
"Circle",
127+
"Square",
128+
"Triangle",
129+
"Select",
130+
"PS",
131+
"",
132+
"L3",
133+
"R3",
134+
"L1",
135+
"R1",
136+
"",
137+
"",
138+
"",
139+
"",
140+
"Microphone",
141+
"",
142+
"",
143+
"",
144+
"",
145+
"",
146+
};
147+
148+
static const char *_joy_button_names_xbox_generic[(size_t)JoyButton::SDL_MAX] = {
149+
"A",
150+
"B",
151+
"X",
152+
"Y",
153+
"Back",
154+
"Home",
155+
"Menu",
156+
"L/LS",
157+
"R/RS",
158+
"LB",
159+
"RB",
160+
"",
161+
"",
162+
"",
163+
"",
164+
"Share",
165+
"",
166+
"",
167+
"",
168+
"",
169+
"",
170+
};
171+
172+
static const char *_joy_axis_names_unknown[(size_t)JoyAxis::SDL_MAX] = {
173+
"Left Stick X-Axis",
174+
"Left Stick Y-Axis",
175+
"Right Stick X-Axis",
176+
"Right Stick Y-Axis",
177+
"Left Trigger",
178+
"Right Trigger",
179+
};
180+
181+
static const char *_joy_axis_names_nintendo_generic[(size_t)JoyAxis::SDL_MAX] = {
182+
"",
183+
"",
184+
"",
185+
"",
186+
"ZL",
187+
"ZR",
188+
};
189+
190+
static const char *_joy_axis_names_playstation_generic[(size_t)JoyAxis::SDL_MAX] = {
191+
"",
192+
"",
193+
"",
194+
"",
195+
"L2",
196+
"R2",
197+
};
198+
199+
static const char *_joy_axis_names_xbox_generic[(size_t)JoyAxis::SDL_MAX] = {
200+
"",
201+
"",
202+
"",
203+
"",
204+
"LT",
205+
"RT",
206+
};
207+
76208
Input *Input::singleton = nullptr;
77209

78210
void (*Input::set_mouse_mode_func)(Input::MouseMode) = nullptr;
@@ -134,12 +266,15 @@ void Input::_bind_methods() {
134266
ClassDB::bind_method(D_METHOD("is_joy_known", "device"), &Input::is_joy_known);
135267
ClassDB::bind_method(D_METHOD("get_joy_axis", "device", "axis"), &Input::get_joy_axis);
136268
ClassDB::bind_method(D_METHOD("get_joy_name", "device"), &Input::get_joy_name);
269+
ClassDB::bind_method(D_METHOD("get_joy_scheme", "device"), &Input::get_joy_scheme);
137270
ClassDB::bind_method(D_METHOD("get_joy_guid", "device"), &Input::get_joy_guid);
138271
ClassDB::bind_method(D_METHOD("get_joy_info", "device"), &Input::get_joy_info);
139272
ClassDB::bind_method(D_METHOD("should_ignore_device", "vendor_id", "product_id"), &Input::should_ignore_device);
140273
ClassDB::bind_method(D_METHOD("get_connected_joypads"), &Input::get_connected_joypads);
141274
ClassDB::bind_method(D_METHOD("get_joy_vibration_strength", "device"), &Input::get_joy_vibration_strength);
142275
ClassDB::bind_method(D_METHOD("get_joy_vibration_duration", "device"), &Input::get_joy_vibration_duration);
276+
ClassDB::bind_method(D_METHOD("get_joy_button_string", "button", "scheme"), &Input::get_joy_button_string, DEFVAL(JoyScheme::JOY_SCHEME_UNKNOWN));
277+
ClassDB::bind_method(D_METHOD("get_joy_axis_string", "axis", "value", "scheme"), &Input::get_joy_axis_string, DEFVAL(0), DEFVAL(JoyScheme::JOY_SCHEME_UNKNOWN));
143278
ClassDB::bind_method(D_METHOD("start_joy_vibration", "device", "weak_magnitude", "strong_magnitude", "duration"), &Input::start_joy_vibration, DEFVAL(0));
144279
ClassDB::bind_method(D_METHOD("stop_joy_vibration", "device"), &Input::stop_joy_vibration);
145280
ClassDB::bind_method(D_METHOD("vibrate_handheld", "duration_ms", "amplitude"), &Input::vibrate_handheld, DEFVAL(500), DEFVAL(-1.0));
@@ -201,6 +336,11 @@ void Input::_bind_methods() {
201336
BIND_ENUM_CONSTANT(CURSOR_HSPLIT);
202337
BIND_ENUM_CONSTANT(CURSOR_HELP);
203338

339+
BIND_ENUM_CONSTANT(JOY_SCHEME_UNKNOWN);
340+
BIND_ENUM_CONSTANT(JOY_SCHEME_NINTENDO_GENERIC);
341+
BIND_ENUM_CONSTANT(JOY_SCHEME_PLAYSTATION_GENERIC);
342+
BIND_ENUM_CONSTANT(JOY_SCHEME_XBOX_GENERIC);
343+
204344
ADD_SIGNAL(MethodInfo("joy_connection_changed", PropertyInfo(Variant::INT, "device"), PropertyInfo(Variant::BOOL, "connected")));
205345
}
206346

@@ -519,9 +659,26 @@ float Input::get_joy_axis(int p_device, JoyAxis p_axis) const {
519659
}
520660
}
521661

522-
String Input::get_joy_name(int p_idx) {
662+
String Input::get_joy_name(int p_device) {
523663
_THREAD_SAFE_METHOD_
524-
return joy_names[p_idx].name;
664+
return joy_names[p_device].name;
665+
}
666+
667+
Input::JoyScheme Input::get_joy_scheme(int p_device) {
668+
String joy_name = get_joy_name(p_device).to_lower();
669+
670+
if (joy_name.contains("nintendo") || joy_name.contains("joy-con") || joy_name.contains("switch pro controller") || joy_name.contains("gamecube")) {
671+
return JOY_SCHEME_NINTENDO_GENERIC;
672+
}
673+
if (joy_name.contains("sony") || joy_name.contains("playstation") || joy_name.contains("dualshock") ||
674+
joy_name.contains("ps1") || joy_name.contains("ps2") || joy_name.contains("ps3") || joy_name.contains("ps4") || joy_name.contains("ps5")) {
675+
return JOY_SCHEME_PLAYSTATION_GENERIC;
676+
}
677+
if (joy_name.contains("microsoft") || joy_name.contains("xbox")) {
678+
return JOY_SCHEME_XBOX_GENERIC;
679+
}
680+
681+
return JOY_SCHEME_UNKNOWN;
525682
}
526683

527684
Vector2 Input::get_joy_vibration_strength(int p_device) {
@@ -548,6 +705,73 @@ float Input::get_joy_vibration_duration(int p_device) {
548705
}
549706
}
550707

708+
String Input::get_joy_button_string(JoyButton p_button, JoyScheme p_scheme) {
709+
String button_name;
710+
switch (p_scheme) {
711+
case JOY_SCHEME_NINTENDO_GENERIC: {
712+
button_name = _joy_button_names_nintendo_generic[(int)p_button];
713+
} break;
714+
case JOY_SCHEME_PLAYSTATION_GENERIC: {
715+
button_name = _joy_button_names_playstation_generic[(int)p_button];
716+
} break;
717+
case JOY_SCHEME_XBOX_GENERIC: {
718+
button_name = _joy_button_names_xbox_generic[(int)p_button];
719+
} break;
720+
default: {
721+
}
722+
}
723+
724+
if (button_name.is_empty()) {
725+
button_name = _joy_button_names_unknown[(int)p_button];
726+
}
727+
728+
return button_name;
729+
}
730+
731+
String Input::get_joy_axis_string(JoyAxis p_axis, float p_value, JoyScheme p_scheme) {
732+
if (p_axis == JoyAxis::TRIGGER_LEFT || p_axis == JoyAxis::TRIGGER_RIGHT) {
733+
String axis_name;
734+
switch (p_scheme) {
735+
case JOY_SCHEME_NINTENDO_GENERIC: {
736+
axis_name = _joy_axis_names_nintendo_generic[(int)p_axis];
737+
} break;
738+
case JOY_SCHEME_PLAYSTATION_GENERIC: {
739+
axis_name = _joy_axis_names_playstation_generic[(int)p_axis];
740+
} break;
741+
case JOY_SCHEME_XBOX_GENERIC: {
742+
axis_name = _joy_axis_names_xbox_generic[(int)p_axis];
743+
} break;
744+
default: {
745+
}
746+
}
747+
748+
if (axis_name.is_empty()) {
749+
axis_name = _joy_axis_names_unknown[(int)p_axis];
750+
}
751+
752+
return axis_name;
753+
}
754+
755+
if (p_value != 0.0) {
756+
String stick;
757+
if (p_axis == JoyAxis::LEFT_X || p_axis == JoyAxis::LEFT_Y) {
758+
stick = "Left Stick ";
759+
} else if (p_axis == JoyAxis::RIGHT_X || p_axis == JoyAxis::RIGHT_Y) {
760+
stick = "Right Stick ";
761+
}
762+
763+
if (p_axis == JoyAxis::LEFT_X || p_axis == JoyAxis::RIGHT_X) {
764+
stick += p_value < 0 ? "Left" : "Right";
765+
} else if (p_axis == JoyAxis::LEFT_Y || p_axis == JoyAxis::RIGHT_Y) {
766+
stick += p_value < 0 ? "Up" : "Down";
767+
}
768+
769+
return stick;
770+
}
771+
772+
return _joy_axis_names_unknown[(int)p_axis];
773+
}
774+
551775
static String _hex_str(uint8_t p_byte) {
552776
static const char *dict = "0123456789abcdef";
553777
char ret[3];
@@ -559,13 +783,13 @@ static String _hex_str(uint8_t p_byte) {
559783
return ret;
560784
}
561785

562-
void Input::joy_connection_changed(int p_idx, bool p_connected, const String &p_name, const String &p_guid, const Dictionary &p_joypad_info) {
786+
void Input::joy_connection_changed(int p_device, bool p_connected, const String &p_name, const String &p_guid, const Dictionary &p_joypad_info) {
563787
_THREAD_SAFE_METHOD_
564788

565789
// Clear the pressed status if a Joypad gets disconnected.
566790
if (!p_connected) {
567791
for (KeyValue<StringName, ActionState> &E : action_states) {
568-
HashMap<int, ActionState::DeviceState>::Iterator it = E.value.device_states.find(p_idx);
792+
HashMap<int, ActionState::DeviceState>::Iterator it = E.value.device_states.find(p_device);
569793
if (it) {
570794
E.value.device_states.remove(it);
571795
_update_action_cache(E.key, E.value);
@@ -598,17 +822,17 @@ void Input::joy_connection_changed(int p_idx, bool p_connected, const String &p_
598822
} else {
599823
js.connected = false;
600824
for (int i = 0; i < (int)JoyButton::MAX; i++) {
601-
JoyButton c = _combine_device((JoyButton)i, p_idx);
825+
JoyButton c = _combine_device((JoyButton)i, p_device);
602826
joy_buttons_pressed.erase(c);
603827
}
604828
for (int i = 0; i < (int)JoyAxis::MAX; i++) {
605-
set_joy_axis(p_idx, (JoyAxis)i, 0.0f);
829+
set_joy_axis(p_device, (JoyAxis)i, 0.0f);
606830
}
607831
}
608-
joy_names[p_idx] = js;
832+
joy_names[p_device] = js;
609833

610834
// Ensure this signal is emitted on the main thread, as some platforms (e.g. Linux) call this from a different thread.
611-
call_deferred("emit_signal", SNAME("joy_connection_changed"), p_idx, p_connected);
835+
call_deferred("emit_signal", SNAME("joy_connection_changed"), p_device, p_connected);
612836
}
613837

614838
Vector3 Input::get_gravity() const {

core/input/input.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
#pragma once
3232

33+
#include "core/input/input_enums.h"
3334
#include "core/input/input_event.h"
3435
#include "core/object/object.h"
3536
#include "core/os/keyboard.h"
@@ -75,7 +76,14 @@ class Input : public Object {
7576
CURSOR_VSPLIT,
7677
CURSOR_HSPLIT,
7778
CURSOR_HELP,
78-
CURSOR_MAX
79+
CURSOR_MAX,
80+
};
81+
82+
enum JoyScheme {
83+
JOY_SCHEME_UNKNOWN,
84+
JOY_SCHEME_NINTENDO_GENERIC,
85+
JOY_SCHEME_PLAYSTATION_GENERIC,
86+
JOY_SCHEME_XBOX_GENERIC,
7987
};
8088

8189
enum {
@@ -315,12 +323,15 @@ class Input : public Object {
315323
Vector2 get_vector(const StringName &p_negative_x, const StringName &p_positive_x, const StringName &p_negative_y, const StringName &p_positive_y, float p_deadzone = -1.0f) const;
316324

317325
float get_joy_axis(int p_device, JoyAxis p_axis) const;
318-
String get_joy_name(int p_idx);
326+
String get_joy_name(int p_device);
327+
JoyScheme get_joy_scheme(int p_device);
319328
TypedArray<int> get_connected_joypads();
320329
Vector2 get_joy_vibration_strength(int p_device);
321330
float get_joy_vibration_duration(int p_device);
322331
uint64_t get_joy_vibration_timestamp(int p_device);
323-
void joy_connection_changed(int p_idx, bool p_connected, const String &p_name, const String &p_guid = "", const Dictionary &p_joypad_info = Dictionary());
332+
String get_joy_button_string(JoyButton p_button, JoyScheme p_scheme = JoyScheme::JOY_SCHEME_UNKNOWN);
333+
String get_joy_axis_string(JoyAxis p_axis, float p_value = 0, JoyScheme p_scheme = JoyScheme::JOY_SCHEME_UNKNOWN);
334+
void joy_connection_changed(int p_device, bool p_connected, const String &p_name, const String &p_guid = "", const Dictionary &p_joypad_info = Dictionary());
324335

325336
Vector3 get_gravity() const;
326337
Vector3 get_accelerometer() const;
@@ -402,3 +413,4 @@ class Input : public Object {
402413

403414
VARIANT_ENUM_CAST(Input::MouseMode);
404415
VARIANT_ENUM_CAST(Input::CursorShape);
416+
VARIANT_ENUM_CAST(Input::JoyScheme);

0 commit comments

Comments
 (0)