add selection mode ui #5

This commit is contained in:
Cat Flynn 2022-12-09 00:30:53 +00:00
parent 989cd8f3c9
commit 2bc92d9580
14 changed files with 247 additions and 174 deletions

View File

@ -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"]

View File

@ -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 )

View File

@ -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

View File

@ -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"]

View File

@ -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="."]

View File

@ -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"]

View File

@ -38,26 +38,26 @@ 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)
{
case ButtonList.Left:
if (mouseButtonEvent.Pressed)
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);
}
else
{
_material.SetShaderParam(T, 0f);
}
break;
}
}
}
public override void _Process(float delta)
@ -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;
}
}

View File

@ -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;
}
}
}

View File

@ -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<BuildMode>(_buildModePath);
Reset();
}
public void Reset()
{
Current = Mode.SELECT;
}
}

View File

@ -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<SelectionMode>(_selectionModePath);
_buildMode = GetNode<BuildMode>(_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();
}
}

View File

@ -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();
}
}

View File

@ -0,0 +1,6 @@
using Godot;
using System;
public class SelectionMode : Mode
{
}

View File

@ -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)

View File

@ -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<SelectionMode>(_selectionModePath);
}
public override void _Process(float delta)
{
base._Process(delta);
Visible = _selectionMode.Active;
}
}