Neg_Utils/NEG/UI/Window/IWindow.cs
2024-02-12 21:26:24 +01:00

112 lines
3.9 KiB
C#

using NEG.UI.Area;
using NEG.UI.WindowSlot;
using NegUtils.NEG.UI;
using UnityEngine;
namespace NEG.UI.Window
{
public interface IWindow : ISlotsHolder, IControllable
{
/// <summary>
/// Parent slot of this window.
/// </summary>
IWindowSlot Parent { get; }
/// <summary>
/// Called internally by slot to open window.
/// </summary>
/// <param name="parentSlot">slot that opens window</param>
/// <param name="data">data</param>
void SetOpenedState(IWindowSlot parentSlot, object data);
/// <summary>
/// Called internally to close window by slot.
/// </summary>
void SetClosedState();
/// <summary>
/// Called internally to hide window by slot.
/// </summary>
void SetHiddenState();
/// <summary>
/// Called internally to set window visible by slot.
/// </summary>
void SeVisibleState();
}
public static class WindowInterfaceExtensions
{
/// <summary>
/// Opens window as slot child. If slot is null or not provided, as child of current area.
/// </summary>
/// <param name="window">window to open</param>
/// <param name="slot">slot to attach window</param>
/// <param name="data">data to send to window</param>
public static void Open(this IWindow window, IWindowSlot slot = null, object data = null)
{
if (slot != null)
{
slot.AttachWindow(window, data);
return;
}
UiManager.Instance.CurrentArea.OpenWindow(window, data);
}
/// <summary>
/// Opens window as child of selected area.
/// </summary>
/// <param name="window">window to open</param>
/// <param name="area">area to attach window</param>
/// <param name="data">data to send to window</param>
public static void Open(this IWindow window, IArea area, object data = null) => area.OpenWindow(window, data);
/// <summary>
/// Open passed window as child of this window.
/// </summary>
/// <param name="window">parent window</param>
/// <param name="windowToOpen">window to open</param>
/// <param name="data">data to send to window</param>
public static void OpenChild(this IWindow window, IWindow windowToOpen, object data = null)
{
if (windowToOpen == null)
{
Debug.LogError("Window to open cannot be null");
return;
}
window.OpenWindow(windowToOpen, data);
}
/// <summary>
/// Open window as child of provided window. If <typeparamref name="parentWindow" /> is null, as child of current main
/// window in <see cref="UiManager" />. If there is no main window, open on current area.
/// </summary>
/// <param name="window">window to open</param>
/// <param name="parentWindow">parent window</param>
/// <param name="data">data to send to window</param>
public static void OpenAsChild(this IWindow window, IWindow parentWindow = null, object data = null)
{
if (parentWindow != null)
{
parentWindow.OpenWindow(window, data);
return;
}
if (UiManager.Instance.CurrentMainWindow != null)
{
UiManager.Instance.CurrentMainWindow.OpenWindow(window, data);
return;
}
UiManager.Instance.CurrentArea.OpenWindow(window, data);
}
/// <summary>
/// Close window.
/// </summary>
/// <param name="window">window to close</param>
public static void Close(this IWindow window) => window.Parent.DetachWindow(window);
}
}