Refactor alot of stuff

This commit is contained in:
2026-10-29 22:54:29 +01:00
parent c23b666ee4
commit 260abc2c98
138 changed files with 2835 additions and 466 deletions
+96 -47
View File
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using KernelPanic.Combat;
using UnityEngine;
@@ -8,32 +9,14 @@ namespace KernelPanic.UI
{
internal sealed class TurnResolutionView : IDisposable
{
public Foldout Element { get; } = new() { text = "Turn resolution / actual results", name = "TurnResolution", value = true };
private readonly Label _sequence = new() { name = "ResolutionSequence", enableRichText = false };
private readonly VisualElement _sequence = new() { name = "ResolutionSequence" };
private readonly ScrollView _actions = new(ScrollViewMode.Vertical) { name = "ResolutionActions" };
private CombatExecutionTrace _trace;
public TurnResolutionView()
{
Element.AddToClassList("execution-preview");
_sequence.style.whiteSpace = WhiteSpace.Normal;
_sequence.text = "Go > Python automation > Python FIFO > Haskell LIFO > Enemies > Cleanup";
_actions.style.maxHeight = 160;
_actions.horizontalScrollerVisibility = ScrollerVisibility.Hidden;
Element.Add(_sequence); Element.Add(_actions);
Element.RegisterValueChangedCallback(_ => { Refresh(); FitRows(); });
Element.RegisterCallback<GeometryChangedEvent>(_ => FitRows());
_sequence.RegisterCallback<GeometryChangedEvent>(_ => FitRows());
}
private void FitRows()
{
var workspace = Element.GetFirstAncestorOfType<ScrollView>();
if (workspace == null) return;
float available = workspace.contentViewport.resolvedStyle.height;
float heading = Element.Q<Toggle>()?.resolvedStyle.height ?? 24;
float sequence = _sequence.resolvedStyle.height;
if (float.IsNaN(available) || float.IsNaN(heading) || float.IsNaN(sequence) || available <= 0) return;
_actions.style.maxHeight = Math.Max(20, Math.Min(160, available - heading - sequence - 20));
}
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;
@@ -41,35 +24,101 @@ namespace KernelPanic.UI
if (_trace != null) _trace.Changed += Refresh;
Refresh();
}
private void Refresh()
{
if (!IsOpen) return;
string stage = _trace?.Stage ?? "Native";
Element.text = "Turn resolution / " + (stage == "Native" ? "planning & immediate cards" : stage);
_sequence.text = string.Join(" > ", new[] { "Go", "Automation", "FIFO", "LIFO", "Enemies", "Cleanup" }
.Select(s => s == stage ? "[ " + s + " ]" : s));
if (!Element.value) return;
_actions.Clear();
if (_trace == null || _trace.Actions.Count == 0)
_actions.Add(TerminalLayout.BodyText("Play a card or end your turn to see actual results. Hidden intents and random outcomes are revealed only when combat reveals them."));
else foreach (var action in _trace.Actions)
_sequence.Clear();
foreach (string step in new[] { "Native", "Go", "Automation", "FIFO", "LIFO", "Enemies", "Cleanup" })
{
var row = new VisualElement();
row.style.marginBottom = 6;
var title = TerminalLayout.BodyText($"{(action.Complete ? "done" : "> resolving")} / {action.Stage} / {action.Label}");
if (!action.Complete) title.style.color = new Color(1f, .75f, .35f);
row.Add(title);
row.Add(TerminalLayout.BodyText($"{action.UptimeDamage} Uptime damage / {action.ShieldDamage} Shield damage / +{action.ShieldGranted} Shield"));
if (action.PlayerUptimeLost > 0 || action.PlayerShieldBlocked > 0)
row.Add(TerminalLayout.BodyText($"Host: {action.PlayerUptimeLost} Uptime lost / {action.PlayerShieldBlocked} blocked by Shield"));
foreach (var detail in action.Details) row.Add(TerminalLayout.BodyText(detail));
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);
}
if (_actions.childCount > 0)
{
var latest = _actions[_actions.childCount - 1];
_actions.schedule.Execute(() => { if (latest.parent != null && Element.value) _actions.ScrollTo(latest); });
}
}
public void Dispose() => Bind(null);
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); }
}
}