com.alicizax.unity.framework/Runtime/UI/UIBase/UIStateMachine.cs

60 lines
2.2 KiB
C#

using System;
using System.Collections.Generic;
namespace AlicizaX.UI.Runtime
{
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;
}
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"
};
}
}
}