Neg_Utils/NEG/UI/UnityUi/WindowSlot/SingleWindowSlotWithHistory.cs
2024-02-12 21:26:24 +01:00

60 lines
1.8 KiB
C#

using NEG.UI;
using NEG.UI.UnityUi.Window;
using NEG.UI.UnityUi.WindowSlot;
using NEG.UI.Window;
using System.Collections.Generic;
using UnityEngine.EventSystems;
namespace NegUtils.NEG.UI.UnityUi.WindowSlot
{
public class SingleWindowSlotWithHistory : MonoWindowSlot
{
private readonly List<IWindow> windowsHistory = new();
private IWindow currentWindow;
public IWindow CurrentWindow
{
get => currentWindow;
set
{
if (value == null)
{
CloseAllWindows();
return;
}
currentWindow?.SetHiddenState();
currentWindow = value;
windowsHistory.Add(currentWindow);
}
}
public override void AttachWindow(IWindow window, object data)
{
CurrentWindow = window;
CurrentWindow.SetOpenedState(this, data);
}
public override void DetachWindow(IWindow window)
{
if (window == null)
return;
window.SetClosedState();
windowsHistory.Remove(window);
if (window != currentWindow || windowsHistory.Count == 0) return;
currentWindow = windowsHistory[^1];
currentWindow.SeVisibleState();
if (UiManager.Instance.CurrentMainWindow == window)
UiManager.Instance.MainWindowClosed(window);
EventSystem.current.SetSelectedGameObject(((MonoWindow)currentWindow).DefaultSelectedItem);
}
public override void CloseAllWindows()
{
currentWindow = null;
foreach (var window in windowsHistory) window.SetClosedState();
windowsHistory.Clear();
}
}
}