Adding documentation + cleanup

This commit is contained in:
Hubert Mattusch 2023-02-01 16:53:27 +01:00
parent 7bbc03cdbf
commit d3327c4360
10 changed files with 100 additions and 27 deletions

View File

@ -7,6 +7,11 @@ namespace NEG.UI.Area
{
public interface IArea : ISlotsHolder, IUiElement
{
/// <summary>
/// Open window as child of
/// </summary>
/// <param name="window"></param>
/// <param name="data"></param>
void OpenChildWindow(IWindow window, object data = null);
}
}

View File

@ -7,16 +7,21 @@ namespace NEG.UI.Popup
[PublicAPI]
public interface IPopup
{
/// <summary>
/// Event to fire when popup is closed
/// </summary>
event Action<PopupData> OnPopupClosed;
/// <summary>
/// Show popup
/// </summary>
/// <param name="data">data assigned to popup, used to give info that popup is closed</param>
void Show(PopupData data);
public void Show(PopupData data);
/// <summary>
/// Close popup or mark as closed if not visible
/// </summary>
/// <param name="silence">if true hide visually, without firing callbacks</param>
/// <param name="silence">if true hide visually, without firing callbacks to properly close</param>
void Close(bool silence = false);
}
}

View File

@ -1,7 +1,15 @@
namespace NEG.UI.Popup
using System;
namespace NEG.UI.Popup
{
public class PopupData
{
public event Action<PopupData> PopupClosedEvent
{
add => popup.OnPopupClosed += value;
remove => popup.OnPopupClosed -= value;
}
public bool IsValid { get; protected set; }
private IPopup popup;

View File

@ -1,19 +1,21 @@
using JetBrains.Annotations;
using NEG.UI.Area;
using NEG.UI.Popup;
using NEG.UI.Window;
using System;
using System.Collections.Generic;
using UnityEngine;
namespace NEG.UI
{
[PublicAPI]
public abstract class UiManager
{
//TODO: use default unity selection
//TODO: window snaping to slots
//TODO: Default prefabs?
public static UiManager Instance { get; private set; }
/// <summary>
/// Current area shown on screen.
/// </summary>
public IArea CurrentArea
{
get => currentArea;
@ -27,9 +29,14 @@ namespace NEG.UI
}
}
/// <summary>
/// Current window that is considered main (focused, lastly opened). Can be null.
/// </summary>
public IWindow CurrentMainWindow { get; protected set; }
private IArea currentArea;
protected IDefaultPopup currentDefaultPopup;
private (PopupData data, int priority) currentShownPopup;
protected IDefaultPopup currentDefaultPopup;
private PriorityQueue<PopupData, int> popupsToShow = new();
@ -115,7 +122,7 @@ namespace NEG.UI
UpdatePopupsState(forceShow, priority, data);
}
public void PopupClosed(PopupData data)
protected void PopupClosed(PopupData data)
{
if (currentShownPopup.data != data)
{
@ -147,8 +154,14 @@ namespace NEG.UI
private void ShowPopup(PopupData data, int priority)
{
if (currentShownPopup.data != null)
{
currentShownPopup.data.PopupClosedEvent -= PopupClosed;
currentShownPopup.data.Hide();
}
currentShownPopup = (data, priority);
data.Show();
data.PopupClosedEvent += PopupClosed;
}
}
}

View File

@ -18,7 +18,7 @@ namespace NEG.UI.Area
[SerializeField] private List<MonoWindowSlot> windowSlots;
[SerializeField] private Queue<IPopup> currentPopups = new();
private Queue<IPopup> currentPopups = new();
public virtual void SetEnabled(bool setEnabled) => gameObject.SetActive(setEnabled);

View File

@ -17,6 +17,9 @@ namespace NEG.UI.UnityUi
/// </summary>
public class MonoUiManager : UiManager
{
//TODO: use default unity selection
//TODO: window snaping to slots
private readonly MonoDefaultPopup defaultPopupPrefab;
private readonly GameObject canvasPrefab;

View File

@ -6,6 +6,8 @@ namespace NEG.UI.UnityUi.Popup
{
public class MonoPopup : MonoBehaviour, IPopup
{
public event Action<PopupData> OnPopupClosed;
protected PopupData data;
public void Show(PopupData data)
@ -21,7 +23,7 @@ namespace NEG.UI.UnityUi.Popup
if(silence)
return;
UiManager.Instance.PopupClosed(data);
OnPopupClosed?.Invoke(data);
}
}

View File

@ -1,4 +1,5 @@
using NEG.UI.Area;
using NEG.UI.UnityUi.Buttons;
using NEG.UI.UnityUi.WindowSlot;
using NEG.UI.Window;
using NEG.UI.WindowSlot;
@ -10,13 +11,17 @@ namespace NEG.UI.UnityUi.Window
{
public class MonoWindow : MonoBehaviour, IWindow
{
public IEnumerable<IWindowSlot> AvailableSlots => windowSlots;
public IWindowSlot Parent { get; private set; }
public IWindowSlot ChildWindowSlot => childWindowArea;
[SerializeField] private MonoWindowSlot childWindowArea;
public bool IsMainWindow { get; private set; }
private IWindowSlot DefaultWindowSlot => windowSlots[0];
[SerializeField] private List<MonoWindowSlot> windowSlots;
[SerializeField] private WindowController controller;
void IWindow.SetOpenedState(IWindowSlot parentSlot)
public void SetOpenedState(IWindowSlot parentSlot)
{
gameObject.SetActive(true);
Parent = parentSlot;
@ -30,12 +35,11 @@ namespace NEG.UI.UnityUi.Window
controller.SetData(data);
}
void IWindow.SetClosedState()
public void SetClosedState()
{
gameObject.SetActive(false);
Parent = null;
if(childWindowArea != null)
ChildWindowSlot.CloseAllWindows();
((ISlotsHolder)this).CloseAllWindows();
}
private void Awake() => ((IWindow)this).SetClosedState();
@ -46,7 +50,10 @@ namespace NEG.UI.UnityUi.Window
controller = GetComponent<WindowController>();
}
public IEnumerable<IWindowSlot> AvailableSlots { get; }
public void OpenWindow(IWindow window, object data = null) => throw new NotImplementedException();
public void OpenWindow(IWindow window, object data = null)
{
DefaultWindowSlot.AttachWindow(window);
window.SetData(data);
}
}
}

View File

@ -16,6 +16,5 @@ namespace NEG.UI.UnityUi.WindowSlot
public abstract void CloseAllWindows();
[SerializeField] private SerializableInterface<ISlotsHolder> slotsHolder;
}
}

View File

@ -7,6 +7,9 @@ namespace NEG.UI.Window
{
public interface IWindow : ISlotsHolder
{
/// <summary>
/// Parent slot of this window.
/// </summary>
IWindowSlot Parent { get; }
/// <summary>
@ -15,6 +18,10 @@ namespace NEG.UI.Window
/// <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>
@ -25,7 +32,12 @@ namespace NEG.UI.Window
public static class WindowInterfaceExtensions
{
//Open
/// <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)
@ -38,7 +50,20 @@ namespace NEG.UI.Window
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)
@ -49,6 +74,12 @@ namespace NEG.UI.Window
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)