Files
linuxdistro-roguelite/Assets/Scripts/UI/ExecutionTrackView.cs
T
2026-10-22 21:59:50 +02:00

129 lines
5.2 KiB
C#

using System;
using System.Collections.Generic;
using KernelPanic.Combat;
using UnityEngine.UIElements;
namespace KernelPanic.UI
{
/// <summary>Retained scheduler readout. Completion badges are receipts, not delayed execution.</summary>
internal sealed class ExecutionTrackView : IDisposable
{
private readonly VisualElement _contents;
private readonly bool _stack;
private readonly Dictionary<CardInstance, VisualElement> _chips = new();
private readonly Dictionary<CardInstance, IVisualElementScheduledItem> _completed = new();
private CardInstance _active;
private bool _disposed;
public ExecutionTrackView(VisualElement contents, bool stack)
{
_contents = contents;
_stack = stack;
contents.EnableInClassList("stack-contents", stack);
}
private VisualElement Chip(CardInstance card)
{
if (_chips.TryGetValue(card, out var chip)) return chip;
chip = new VisualElement { userData = card, tooltip = CardEffectFactory.GetRulesText(card) };
chip.AddToClassList("card-chip");
chip.Add(SoftwareCardView.Text("", "execution-order"));
chip.Add(SoftwareCardView.Text(card.Definition.DisplayName, "card-chip-name"));
_chips.Add(card, chip);
_contents.Add(chip);
return chip;
}
public void Sync(IReadOnlyList<CardInstance> pending)
{
if (_disposed) return;
_contents.Q(className: "track-empty")?.RemoveFromHierarchy();
var retained = new HashSet<CardInstance>(pending);
if (_active != null) retained.Add(_active);
var stale = new List<CardInstance>();
foreach (var entry in _chips)
if (!retained.Contains(entry.Key) && !_completed.ContainsKey(entry.Key)) stale.Add(entry.Key);
foreach (var card in stale) { _chips[card].RemoveFromHierarchy(); _chips.Remove(card); }
for (int i = 0; i < pending.Count; i++)
{
var card = pending[i];
if (_completed.Remove(card, out var oldReceipt)) oldReceipt.Pause();
var chip = Chip(card);
chip.RemoveFromClassList("operation-complete");
chip.RemoveFromClassList("operation-active");
chip.EnableInClassList("operation-next", i == 0);
chip.Q<Label>(className: "execution-order").text = _stack
? i == 0 ? "TOP >" : $" {i + 1:00} |"
: $"{i + 1:00} {(i == 0 ? ">" : ":")}";
if (_contents.IndexOf(chip) != i) _contents.Insert(i, chip);
}
UpdateIdle();
}
public void Resolving(CardInstance card)
{
if (_disposed || card?.Definition == null) return;
_active = card;
var chip = Chip(card);
chip.RemoveFromClassList("operation-next");
chip.AddToClassList("operation-active");
chip.Q<Label>(className: "execution-order").text = _stack ? "POP >" : "RUN >";
UpdateIdle();
}
public void Resolved(CardInstance card, int sequence)
{
if (_disposed || card?.Definition == null) return;
if (_active == card) _active = null;
var chip = Chip(card);
chip.RemoveFromClassList("operation-active");
chip.RemoveFromClassList("operation-next");
chip.AddToClassList("operation-complete");
chip.Q<Label>(className: "execution-order").text = $"{sequence:00} OK";
chip.BringToFront(); // Completed receipts follow pending work in chronological order.
if (_completed.Remove(card, out var previous)) previous.Pause();
_completed[card] = _contents.schedule.Execute(() =>
{
if (_disposed) return;
_completed.Remove(card);
_chips.Remove(card);
chip.RemoveFromHierarchy();
UpdateIdle();
}).StartingIn(650);
UpdateIdle();
}
public void EndExecution()
{
// The combat engine can leave resolution early on victory/loss, without a completion event.
// Drop that live indicator; do not manufacture an OK receipt.
if (_active == null) return;
if (_chips.Remove(_active, out var chip)) chip.RemoveFromHierarchy();
_active = null;
UpdateIdle();
}
private void UpdateIdle()
{
bool idle = _chips.Count == 0;
((VisualElement)_contents.userData).EnableInClassList("track-idle", idle);
if (!idle) _contents.Q(className: "track-empty")?.RemoveFromHierarchy();
else if (_contents.Q(className: "track-empty") == null)
{
var label = SoftwareCardView.Text("empty", "empty-state");
label.AddToClassList("track-empty");
_contents.Add(label);
}
}
public void Dispose()
{
_disposed = true;
foreach (var item in _completed.Values) item.Pause();
_completed.Clear();
_chips.Clear();
_contents.Clear();
}
}
}