Refactor alot of stuff
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using KernelPanic.Combat;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
@@ -6,56 +8,27 @@ namespace KernelPanic.UI
|
||||
{
|
||||
internal sealed class ExecutionPreviewView
|
||||
{
|
||||
public Foldout Element { get; } = new() { text = "End-turn execution preview", name = "ExecutionPreview", value = false };
|
||||
private readonly ScrollView _rows = new(ScrollViewMode.Vertical);
|
||||
private readonly ScrollView _rows = new(ScrollViewMode.Vertical) { name = "ExecutionPreviewSteps" };
|
||||
private CombatManager _combat;
|
||||
private VisualElement _popup;
|
||||
public bool IsOpen => _popup != null;
|
||||
|
||||
public ExecutionPreviewView()
|
||||
public void Open(VisualElement root)
|
||||
{
|
||||
Element.AddToClassList("execution-preview");
|
||||
_rows.style.maxHeight = 220;
|
||||
_rows.horizontalScrollerVisibility = ScrollerVisibility.Hidden;
|
||||
Element.Add(_rows);
|
||||
Element.RegisterValueChangedCallback(evt =>
|
||||
{
|
||||
if (!evt.newValue) { Close(); return; }
|
||||
Refresh(_combat);
|
||||
Element.schedule.Execute(() =>
|
||||
{
|
||||
if (!Element.value) return;
|
||||
var workspace = Element.GetFirstAncestorOfType<ScrollView>();
|
||||
if (workspace == null) return;
|
||||
if (workspace.contentViewport.resolvedStyle.height < 180)
|
||||
{ OpenPopup(workspace.parent); return; }
|
||||
FitRows(workspace);
|
||||
Element.schedule.Execute(() => workspace.ScrollTo(Element));
|
||||
});
|
||||
});
|
||||
Element.RegisterCallback<GeometryChangedEvent>(_ =>
|
||||
{
|
||||
if (Element.value && !IsOpen && Element.GetFirstAncestorOfType<ScrollView>() is { } workspace)
|
||||
FitRows(workspace);
|
||||
});
|
||||
}
|
||||
|
||||
private void OpenPopup(VisualElement root)
|
||||
{
|
||||
if (IsOpen || root == null) return;
|
||||
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 execution preview", "ExecutionPreviewDialog");
|
||||
var panel = TerminalLayout.Section("End-turn preview", "ExecutionPreviewDialog");
|
||||
panel.AddToClassList("execution-preview-dialog");
|
||||
_rows.RemoveFromHierarchy();
|
||||
_rows.style.maxHeight = StyleKeyword.None;
|
||||
_rows.style.flexGrow = 1;
|
||||
_rows.style.minHeight = 0;
|
||||
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();
|
||||
@@ -64,37 +37,81 @@ namespace KernelPanic.UI
|
||||
TerminalTheme.ConstrainModalFocus(_popup);
|
||||
root.Add(_popup);
|
||||
_popup.BringToFront();
|
||||
Refresh(_combat);
|
||||
close.Focus();
|
||||
}
|
||||
|
||||
public void Close()
|
||||
{
|
||||
bool hadPopup = IsOpen;
|
||||
var root = _popup?.parent;
|
||||
_popup?.RemoveFromHierarchy();
|
||||
_popup = null;
|
||||
Element.SetValueWithoutNotify(false);
|
||||
_rows.style.maxHeight = 220;
|
||||
_rows.style.flexGrow = 0;
|
||||
if (_rows.parent != Element.contentContainer) Element.Add(_rows);
|
||||
if (hadPopup) Element.Q<Toggle>()?.Focus();
|
||||
}
|
||||
|
||||
private void FitRows(ScrollView workspace)
|
||||
{
|
||||
float height = workspace.contentViewport.resolvedStyle.height;
|
||||
if (!float.IsNaN(height) && height > 0)
|
||||
_rows.style.maxHeight = System.Math.Max(36, System.Math.Min(220, height - 42));
|
||||
root?.Q<Button>("OpenExecutionPreview")?.Focus();
|
||||
}
|
||||
|
||||
public void Refresh(CombatManager combat)
|
||||
{
|
||||
_combat = combat;
|
||||
bool available = combat != null && !combat.RunFinished && !combat.AwaitingWaveContinue;
|
||||
Element.style.display = available ? DisplayStyle.Flex : DisplayStyle.None;
|
||||
if (!available) Close();
|
||||
if (!available || !Element.value) return;
|
||||
if (combat == null || combat.RunFinished || combat.AwaitingWaveContinue) { Close(); return; }
|
||||
if (!IsOpen) return;
|
||||
_rows.Clear();
|
||||
foreach (string line in ExecutionPreview.Build(combat)) _rows.Add(TerminalLayout.BodyText(line));
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user