From 03cad36bcb4d6b808c8feef409fc77f7d7f91a15 Mon Sep 17 00:00:00 2001 From: Scott Wadden Date: Fri, 25 Oct 2024 22:31:53 -0300 Subject: [PATCH] Jump buttons for touch controller --- app/scenes/GUI.tscn | 35 +++++++++++++++++++++++++++++++++-- src/ui/gui.nim | 37 +++++++++++++++++++++++++++++++++---- 2 files changed, 66 insertions(+), 6 deletions(-) diff --git a/app/scenes/GUI.tscn b/app/scenes/GUI.tscn index 260e2076..58fc183a 100644 --- a/app/scenes/GUI.tscn +++ b/app/scenes/GUI.tscn @@ -115,6 +115,36 @@ margin_top = 0.0 margin_right = 1920.0 margin_bottom = 516.0 +[node name="HBoxContainer2" type="HBoxContainer" parent="."] +anchor_left = 0.821 +anchor_top = 0.646 +anchor_right = 0.821 +anchor_bottom = 0.646 +margin_left = -5.34058e-05 +margin_top = 2.38419e-05 +margin_right = 118.0 +margin_bottom = 33.0 +size_flags_horizontal = 0 +size_flags_vertical = 0 +theme = ExtResource( 6 ) +custom_constants/separation = 20 + +[node name="Up" type="Button" parent="HBoxContainer2"] +visible = false +margin_right = 49.0 +margin_bottom = 33.0 +mouse_default_cursor_shape = 2 +theme_type_variation = "MonoButton" +text = " ↑ " + +[node name="Down" type="Button" parent="HBoxContainer2"] +visible = false +margin_right = 49.0 +margin_bottom = 33.0 +mouse_default_cursor_shape = 2 +theme_type_variation = "MonoButton" +text = " ↓ " + [node name="LeftPanel" type="MarginContainer" parent="."] anchor_right = 0.5 anchor_bottom = 1.0 @@ -133,7 +163,7 @@ custom_constants/separation = 2 [node name="MarginContainer" type="MarginContainer" parent="LeftPanel/ThemeHolder"] margin_right = 961.0 -margin_bottom = 678.0 +margin_bottom = 1080.0 mouse_filter = 2 size_flags_horizontal = 3 size_flags_vertical = 3 @@ -142,10 +172,11 @@ size_flags_stretch_ratio = 2.0 [node name="Editor" parent="LeftPanel/ThemeHolder/MarginContainer" instance=ExtResource( 1 )] visible = false margin_right = 961.0 -margin_bottom = 678.0 +margin_bottom = 1080.0 mouse_filter = 1 [node name="Console" parent="LeftPanel/ThemeHolder" instance=ExtResource( 2 )] +visible = false margin_top = 680.0 margin_right = 961.0 margin_bottom = 1080.0 diff --git a/src/ui/gui.nim b/src/ui/gui.nim index ab8f4c37..12621cd8 100644 --- a/src/ui/gui.nim +++ b/src/ui/gui.nim @@ -1,17 +1,46 @@ import pkg/godot import - godotapi/ - [control, input_event_screen_touch, input_event_screen_drag, scene_tree] + godotapi/[ + control, input_event_screen_touch, input_event_screen_drag, scene_tree, + input_event_action, input + ] import core, nodes/player_node, gdutils gdobj GUI of Control: - var left_stick: Control + var + left_stick: Control + up: Control + down: Control + method ready() = + self.left_stick = find("LeftStick", Control) + self.up = find("Up", Control) + self.down = find("Down", Control) + self.bind_signals self, "mouse_entered", "mouse_exited", "focus_entered", "focus_exited" - self.left_stick = find("LeftStick", Control) + + for button in [self.up, self.down]: + self.bind_signal(button, "button_up", button.name) + self.bind_signal(button, "button_down", button.name) + state.local_flags.changes: self.left_stick.visible = TouchControls in state.local_flags + self.up.visible = TouchControls in state.local_flags + self.down.visible = + TouchControls in state.local_flags and Flying in state.local_flags + + method on_button_up(name: string) = + var ev = gdnew[InputEventAction]() + ev.action = if name == "Up": "jump" else: "crouch" + ev.pressed = false + parse_input_event(ev) + + method on_button_down(name: string) = + var ev = gdnew[InputEventAction]() + ev.action = if name == "Up": "jump" else: "crouch" + ev.pressed = true + parse_input_event(ev) method on_mouse_entered() = state.push_flag ViewportFocused