Neg_Utils/NEG/UI/UnityUi/WindowSlot/SingleWindowSlotWithHistory.cs
2023-06-09 01:01:46 +02:00

54 lines
1.5 KiB
C#

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