Files
linuxdistro-roguelite/Assets/Scripts/UI/ExecutionPreviewView.cs
T
2026-10-29 22:54:29 +01:00

118 lines
6.3 KiB
C#

using System.Collections.Generic;
using System.Linq;
using KernelPanic.Combat;
using UnityEngine;
using UnityEngine.UIElements;
namespace KernelPanic.UI
{
internal sealed class ExecutionPreviewView
{
private readonly ScrollView _rows = new(ScrollViewMode.Vertical) { name = "ExecutionPreviewSteps" };
private CombatManager _combat;
private VisualElement _popup;
public bool IsOpen => _popup != null;
public void Open(VisualElement root)
{
if (IsOpen || root == null || _combat == null || _combat.RunFinished || _combat.AwaitingWaveContinue) return;
_popup = new VisualElement { name = "ExecutionPreviewPopup", focusable = true };
_popup.AddToClassList("execution-preview-shade");
var panel = TerminalLayout.Section("End-turn preview", "ExecutionPreviewDialog");
panel.AddToClassList("execution-preview-dialog");
panel.Add(TerminalLayout.BodyText("Execution order / current estimates", className: "execution-subtitle"));
_rows.AddToClassList("execution-dialog-scroll");
_rows.horizontalScrollerVisibility = ScrollerVisibility.Hidden;
panel.Add(_rows);
var close = new Button(Close) { text = "Close / Escape", name = "CloseExecutionPreview" };
close.AddToClassList("primary-action");
panel.Add(close);
_popup.Add(panel);
_popup.RegisterCallback<ClickEvent>(evt => { if (ReferenceEquals(evt.target, _popup)) Close(); });
_popup.RegisterCallback<KeyDownEvent>(evt =>
{
if (evt.keyCode == KeyCode.Escape) Close();
evt.StopPropagation();
});
TerminalTheme.ConstrainModalFocus(_popup);
root.Add(_popup);
_popup.BringToFront();
Refresh(_combat);
close.Focus();
}
public void Close()
{
var root = _popup?.parent;
_popup?.RemoveFromHierarchy();
_popup = null;
root?.Q<Button>("OpenExecutionPreview")?.Focus();
}
public void Refresh(CombatManager combat)
{
_combat = combat;
if (combat == null || combat.RunFinished || combat.AwaitingWaveContinue) { Close(); return; }
if (!IsOpen) return;
_rows.Clear();
var go = Step("01", "Go goroutines", combat.Go.Count == 0 ? "No active work" : "Eligible work ticks once");
foreach (var work in combat.Go.Active)
go.Add(Item(work.SourceCard.Definition.DisplayName,
$"PID {work.Id:000} / {work.RemainingTicks} ticks left",
work.DeploymentRound >= combat.RoundNumber ? "Starts next round" : "Ticks this turn"));
bool pythonActive = combat.Python.AutomationCount > 0 || combat.Python.RepeatCount > 0 || combat.Python.AreaCount > 0;
var automation = Step("02", "Python automation", pythonActive ? "Ready scripts run" : "No scripts ready");
if (pythonActive)
foreach (string line in combat.Python.Readout) automation.Add(TerminalLayout.BodyText(line));
var fifo = Step("03", "Python queue", combat.InterpreterQueue.Count == 0 ? "Empty / FIFO" : "FIFO / first in, first out");
foreach (var card in combat.InterpreterQueue.Cards) fifo.Add(Card(combat, card, card.TargetSnapshot));
var lifo = Step("04", "Haskell stack", combat.LazyStack.Count == 0 ? "Empty / LIFO" : "LIFO / newest first");
foreach (var entry in combat.LazyStack.Entries) lifo.Add(Card(combat, entry.Card, entry.Targets, entry.MultiplierLabel));
var enemies = Step("05", "Surviving enemies", "Announced intents / status ticks before each action");
foreach (var enemy in combat.Enemies)
{
if (enemy.PendingRevive) enemies.Add(Item(enemy.Name, "Revival pending", "Blocks wave completion"));
else if (!enemy.State.IsDefeated)
enemies.Add(Item(enemy.Name, enemy.DisplayIntent.DisplayText,
enemy.HasBehavior(EnemyBehaviorFlags.RacePair) ? "Order may swap / partner bonuses apply" : null));
}
Step("06", "Cleanup", "Status ticks, delayed effects and next-round resources");
var notes = new Foldout { text = "How to read estimates", value = false };
notes.Add(TerminalLayout.BodyText("Native cards and Python preparation have already run. Each estimate uses the current state independently. Earlier actions, deaths, retargeting, spawns, buffs and random rolls can change later results."));
_rows.Add(TerminalLayout.BodyText("Estimates only / outcomes can change as actions resolve.", className: "execution-muted"));
_rows.Add(notes);
}
private VisualElement Step(string number, string title, string subtitle)
{
var section = TerminalLayout.Section(number + " / " + title, sectionClass: "execution-step");
section.Add(TerminalLayout.BodyText(subtitle, className: "execution-muted"));
_rows.Add(section);
return section;
}
private static VisualElement Card(CombatManager combat, CardInstance card, IReadOnlyList<CombatantState> targets, string multiplier = null)
{
var names = targets.Select(target => ReferenceEquals(target, combat.PlayerState) ? "self"
: combat.Enemies.FirstOrDefault(enemy => ReferenceEquals(enemy.State, target))?.Name ?? "departed target");
string targetText = targets.Count == 0 ? "card-defined targets" : string.Join(", ", names);
return Item(card.Definition.DisplayName + (multiplier == null ? "" : " / " + multiplier),
"Target: " + targetText, CardPreviewService.Build(combat, card).Summary);
}
private static VisualElement Item(string title, string detail, string estimate)
{
var row = new VisualElement();
row.AddToClassList("execution-item");
row.Add(TerminalLayout.BodyText(title, className: "execution-item-title"));
row.Add(TerminalLayout.BodyText(detail, className: "execution-muted"));
if (!string.IsNullOrEmpty(estimate)) row.Add(TerminalLayout.BodyText(estimate, className: "execution-estimate"));
return row;
}
}
}