125 lines
5.9 KiB
C#
125 lines
5.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using KernelPanic.Combat;
|
|
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
|
|
namespace KernelPanic.UI
|
|
{
|
|
internal sealed class TurnResolutionView : IDisposable
|
|
{
|
|
private readonly VisualElement _sequence = new() { name = "ResolutionSequence" };
|
|
private readonly ScrollView _actions = new(ScrollViewMode.Vertical) { name = "ResolutionActions" };
|
|
private CombatExecutionTrace _trace;
|
|
private VisualElement _popup;
|
|
public bool IsOpen => _popup != null;
|
|
private readonly HashSet<ExecutionAction> _expandedDamage = new();
|
|
private readonly HashSet<ExecutionAction> _expandedDetails = new();
|
|
|
|
public void Bind(CombatExecutionTrace trace)
|
|
{
|
|
if (_trace != null) _trace.Changed -= Refresh;
|
|
_trace = trace;
|
|
if (_trace != null) _trace.Changed += Refresh;
|
|
Refresh();
|
|
}
|
|
|
|
private void Refresh()
|
|
{
|
|
if (!IsOpen) return;
|
|
string stage = _trace?.Stage ?? "Native";
|
|
_sequence.Clear();
|
|
foreach (string step in new[] { "Native", "Go", "Automation", "FIFO", "LIFO", "Enemies", "Cleanup" })
|
|
{
|
|
var chip = new Label(step == "Native" ? "Immediate" : step);
|
|
chip.AddToClassList("execution-phase");
|
|
chip.EnableInClassList("execution-phase-active", step == stage);
|
|
_sequence.Add(chip);
|
|
}
|
|
_actions.Clear();
|
|
_expandedDamage.RemoveWhere(action => _trace == null || !_trace.Actions.Contains(action));
|
|
_expandedDetails.RemoveWhere(action => _trace == null || !_trace.Actions.Contains(action));
|
|
if (_trace == null || _trace.Actions.Count == 0)
|
|
_actions.Add(TerminalLayout.BodyText("No actions yet. Play a card or end your turn to see results."));
|
|
else foreach (var action in _trace.Actions.Reverse())
|
|
{
|
|
var row = TerminalLayout.Section(action.Label, sectionClass: "execution-result");
|
|
row.Add(TerminalLayout.BodyText($"{action.Stage} / {(action.Complete ? "done" : "resolving")}", className: "execution-muted"));
|
|
var metrics = new VisualElement();
|
|
metrics.AddToClassList("execution-metrics");
|
|
AddMetric(metrics, action.UptimeDamage, "damage");
|
|
AddMetric(metrics, action.ShieldDamage, "Shield damage");
|
|
AddMetric(metrics, action.ShieldGranted, "Shield gained");
|
|
AddMetric(metrics, action.PlayerUptimeLost, "host Uptime lost");
|
|
AddMetric(metrics, action.PlayerShieldBlocked, "blocked");
|
|
row.Add(metrics);
|
|
if (action.Details.Count > 0)
|
|
{
|
|
var details = new Foldout { text = "Action details", value = _expandedDetails.Contains(action) };
|
|
details.RegisterValueChangedCallback(evt =>
|
|
{ if (evt.newValue) _expandedDetails.Add(action); else _expandedDetails.Remove(action); evt.StopPropagation(); });
|
|
foreach (var detail in action.Details) details.Add(TerminalLayout.BodyText(detail));
|
|
row.Add(details);
|
|
}
|
|
if (action.DamageBreakdowns.Count > 0)
|
|
{
|
|
var breakdown = new Foldout { text = "Damage calculation / recent hits", value = _expandedDamage.Contains(action) };
|
|
breakdown.RegisterValueChangedCallback(evt =>
|
|
{ if (evt.newValue) _expandedDamage.Add(action); else _expandedDamage.Remove(action); evt.StopPropagation(); });
|
|
foreach (var detail in action.DamageBreakdowns) breakdown.Add(TerminalLayout.BodyText(detail));
|
|
row.Add(breakdown);
|
|
}
|
|
_actions.Add(row);
|
|
}
|
|
}
|
|
|
|
private static void AddMetric(VisualElement row, long value, string label)
|
|
{
|
|
if (value <= 0) return;
|
|
row.Add(TerminalLayout.BodyText($"{value} {label}", className: "execution-metric"));
|
|
}
|
|
|
|
public void Open(VisualElement root)
|
|
{
|
|
if (IsOpen || root == null) return;
|
|
_popup = new VisualElement { name = "TurnResolutionPopup", focusable = true };
|
|
_popup.AddToClassList("execution-preview-shade");
|
|
var panel = TerminalLayout.Section("Turn resolution", "TurnResolutionDialog");
|
|
panel.AddToClassList("execution-preview-dialog");
|
|
panel.Add(TerminalLayout.BodyText("Actual results / latest first", className: "execution-subtitle"));
|
|
_sequence.AddToClassList("execution-phases");
|
|
_actions.AddToClassList("execution-dialog-scroll");
|
|
_actions.horizontalScrollerVisibility = ScrollerVisibility.Hidden;
|
|
panel.Add(_sequence);
|
|
panel.Add(_actions);
|
|
var close = new Button(Close) { text = "Close / Escape", name = "CloseTurnResolution" };
|
|
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();
|
|
_actions.scrollOffset = Vector2.zero;
|
|
close.Focus();
|
|
}
|
|
|
|
public void Close()
|
|
{
|
|
if (!IsOpen) return;
|
|
var root = _popup.parent;
|
|
_popup.RemoveFromHierarchy();
|
|
_popup = null;
|
|
root?.Q<Button>("OpenTurnResolution")?.Focus();
|
|
}
|
|
public void Dispose() { Close(); Bind(null); }
|
|
}
|
|
}
|