86 lines
2.1 KiB
C#
86 lines
2.1 KiB
C#
using Godot;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
|
|
public class BuildModeUI : Control
|
|
{
|
|
[Signal]
|
|
delegate void OnExit();
|
|
|
|
[Export]
|
|
private NodePath _buildModePath;
|
|
private BuildMode _buildMode;
|
|
|
|
private readonly Dictionary<TileType, Button> _typeButtons
|
|
= new Dictionary<TileType, Button>();
|
|
|
|
// Called when the node enters the scene tree for the first time.
|
|
public override void _Ready()
|
|
{
|
|
_buildMode = GetNode<BuildMode>(_buildModePath);
|
|
|
|
var buttonHeight = 40;
|
|
|
|
int x = 50;
|
|
int margin = 5;
|
|
foreach (var tt in _buildMode.BuildableTiles)
|
|
{
|
|
SpawnButton(tt, buttonHeight, ref x);
|
|
x += margin;
|
|
}
|
|
}
|
|
|
|
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;
|
|
|
|
var button = new Button();
|
|
this.AddChild(button);
|
|
|
|
button.Text = tileType.BuildLabel;
|
|
var parameters = new Godot.Collections.Array(button, tileType);
|
|
button.Connect("pressed", this, nameof(SelectTileType), parameters);
|
|
|
|
button.SetAnchorsPreset(LayoutPreset.BottomLeft);
|
|
button.MarginBottom = 0;
|
|
|
|
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(Button button, TileType tileType)
|
|
{
|
|
_buildMode.SelectedTileType = button.Pressed
|
|
? tileType
|
|
: null;
|
|
}
|
|
|
|
public void Exit()
|
|
{
|
|
EmitSignal(nameof(OnExit));
|
|
}
|
|
|
|
public override void _Process(float delta)
|
|
{
|
|
base._Process(delta);
|
|
|
|
Visible = _buildMode.Active;
|
|
}
|
|
}
|