From 9c91269a9e748b4a589849d5f76ad39946996a0e Mon Sep 17 00:00:00 2001 From: Hubert Mattusch Date: Sat, 12 Aug 2023 11:14:57 +0200 Subject: [PATCH] New componnets --- NEG/UI/Area/IArea.cs | 4 +-- NEG/UI/IControllable.cs | 23 ++++++++++++++ NEG/UI/IControllable.cs.meta | 3 ++ NEG/UI/IController.cs | 11 +++++++ NEG/UI/IController.cs.meta | 3 ++ NEG/UI/UiManager.cs | 22 ++++++++++++++ NEG/UI/UnityUi/Area/CloseMainWindowOnBack.cs | 27 +++++++++++++++++ .../Area/CloseMainWindowOnBack.cs.meta | 3 ++ NEG/UI/UnityUi/Area/MonoArea.cs | 18 +++++++---- .../UnityUi/Buttons/OpenAsCurrentMainChild.cs | 15 ++++++++++ .../Buttons/OpenAsCurrentMainChild.cs.meta | 3 ++ NEG/UI/UnityUi/Window/CloseWindowOnBack.cs | 30 +++++++++++++++++++ .../UnityUi/Window/CloseWindowOnBack.cs.meta | 3 ++ NEG/UI/UnityUi/Window/MonoWindow.cs | 29 ++++++++++-------- NEG/UI/UnityUi/Window/WindowController.cs | 25 ++++++++++------ NEG/UI/UnityUi/WindowSlot/MonoWindowSlot.cs | 2 +- NEG/UI/UnityUi/WindowSlot/SingleWindowSlot.cs | 7 ++++- .../WindowSlot/SingleWindowSlotWithHistory.cs | 6 +++- NEG/UI/Window/IWindow.cs | 14 ++++----- NEG/UI/WindowSlot/IWindowSlot.cs | 4 +-- 20 files changed, 209 insertions(+), 43 deletions(-) create mode 100644 NEG/UI/IControllable.cs create mode 100644 NEG/UI/IControllable.cs.meta create mode 100644 NEG/UI/IController.cs create mode 100644 NEG/UI/IController.cs.meta create mode 100644 NEG/UI/UnityUi/Area/CloseMainWindowOnBack.cs create mode 100644 NEG/UI/UnityUi/Area/CloseMainWindowOnBack.cs.meta create mode 100644 NEG/UI/UnityUi/Buttons/OpenAsCurrentMainChild.cs create mode 100644 NEG/UI/UnityUi/Buttons/OpenAsCurrentMainChild.cs.meta create mode 100644 NEG/UI/UnityUi/Window/CloseWindowOnBack.cs create mode 100644 NEG/UI/UnityUi/Window/CloseWindowOnBack.cs.meta diff --git a/NEG/UI/Area/IArea.cs b/NEG/UI/Area/IArea.cs index 4c84e27..d542ee6 100644 --- a/NEG/UI/Area/IArea.cs +++ b/NEG/UI/Area/IArea.cs @@ -1,9 +1,9 @@ using NEG.UI.WindowSlot; +using NegUtils.NEG.UI; namespace NEG.UI.Area { - public interface IArea : ISlotsHolder, IUiElement + public interface IArea : ISlotsHolder, IUiElement, IControllable { - } } \ No newline at end of file diff --git a/NEG/UI/IControllable.cs b/NEG/UI/IControllable.cs new file mode 100644 index 0000000..e030148 --- /dev/null +++ b/NEG/UI/IControllable.cs @@ -0,0 +1,23 @@ +using System; + +namespace NegUtils.NEG.UI +{ + public interface IControllable + { + public class BackUsed + { + public bool Used { get; set; } + } + + event Action OnOpened; + event Action OnClosed; + /// + /// + /// + event Action UseBack; + + public void FireOpenCallback(object data); + public void FireOnClosed(); + public void FireUseBack(ref BackUsed backUsed); + } +} \ No newline at end of file diff --git a/NEG/UI/IControllable.cs.meta b/NEG/UI/IControllable.cs.meta new file mode 100644 index 0000000..d595b1d --- /dev/null +++ b/NEG/UI/IControllable.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 175798310e4048eda768cf40e6ee6de3 +timeCreated: 1686596400 \ No newline at end of file diff --git a/NEG/UI/IController.cs b/NEG/UI/IController.cs new file mode 100644 index 0000000..a48a051 --- /dev/null +++ b/NEG/UI/IController.cs @@ -0,0 +1,11 @@ +namespace NegUtils.NEG.UI +{ + public interface IController + { + void OnOpened(object data); + + void OnClosed(); + + void UseBack(IControllable.BackUsed backUsed); + } +} \ No newline at end of file diff --git a/NEG/UI/IController.cs.meta b/NEG/UI/IController.cs.meta new file mode 100644 index 0000000..f16c28d --- /dev/null +++ b/NEG/UI/IController.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: b88f4a93020a4bc6bde40e0438d296a9 +timeCreated: 1686595825 \ No newline at end of file diff --git a/NEG/UI/UiManager.cs b/NEG/UI/UiManager.cs index 8e315cc..9c53e00 100644 --- a/NEG/UI/UiManager.cs +++ b/NEG/UI/UiManager.cs @@ -3,6 +3,7 @@ using NEG.UI.Area; using NEG.UI.Popup; using NEG.UI.Window; using NEG.Utils; +using NegUtils.NEG.UI; using System; using System.Collections.Generic; using UnityEngine; @@ -125,6 +126,17 @@ namespace NEG.UI 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() { if(currentShownPopup.data is { IsValid: true }) @@ -134,6 +146,16 @@ namespace NEG.UI 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) { if (currentShownPopup.data != data) diff --git a/NEG/UI/UnityUi/Area/CloseMainWindowOnBack.cs b/NEG/UI/UnityUi/Area/CloseMainWindowOnBack.cs new file mode 100644 index 0000000..00a0b70 --- /dev/null +++ b/NEG/UI/UnityUi/Area/CloseMainWindowOnBack.cs @@ -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(); + controllable.UseBack += UseBack; + } + } +} \ No newline at end of file diff --git a/NEG/UI/UnityUi/Area/CloseMainWindowOnBack.cs.meta b/NEG/UI/UnityUi/Area/CloseMainWindowOnBack.cs.meta new file mode 100644 index 0000000..4260da6 --- /dev/null +++ b/NEG/UI/UnityUi/Area/CloseMainWindowOnBack.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 6ec2bf64cda740a1849d024d4f163a01 +timeCreated: 1686598066 \ No newline at end of file diff --git a/NEG/UI/UnityUi/Area/MonoArea.cs b/NEG/UI/UnityUi/Area/MonoArea.cs index e5f92b0..b79a409 100644 --- a/NEG/UI/UnityUi/Area/MonoArea.cs +++ b/NEG/UI/UnityUi/Area/MonoArea.cs @@ -5,12 +5,17 @@ using NEG.UI.UnityUi.Window; using NEG.UI.UnityUi.WindowSlot; using NEG.UI.Window; using NEG.UI.WindowSlot; +using NegUtils.NEG.UI; using System; namespace NEG.UI.Area { public class MonoArea : MonoBehaviour, IArea { + public event Action OnOpened; + public event Action OnClosed; + public event Action UseBack; + public IEnumerable AvailableSlots => windowSlots; 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 OpenWindow(IWindow window, object data = null) - { - DefaultWindowSlot.AttachWindow(window); - window.SetData(data); - } + public virtual void OpenWindow(IWindow window, object data = null) => DefaultWindowSlot.AttachWindow(window, data); protected virtual void Awake() { @@ -37,5 +38,12 @@ namespace NEG.UI.Area if (ReferenceEquals(UiManager.Instance.CurrentArea, this)) 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); } } \ No newline at end of file diff --git a/NEG/UI/UnityUi/Buttons/OpenAsCurrentMainChild.cs b/NEG/UI/UnityUi/Buttons/OpenAsCurrentMainChild.cs new file mode 100644 index 0000000..1d9bcf2 --- /dev/null +++ b/NEG/UI/UnityUi/Buttons/OpenAsCurrentMainChild.cs @@ -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); + } +} \ No newline at end of file diff --git a/NEG/UI/UnityUi/Buttons/OpenAsCurrentMainChild.cs.meta b/NEG/UI/UnityUi/Buttons/OpenAsCurrentMainChild.cs.meta new file mode 100644 index 0000000..ac897a0 --- /dev/null +++ b/NEG/UI/UnityUi/Buttons/OpenAsCurrentMainChild.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 049e222140c94fc28fb36bca4aaddba4 +timeCreated: 1686595194 \ No newline at end of file diff --git a/NEG/UI/UnityUi/Window/CloseWindowOnBack.cs b/NEG/UI/UnityUi/Window/CloseWindowOnBack.cs new file mode 100644 index 0000000..9e2cc9f --- /dev/null +++ b/NEG/UI/UnityUi/Window/CloseWindowOnBack.cs @@ -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(); + controllable.UseBack += UseBack; + } + } +} \ No newline at end of file diff --git a/NEG/UI/UnityUi/Window/CloseWindowOnBack.cs.meta b/NEG/UI/UnityUi/Window/CloseWindowOnBack.cs.meta new file mode 100644 index 0000000..535a375 --- /dev/null +++ b/NEG/UI/UnityUi/Window/CloseWindowOnBack.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: b4a156da6df74abeb281176000738ebb +timeCreated: 1686598323 \ No newline at end of file diff --git a/NEG/UI/UnityUi/Window/MonoWindow.cs b/NEG/UI/UnityUi/Window/MonoWindow.cs index 664971b..219265e 100644 --- a/NEG/UI/UnityUi/Window/MonoWindow.cs +++ b/NEG/UI/UnityUi/Window/MonoWindow.cs @@ -3,6 +3,7 @@ using NEG.UI.UnityUi.Buttons; using NEG.UI.UnityUi.WindowSlot; using NEG.UI.Window; using NEG.UI.WindowSlot; +using NegUtils.NEG.UI; using System; using System.Collections.Generic; using UnityEngine; @@ -12,8 +13,13 @@ namespace NEG.UI.UnityUi.Window { public class MonoWindow : MonoBehaviour, IWindow { + public event Action OnOpened; + public event Action OnClosed; + public event Action UseBack; + public IEnumerable AvailableSlots => windowSlots; public IWindowSlot Parent { get; private set; } + public IController Controller { get; } public bool IsMainWindow { get; private set; } @@ -29,14 +35,8 @@ namespace NEG.UI.UnityUi.Window gameObject.SetActive(true); Parent = parentSlot; EventSystem.current.SetSelectedGameObject(defaultSelectedItem); - if (controller != null) - controller.OnOpened(); - } - - public void SetData(object data) - { - if (controller != null) - controller.SetData(data); + if (parentSlot.OpenWindowAsMain) + UiManager.Instance.SetMainWindow(this); } public void SetClosedState() @@ -44,6 +44,7 @@ namespace NEG.UI.UnityUi.Window gameObject.SetActive(false); Parent = null; ((ISlotsHolder)this).CloseAllWindows(); + UiManager.Instance.OnWindowClosed(this); if(controller != null) controller.OnClosed(); } @@ -64,12 +65,14 @@ namespace NEG.UI.UnityUi.Window #endif } - public void OpenWindow(IWindow window, object data = null) - { - DefaultWindowSlot.AttachWindow(window); - window.SetData(data); - } + public void OpenWindow(IWindow window, object data = null) => DefaultWindowSlot.AttachWindow(window, data); 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); } } \ No newline at end of file diff --git a/NEG/UI/UnityUi/Window/WindowController.cs b/NEG/UI/UnityUi/Window/WindowController.cs index 6451e30..4be335c 100644 --- a/NEG/UI/UnityUi/Window/WindowController.cs +++ b/NEG/UI/UnityUi/Window/WindowController.cs @@ -1,20 +1,27 @@ -using System; +using NegUtils.NEG.UI; +using System; using UnityEngine; namespace NEG.UI.UnityUi.Window { - [RequireComponent(typeof(MonoWindow))] - //Due to prefab variants we need this - public abstract class WindowController : MonoBehaviour + public abstract class WindowController : MonoBehaviour, IController { - protected MonoWindow window; - - public abstract void SetData(object data); + protected IControllable controllable; - public virtual void OnOpened() { } + public virtual void OnOpened(object data) { } public virtual void OnClosed() { } - protected virtual void Awake() => window = GetComponent(); + protected virtual void Awake() + { + controllable = GetComponent(); + controllable.OnOpened += OnOpened; + controllable.OnClosed += OnClosed; + controllable.UseBack += UseBack; + } + + public void UseBack(IControllable.BackUsed obj) + { + } } } \ No newline at end of file diff --git a/NEG/UI/UnityUi/WindowSlot/MonoWindowSlot.cs b/NEG/UI/UnityUi/WindowSlot/MonoWindowSlot.cs index 49c94da..90fb9cf 100644 --- a/NEG/UI/UnityUi/WindowSlot/MonoWindowSlot.cs +++ b/NEG/UI/UnityUi/WindowSlot/MonoWindowSlot.cs @@ -13,7 +13,7 @@ namespace NEG.UI.UnityUi.WindowSlot [field: SerializeField] public bool OpenWindowAsMain { get; private set; } 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 CloseAllWindows(); diff --git a/NEG/UI/UnityUi/WindowSlot/SingleWindowSlot.cs b/NEG/UI/UnityUi/WindowSlot/SingleWindowSlot.cs index b1b7841..023d734 100644 --- a/NEG/UI/UnityUi/WindowSlot/SingleWindowSlot.cs +++ b/NEG/UI/UnityUi/WindowSlot/SingleWindowSlot.cs @@ -19,7 +19,12 @@ namespace NEG.UI.WindowSlot 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 CloseAllWindows() => CurrentWindow = null; } diff --git a/NEG/UI/UnityUi/WindowSlot/SingleWindowSlotWithHistory.cs b/NEG/UI/UnityUi/WindowSlot/SingleWindowSlotWithHistory.cs index facab85..300a09d 100644 --- a/NEG/UI/UnityUi/WindowSlot/SingleWindowSlotWithHistory.cs +++ b/NEG/UI/UnityUi/WindowSlot/SingleWindowSlotWithHistory.cs @@ -29,7 +29,11 @@ namespace NegUtils.NEG.UI.UnityUi.WindowSlot private readonly List windowsHistory = new List(); - 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) { diff --git a/NEG/UI/Window/IWindow.cs b/NEG/UI/Window/IWindow.cs index 9495701..095bc13 100644 --- a/NEG/UI/Window/IWindow.cs +++ b/NEG/UI/Window/IWindow.cs @@ -1,16 +1,19 @@ using JetBrains.Annotations; using NEG.UI.Area; using NEG.UI.WindowSlot; +using NegUtils.NEG.UI; using UnityEngine; namespace NEG.UI.Window { - public interface IWindow : ISlotsHolder + public interface IWindow : ISlotsHolder, IControllable { /// /// Parent slot of this window. /// IWindowSlot Parent { get; } + + IController Controller { get; } /// /// Called internally by slot to open window. @@ -18,12 +21,6 @@ namespace NEG.UI.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. /// @@ -52,8 +49,7 @@ namespace NEG.UI.Window { if (slot != null) { - slot.AttachWindow(window); - window.SetData(data); + slot.AttachWindow(window, data); return; } diff --git a/NEG/UI/WindowSlot/IWindowSlot.cs b/NEG/UI/WindowSlot/IWindowSlot.cs index 07dfa27..8b06bbd 100644 --- a/NEG/UI/WindowSlot/IWindowSlot.cs +++ b/NEG/UI/WindowSlot/IWindowSlot.cs @@ -3,11 +3,11 @@ using NEG.UI.Window; namespace NEG.UI.WindowSlot { - public interface IWindowSlot + public interface IWindowSlot { bool OpenWindowAsMain { get; } ISlotsHolder ParentHolder { get; } - void AttachWindow(IWindow window); + void AttachWindow(IWindow window, object data); void DetachWindow(IWindow window); void CloseAllWindows(); }