Neg_Utils/NEG/UI/Window/IWindow.cs

97 lines
3.4 KiB
C#

using JetBrains.Annotations;
using NEG.UI.Area;
using NEG.UI.WindowSlot;
using UnityEngine;
namespace NEG.UI.Window
{
public interface IWindow : ISlotsHolder
{
/// <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>
void SetOpenedState(IWindowSlot parentSlot);
/// <summary>
/// Sets data for window, usually used for dynamic content set by external logic controller.
/// </summary>
/// <param name="data">can be any type, window should cast for expected type</param>
void SetData(object data);
/// <summary>
/// Called internally to close window by slot.
/// </summary>
void SetClosedState();
}
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);
window.SetData(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"/>
/// </summary>
/// <param name="window">window to open</param>
/// <param name="parentWindow"></param>
/// <param name="data"></param>
public static void OpenAsChild(this IWindow window, IWindow parentWindow = null, object data = null)
{
if (parentWindow != null)
{
parentWindow.OpenWindow(window);
return;
}
UiManager.Instance.CurrentArea.OpenChildWindow(window, data);
}
public static void Close(this IWindow window) => window.Parent.DetachWindow(window);
}
}