Browse Source

Improve joypads demo (#830)

Hugo Locurcio 2 years ago
parent
commit
8fcf95c120

+ 5 - 3
misc/joypads/README.md

@@ -1,13 +1,15 @@
 # Joypads Demo
 # Joypads Demo
 
 
-A tool for testing joypad input.
+A tool for testing
+[joypad input](https://docs.godotengine.org/en/latest/tutorials/inputs/controllers_gamepads_joysticks.html)
+and generating controller mapping strings.
 
 
 Language: GDScript
 Language: GDScript
 
 
-Renderer: GLES 2
+Renderer: Compatibility
 
 
 Check out this demo on the asset library: https://godotengine.org/asset-library/asset/140
 Check out this demo on the asset library: https://godotengine.org/asset-library/asset/140
 
 
 ## Screenshots
 ## Screenshots
 
 
-![Screenshot](screenshots/joypads.png)
+![Screenshot](screenshots/joypads.webp)

+ 38 - 7
misc/joypads/joypads.gd

@@ -9,7 +9,7 @@ extends Control
 
 
 const DEADZONE = 0.2
 const DEADZONE = 0.2
 const FONT_COLOR_DEFAULT = Color(1.0, 1.0, 1.0, 0.5)
 const FONT_COLOR_DEFAULT = Color(1.0, 1.0, 1.0, 0.5)
-const FONT_COLOR_ACTIVE = Color.WHITE
+const FONT_COLOR_ACTIVE = Color(0.2, 1.0, 0.2, 1.0)
 
 
 var joy_num
 var joy_num
 var cur_joy = -1
 var cur_joy = -1
@@ -23,24 +23,29 @@ var axis_value
 @onready var joypad_number = $DeviceInfo/JoyNumber
 @onready var joypad_number = $DeviceInfo/JoyNumber
 
 
 func _ready():
 func _ready():
-	set_physics_process(true)
 	Input.joy_connection_changed.connect(self._on_joy_connection_changed)
 	Input.joy_connection_changed.connect(self._on_joy_connection_changed)
 
 
+	for joypad in Input.get_connected_joypads():
+		print_rich("Found joypad #%d: [b]%s[/b] - %s" % [joypad, Input.get_joy_name(joypad), Input.get_joy_guid(joypad)])
 
 
 func _process(_delta):
 func _process(_delta):
 	# Get the joypad device number from the spinbox.
 	# Get the joypad device number from the spinbox.
-	joy_num = joypad_number.get_value()
+	joy_num = joypad_number.value
 
 
 	# Display the name of the joypad if we haven't already.
 	# Display the name of the joypad if we haven't already.
 	if joy_num != cur_joy:
 	if joy_num != cur_joy:
 		cur_joy = joy_num
 		cur_joy = joy_num
-		joypad_name.set_text(Input.get_joy_name(joy_num) + "\n" + Input.get_joy_guid(joy_num))
+		if Input.get_joy_name(joy_num) != "":
+			set_joypad_name(Input.get_joy_name(joy_num), Input.get_joy_guid(joy_num))
+		else:
+			clear_joypad_name()
+
 
 
 	# Loop through the axes and show their current values.
 	# Loop through the axes and show their current values.
 	for axis in range(int(min(JOY_AXIS_MAX, 10))):
 	for axis in range(int(min(JOY_AXIS_MAX, 10))):
 		axis_value = Input.get_joy_axis(joy_num, axis)
 		axis_value = Input.get_joy_axis(joy_num, axis)
 		axes.get_node("Axis" + str(axis) + "/ProgressBar").set_value(100 * axis_value)
 		axes.get_node("Axis" + str(axis) + "/ProgressBar").set_value(100 * axis_value)
-		axes.get_node("Axis" + str(axis) + "/ProgressBar/Value").set_text(str(axis_value))
+		axes.get_node("Axis" + str(axis) + "/ProgressBar/Value").set_text("[center][fade start=2 length=16]%s[/fade][/center]" % axis_value)
 		# Scaled value used for alpha channel using valid range rather than including unusable deadzone values.
 		# Scaled value used for alpha channel using valid range rather than including unusable deadzone values.
 		var scaled_alpha_value = (abs(axis_value) - DEADZONE) / (1.0 - DEADZONE)
 		var scaled_alpha_value = (abs(axis_value) - DEADZONE) / (1.0 - DEADZONE)
 		# Show joypad direction indicators
 		# Show joypad direction indicators
@@ -85,11 +90,17 @@ func _process(_delta):
 
 
 # Called whenever a joypad has been connected or disconnected.
 # Called whenever a joypad has been connected or disconnected.
 func _on_joy_connection_changed(device_id, connected):
 func _on_joy_connection_changed(device_id, connected):
+	if connected:
+		print_rich("[color=green]Found newly connected joypad #%d: [b]%s[/b] - %s[/color]" % [device_id, Input.get_joy_name(device_id), Input.get_joy_guid(device_id)])
+	else:
+		print_rich("[color=red]Disconnected joypad #%d.[/color]" % device_id)
+
 	if device_id == cur_joy:
 	if device_id == cur_joy:
+		# Update current joypad label.
 		if connected:
 		if connected:
-			joypad_name.set_text(Input.get_joy_name(device_id) + "\n" + Input.get_joy_guid(device_id))
+			set_joypad_name(Input.get_joy_name(device_id), Input.get_joy_guid(device_id))
 		else:
 		else:
-			joypad_name.set_text("")
+			clear_joypad_name()
 
 
 
 
 func _on_start_vibration_pressed():
 func _on_start_vibration_pressed():
@@ -117,3 +128,23 @@ func _on_Clear_pressed():
 
 
 func _on_Show_pressed():
 func _on_Show_pressed():
 	$RemapWizard.show_map()
 	$RemapWizard.show_map()
+
+
+func _on_joy_name_meta_clicked(meta):
+	OS.shell_open(meta)
+
+
+func set_joypad_name(joy_name, joy_guid):
+	# Make the GUID clickable (and point to Godot's game controller database for easier lookup).
+	joypad_name.set_text("%s\n[color=#fff9][url=https://github.com/godotengine/godot/blob/master/core/input/gamecontrollerdb.txt]%s[/url][/color]" % [joy_name, joy_guid])
+
+	# Make the rest of the UI appear as enabled.
+	for node in [$JoypadDiagram, $Axes, $Buttons, $Vibration, $VBoxContainer]:
+		node.modulate.a = 1.0
+
+func clear_joypad_name():
+	joypad_name.set_text("[i]No controller detected at ID %d.[/i]" % joypad_number.value)
+
+	# Make the rest of the UI appear as disabled.
+	for node in [$JoypadDiagram, $Axes, $Buttons, $Vibration, $VBoxContainer]:
+		node.modulate.a = 0.5

+ 62 - 44
misc/joypads/joypads.tscn

@@ -64,9 +64,11 @@ position = Vector2(415, 183)
 scale = Vector2(0.5, 0.5)
 scale = Vector2(0.5, 0.5)
 
 
 [node name="DeviceInfo" type="HBoxContainer" parent="."]
 [node name="DeviceInfo" type="HBoxContainer" parent="."]
-layout_mode = 0
+layout_mode = 1
+anchors_preset = 10
 anchor_right = 1.0
 anchor_right = 1.0
-offset_bottom = 30.0
+offset_top = -10.0
+offset_bottom = 40.0
 grow_horizontal = 2
 grow_horizontal = 2
 
 
 [node name="Label" type="Label" parent="DeviceInfo"]
 [node name="Label" type="Label" parent="DeviceInfo"]
@@ -83,11 +85,16 @@ max_value = 16.0
 modulate = Color(1, 1, 1, 0)
 modulate = Color(1, 1, 1, 0)
 layout_mode = 2
 layout_mode = 2
 
 
-[node name="JoyName" type="Label" parent="DeviceInfo"]
+[node name="JoyName" type="RichTextLabel" parent="DeviceInfo"]
+custom_minimum_size = Vector2(0, 50)
 layout_mode = 2
 layout_mode = 2
 size_flags_horizontal = 3
 size_flags_horizontal = 3
-size_flags_vertical = 1
+focus_mode = 2
+bbcode_enabled = true
 text = "Controller Name Here"
 text = "Controller Name Here"
+scroll_active = false
+context_menu_enabled = true
+selection_enabled = true
 
 
 [node name="Axes" type="VBoxContainer" parent="."]
 [node name="Axes" type="VBoxContainer" parent="."]
 layout_mode = 0
 layout_mode = 0
@@ -116,15 +123,16 @@ min_value = -100.0
 step = 0.0001
 step = 0.0001
 show_percentage = false
 show_percentage = false
 
 
-[node name="Value" type="Label" parent="Axes/Axis0/ProgressBar"]
+[node name="Value" type="RichTextLabel" parent="Axes/Axis0/ProgressBar"]
+custom_minimum_size = Vector2(0, 24)
 layout_mode = 0
 layout_mode = 0
 anchor_right = 1.0
 anchor_right = 1.0
 anchor_bottom = 1.0
 anchor_bottom = 1.0
 grow_horizontal = 2
 grow_horizontal = 2
 grow_vertical = 2
 grow_vertical = 2
-size_flags_vertical = 1
-text = "0"
-horizontal_alignment = 1
+bbcode_enabled = true
+text = "[center]0[/center]"
+scroll_active = false
 
 
 [node name="Axis1" type="HBoxContainer" parent="Axes"]
 [node name="Axis1" type="HBoxContainer" parent="Axes"]
 layout_mode = 2
 layout_mode = 2
@@ -146,15 +154,16 @@ min_value = -100.0
 step = 0.0001
 step = 0.0001
 show_percentage = false
 show_percentage = false
 
 
-[node name="Value" type="Label" parent="Axes/Axis1/ProgressBar"]
+[node name="Value" type="RichTextLabel" parent="Axes/Axis1/ProgressBar"]
+custom_minimum_size = Vector2(0, 24)
 layout_mode = 0
 layout_mode = 0
 anchor_right = 1.0
 anchor_right = 1.0
 anchor_bottom = 1.0
 anchor_bottom = 1.0
 grow_horizontal = 2
 grow_horizontal = 2
 grow_vertical = 2
 grow_vertical = 2
-size_flags_vertical = 1
-text = "0"
-horizontal_alignment = 1
+bbcode_enabled = true
+text = "[center]0[/center]"
+scroll_active = false
 
 
 [node name="Axis2" type="HBoxContainer" parent="Axes"]
 [node name="Axis2" type="HBoxContainer" parent="Axes"]
 layout_mode = 2
 layout_mode = 2
@@ -176,15 +185,16 @@ min_value = -100.0
 step = 0.0001
 step = 0.0001
 show_percentage = false
 show_percentage = false
 
 
-[node name="Value" type="Label" parent="Axes/Axis2/ProgressBar"]
+[node name="Value" type="RichTextLabel" parent="Axes/Axis2/ProgressBar"]
+custom_minimum_size = Vector2(0, 24)
 layout_mode = 0
 layout_mode = 0
 anchor_right = 1.0
 anchor_right = 1.0
 anchor_bottom = 1.0
 anchor_bottom = 1.0
 grow_horizontal = 2
 grow_horizontal = 2
 grow_vertical = 2
 grow_vertical = 2
-size_flags_vertical = 1
-text = "0"
-horizontal_alignment = 1
+bbcode_enabled = true
+text = "[center]0[/center]"
+scroll_active = false
 
 
 [node name="Axis3" type="HBoxContainer" parent="Axes"]
 [node name="Axis3" type="HBoxContainer" parent="Axes"]
 layout_mode = 2
 layout_mode = 2
@@ -206,15 +216,16 @@ min_value = -100.0
 step = 0.0001
 step = 0.0001
 show_percentage = false
 show_percentage = false
 
 
-[node name="Value" type="Label" parent="Axes/Axis3/ProgressBar"]
+[node name="Value" type="RichTextLabel" parent="Axes/Axis3/ProgressBar"]
+custom_minimum_size = Vector2(0, 24)
 layout_mode = 0
 layout_mode = 0
 anchor_right = 1.0
 anchor_right = 1.0
 anchor_bottom = 1.0
 anchor_bottom = 1.0
 grow_horizontal = 2
 grow_horizontal = 2
 grow_vertical = 2
 grow_vertical = 2
-size_flags_vertical = 1
-text = "0"
-horizontal_alignment = 1
+bbcode_enabled = true
+text = "[center]0[/center]"
+scroll_active = false
 
 
 [node name="Axis4" type="HBoxContainer" parent="Axes"]
 [node name="Axis4" type="HBoxContainer" parent="Axes"]
 layout_mode = 2
 layout_mode = 2
@@ -236,15 +247,16 @@ min_value = -100.0
 step = 0.0001
 step = 0.0001
 show_percentage = false
 show_percentage = false
 
 
-[node name="Value" type="Label" parent="Axes/Axis4/ProgressBar"]
+[node name="Value" type="RichTextLabel" parent="Axes/Axis4/ProgressBar"]
+custom_minimum_size = Vector2(0, 24)
 layout_mode = 0
 layout_mode = 0
 anchor_right = 1.0
 anchor_right = 1.0
 anchor_bottom = 1.0
 anchor_bottom = 1.0
 grow_horizontal = 2
 grow_horizontal = 2
 grow_vertical = 2
 grow_vertical = 2
-size_flags_vertical = 1
-text = "0"
-horizontal_alignment = 1
+bbcode_enabled = true
+text = "[center]0[/center]"
+scroll_active = false
 
 
 [node name="Axis5" type="HBoxContainer" parent="Axes"]
 [node name="Axis5" type="HBoxContainer" parent="Axes"]
 layout_mode = 2
 layout_mode = 2
@@ -266,15 +278,16 @@ min_value = -100.0
 step = 0.0001
 step = 0.0001
 show_percentage = false
 show_percentage = false
 
 
-[node name="Value" type="Label" parent="Axes/Axis5/ProgressBar"]
+[node name="Value" type="RichTextLabel" parent="Axes/Axis5/ProgressBar"]
+custom_minimum_size = Vector2(0, 24)
 layout_mode = 0
 layout_mode = 0
 anchor_right = 1.0
 anchor_right = 1.0
 anchor_bottom = 1.0
 anchor_bottom = 1.0
 grow_horizontal = 2
 grow_horizontal = 2
 grow_vertical = 2
 grow_vertical = 2
-size_flags_vertical = 1
-text = "0"
-horizontal_alignment = 1
+bbcode_enabled = true
+text = "[center]0[/center]"
+scroll_active = false
 
 
 [node name="Axis6" type="HBoxContainer" parent="Axes"]
 [node name="Axis6" type="HBoxContainer" parent="Axes"]
 layout_mode = 2
 layout_mode = 2
@@ -296,15 +309,16 @@ min_value = -100.0
 step = 0.0001
 step = 0.0001
 show_percentage = false
 show_percentage = false
 
 
-[node name="Value" type="Label" parent="Axes/Axis6/ProgressBar"]
+[node name="Value" type="RichTextLabel" parent="Axes/Axis6/ProgressBar"]
+custom_minimum_size = Vector2(0, 24)
 layout_mode = 0
 layout_mode = 0
 anchor_right = 1.0
 anchor_right = 1.0
 anchor_bottom = 1.0
 anchor_bottom = 1.0
 grow_horizontal = 2
 grow_horizontal = 2
 grow_vertical = 2
 grow_vertical = 2
-size_flags_vertical = 1
-text = "0"
-horizontal_alignment = 1
+bbcode_enabled = true
+text = "[center]0[/center]"
+scroll_active = false
 
 
 [node name="Axis7" type="HBoxContainer" parent="Axes"]
 [node name="Axis7" type="HBoxContainer" parent="Axes"]
 layout_mode = 2
 layout_mode = 2
@@ -326,15 +340,16 @@ min_value = -100.0
 step = 0.0001
 step = 0.0001
 show_percentage = false
 show_percentage = false
 
 
-[node name="Value" type="Label" parent="Axes/Axis7/ProgressBar"]
+[node name="Value" type="RichTextLabel" parent="Axes/Axis7/ProgressBar"]
+custom_minimum_size = Vector2(0, 24)
 layout_mode = 0
 layout_mode = 0
 anchor_right = 1.0
 anchor_right = 1.0
 anchor_bottom = 1.0
 anchor_bottom = 1.0
 grow_horizontal = 2
 grow_horizontal = 2
 grow_vertical = 2
 grow_vertical = 2
-size_flags_vertical = 1
-text = "0"
-horizontal_alignment = 1
+bbcode_enabled = true
+text = "[center]0[/center]"
+scroll_active = false
 
 
 [node name="Axis8" type="HBoxContainer" parent="Axes"]
 [node name="Axis8" type="HBoxContainer" parent="Axes"]
 layout_mode = 2
 layout_mode = 2
@@ -356,15 +371,16 @@ min_value = -100.0
 step = 0.0001
 step = 0.0001
 show_percentage = false
 show_percentage = false
 
 
-[node name="Value" type="Label" parent="Axes/Axis8/ProgressBar"]
+[node name="Value" type="RichTextLabel" parent="Axes/Axis8/ProgressBar"]
+custom_minimum_size = Vector2(0, 24)
 layout_mode = 0
 layout_mode = 0
 anchor_right = 1.0
 anchor_right = 1.0
 anchor_bottom = 1.0
 anchor_bottom = 1.0
 grow_horizontal = 2
 grow_horizontal = 2
 grow_vertical = 2
 grow_vertical = 2
-size_flags_vertical = 1
-text = "0"
-horizontal_alignment = 1
+bbcode_enabled = true
+text = "[center]0[/center]"
+scroll_active = false
 
 
 [node name="Axis9" type="HBoxContainer" parent="Axes"]
 [node name="Axis9" type="HBoxContainer" parent="Axes"]
 layout_mode = 2
 layout_mode = 2
@@ -386,15 +402,16 @@ min_value = -100.0
 step = 0.0001
 step = 0.0001
 show_percentage = false
 show_percentage = false
 
 
-[node name="Value" type="Label" parent="Axes/Axis9/ProgressBar"]
+[node name="Value" type="RichTextLabel" parent="Axes/Axis9/ProgressBar"]
+custom_minimum_size = Vector2(0, 24)
 layout_mode = 0
 layout_mode = 0
 anchor_right = 1.0
 anchor_right = 1.0
 anchor_bottom = 1.0
 anchor_bottom = 1.0
 grow_horizontal = 2
 grow_horizontal = 2
 grow_vertical = 2
 grow_vertical = 2
-size_flags_vertical = 1
-text = "0"
-horizontal_alignment = 1
+bbcode_enabled = true
+text = "[center]0[/center]"
+scroll_active = false
 
 
 [node name="Buttons" type="VBoxContainer" parent="."]
 [node name="Buttons" type="VBoxContainer" parent="."]
 layout_mode = 1
 layout_mode = 1
@@ -655,6 +672,7 @@ text = "Show"
 
 
 [node name="RemapWizard" parent="." instance=ExtResource("3")]
 [node name="RemapWizard" parent="." instance=ExtResource("3")]
 
 
+[connection signal="meta_clicked" from="DeviceInfo/JoyName" to="." method="_on_joy_name_meta_clicked"]
 [connection signal="pressed" from="Vibration/Buttons/Start" to="." method="_on_start_vibration_pressed"]
 [connection signal="pressed" from="Vibration/Buttons/Start" to="." method="_on_start_vibration_pressed"]
 [connection signal="pressed" from="Vibration/Buttons/Stop" to="." method="_on_stop_vibration_pressed"]
 [connection signal="pressed" from="Vibration/Buttons/Stop" to="." method="_on_stop_vibration_pressed"]
 [connection signal="pressed" from="VBoxContainer/Clear" to="." method="_on_Clear_pressed"]
 [connection signal="pressed" from="VBoxContainer/Clear" to="." method="_on_Clear_pressed"]

+ 5 - 0
misc/joypads/project.godot

@@ -24,3 +24,8 @@ window/size/viewport_height=560
 window/vsync/vsync_mode=0
 window/vsync/vsync_mode=0
 window/stretch/mode="canvas_items"
 window/stretch/mode="canvas_items"
 window/stretch/aspect="expand"
 window/stretch/aspect="expand"
+
+[rendering]
+
+renderer/rendering_method="gl_compatibility"
+renderer/rendering_method.mobile="gl_compatibility"

BIN
misc/joypads/screenshots/joypads.png


BIN
misc/joypads/screenshots/joypads.webp