From e647cd7b2967cc93d697e41c81688dd294f66897 Mon Sep 17 00:00:00 2001 From: Gabriel Kaszewski Date: Thu, 19 Mar 2026 03:44:35 +0100 Subject: [PATCH] feat: marketplace keyboard/gamepad navigation --- objects/ui/marketplace_button.tscn | 1 + objects/ui/skill_button.tscn | 1 + scripts/UI/Marketplace.cs | 43 ++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/objects/ui/marketplace_button.tscn b/objects/ui/marketplace_button.tscn index ec805dd..d94892d 100644 --- a/objects/ui/marketplace_button.tscn +++ b/objects/ui/marketplace_button.tscn @@ -4,6 +4,7 @@ [ext_resource type="Script" uid="uid://vokgv56bjpf1" path="res://scripts/UI/MarketplaceButton.cs" id="2_vb2qn"] [node name="MarketplaceButton" type="Button" node_paths=PackedStringArray("SkillLevelContainer")] +focus_mode = 2 offset_right = 8.0 offset_bottom = 8.0 size_flags_horizontal = 3 diff --git a/objects/ui/skill_button.tscn b/objects/ui/skill_button.tscn index f7e892e..c38702e 100644 --- a/objects/ui/skill_button.tscn +++ b/objects/ui/skill_button.tscn @@ -4,6 +4,7 @@ [ext_resource type="Script" uid="uid://bw8dlgq86jrtt" path="res://scripts/UI/SkillButton.cs" id="2_m2732"] [node name="SkillButton" type="Button"] +focus_mode = 2 offset_right = 8.0 offset_bottom = 8.0 size_flags_horizontal = 3 diff --git a/scripts/UI/Marketplace.cs b/scripts/UI/Marketplace.cs index d5585b1..4114c86 100644 --- a/scripts/UI/Marketplace.cs +++ b/scripts/UI/Marketplace.cs @@ -38,6 +38,9 @@ public partial class Marketplace : Control SkillUnlockerComponent.SkillUnlocked += OnSkillUnlocked; EventBus.Instance.SkillCollected += OnGlobalSkillCollected; + + if (InputDeviceManager.Instance != null) + InputDeviceManager.Instance.DeviceChanged += OnDeviceChanged; } public override void _ExitTree() @@ -47,10 +50,21 @@ public partial class Marketplace : Control { EventBus.Instance.SkillCollected -= OnGlobalSkillCollected; } + + if (InputDeviceManager.Instance != null) + InputDeviceManager.Instance.DeviceChanged -= OnDeviceChanged; } public override void _Input(InputEvent @event) { + if (@event.IsActionPressed("ui_cancel") && IsVisible()) + { + Hide(); + foreach (var c in ComponentsToDisable) c.ProcessMode = ProcessModeEnum.Inherit; + GetViewport().SetInputAsHandled(); + return; + } + if (!@event.IsActionPressed("show_marketplace")) return; if (IsVisible()) @@ -62,6 +76,35 @@ public partial class Marketplace : Control { Show(); foreach (var c in ComponentsToDisable) c.ProcessMode = ProcessModeEnum.Disabled; + if (InputDeviceManager.Instance?.IsPointerless == true) + GrabFirstFocus(); + } + } + + private void OnDeviceChanged(int device) + { + var d = (InputDeviceManager.InputDevice)device; + if (d != InputDeviceManager.InputDevice.Mouse && IsVisible()) + GrabFirstFocus(); + } + + private void GrabFirstFocus() + { + foreach (var btn in _unlockButtons) + { + if (btn.Visible && !btn.Disabled) + { + btn.GrabFocus(); + return; + } + } + foreach (var btn in _skillButtons) + { + if (btn.Visible) + { + btn.GrabFocus(); + return; + } } }