101 lines
4.0 KiB
C#
101 lines
4.0 KiB
C#
using KernelPanic.Combat;
|
|
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
|
|
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 CombatManager _combat;
|
|
private VisualElement _popup;
|
|
public bool IsOpen => _popup != null;
|
|
|
|
public ExecutionPreviewView()
|
|
{
|
|
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;
|
|
_popup = new VisualElement { name = "ExecutionPreviewPopup", focusable = true };
|
|
_popup.AddToClassList("execution-preview-shade");
|
|
var panel = TerminalLayout.Section("End-turn execution preview", "ExecutionPreviewDialog");
|
|
panel.AddToClassList("execution-preview-dialog");
|
|
_rows.RemoveFromHierarchy();
|
|
_rows.style.maxHeight = StyleKeyword.None;
|
|
_rows.style.flexGrow = 1;
|
|
_rows.style.minHeight = 0;
|
|
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<KeyDownEvent>(evt =>
|
|
{
|
|
if (evt.keyCode == KeyCode.Escape) Close();
|
|
evt.StopPropagation();
|
|
});
|
|
TerminalTheme.ConstrainModalFocus(_popup);
|
|
root.Add(_popup);
|
|
_popup.BringToFront();
|
|
close.Focus();
|
|
}
|
|
|
|
public void Close()
|
|
{
|
|
bool hadPopup = IsOpen;
|
|
_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));
|
|
}
|
|
|
|
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;
|
|
_rows.Clear();
|
|
foreach (string line in ExecutionPreview.Build(combat)) _rows.Add(TerminalLayout.BodyText(line));
|
|
}
|
|
}
|
|
}
|