Skip to content

Commit 2a13307

Browse files
committed
[Joypads] Add SDL config re-mapping tool.
Most of the code is in the remap folder, but it depends on the gamepad diagram scene. It allows remapping of pads to values that godot can understand. It also comes with some default mapping for the HTML5 platform.
1 parent 5618c2b commit 2a13307

File tree

6 files changed

+670
-5
lines changed

6 files changed

+670
-5
lines changed

misc/joypads/joypads.gd

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func _process(_delta):
3232
# Display the name of the joypad if we haven't already.
3333
if joy_num != cur_joy:
3434
cur_joy = joy_num
35-
joypad_name.set_text(Input.get_joy_name(joy_num))
35+
joypad_name.set_text(Input.get_joy_name(joy_num) + "\n" + Input.get_joy_guid(joy_num))
3636

3737
# Loop through the axes and show their current values.
3838
for axis in range(JOY_AXIS_MAX):
@@ -64,7 +64,10 @@ func _process(_delta):
6464
# Called whenever a joypad has been connected or disconnected.
6565
func _on_joy_connection_changed(device_id, connected):
6666
if device_id == cur_joy:
67-
joypad_name.set_text(Input.get_joy_name(device_id) if connected else "")
67+
if connected:
68+
joypad_name.set_text(Input.get_joy_name(device_id) + "\n" + Input.get_joy_guid(device_id))
69+
else:
70+
joypad_name.set_text("")
6871

6972

7073
func _on_start_vibration_pressed():
@@ -76,3 +79,19 @@ func _on_start_vibration_pressed():
7679

7780
func _on_stop_vibration_pressed():
7881
Input.stop_joy_vibration(cur_joy)
82+
83+
84+
func _on_Remap_pressed():
85+
$RemapWizard.start(cur_joy)
86+
87+
88+
func _on_Clear_pressed():
89+
var guid = Input.get_joy_guid(cur_joy)
90+
if guid.empty():
91+
printerr("No gamepad selected")
92+
return
93+
Input.remove_joy_mapping(guid)
94+
95+
96+
func _on_Show_pressed():
97+
$RemapWizard.show_map()

misc/joypads/joypads.tscn

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
[gd_scene load_steps=3 format=2]
1+
[gd_scene load_steps=4 format=2]
22

33
[ext_resource path="res://joypads.gd" type="Script" id=1]
44
[ext_resource path="res://joypad_diagram.tscn" type="PackedScene" id=2]
5+
[ext_resource path="res://remap/remap_wizard.tscn" type="PackedScene" id=3]
56

67
[node name="joypads" type="Control"]
78
anchor_left = 0.5
@@ -979,5 +980,42 @@ text = "Stop Vibration"
979980
__meta__ = {
980981
"_edit_use_anchors_": false
981982
}
983+
984+
[node name="VBoxContainer" type="HBoxContainer" parent="."]
985+
anchor_left = 1.0
986+
anchor_top = 1.0
987+
anchor_right = 1.0
988+
anchor_bottom = 1.0
989+
margin_left = -250.0
990+
margin_top = -42.0
991+
margin_bottom = -12.0
992+
alignment = 1
993+
__meta__ = {
994+
"_edit_use_anchors_": false
995+
}
996+
997+
[node name="Clear" type="Button" parent="VBoxContainer"]
998+
margin_left = 11.0
999+
margin_right = 130.0
1000+
margin_bottom = 30.0
1001+
text = "Set Raw Mapping"
1002+
1003+
[node name="Remap" type="Button" parent="VBoxContainer"]
1004+
margin_left = 134.0
1005+
margin_right = 190.0
1006+
margin_bottom = 30.0
1007+
text = "Remap"
1008+
1009+
[node name="Show" type="Button" parent="VBoxContainer"]
1010+
margin_left = 194.0
1011+
margin_right = 239.0
1012+
margin_bottom = 30.0
1013+
text = "Show"
1014+
1015+
[node name="RemapWizard" parent="." instance=ExtResource( 3 )]
1016+
9821017
[connection signal="pressed" from="Vibration/Buttons/Start" to="." method="_on_start_vibration_pressed"]
9831018
[connection signal="pressed" from="Vibration/Buttons/Stop" to="." method="_on_stop_vibration_pressed"]
1019+
[connection signal="pressed" from="VBoxContainer/Clear" to="." method="_on_Clear_pressed"]
1020+
[connection signal="pressed" from="VBoxContainer/Remap" to="." method="_on_Remap_pressed"]
1021+
[connection signal="pressed" from="VBoxContainer/Show" to="." method="_on_Show_pressed"]

misc/joypads/project.godot

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@
88

99
config_version=4
1010

11-
_global_script_classes=[ ]
11+
_global_script_classes=[ {
12+
"base": "Reference",
13+
"class": "JoyMapping",
14+
"language": "GDScript",
15+
"path": "res://remap/joy_mapping.gd"
16+
} ]
1217
_global_script_class_icons={
13-
18+
"JoyMapping": ""
1419
}
1520

1621
[application]

misc/joypads/remap/joy_mapping.gd

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
extends Reference
2+
class_name JoyMapping
3+
4+
5+
enum TYPE {NONE, BTN, AXIS}
6+
enum AXIS {FULL, HALF_PLUS, HALF_MINUS}
7+
8+
const PLATFORMS = {
9+
# From gamecontrollerdb
10+
"Windows": "Windows",
11+
"OSX": "Mac OS X",
12+
"X11": "Linux",
13+
"Android": "Android",
14+
"iOS": "iOS",
15+
# Godot customs
16+
"HTML5": "Javascript",
17+
"UWP": "UWP",
18+
# 4.x compat
19+
"Linux": "Linux",
20+
"FreeBSD": "Linux",
21+
"NetBSD": "Linux",
22+
"BSD": "Linux",
23+
"macOS": "Mac OS X",
24+
}
25+
26+
const BASE = {
27+
# Buttons
28+
"a": JOY_XBOX_A,
29+
"b": JOY_XBOX_B,
30+
"y": JOY_XBOX_Y,
31+
"x": JOY_XBOX_X,
32+
"start": JOY_START,
33+
"back": JOY_SELECT,
34+
"leftstick": JOY_BUTTON_8,
35+
"rightstick": JOY_BUTTON_9,
36+
"leftshoulder": JOY_L,
37+
"rightshoulder": JOY_R,
38+
"dpup": JOY_DPAD_UP,
39+
"dpleft": JOY_DPAD_LEFT,
40+
"dpdown": JOY_DPAD_DOWN,
41+
"dpright": JOY_DPAD_RIGHT,
42+
43+
# Axis
44+
"leftx": JOY_AXIS_0,
45+
"lefty": JOY_AXIS_1,
46+
"rightx": JOY_AXIS_2,
47+
"righty": JOY_AXIS_3,
48+
"lefttrigger": JOY_ANALOG_L2,
49+
"righttrigger": JOY_ANALOG_R2,
50+
}
51+
52+
const XBOX = {
53+
"a": "b0",
54+
"b": "b1",
55+
"y": "b3",
56+
"x": "b2",
57+
"start": "b7",
58+
"guide": "b8",
59+
"back": "b6",
60+
"leftstick": "b9",
61+
"rightstick": "b10",
62+
"leftshoulder": "b4",
63+
"rightshoulder": "b5",
64+
"dpup": "-a7",
65+
"dpleft":"-a6",
66+
"dpdown": "+a7",
67+
"dpright": "+a6",
68+
"leftx": "a0",
69+
"lefty": "a1",
70+
"rightx": "a3",
71+
"righty": "a4",
72+
"lefttrigger": "a2",
73+
"righttrigger": "a5",
74+
}
75+
76+
const XBOX_OSX = {
77+
"a": "b11",
78+
"b": "b12",
79+
"y": "b14",
80+
"x": "b13",
81+
"start": "b4",
82+
"back": "b5",
83+
"leftstick": "b6",
84+
"rightstick": "b7",
85+
"leftshoulder": "b8",
86+
"rightshoulder": "b9",
87+
"dpup": "b0",
88+
"dpleft": "b2",
89+
"dpdown": "b1",
90+
"dpright": "b3",
91+
"leftx": "a0",
92+
"lefty": "a1",
93+
"rightx": "a2",
94+
"righty": "a3",
95+
"lefttrigger": "a4",
96+
"righttrigger":"a5",
97+
}
98+
99+
var type = TYPE.NONE
100+
var idx = -1
101+
var axis = AXIS.FULL
102+
var inverted = false
103+
104+
105+
func _init(p_type = TYPE.NONE, p_idx = -1, p_axis = AXIS.FULL):
106+
type = p_type
107+
idx = p_idx
108+
axis = p_axis
109+
110+
111+
func _to_string():
112+
if type == TYPE.NONE:
113+
return ""
114+
var ts = "b" if type == TYPE.BTN else "a"
115+
var prefix = ""
116+
var suffix = "~" if inverted else ""
117+
match axis:
118+
AXIS.HALF_PLUS:
119+
prefix = "+"
120+
AXIS.HALF_MINUS:
121+
prefix = "-"
122+
return "%s%s%d%s" % [prefix, ts, idx, suffix]
123+
124+
125+
func to_human_string():
126+
if type == TYPE.BTN:
127+
return "Button %d" % idx
128+
if type == TYPE.AXIS:
129+
var prefix = ""
130+
match axis:
131+
AXIS.HALF_PLUS:
132+
prefix = "(+) "
133+
AXIS.HALF_MINUS:
134+
prefix = "(-) "
135+
var suffix = " (inverted)" if inverted else ""
136+
return "Axis %s%d%s" % [prefix, idx, suffix]
137+
return ""

0 commit comments

Comments
 (0)