2025-12-24 20:44:36 +08:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
namespace AlicizaX.UI.Runtime
|
|
|
|
|
{
|
2026-03-09 20:13:40 +08:00
|
|
|
|
2025-12-24 20:44:36 +08:00
|
|
|
internal static class UIStateMachine
|
|
|
|
|
{
|
|
|
|
|
private static readonly Dictionary<UIState, HashSet<UIState>> _validTransitions = new()
|
|
|
|
|
{
|
|
|
|
|
[UIState.Uninitialized] = new() { UIState.CreatedUI },
|
|
|
|
|
[UIState.CreatedUI] = new() { UIState.Loaded },
|
|
|
|
|
[UIState.Loaded] = new() { UIState.Initialized },
|
|
|
|
|
[UIState.Initialized] = new() { UIState.Opened },
|
|
|
|
|
[UIState.Opened] = new() { UIState.Closed, UIState.Destroying },
|
|
|
|
|
[UIState.Closed] = new() { UIState.Opened, UIState.Destroying },
|
|
|
|
|
[UIState.Destroying] = new() { UIState.Destroyed },
|
|
|
|
|
[UIState.Destroyed] = new() { }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public static bool IsValidTransition(UIState from, UIState to)
|
|
|
|
|
{
|
|
|
|
|
return _validTransitions.TryGetValue(from, out var validStates) && validStates.Contains(to);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool ValidateTransition(string uiName, UIState from, UIState to)
|
|
|
|
|
{
|
|
|
|
|
if (IsValidTransition(from, to))
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
Log.Error($"[UI] Invalid state transition for {uiName}: {from} -> {to}");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-09 20:13:40 +08:00
|
|
|
|
2025-12-24 20:44:36 +08:00
|
|
|
public static HashSet<UIState> GetValidNextStates(UIState currentState)
|
|
|
|
|
{
|
|
|
|
|
return _validTransitions.TryGetValue(currentState, out var states)
|
|
|
|
|
? states
|
|
|
|
|
: new HashSet<UIState>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string GetStateDescription(UIState state)
|
|
|
|
|
{
|
|
|
|
|
return state switch
|
|
|
|
|
{
|
|
|
|
|
UIState.Uninitialized => "Not yet created",
|
|
|
|
|
UIState.CreatedUI => "UI logic created, awaiting resource load",
|
|
|
|
|
UIState.Loaded => "Resources loaded, awaiting initialization",
|
|
|
|
|
UIState.Initialized => "Initialized, ready to open",
|
|
|
|
|
UIState.Opened => "Currently visible and active",
|
|
|
|
|
UIState.Closed => "Hidden but cached",
|
|
|
|
|
UIState.Destroying => "Being destroyed",
|
|
|
|
|
UIState.Destroyed => "Fully destroyed",
|
|
|
|
|
_ => "Unknown state"
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|