Skip to content

Commit

Permalink
Fix joypad demo for Godot 4.x (#943)
Browse files Browse the repository at this point in the history
  • Loading branch information
noidexe authored Apr 13, 2024
1 parent 035e905 commit 01db4a6
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 34 deletions.
31 changes: 13 additions & 18 deletions misc/joypads/joypads.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,15 @@ corner_radius_bottom_right = 3
corner_radius_bottom_left = 3
corner_detail = 3

[node name="joypads" type="Control"]
[node name="JoypadsDemo" type="Control"]
layout_mode = 3
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -270.0
offset_top = -240.0
offset_right = 270.0
offset_bottom = 240.0
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
offset_left = 30.0
offset_top = 30.0
offset_right = -30.0
offset_bottom = -30.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1")
Expand All @@ -67,8 +65,7 @@ scale = Vector2(0.5, 0.5)
layout_mode = 1
anchors_preset = 10
anchor_right = 1.0
offset_top = -10.0
offset_bottom = 40.0
offset_bottom = 50.0
grow_horizontal = 2

[node name="Label" type="Label" parent="DeviceInfo"]
Expand Down Expand Up @@ -421,8 +418,8 @@ anchor_top = 1.0
anchor_right = 1.0
anchor_bottom = 1.0
offset_left = -250.0
offset_top = -138.0
offset_bottom = -22.0
offset_top = -150.0
offset_bottom = -34.0
grow_horizontal = 0
grow_vertical = 0

Expand Down Expand Up @@ -567,9 +564,8 @@ layout_mode = 1
anchors_preset = 2
anchor_top = 1.0
anchor_bottom = 1.0
offset_top = -128.0
offset_top = -144.0
offset_right = 270.0
offset_bottom = 16.0
grow_vertical = 0

[node name="Weak" type="HBoxContainer" parent="Vibration"]
Expand Down Expand Up @@ -652,8 +648,7 @@ anchor_top = 1.0
anchor_right = 1.0
anchor_bottom = 1.0
offset_left = -261.0
offset_top = -18.0
offset_bottom = 13.0
offset_top = -31.0
grow_horizontal = 0
grow_vertical = 0
alignment = 1
Expand Down
38 changes: 25 additions & 13 deletions misc/joypads/remap/remap_wizard.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ extends Node

const DEADZONE = 0.3

var joy_guid = ""
var joy_name = ""
var joy_index: int = -1
var joy_guid: String = ""
var joy_name: String = ""

var steps = JoyMapping.BASE.keys()
var cur_step = -1
var cur_mapping = {}
var last_mapping = ""
var steps: Array = JoyMapping.BASE.keys()
var cur_step: int = -1
var cur_mapping: Dictionary = {}
var last_mapping: String = ""

@onready var joy_buttons = $Mapping/Margin/VBox/SubViewportContainer/SubViewport/JoypadDiagram/Buttons
@onready var joy_axes = $Mapping/Margin/VBox/SubViewportContainer/SubViewport/JoypadDiagram/Axes
Expand All @@ -18,17 +19,25 @@ var last_mapping = ""
@onready var joy_mapping_axis_invert = $Mapping/Margin/VBox/Info/Extra/InvertAxis


# Connected to Mapping.window_input, otherwise no gamepad events
# will be received when the subwindow is focused.
func _input(event):
if cur_step == -1:
return
# Ignore events not related to gamepads.
if not (event is InputEventJoypadButton or event is InputEventJoypadMotion):
return
# Ignore devices other than the one being remapped. Handles accidental input and analog drift.
if event.device != joy_index:
return
if event is InputEventJoypadMotion:
get_viewport().set_input_as_handled()
var motion = event as InputEventJoypadMotion
if abs(motion.axis_value) > DEADZONE:
var idx = motion.axis
var map = JoyMapping.new(JoyMapping.TYPE.AXIS, idx)
map.inverted = joy_mapping_axis_invert.pressed
if joy_mapping_full_axis.pressed:
map.inverted = joy_mapping_axis_invert.button_pressed
if joy_mapping_full_axis.button_pressed:
map.axis = JoyMapping.AXIS.FULL
elif motion.axis_value > 0:
map.axis = JoyMapping.AXIS.HALF_PLUS
Expand All @@ -44,7 +53,7 @@ func _input(event):
cur_mapping[steps[cur_step]] = map


func create_mapping_string(mapping):
func create_mapping_string(mapping: Dictionary) -> String:
var string = "%s,%s," % [joy_guid, joy_name]
for k in mapping:
var m = mapping[k]
Expand All @@ -57,7 +66,8 @@ func create_mapping_string(mapping):
return string + "platform:" + platform


func start(idx):
func start(idx: int):
joy_index = idx
joy_guid = Input.get_joy_guid(idx)
joy_name = Input.get_joy_name(idx)
if joy_guid.is_empty():
Expand All @@ -72,7 +82,7 @@ func start(idx):
_on_Wizard_pressed()


func remap_and_close(mapping):
func remap_and_close(mapping: Dictionary) -> void:
last_mapping = create_mapping_string(mapping)
Input.add_joy_mapping(last_mapping, true)
reset()
Expand Down Expand Up @@ -116,6 +126,8 @@ func _update_step():
if key in ["leftx", "lefty", "rightx", "righty"]:
joy_axes.get_node(str(idx) + "+").show()
joy_axes.get_node(str(idx) + "-").show()
elif key in ["lefttrigger", "righttrigger"]:
joy_axes.get_node(str(idx)).show()
else:
joy_buttons.get_node(str(idx)).show()

Expand All @@ -125,8 +137,8 @@ func _update_step():
var cur = cur_mapping[steps[cur_step]]
joy_mapping_text.text = cur.to_human_string()
if cur.type == JoyMapping.TYPE.AXIS:
joy_mapping_full_axis.pressed = cur.axis == JoyMapping.AXIS.FULL
joy_mapping_axis_invert.pressed = cur.inverted
joy_mapping_full_axis.button_pressed = cur.axis == JoyMapping.AXIS.FULL
joy_mapping_axis_invert.button_pressed = cur.inverted


func _on_Wizard_pressed():
Expand Down
12 changes: 9 additions & 3 deletions misc/joypads/remap/remap_wizard.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ offset_bottom = 31.0
text = "Wizard"

[node name="Mapping" type="Window" parent="."]
position = Vector2i(130, 200)
size = Vector2i(340, 100)
position = Vector2i(130, 100)
size = Vector2i(340, 400)
visible = false
min_size = Vector2i(340, 100)

Expand All @@ -104,20 +104,25 @@ anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
theme_override_constants/margin_left = 30
theme_override_constants/margin_top = 30
theme_override_constants/margin_right = 30
theme_override_constants/margin_bottom = 30

[node name="VBox" type="VBoxContainer" parent="Mapping/Margin"]
layout_mode = 2
offset_right = 600.0
offset_bottom = 540.0

[node name="SubViewportContainer" type="SubViewportContainer" parent="Mapping/Margin/VBox"]
custom_minimum_size = Vector2(0, 260)
layout_mode = 2
offset_right = 600.0
stretch = true

[node name="SubViewport" type="SubViewport" parent="Mapping/Margin/VBox/SubViewportContainer"]
handle_input_locally = false
size = Vector2i(600, 0)
size = Vector2i(600, 260)
render_target_update_mode = 0

[node name="JoypadDiagram" parent="Mapping/Margin/VBox/SubViewportContainer/SubViewport" instance=ExtResource("2")]
Expand Down Expand Up @@ -248,6 +253,7 @@ size_flags_vertical = 3
[connection signal="pressed" from="Start/Margin/Layout/Buttons/Cancel" to="." method="_on_Cancel_pressed"]
[connection signal="pressed" from="Start/Margin/Layout/Buttons/Wizard" to="." method="_on_Wizard_pressed"]
[connection signal="close_requested" from="Mapping" to="." method="_on_mapping_close_requested"]
[connection signal="window_input" from="Mapping" to="." method="_input"]
[connection signal="toggled" from="Mapping/Margin/VBox/Info/Extra/FullAxis" to="." method="_on_FullAxis_toggled"]
[connection signal="toggled" from="Mapping/Margin/VBox/Info/Extra/InvertAxis" to="." method="_on_InvertAxis_toggled"]
[connection signal="pressed" from="Mapping/Margin/VBox/Info/Buttons/Prev" to="." method="_on_Prev_pressed"]
Expand Down

0 comments on commit 01db4a6

Please sign in to comment.