From 2bc92d958097fca323de94d89558577b001e8303 Mon Sep 17 00:00:00 2001 From: Cat Flynn Date: Fri, 9 Dec 2022 00:30:53 +0000 Subject: [PATCH] add selection mode ui #5 --- half-earth/nodes/game.tscn | 15 +++- .../interaction_modes/interaction_mode.tscn | 2 +- .../interaction_modes/selection_mode.tscn | 7 ++ half-earth/nodes/ui/build_mode_ui.tscn | 43 +++-------- half-earth/nodes/ui/debug_ui.tscn | 5 +- half-earth/nodes/ui/selection_mode_ui.tscn | 19 +++++ half-earth/scripts/GridCursor.cs | 43 +++++------ .../scripts/interaction_modes/BuildMode.cs | 59 +-------------- .../interaction_modes/InteractionMode.cs | 54 ------------- .../interaction_modes/InteractionModes.cs | 75 +++++++++++++++++++ half-earth/scripts/interaction_modes/Mode.cs | 63 ++++++++++++++++ .../interaction_modes/SelectionMode.cs | 6 ++ half-earth/scripts/ui/DebugUI.cs | 4 +- half-earth/scripts/ui/SelectionModeUI.cs | 26 +++++++ 14 files changed, 247 insertions(+), 174 deletions(-) create mode 100644 half-earth/nodes/interaction_modes/selection_mode.tscn create mode 100644 half-earth/nodes/ui/selection_mode_ui.tscn delete mode 100644 half-earth/scripts/interaction_modes/InteractionMode.cs create mode 100644 half-earth/scripts/interaction_modes/InteractionModes.cs create mode 100644 half-earth/scripts/interaction_modes/Mode.cs create mode 100644 half-earth/scripts/interaction_modes/SelectionMode.cs create mode 100644 half-earth/scripts/ui/SelectionModeUI.cs diff --git a/half-earth/nodes/game.tscn b/half-earth/nodes/game.tscn index 18d4afc..261aa83 100644 --- a/half-earth/nodes/game.tscn +++ b/half-earth/nodes/game.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=9 format=2] +[gd_scene load_steps=11 format=2] [ext_resource path="res://nodes/grid.tscn" type="PackedScene" id=1] [ext_resource path="res://nodes/grid_cursor.tscn" type="PackedScene" id=2] @@ -7,6 +7,8 @@ [ext_resource path="res://nodes/interaction_modes/interaction_mode.tscn" type="PackedScene" id=5] [ext_resource path="res://nodes/ui/build_mode_ui.tscn" type="PackedScene" id=6] [ext_resource path="res://nodes/interaction_modes/build_mode.tscn" type="PackedScene" id=7] +[ext_resource path="res://nodes/interaction_modes/selection_mode.tscn" type="PackedScene" id=8] +[ext_resource path="res://nodes/ui/selection_mode_ui.tscn" type="PackedScene" id=9] [sub_resource type="Curve" id=1] _data = [ Vector2( 0, 0 ), 0.0, 5.0, 0, 0, Vector2( 0.5, 1 ), 0.0, 0.0, 0, 0, Vector2( 1, 0 ), -4.0, 0.0, 0, 0 ] @@ -25,19 +27,27 @@ _pulseShape = SubResource( 1 ) [node name="Interaction Mode" parent="." instance=ExtResource( 5 )] _buildModePath = NodePath("../Build Mode") +_selectionModePath = NodePath("../Selection Mode") [node name="Build Mode" parent="." instance=ExtResource( 7 )] _key = 66 +[node name="Selection Mode" parent="." instance=ExtResource( 8 )] + [node name="UI" type="Control" parent="."] anchor_right = 1.0 anchor_bottom = 1.0 +mouse_filter = 2 [node name="Debug" parent="UI" instance=ExtResource( 3 )] [node name="Build Mode" parent="UI" instance=ExtResource( 6 )] +visible = false _buildModePath = NodePath("../../Build Mode") +[node name="Selection Mode" parent="UI" instance=ExtResource( 9 )] +_selectionModePath = NodePath("../../Selection Mode") + [connection signal="OnPauseChanged" from="Clock" to="UI/Debug" method="_on_Clock_OnPauseChanged"] [connection signal="OnTick" from="Clock" to="Grid" method="_on_Clock_OnTick"] [connection signal="OnTick" from="Clock" to="UI/Debug" method="_on_Clock_OnTick"] @@ -45,4 +55,7 @@ _buildModePath = NodePath("../../Build Mode") [connection signal="OnInteractionModeChanged" from="Interaction Mode" to="UI/Debug" method="_on_Interaction_Mode_OnInteractionModeChanged"] [connection signal="OnModeEntered" from="Build Mode" to="Interaction Mode" method="_on_Build_Mode_OnModeEntered"] [connection signal="OnModeExited" from="Build Mode" to="Interaction Mode" method="_on_Build_Mode_OnModeExited"] +[connection signal="OnModeEntered" from="Selection Mode" to="Interaction Mode" method="_on_Selection_Mode_OnModeEntered"] +[connection signal="OnModeExited" from="Selection Mode" to="Interaction Mode" method="_on_Selection_Mode_OnModeExited"] [connection signal="OnExit" from="UI/Build Mode" to="Interaction Mode" method="Reset"] +[connection signal="EnableBuildMode" from="UI/Selection Mode" to="Build Mode" method="Enable"] diff --git a/half-earth/nodes/interaction_modes/interaction_mode.tscn b/half-earth/nodes/interaction_modes/interaction_mode.tscn index a0f0926..c5caa48 100644 --- a/half-earth/nodes/interaction_modes/interaction_mode.tscn +++ b/half-earth/nodes/interaction_modes/interaction_mode.tscn @@ -1,6 +1,6 @@ [gd_scene load_steps=2 format=2] -[ext_resource path="res://scripts/interaction_modes/InteractionMode.cs" type="Script" id=1] +[ext_resource path="res://scripts/interaction_modes/InteractionModes.cs" type="Script" id=1] [node name="Interaction Mode" type="Node"] script = ExtResource( 1 ) diff --git a/half-earth/nodes/interaction_modes/selection_mode.tscn b/half-earth/nodes/interaction_modes/selection_mode.tscn new file mode 100644 index 0000000..bcc4fc8 --- /dev/null +++ b/half-earth/nodes/interaction_modes/selection_mode.tscn @@ -0,0 +1,7 @@ +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://scripts/interaction_modes/SelectionMode.cs" type="Script" id=1] + +[node name="Selection Mode" type="Node"] +script = ExtResource( 1 ) +_key = 16777217 diff --git a/half-earth/nodes/ui/build_mode_ui.tscn b/half-earth/nodes/ui/build_mode_ui.tscn index 9e0be0e..7b13ee8 100644 --- a/half-earth/nodes/ui/build_mode_ui.tscn +++ b/half-earth/nodes/ui/build_mode_ui.tscn @@ -3,43 +3,24 @@ [ext_resource path="res://scripts/ui/BuildModeUI.cs" type="Script" id=1] [node name="Build Mode" type="Control"] -anchor_right = 1.0 +anchor_top = 1.0 anchor_bottom = 1.0 +margin_top = -600.0 +margin_bottom = -600.0 script = ExtResource( 1 ) -[node name="ColorRect" type="ColorRect" parent="."] -anchor_top = 1.0 -anchor_right = 1.0 -anchor_bottom = 1.0 -margin_top = -40.0 -grow_horizontal = 0 -grow_vertical = 0 - -[node name="Exit" type="Button" parent="ColorRect"] +[node name="Exit" type="Button" parent="."] margin_left = 2.0 -margin_top = 2.0 +margin_top = 562.0 margin_right = 38.0 -margin_bottom = 38.0 +margin_bottom = 598.0 +text = "Exit" -[node name="Label" type="Label" parent="ColorRect/Exit"] -anchor_right = 1.0 -anchor_bottom = 1.0 -text = "exit" -align = 1 -valign = 1 - -[node name="Exit2" type="Button" parent="ColorRect"] +[node name="Developed" type="Button" parent="."] margin_left = 54.0 -margin_top = 2.0 +margin_top = 562.0 margin_right = 90.0 -margin_bottom = 38.0 +margin_bottom = 598.0 +text = "Developed" -[node name="Label" type="Label" parent="ColorRect/Exit2"] -anchor_right = 1.0 -anchor_bottom = 1.0 -text = "developed" -align = 1 -valign = 1 -autowrap = true - -[connection signal="pressed" from="ColorRect/Exit" to="." method="_on_Exit_pressed"] +[connection signal="pressed" from="Exit" to="." method="_on_Exit_pressed"] diff --git a/half-earth/nodes/ui/debug_ui.tscn b/half-earth/nodes/ui/debug_ui.tscn index a880e13..6542174 100644 --- a/half-earth/nodes/ui/debug_ui.tscn +++ b/half-earth/nodes/ui/debug_ui.tscn @@ -3,8 +3,9 @@ [ext_resource path="res://scripts/ui/DebugUI.cs" type="Script" id=1] [node name="Debug UI" type="Control"] +anchor_left = 1.0 anchor_right = 1.0 -anchor_bottom = 1.0 +mouse_filter = 2 script = ExtResource( 1 ) TicksLabelPath = NodePath("Ticks") PauseLabelPath = NodePath("Paused") @@ -19,7 +20,7 @@ margin_right = -14.0 margin_bottom = 29.0 grow_horizontal = 0 grow_vertical = 0 -text = "mode: SELECT" +text = "mode: Selection" align = 2 [node name="Ticks" type="Label" parent="."] diff --git a/half-earth/nodes/ui/selection_mode_ui.tscn b/half-earth/nodes/ui/selection_mode_ui.tscn new file mode 100644 index 0000000..58fd1c5 --- /dev/null +++ b/half-earth/nodes/ui/selection_mode_ui.tscn @@ -0,0 +1,19 @@ +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://scripts/ui/SelectionModeUI.cs" type="Script" id=1] + +[node name="Selection Mode UI" type="Control"] +anchor_top = 1.0 +anchor_bottom = 1.0 +margin_top = -600.0 +margin_bottom = -600.0 +script = ExtResource( 1 ) + +[node name="Build Mode" type="Button" parent="."] +margin_left = 2.0 +margin_top = 562.0 +margin_right = 38.0 +margin_bottom = 598.0 +text = "Build Mode" + +[connection signal="pressed" from="Build Mode" to="." method="_on_Build_Mode_pressed"] diff --git a/half-earth/scripts/GridCursor.cs b/half-earth/scripts/GridCursor.cs index 3039a65..c6f7a1f 100644 --- a/half-earth/scripts/GridCursor.cs +++ b/half-earth/scripts/GridCursor.cs @@ -38,25 +38,25 @@ public class GridCursor : Sprite this.Visible = _grid.IsInBounds(x, y); var position = new Vector2(x + .5f, y + .5f) * _grid.CellSize; this.Position = position; + return; } - else if (@event is InputEventMouseButton mouseButtonEvent) + + if (@event is InputEventMouseButton mouseButtonEvent) { - switch ((ButtonList)mouseButtonEvent.ButtonIndex) + var button = (ButtonList)mouseButtonEvent.ButtonIndex; + if (button != ButtonList.Left) + return; + + if (!mouseButtonEvent.Pressed) { - case ButtonList.Left: - if (mouseButtonEvent.Pressed) - { - var pos = mouseButtonEvent.Position; - _grid.GetGridPos(pos, out var x, out var y); - _grid.SetTileValue(x, y, 1.0f); - _material.SetShaderParam(T, 1f); - } - else - { - _material.SetShaderParam(T, 0f); - } - break; + _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); } } @@ -74,17 +74,8 @@ public class GridCursor : Sprite _material.SetShaderParam(T, a); } - public void _on_Interaction_Mode_OnInteractionModeChanged(InteractionMode.Mode oldMode, InteractionMode.Mode newMode) + public void _on_Interaction_Mode_OnInteractionModeChanged(Mode oldMode, Mode newMode) { - switch (newMode) - { - case InteractionMode.Mode.SELECT: - _pulsing = false; - break; - - case InteractionMode.Mode.BUILD: - _pulsing = true; - break; - } + _pulsing = newMode is BuildMode; } } diff --git a/half-earth/scripts/interaction_modes/BuildMode.cs b/half-earth/scripts/interaction_modes/BuildMode.cs index 321311e..82e27fa 100644 --- a/half-earth/scripts/interaction_modes/BuildMode.cs +++ b/half-earth/scripts/interaction_modes/BuildMode.cs @@ -1,66 +1,9 @@ using Godot; using System; -public class BuildMode : Node +public class BuildMode : Mode { - [Signal] - delegate void OnModeEntered(); - [Signal] - delegate void OnModeExited(); - - [Export] - private KeyList _key; - - public bool Active - { - get => _active; - private set - { - if (value == _active) - return; - - _active = value; - - var signal = _active - ? nameof(OnModeEntered) - : nameof(OnModeExited); - EmitSignal(signal); - } - } - private bool _active; - private const int NO_TILE_TYPE_SELECTED = -1; public int SelectedTileType { get; set; } = NO_TILE_TYPE_SELECTED; - - public override void _Ready() - { - - } - - public override void _Input(InputEvent @event) - { - base._Input(@event); - - if (!(@event is InputEventKey keyEvent)) - return; - - if (!keyEvent.Pressed) - return; - - var key = (KeyList)keyEvent.Scancode; - - // TODO: this should be developed into a UI stack - if (key == KeyList.Escape) - { - Active = false; - return; - } - - if (key == _key) - { - Active = true; - return; - } - } } diff --git a/half-earth/scripts/interaction_modes/InteractionMode.cs b/half-earth/scripts/interaction_modes/InteractionMode.cs deleted file mode 100644 index 4f2e64b..0000000 --- a/half-earth/scripts/interaction_modes/InteractionMode.cs +++ /dev/null @@ -1,54 +0,0 @@ -using Godot; -using System; - -public class InteractionMode : Node -{ - [Export] - private NodePath _buildModePath; - private BuildMode _buildMode; - - [Signal] - delegate void OnInteractionModeChanged(Mode oldMode, Mode newMode); - - public enum Mode - { - SELECT = 0, - BUILD = 1 - } - - public Mode Current { get; private set; } - - public void _on_Build_Mode_OnModeEntered() - { - ChangeMode(Mode.BUILD); - } - public void _on_Build_Mode_OnModeExited() - { - ChangeMode(Mode.SELECT); - } - - private void ChangeMode(Mode mode) - { - if (Current == mode) - return; - - var old = Current; - Current = mode; - - EmitSignal(nameof(OnInteractionModeChanged), old, mode); - } - - public override void _Ready() - { - base._Ready(); - - _buildMode = GetNode(_buildModePath); - - Reset(); - } - - public void Reset() - { - Current = Mode.SELECT; - } -} diff --git a/half-earth/scripts/interaction_modes/InteractionModes.cs b/half-earth/scripts/interaction_modes/InteractionModes.cs new file mode 100644 index 0000000..dc51224 --- /dev/null +++ b/half-earth/scripts/interaction_modes/InteractionModes.cs @@ -0,0 +1,75 @@ +using Godot; +using System; + +public class InteractionModes : Node +{ + [Export] + private NodePath _buildModePath; + private BuildMode _buildMode; + [Export] + private NodePath _selectionModePath; + private SelectionMode _selectionMode; + + [Signal] + delegate void OnInteractionModeChanged(Mode oldMode, Mode newMode); + + private Mode _current = null; + + public override void _Ready() + { + base._Ready(); + + _selectionMode = GetNode(_selectionModePath); + _buildMode = GetNode(_buildModePath); + } + + public override void _Process(float delta) + { + base._Process(delta); + + if (_current == null) + { + Reset(); + } + } + + public void _on_Build_Mode_OnModeEntered() + { + _selectionMode.Disable(); + ChangeMode(_selectionMode, _buildMode); + } + public void _on_Build_Mode_OnModeExited() + { + } + + public void _on_Selection_Mode_OnModeEntered() + { + _buildMode.Disable(); + ChangeMode(_buildMode, _selectionMode); + } + public void _on_Selection_Mode_OnModeExited() + { + } + + private void ChangeMode(Mode oldMode, Mode newMode) + { + if (oldMode == newMode) + { + throw new InvalidOperationException(); + } + + if (_current != null && oldMode != _current) + { + throw new InvalidOperationException(); + } + + _current = newMode; + EmitSignal(nameof(OnInteractionModeChanged), oldMode, newMode); + } + + public void Reset() + { + _buildMode.Disable(); + _selectionMode.Enable(); + } +} diff --git a/half-earth/scripts/interaction_modes/Mode.cs b/half-earth/scripts/interaction_modes/Mode.cs new file mode 100644 index 0000000..7e58739 --- /dev/null +++ b/half-earth/scripts/interaction_modes/Mode.cs @@ -0,0 +1,63 @@ +using Godot; +using System; + +public abstract class Mode : Node +{ + [Signal] + delegate void OnModeEntered(); + [Signal] + delegate void OnModeExited(); + + [Export] + private KeyList _key; + + public bool Active + { + get => _active; + private set + { + if (value == _active) + return; + + _active = value; + + var signal = _active + ? nameof(OnModeEntered) + : nameof(OnModeExited); + EmitSignal(signal); + } + } + private bool _active; + + public override void _Input(InputEvent @event) + { + base._Input(@event); + + if (!(@event is InputEventKey keyEvent)) + return; + + if (!keyEvent.Pressed) + return; + + var key = (KeyList)keyEvent.Scancode; + + if (key == _key) + { + Active = true; + } + } + + public void Enable() + { + Active = true; + } + public void Disable() + { + Active = false; + } + + public override string ToString() + { + return GetType().ToString(); + } +} diff --git a/half-earth/scripts/interaction_modes/SelectionMode.cs b/half-earth/scripts/interaction_modes/SelectionMode.cs new file mode 100644 index 0000000..9e88fe0 --- /dev/null +++ b/half-earth/scripts/interaction_modes/SelectionMode.cs @@ -0,0 +1,6 @@ +using Godot; +using System; + +public class SelectionMode : Mode +{ +} diff --git a/half-earth/scripts/ui/DebugUI.cs b/half-earth/scripts/ui/DebugUI.cs index 5c6fe7a..c810527 100644 --- a/half-earth/scripts/ui/DebugUI.cs +++ b/half-earth/scripts/ui/DebugUI.cs @@ -28,8 +28,10 @@ public class DebugUI : Control public void _on_Clock_OnTick(int ticks) => SetLabelText(_ticksLabel, "ticks", ticks); - public void _on_Interaction_Mode_OnInteractionModeChanged(InteractionMode.Mode oldMode, InteractionMode.Mode newMode) => + public void _on_Interaction_Mode_OnInteractionModeChanged(Mode oldMode, Mode newMode) + { SetLabelText(_interactionModeLabel, "mode", newMode); + } #endregion private void SetLabelText(Label label, string key, object value) diff --git a/half-earth/scripts/ui/SelectionModeUI.cs b/half-earth/scripts/ui/SelectionModeUI.cs new file mode 100644 index 0000000..10ef357 --- /dev/null +++ b/half-earth/scripts/ui/SelectionModeUI.cs @@ -0,0 +1,26 @@ +using Godot; +using System; + +public class SelectionModeUI : Control +{ + [Signal] + delegate void EnableBuildMode(); + + [Export] + private NodePath _selectionModePath; + public SelectionMode _selectionMode; + + public void _on_Build_Mode_pressed() => EmitSignal(nameof(EnableBuildMode)); + + public override void _Ready() + { + _selectionMode = GetNode(_selectionModePath); + } + + public override void _Process(float delta) + { + base._Process(delta); + + Visible = _selectionMode.Active; + } +}