using JetBrains.Annotations;
using NEG.UI.Area;
using NEG.UI.WindowSlot;
using UnityEngine;
namespace NEG.UI.Window
{
public interface IWindow : ISlotsHolder
{
///
/// Parent slot of this window.
///
IWindowSlot Parent { get; }
///
/// Called internally by slot to open window.
///
/// slot that opens window
void SetOpenedState(IWindowSlot parentSlot);
///
/// Sets data for window, usually used for dynamic content set by external logic controller.
///
/// can be any type, window should cast for expected type
void SetData(object data);
///
/// Called internally to close window by slot.
///
void SetClosedState();
}
public static class WindowInterfaceExtensions
{
///
/// Opens window as slot child. If slot is null or not provided, as child of current area.
///
/// window to open
/// slot to attach window
/// data to send to window
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);
}
///
/// Opens window as child of selected area.
///
/// window to open
/// area to attach window
/// data to send to window
public static void Open(this IWindow window, IArea area, object data = null) => area.OpenWindow(window, data);
///
/// Open passed window as child of this window.
///
/// parent window
/// window to open
/// data to send to window
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);
}
///
/// Open window as child of provided window. If is null, as child of current main window in . If there is no main window, open on current area.
///
/// window to open
/// parent window
/// data to send to window
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);
}
///
/// Close window.
///
/// window to close
public static void Close(this IWindow window) => window.Parent.DetachWindow(window);
}
}