diff --git a/NEG/UI/Area/IArea.cs b/NEG/UI/Area/IArea.cs index d542ee6..11f0559 100644 --- a/NEG/UI/Area/IArea.cs +++ b/NEG/UI/Area/IArea.cs @@ -3,7 +3,9 @@ using NegUtils.NEG.UI; namespace NEG.UI.Area { - public interface IArea : ISlotsHolder, IUiElement, IControllable + public interface IArea : ISlotsHolder, IControllable { + void Open(); + void Close(); } } \ No newline at end of file diff --git a/NEG/UI/IControllable.cs b/NEG/UI/IControllable.cs index e030148..9fbc5e1 100644 --- a/NEG/UI/IControllable.cs +++ b/NEG/UI/IControllable.cs @@ -11,13 +11,8 @@ namespace NegUtils.NEG.UI event Action OnOpened; event Action OnClosed; - /// - /// - /// event Action UseBack; - - public void FireOpenCallback(object data); - public void FireOnClosed(); - public void FireUseBack(ref BackUsed backUsed); + + public void TryUseBack(ref BackUsed backUsed); } } \ No newline at end of file diff --git a/NEG/UI/IController.cs b/NEG/UI/IController.cs index a48a051..ddb99ff 100644 --- a/NEG/UI/IController.cs +++ b/NEG/UI/IController.cs @@ -2,10 +2,6 @@ { public interface IController { - void OnOpened(object data); - - void OnClosed(); - - void UseBack(IControllable.BackUsed backUsed); + IControllable Controllable { get; } } } \ No newline at end of file diff --git a/NEG/UI/IUiElement.cs b/NEG/UI/IUiElement.cs deleted file mode 100644 index 6cfde2b..0000000 --- a/NEG/UI/IUiElement.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace NEG.UI -{ - public interface IUiElement - { - /// - /// Sets only visible state of element - /// - /// - void SetEnabled(bool setEnabled); - } -} \ No newline at end of file diff --git a/NEG/UI/IUiElement.cs.meta b/NEG/UI/IUiElement.cs.meta deleted file mode 100644 index 765fb83..0000000 --- a/NEG/UI/IUiElement.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: 296bf6969a6347f8aea788a7bdd086af -timeCreated: 1670693177 \ No newline at end of file diff --git a/NEG/UI/UiManager.cs b/NEG/UI/UiManager.cs index 048b860..c25e4b3 100644 --- a/NEG/UI/UiManager.cs +++ b/NEG/UI/UiManager.cs @@ -23,11 +23,11 @@ namespace NEG.UI get => currentArea; set { - currentArea?.SetEnabled(false); + currentArea?.Close(); currentArea = value; - currentArea?.SetEnabled(true); + currentArea?.Open(); } } @@ -130,10 +130,10 @@ namespace NEG.UI { IControllable.BackUsed backUsed = new(); - CurrentMainWindow?.FireUseBack(ref backUsed); + CurrentMainWindow?.TryUseBack(ref backUsed); if(backUsed.Used) return; - CurrentArea.FireUseBack(ref backUsed); + CurrentArea.TryUseBack(ref backUsed); } diff --git a/NEG/UI/UnityUi/Area/CloseMainWindowOnBack.cs b/NEG/UI/UnityUi/Area/CloseMainWindowOnBack.cs index 00a0b70..feb80a7 100644 --- a/NEG/UI/UnityUi/Area/CloseMainWindowOnBack.cs +++ b/NEG/UI/UnityUi/Area/CloseMainWindowOnBack.cs @@ -1,27 +1,19 @@ -using NEG.UI.Window; +using KBCore.Refs; +using NEG.UI.UnityUi; +using NEG.UI.Window; using NegUtils.NEG.UI; +using System; using UnityEngine; namespace NEG.UI.Area { - public class CloseMainWindowOnBack : MonoBehaviour, IController + public class CloseMainWindowOnBack : MonoController { - private IControllable controllable; - - public void OnOpened(object data) { } - - public void OnClosed() { } - - public void UseBack(IControllable.BackUsed backUsed) + protected override void UseBack(IControllable.BackUsed backUsed) { + base.UseBack(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/MonoArea.cs b/NEG/UI/UnityUi/Area/MonoArea.cs index 8b46887..f6d2d6f 100644 --- a/NEG/UI/UnityUi/Area/MonoArea.cs +++ b/NEG/UI/UnityUi/Area/MonoArea.cs @@ -10,7 +10,7 @@ using System; namespace NEG.UI.Area { - public class MonoArea : MonoBehaviour, IArea, IControllable + public class MonoArea : MonoBehaviour, IArea { public event Action OnOpened; public event Action OnClosed; @@ -22,34 +22,38 @@ namespace NEG.UI.Area [SerializeField] private bool setAsDefaultArea; [SerializeField] private List windowSlots; + + public void Open() + { + gameObject.SetActive(true); + OnOpened?.Invoke(null); + } - public virtual void SetEnabled(bool setEnabled) => gameObject.SetActive(setEnabled); + public void Close(){ + gameObject.SetActive(false); + OnClosed?.Invoke(); + } + + public void OpenWindow(IWindow window, object data = null) => DefaultWindowSlot.AttachWindow(window, data); - public virtual void OpenWindow(IWindow window, object data = null) => DefaultWindowSlot.AttachWindow(window, data); - - protected virtual void Awake() + private void Awake() { if (setAsDefaultArea) UiManager.Instance.CurrentArea = this; } - protected void Start() + private void Start() { if(!setAsDefaultArea) - SetEnabled(false); + Close(); } - protected virtual void OnDestroy() + private void OnDestroy() { 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); + public void TryUseBack(ref IControllable.BackUsed backUsed) => UseBack?.Invoke(backUsed); } } \ No newline at end of file diff --git a/NEG/UI/UnityUi/MonoController.cs b/NEG/UI/UnityUi/MonoController.cs index d2d6640..79d538a 100644 --- a/NEG/UI/UnityUi/MonoController.cs +++ b/NEG/UI/UnityUi/MonoController.cs @@ -7,12 +7,10 @@ namespace NEG.UI.UnityUi { public abstract class MonoController : MonoBehaviour, IController { + public IControllable Controllable => controllable.Value; + [SerializeField, Self] protected InterfaceRef controllable; - - public virtual void OnOpened(object data) { } - - public virtual void OnClosed() { } - + protected virtual void Awake() { controllable.Value.OnOpened += OnOpened; @@ -21,9 +19,11 @@ namespace NEG.UI.UnityUi } private void OnValidate() => this.ValidateRefs(); + + protected virtual void OnOpened(object data) { } - public virtual void UseBack(IControllable.BackUsed obj) - { - } + protected virtual void OnClosed() { } + + protected virtual void UseBack(IControllable.BackUsed obj) { } } } \ No newline at end of file diff --git a/NEG/UI/UnityUi/Window/CloseWindowOnBack.cs b/NEG/UI/UnityUi/Window/CloseWindowOnBack.cs index 9e2cc9f..bdc9bef 100644 --- a/NEG/UI/UnityUi/Window/CloseWindowOnBack.cs +++ b/NEG/UI/UnityUi/Window/CloseWindowOnBack.cs @@ -5,26 +5,15 @@ using UnityEngine; namespace NEG.UI.UnityUi.Window { - public class CloseWindowOnBack : MonoBehaviour, IController + public class CloseWindowOnBack : MonoController { [SerializeField, Self(Flag.Editable)] private MonoWindow window; - - private IControllable controllable; - public void OnOpened(object data) { } - - public void OnClosed() { } - - public void UseBack(IControllable.BackUsed backUsed) + protected override void UseBack(IControllable.BackUsed backUsed) { + base.UseBack(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/MonoWindow.cs b/NEG/UI/UnityUi/Window/MonoWindow.cs index 8027e00..b9b400d 100644 --- a/NEG/UI/UnityUi/Window/MonoWindow.cs +++ b/NEG/UI/UnityUi/Window/MonoWindow.cs @@ -20,24 +20,23 @@ namespace NEG.UI.UnityUi.Window public IEnumerable AvailableSlots => windowSlots; public IWindowSlot Parent { get; private set; } - public IController Controller { get; } public bool IsMainWindow { get; private set; } private IWindowSlot DefaultWindowSlot => windowSlots[0]; [SerializeField] private List windowSlots; - [SerializeField] private MonoController monoController; [SerializeField] private GameObject defaultSelectedItem; - public void SetOpenedState(IWindowSlot parentSlot) + public void SetOpenedState(IWindowSlot parentSlot, object data) { gameObject.SetActive(true); Parent = parentSlot; EventSystem.current.SetSelectedGameObject(defaultSelectedItem); if (parentSlot.OpenWindowAsMain) UiManager.Instance.SetMainWindow(this); + OnOpened?.Invoke(data); } public void SetClosedState() @@ -46,8 +45,7 @@ namespace NEG.UI.UnityUi.Window Parent = null; ((ISlotsHolder)this).CloseAllWindows(); UiManager.Instance.OnWindowClosed(this); - if(monoController != null) - monoController.OnClosed(); + OnClosed?.Invoke(); } public void SetHiddenState() => gameObject.SetActive(false); @@ -58,8 +56,6 @@ namespace NEG.UI.UnityUi.Window private void OnValidate() { - if (monoController == null) - monoController = GetComponent(); #if !NEG_UI_DISABLE_WARNING_DEFAULT_SELECTION if(defaultSelectedItem == null) Debug.LogWarning($"Window {name} should have default selected item set"); @@ -70,10 +66,6 @@ namespace NEG.UI.UnityUi.Window 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); + public void TryUseBack(ref IControllable.BackUsed backUsed) => UseBack?.Invoke(backUsed); } } \ No newline at end of file diff --git a/NEG/UI/UnityUi/WindowSlot/SingleWindowSlot.cs b/NEG/UI/UnityUi/WindowSlot/SingleWindowSlot.cs index 023d734..04c49b9 100644 --- a/NEG/UI/UnityUi/WindowSlot/SingleWindowSlot.cs +++ b/NEG/UI/UnityUi/WindowSlot/SingleWindowSlot.cs @@ -9,11 +9,10 @@ namespace NEG.UI.WindowSlot public IWindow CurrentWindow { get => currentWindow; - set + protected set { currentWindow?.SetClosedState(); currentWindow = value; - currentWindow?.SetOpenedState(this); } } @@ -22,7 +21,7 @@ namespace NEG.UI.WindowSlot public override void AttachWindow(IWindow window, object data) { CurrentWindow = window; - window.FireOpenCallback(data); + window.SetOpenedState(this, data); } public override void DetachWindow(IWindow window) => CurrentWindow = null; diff --git a/NEG/UI/UnityUi/WindowSlot/SingleWindowSlotWithHistory.cs b/NEG/UI/UnityUi/WindowSlot/SingleWindowSlotWithHistory.cs index 300a09d..a718257 100644 --- a/NEG/UI/UnityUi/WindowSlot/SingleWindowSlotWithHistory.cs +++ b/NEG/UI/UnityUi/WindowSlot/SingleWindowSlotWithHistory.cs @@ -21,7 +21,6 @@ namespace NegUtils.NEG.UI.UnityUi.WindowSlot currentWindow?.SetHiddenState(); currentWindow = value; windowsHistory.Add(currentWindow); - currentWindow?.SetOpenedState(this); } } @@ -32,7 +31,7 @@ namespace NegUtils.NEG.UI.UnityUi.WindowSlot public override void AttachWindow(IWindow window, object data) { CurrentWindow = window; - CurrentWindow.FireOpenCallback(data); + CurrentWindow.SetOpenedState(this, data); } public override void DetachWindow(IWindow window) diff --git a/NEG/UI/Window/IWindow.cs b/NEG/UI/Window/IWindow.cs index 095bc13..e470343 100644 --- a/NEG/UI/Window/IWindow.cs +++ b/NEG/UI/Window/IWindow.cs @@ -12,14 +12,13 @@ namespace NEG.UI.Window /// Parent slot of this window. /// IWindowSlot Parent { get; } - - IController Controller { get; } /// /// Called internally by slot to open window. /// /// slot that opens window - void SetOpenedState(IWindowSlot parentSlot); + /// data + void SetOpenedState(IWindowSlot parentSlot, object data); /// /// Called internally to close window by slot.