New componnets

This commit is contained in:
Hubert Mattusch 2023-08-12 11:14:57 +02:00
parent 34807988d8
commit 9c91269a9e
20 changed files with 209 additions and 43 deletions

View File

@ -1,9 +1,9 @@
using NEG.UI.WindowSlot; using NEG.UI.WindowSlot;
using NegUtils.NEG.UI;
namespace NEG.UI.Area namespace NEG.UI.Area
{ {
public interface IArea : ISlotsHolder, IUiElement public interface IArea : ISlotsHolder, IUiElement, IControllable
{ {
} }
} }

23
NEG/UI/IControllable.cs Normal file
View File

@ -0,0 +1,23 @@
using System;
namespace NegUtils.NEG.UI
{
public interface IControllable
{
public class BackUsed
{
public bool Used { get; set; }
}
event Action<object> OnOpened;
event Action OnClosed;
/// <summary>
///
/// </summary>
event Action<BackUsed> UseBack;
public void FireOpenCallback(object data);
public void FireOnClosed();
public void FireUseBack(ref BackUsed backUsed);
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 175798310e4048eda768cf40e6ee6de3
timeCreated: 1686596400

11
NEG/UI/IController.cs Normal file
View File

@ -0,0 +1,11 @@
namespace NegUtils.NEG.UI
{
public interface IController
{
void OnOpened(object data);
void OnClosed();
void UseBack(IControllable.BackUsed backUsed);
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: b88f4a93020a4bc6bde40e0438d296a9
timeCreated: 1686595825

View File

@ -3,6 +3,7 @@ using NEG.UI.Area;
using NEG.UI.Popup; using NEG.UI.Popup;
using NEG.UI.Window; using NEG.UI.Window;
using NEG.Utils; using NEG.Utils;
using NegUtils.NEG.UI;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
@ -125,6 +126,17 @@ namespace NEG.UI
UpdatePopupsState(forceShow, priority, data); UpdatePopupsState(forceShow, priority, data);
} }
public void UseBack()
{
IControllable.BackUsed backUsed = new();
CurrentMainWindow?.FireUseBack(ref backUsed);
if(backUsed.Used)
return;
CurrentArea.FireUseBack(ref backUsed);
}
public void RefreshPopups() public void RefreshPopups()
{ {
if(currentShownPopup.data is { IsValid: true }) if(currentShownPopup.data is { IsValid: true })
@ -134,6 +146,16 @@ namespace NEG.UI
public virtual void Dispose() => Instance = null; public virtual void Dispose() => Instance = null;
public void SetMainWindow(IWindow window) => CurrentMainWindow = window;
public void OnWindowClosed(IWindow window)
{
if(CurrentMainWindow != window)
return;
//TODO: select new main window
}
protected void PopupClosed(PopupData data) protected void PopupClosed(PopupData data)
{ {
if (currentShownPopup.data != data) if (currentShownPopup.data != data)

View File

@ -0,0 +1,27 @@
using NEG.UI.Window;
using NegUtils.NEG.UI;
using UnityEngine;
namespace NEG.UI.Area
{
public class CloseMainWindowOnBack : MonoBehaviour, IController
{
private IControllable controllable;
public void OnOpened(object data) { }
public void OnClosed() { }
public void UseBack(IControllable.BackUsed backUsed)
{
UiManager.Instance.CurrentMainWindow.Close();
backUsed.Used = true;
}
private void Awake()
{
controllable = GetComponent<IControllable>();
controllable.UseBack += UseBack;
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 6ec2bf64cda740a1849d024d4f163a01
timeCreated: 1686598066

View File

@ -5,12 +5,17 @@ using NEG.UI.UnityUi.Window;
using NEG.UI.UnityUi.WindowSlot; using NEG.UI.UnityUi.WindowSlot;
using NEG.UI.Window; using NEG.UI.Window;
using NEG.UI.WindowSlot; using NEG.UI.WindowSlot;
using NegUtils.NEG.UI;
using System; using System;
namespace NEG.UI.Area namespace NEG.UI.Area
{ {
public class MonoArea : MonoBehaviour, IArea public class MonoArea : MonoBehaviour, IArea
{ {
public event Action<object> OnOpened;
public event Action OnClosed;
public event Action<IControllable.BackUsed> UseBack;
public IEnumerable<IWindowSlot> AvailableSlots => windowSlots; public IEnumerable<IWindowSlot> AvailableSlots => windowSlots;
public IWindowSlot DefaultWindowSlot => windowSlots[0]; public IWindowSlot DefaultWindowSlot => windowSlots[0];
@ -20,11 +25,7 @@ namespace NEG.UI.Area
public virtual void SetEnabled(bool setEnabled) => gameObject.SetActive(setEnabled); public virtual void SetEnabled(bool setEnabled) => gameObject.SetActive(setEnabled);
public virtual void OpenWindow(IWindow window, object data = null) public virtual void OpenWindow(IWindow window, object data = null) => DefaultWindowSlot.AttachWindow(window, data);
{
DefaultWindowSlot.AttachWindow(window);
window.SetData(data);
}
protected virtual void Awake() protected virtual void Awake()
{ {
@ -37,5 +38,12 @@ namespace NEG.UI.Area
if (ReferenceEquals(UiManager.Instance.CurrentArea, this)) if (ReferenceEquals(UiManager.Instance.CurrentArea, this))
UiManager.Instance.CurrentArea = null; UiManager.Instance.CurrentArea = null;
} }
public void FireOpenCallback(object data) => OnOpened?.Invoke(data);
public void FireOnClosed() => OnClosed?.Invoke();
public void FireUseBack(ref IControllable.BackUsed backUsed) => UseBack?.Invoke(backUsed);
} }
} }

View File

@ -0,0 +1,15 @@
using KBCore.Refs;
using NEG.UI.UnityUi.Window;
using NEG.UI.Window;
using UnityEngine;
namespace NEG.UI.UnityUi.Buttons
{
public class OpenAsCurrentMainChild : ButtonReaction
{
[SerializeField]
private MonoWindow windowToOpen;
protected override void OnClicked() => UiManager.Instance.CurrentMainWindow.OpenAsChild(windowToOpen);
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 049e222140c94fc28fb36bca4aaddba4
timeCreated: 1686595194

View File

@ -0,0 +1,30 @@
using KBCore.Refs;
using NEG.UI.Window;
using NegUtils.NEG.UI;
using UnityEngine;
namespace NEG.UI.UnityUi.Window
{
public class CloseWindowOnBack : MonoBehaviour, IController
{
[SerializeField, Self(Flag.Editable)] private MonoWindow window;
private IControllable controllable;
public void OnOpened(object data) { }
public void OnClosed() { }
public void UseBack(IControllable.BackUsed backUsed)
{
window.Close();
backUsed.Used = true;
}
private void Awake()
{
controllable = GetComponent<IControllable>();
controllable.UseBack += UseBack;
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: b4a156da6df74abeb281176000738ebb
timeCreated: 1686598323

View File

@ -3,6 +3,7 @@ using NEG.UI.UnityUi.Buttons;
using NEG.UI.UnityUi.WindowSlot; using NEG.UI.UnityUi.WindowSlot;
using NEG.UI.Window; using NEG.UI.Window;
using NEG.UI.WindowSlot; using NEG.UI.WindowSlot;
using NegUtils.NEG.UI;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
@ -12,8 +13,13 @@ namespace NEG.UI.UnityUi.Window
{ {
public class MonoWindow : MonoBehaviour, IWindow public class MonoWindow : MonoBehaviour, IWindow
{ {
public event Action<object> OnOpened;
public event Action OnClosed;
public event Action<IControllable.BackUsed> UseBack;
public IEnumerable<IWindowSlot> AvailableSlots => windowSlots; public IEnumerable<IWindowSlot> AvailableSlots => windowSlots;
public IWindowSlot Parent { get; private set; } public IWindowSlot Parent { get; private set; }
public IController Controller { get; }
public bool IsMainWindow { get; private set; } public bool IsMainWindow { get; private set; }
@ -29,14 +35,8 @@ namespace NEG.UI.UnityUi.Window
gameObject.SetActive(true); gameObject.SetActive(true);
Parent = parentSlot; Parent = parentSlot;
EventSystem.current.SetSelectedGameObject(defaultSelectedItem); EventSystem.current.SetSelectedGameObject(defaultSelectedItem);
if (controller != null) if (parentSlot.OpenWindowAsMain)
controller.OnOpened(); UiManager.Instance.SetMainWindow(this);
}
public void SetData(object data)
{
if (controller != null)
controller.SetData(data);
} }
public void SetClosedState() public void SetClosedState()
@ -44,6 +44,7 @@ namespace NEG.UI.UnityUi.Window
gameObject.SetActive(false); gameObject.SetActive(false);
Parent = null; Parent = null;
((ISlotsHolder)this).CloseAllWindows(); ((ISlotsHolder)this).CloseAllWindows();
UiManager.Instance.OnWindowClosed(this);
if(controller != null) if(controller != null)
controller.OnClosed(); controller.OnClosed();
} }
@ -64,12 +65,14 @@ namespace NEG.UI.UnityUi.Window
#endif #endif
} }
public void OpenWindow(IWindow window, object data = null) public void OpenWindow(IWindow window, object data = null) => DefaultWindowSlot.AttachWindow(window, data);
{
DefaultWindowSlot.AttachWindow(window);
window.SetData(data);
}
public void SetDefaultSelectedItem(GameObject item) => defaultSelectedItem = item; public void SetDefaultSelectedItem(GameObject item) => defaultSelectedItem = item;
public void FireOpenCallback(object data) => OnOpened?.Invoke(data);
public void FireOnClosed() => OnClosed?.Invoke();
public void FireUseBack(ref IControllable.BackUsed backUsed) => UseBack?.Invoke(backUsed);
} }
} }

View File

@ -1,20 +1,27 @@
using System; using NegUtils.NEG.UI;
using System;
using UnityEngine; using UnityEngine;
namespace NEG.UI.UnityUi.Window namespace NEG.UI.UnityUi.Window
{ {
[RequireComponent(typeof(MonoWindow))] public abstract class WindowController : MonoBehaviour, IController
//Due to prefab variants we need this
public abstract class WindowController : MonoBehaviour
{ {
protected MonoWindow window; protected IControllable controllable;
public abstract void SetData(object data); public virtual void OnOpened(object data) { }
public virtual void OnOpened() { }
public virtual void OnClosed() { } public virtual void OnClosed() { }
protected virtual void Awake() => window = GetComponent<MonoWindow>(); protected virtual void Awake()
{
controllable = GetComponent<IControllable>();
controllable.OnOpened += OnOpened;
controllable.OnClosed += OnClosed;
controllable.UseBack += UseBack;
}
public void UseBack(IControllable.BackUsed obj)
{
}
} }
} }

View File

@ -13,7 +13,7 @@ namespace NEG.UI.UnityUi.WindowSlot
[field: SerializeField] public bool OpenWindowAsMain { get; private set; } [field: SerializeField] public bool OpenWindowAsMain { get; private set; }
public ISlotsHolder ParentHolder => slotsHolder.Value; public ISlotsHolder ParentHolder => slotsHolder.Value;
public abstract void AttachWindow(IWindow window); public abstract void AttachWindow(IWindow window, object data);
public abstract void DetachWindow(IWindow window); public abstract void DetachWindow(IWindow window);
public abstract void CloseAllWindows(); public abstract void CloseAllWindows();

View File

@ -19,7 +19,12 @@ namespace NEG.UI.WindowSlot
private IWindow currentWindow; private IWindow currentWindow;
public override void AttachWindow(IWindow window) => CurrentWindow = window; public override void AttachWindow(IWindow window, object data)
{
CurrentWindow = window;
window.FireOpenCallback(data);
}
public override void DetachWindow(IWindow window) => CurrentWindow = null; public override void DetachWindow(IWindow window) => CurrentWindow = null;
public override void CloseAllWindows() => CurrentWindow = null; public override void CloseAllWindows() => CurrentWindow = null;
} }

View File

@ -29,7 +29,11 @@ namespace NegUtils.NEG.UI.UnityUi.WindowSlot
private readonly List<IWindow> windowsHistory = new List<IWindow>(); private readonly List<IWindow> windowsHistory = new List<IWindow>();
public override void AttachWindow(IWindow window) => CurrentWindow = window; public override void AttachWindow(IWindow window, object data)
{
CurrentWindow = window;
CurrentWindow.FireOpenCallback(data);
}
public override void DetachWindow(IWindow window) public override void DetachWindow(IWindow window)
{ {

View File

@ -1,29 +1,26 @@
using JetBrains.Annotations; using JetBrains.Annotations;
using NEG.UI.Area; using NEG.UI.Area;
using NEG.UI.WindowSlot; using NEG.UI.WindowSlot;
using NegUtils.NEG.UI;
using UnityEngine; using UnityEngine;
namespace NEG.UI.Window namespace NEG.UI.Window
{ {
public interface IWindow : ISlotsHolder public interface IWindow : ISlotsHolder, IControllable
{ {
/// <summary> /// <summary>
/// Parent slot of this window. /// Parent slot of this window.
/// </summary> /// </summary>
IWindowSlot Parent { get; } IWindowSlot Parent { get; }
IController Controller { get; }
/// <summary> /// <summary>
/// Called internally by slot to open window. /// Called internally by slot to open window.
/// </summary> /// </summary>
/// <param name="parentSlot">slot that opens window</param> /// <param name="parentSlot">slot that opens window</param>
void SetOpenedState(IWindowSlot parentSlot); 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> /// <summary>
/// Called internally to close window by slot. /// Called internally to close window by slot.
/// </summary> /// </summary>
@ -52,8 +49,7 @@ namespace NEG.UI.Window
{ {
if (slot != null) if (slot != null)
{ {
slot.AttachWindow(window); slot.AttachWindow(window, data);
window.SetData(data);
return; return;
} }

View File

@ -7,7 +7,7 @@ namespace NEG.UI.WindowSlot
{ {
bool OpenWindowAsMain { get; } bool OpenWindowAsMain { get; }
ISlotsHolder ParentHolder { get; } ISlotsHolder ParentHolder { get; }
void AttachWindow(IWindow window); void AttachWindow(IWindow window, object data);
void DetachWindow(IWindow window); void DetachWindow(IWindow window);
void CloseAllWindows(); void CloseAllWindows();
} }