From 52287d413ce1e4ed8674844a1b01f719439aa83b Mon Sep 17 00:00:00 2001 From: Cat Flynn Date: Sat, 10 Dec 2022 00:35:35 +0000 Subject: [PATCH] improve ui readability #5 --- half-earth/nodes/game.tscn | 1 + .../resources/tile_types/developed.tres | 1 + half-earth/scripts/GridCursor.cs | 18 ------------- half-earth/scripts/TileType.cs | 3 +++ .../scripts/interaction_modes/BuildMode.cs | 4 +++ half-earth/scripts/ui/BuildModeUI.cs | 27 ++++++++++++++++--- 6 files changed, 32 insertions(+), 22 deletions(-) diff --git a/half-earth/nodes/game.tscn b/half-earth/nodes/game.tscn index 6fb6de1..4a42a0a 100644 --- a/half-earth/nodes/game.tscn +++ b/half-earth/nodes/game.tscn @@ -51,6 +51,7 @@ _buildModePath = NodePath("../../Interaction Modes/Build Mode") [connection signal="OnModeEntered" from="Interaction Modes/Build Mode" to="Interaction Modes" method="_on_Build_Mode_OnModeEntered"] [connection signal="OnModeEntered" from="Interaction Modes/Build Mode" to="UI/Mode Selection" method="EnableBuildMode"] [connection signal="OnModeExited" from="Interaction Modes/Build Mode" to="Interaction Modes" method="_on_Build_Mode_OnModeExited"] +[connection signal="SelectedTileTypeChanged" from="Interaction Modes/Build Mode" to="UI/Build Mode" method="UpdateButtonToggleState"] [connection signal="OnExit" from="UI/Build Mode" to="Interaction Modes" method="Reset"] [connection signal="OnExit" from="UI/Build Mode" to="UI/Mode Selection" method="Enable"] [connection signal="BuildModeEnabled" from="UI/Mode Selection" to="Interaction Modes/Build Mode" method="Enable"] diff --git a/half-earth/resources/tile_types/developed.tres b/half-earth/resources/tile_types/developed.tres index 07fcbbf..011fa7b 100644 --- a/half-earth/resources/tile_types/developed.tres +++ b/half-earth/resources/tile_types/developed.tres @@ -5,5 +5,6 @@ [resource] script = ExtResource( 1 ) Name = "Developed" +BuildLabel = "[D]eveloped" Key = 68 Color = Color( 0.372549, 0.372549, 0.372549, 1 ) diff --git a/half-earth/scripts/GridCursor.cs b/half-earth/scripts/GridCursor.cs index 2d5be80..b41a5fd 100644 --- a/half-earth/scripts/GridCursor.cs +++ b/half-earth/scripts/GridCursor.cs @@ -46,24 +46,6 @@ public class GridCursor : Sprite this.Position = position; return; } - - //if (@event is InputEventMouseButton mouseButtonEvent) - //{ - // var button = (ButtonList)mouseButtonEvent.ButtonIndex; - // if (button != ButtonList.Left) - // return; - - // if (!mouseButtonEvent.Pressed) - // { - // _material.SetShaderParam(T, 0f); - // return; - // } - - // var pos = mouseButtonEvent.Position; - // _grid.GetGridPos(pos, out var x, out var y); - // _grid.SetTileValue(x, y, 1.0f); - // _material.SetShaderParam(T, 1f); - //} } public override void _Process(float delta) diff --git a/half-earth/scripts/TileType.cs b/half-earth/scripts/TileType.cs index a358d72..c6dc2ab 100644 --- a/half-earth/scripts/TileType.cs +++ b/half-earth/scripts/TileType.cs @@ -5,6 +5,9 @@ public class TileType : Resource [Export] public string Name { get; private set; } + [Export] + public string BuildLabel { get; private set; } + [Export] public KeyList Key { get; private set; } diff --git a/half-earth/scripts/interaction_modes/BuildMode.cs b/half-earth/scripts/interaction_modes/BuildMode.cs index 7add3ea..d2b2b25 100644 --- a/half-earth/scripts/interaction_modes/BuildMode.cs +++ b/half-earth/scripts/interaction_modes/BuildMode.cs @@ -3,6 +3,9 @@ using System; public class BuildMode : Mode { + [Signal] + delegate void SelectedTileTypeChanged(); + [Export] private NodePath _gridPath; private WorldGrid _grid; @@ -33,6 +36,7 @@ public class BuildMode : Mode } _selectedTileType = value; + EmitSignal(nameof(SelectedTileTypeChanged), _selectedTileType); } } private TileType _selectedTileType = null; diff --git a/half-earth/scripts/ui/BuildModeUI.cs b/half-earth/scripts/ui/BuildModeUI.cs index 06b6de8..92ddfa0 100644 --- a/half-earth/scripts/ui/BuildModeUI.cs +++ b/half-earth/scripts/ui/BuildModeUI.cs @@ -1,5 +1,6 @@ using Godot; using System; +using System.Collections.Generic; public class BuildModeUI : Control { @@ -10,6 +11,9 @@ public class BuildModeUI : Control private NodePath _buildModePath; private BuildMode _buildMode; + private readonly Dictionary _typeButtons + = new Dictionary(); + // Called when the node enters the scene tree for the first time. public override void _Ready() { @@ -22,6 +26,15 @@ public class BuildModeUI : Control SpawnButton(tileType, buttonHeight, ref x); } + private void UpdateButtonToggleState(TileType tileType) + { + foreach (var kvp in _typeButtons) + { + var button = _typeButtons[kvp.Key]; + button.SetPressedNoSignal(kvp.Key == tileType); + } + } + private void SpawnButton(TileType tileType, int buttonHeight, ref int x) { const int MARGIN = 5; @@ -29,8 +42,8 @@ public class BuildModeUI : Control var button = new Button(); this.AddChild(button); - button.Text = tileType.Name; - var parameters = new Godot.Collections.Array(tileType); + button.Text = tileType.BuildLabel; + var parameters = new Godot.Collections.Array(button, tileType); button.Connect("pressed", this, nameof(SelectTileType), parameters); button.SetAnchorsPreset(LayoutPreset.BottomLeft); @@ -39,13 +52,19 @@ public class BuildModeUI : Control var size = button.RectSize; button.RectSize = new Vector2(size.x, buttonHeight); + button.ToggleMode = true; + button.SetPosition(new Vector2(x, -buttonHeight)); x += Mathf.RoundToInt(size.x) + MARGIN; + + _typeButtons[tileType] = button; } - private void SelectTileType(TileType tileType) + private void SelectTileType(Button button, TileType tileType) { - _buildMode.SelectedTileType = tileType; + _buildMode.SelectedTileType = button.Pressed + ? tileType + : null; } public void Exit()