2026-04-10 14:58:03 +02:00
|
|
|
using Godot;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using Chessistics.Engine.Model;
|
|
|
|
|
|
|
|
|
|
namespace Chessistics.Scripts.UI;
|
|
|
|
|
|
|
|
|
|
public partial class PieceStockPanel : VBoxContainer
|
|
|
|
|
{
|
|
|
|
|
[Signal]
|
|
|
|
|
public delegate void PieceSelectedEventHandler(int kindIndex);
|
|
|
|
|
|
|
|
|
|
private readonly Dictionary<PieceKind, (Button button, Label countLabel, int remaining)> _entries = new();
|
|
|
|
|
private PieceKind? _selectedKind;
|
|
|
|
|
|
|
|
|
|
public PieceKind? SelectedKind => _selectedKind;
|
|
|
|
|
|
|
|
|
|
public void Setup(IReadOnlyList<PieceStock> stock)
|
|
|
|
|
{
|
|
|
|
|
foreach (var child in GetChildren())
|
|
|
|
|
child.QueueFree();
|
|
|
|
|
_entries.Clear();
|
|
|
|
|
_selectedKind = null;
|
|
|
|
|
|
|
|
|
|
var title = new Label { Text = "PIECES" };
|
|
|
|
|
title.AddThemeFontSizeOverride("font_size", 16);
|
2026-04-10 21:44:12 +02:00
|
|
|
title.AddThemeColorOverride("font_color", new Color("#FFD700"));
|
2026-04-10 14:58:03 +02:00
|
|
|
AddChild(title);
|
|
|
|
|
|
|
|
|
|
AddChild(new HSeparator());
|
|
|
|
|
|
|
|
|
|
foreach (var entry in stock)
|
|
|
|
|
{
|
|
|
|
|
var hbox = new HBoxContainer();
|
|
|
|
|
|
|
|
|
|
var button = new Button
|
|
|
|
|
{
|
|
|
|
|
Text = GetPieceName(entry.Kind),
|
|
|
|
|
CustomMinimumSize = new Vector2(120, 32),
|
|
|
|
|
ToggleMode = true
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var countLabel = new Label { Text = $"x{entry.Count}" };
|
|
|
|
|
countLabel.AddThemeFontSizeOverride("font_size", 14);
|
|
|
|
|
|
|
|
|
|
var kind = entry.Kind;
|
|
|
|
|
button.Pressed += () => OnPieceButtonPressed(kind);
|
|
|
|
|
|
|
|
|
|
hbox.AddChild(button);
|
|
|
|
|
hbox.AddChild(countLabel);
|
|
|
|
|
AddChild(hbox);
|
|
|
|
|
|
|
|
|
|
_entries[entry.Kind] = (button, countLabel, entry.Count);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnPieceButtonPressed(PieceKind kind)
|
|
|
|
|
{
|
|
|
|
|
if (_selectedKind == kind)
|
|
|
|
|
{
|
|
|
|
|
_selectedKind = null;
|
|
|
|
|
UpdateButtonStates();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_selectedKind = kind;
|
|
|
|
|
UpdateButtonStates();
|
|
|
|
|
EmitSignal(SignalName.PieceSelected, (int)kind);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateButtonStates()
|
|
|
|
|
{
|
|
|
|
|
foreach (var (k, (button, _, remaining)) in _entries)
|
|
|
|
|
{
|
|
|
|
|
button.ButtonPressed = k == _selectedKind;
|
|
|
|
|
button.Disabled = remaining <= 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UpdateCount(PieceKind kind, int remaining)
|
|
|
|
|
{
|
|
|
|
|
if (!_entries.TryGetValue(kind, out var entry)) return;
|
|
|
|
|
_entries[kind] = (entry.button, entry.countLabel, remaining);
|
|
|
|
|
entry.countLabel.Text = $"x{remaining}";
|
|
|
|
|
entry.button.Disabled = remaining <= 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ClearSelection()
|
|
|
|
|
{
|
|
|
|
|
_selectedKind = null;
|
|
|
|
|
UpdateButtonStates();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string GetPieceName(PieceKind kind) => kind switch
|
|
|
|
|
{
|
|
|
|
|
PieceKind.Rook => "Tour II",
|
|
|
|
|
PieceKind.Bishop => "Fou II",
|
|
|
|
|
PieceKind.Knight => "Cavalier",
|
|
|
|
|
_ => kind.ToString()
|
|
|
|
|
};
|
|
|
|
|
}
|