final update befor pr

This commit is contained in:
Hubert Mattusch 2023-09-01 21:42:37 +02:00
parent 33d0a9078f
commit fb90337a3f
14 changed files with 55 additions and 102 deletions

View File

@ -3,7 +3,9 @@ using NegUtils.NEG.UI;
namespace NEG.UI.Area namespace NEG.UI.Area
{ {
public interface IArea : ISlotsHolder, IUiElement, IControllable public interface IArea : ISlotsHolder, IControllable
{ {
void Open();
void Close();
} }
} }

View File

@ -11,13 +11,8 @@ namespace NegUtils.NEG.UI
event Action<object> OnOpened; event Action<object> OnOpened;
event Action OnClosed; event Action OnClosed;
/// <summary>
///
/// </summary>
event Action<BackUsed> UseBack; event Action<BackUsed> UseBack;
public void FireOpenCallback(object data); public void TryUseBack(ref BackUsed backUsed);
public void FireOnClosed();
public void FireUseBack(ref BackUsed backUsed);
} }
} }

View File

@ -2,10 +2,6 @@
{ {
public interface IController public interface IController
{ {
void OnOpened(object data); IControllable Controllable { get; }
void OnClosed();
void UseBack(IControllable.BackUsed backUsed);
} }
} }

View File

@ -1,11 +0,0 @@
namespace NEG.UI
{
public interface IUiElement
{
/// <summary>
/// Sets only visible state of element
/// </summary>
/// <param name="setEnabled"></param>
void SetEnabled(bool setEnabled);
}
}

View File

@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: 296bf6969a6347f8aea788a7bdd086af
timeCreated: 1670693177

View File

@ -23,11 +23,11 @@ namespace NEG.UI
get => currentArea; get => currentArea;
set set
{ {
currentArea?.SetEnabled(false); currentArea?.Close();
currentArea = value; currentArea = value;
currentArea?.SetEnabled(true); currentArea?.Open();
} }
} }
@ -130,10 +130,10 @@ namespace NEG.UI
{ {
IControllable.BackUsed backUsed = new(); IControllable.BackUsed backUsed = new();
CurrentMainWindow?.FireUseBack(ref backUsed); CurrentMainWindow?.TryUseBack(ref backUsed);
if(backUsed.Used) if(backUsed.Used)
return; return;
CurrentArea.FireUseBack(ref backUsed); CurrentArea.TryUseBack(ref backUsed);
} }

View File

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

View File

@ -10,7 +10,7 @@ using System;
namespace NEG.UI.Area namespace NEG.UI.Area
{ {
public class MonoArea : MonoBehaviour, IArea, IControllable public class MonoArea : MonoBehaviour, IArea
{ {
public event Action<object> OnOpened; public event Action<object> OnOpened;
public event Action OnClosed; public event Action OnClosed;
@ -23,33 +23,37 @@ namespace NEG.UI.Area
[SerializeField] private List<MonoWindowSlot> windowSlots; [SerializeField] private List<MonoWindowSlot> windowSlots;
public virtual void SetEnabled(bool setEnabled) => gameObject.SetActive(setEnabled); public void Open()
{
gameObject.SetActive(true);
OnOpened?.Invoke(null);
}
public virtual void OpenWindow(IWindow window, object data = null) => DefaultWindowSlot.AttachWindow(window, data); public void Close(){
gameObject.SetActive(false);
OnClosed?.Invoke();
}
protected virtual void Awake() public void OpenWindow(IWindow window, object data = null) => DefaultWindowSlot.AttachWindow(window, data);
private void Awake()
{ {
if (setAsDefaultArea) if (setAsDefaultArea)
UiManager.Instance.CurrentArea = this; UiManager.Instance.CurrentArea = this;
} }
protected void Start() private void Start()
{ {
if(!setAsDefaultArea) if(!setAsDefaultArea)
SetEnabled(false); Close();
} }
protected virtual void OnDestroy() private void OnDestroy()
{ {
if (ReferenceEquals(UiManager.Instance.CurrentArea, this)) if (ReferenceEquals(UiManager.Instance.CurrentArea, this))
UiManager.Instance.CurrentArea = null; UiManager.Instance.CurrentArea = null;
} }
public void TryUseBack(ref IControllable.BackUsed backUsed) => UseBack?.Invoke(backUsed);
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

@ -7,12 +7,10 @@ namespace NEG.UI.UnityUi
{ {
public abstract class MonoController : MonoBehaviour, IController public abstract class MonoController : MonoBehaviour, IController
{ {
public IControllable Controllable => controllable.Value;
[SerializeField, Self] protected InterfaceRef<IControllable> controllable; [SerializeField, Self] protected InterfaceRef<IControllable> controllable;
public virtual void OnOpened(object data) { }
public virtual void OnClosed() { }
protected virtual void Awake() protected virtual void Awake()
{ {
controllable.Value.OnOpened += OnOpened; controllable.Value.OnOpened += OnOpened;
@ -22,8 +20,10 @@ namespace NEG.UI.UnityUi
private void OnValidate() => this.ValidateRefs(); private void OnValidate() => this.ValidateRefs();
public virtual void UseBack(IControllable.BackUsed obj) protected virtual void OnOpened(object data) { }
{
} protected virtual void OnClosed() { }
protected virtual void UseBack(IControllable.BackUsed obj) { }
} }
} }

View File

@ -5,26 +5,15 @@ using UnityEngine;
namespace NEG.UI.UnityUi.Window namespace NEG.UI.UnityUi.Window
{ {
public class CloseWindowOnBack : MonoBehaviour, IController public class CloseWindowOnBack : MonoController
{ {
[SerializeField, Self(Flag.Editable)] private MonoWindow window; [SerializeField, Self(Flag.Editable)] private MonoWindow window;
private IControllable controllable; protected override void UseBack(IControllable.BackUsed backUsed)
public void OnOpened(object data) { }
public void OnClosed() { }
public void UseBack(IControllable.BackUsed backUsed)
{ {
base.UseBack(backUsed);
window.Close(); window.Close();
backUsed.Used = true; backUsed.Used = true;
} }
private void Awake()
{
controllable = GetComponent<IControllable>();
controllable.UseBack += UseBack;
}
} }
} }

View File

@ -20,24 +20,23 @@ namespace NEG.UI.UnityUi.Window
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; }
private IWindowSlot DefaultWindowSlot => windowSlots[0]; private IWindowSlot DefaultWindowSlot => windowSlots[0];
[SerializeField] private List<MonoWindowSlot> windowSlots; [SerializeField] private List<MonoWindowSlot> windowSlots;
[SerializeField] private MonoController monoController;
[SerializeField] private GameObject defaultSelectedItem; [SerializeField] private GameObject defaultSelectedItem;
public void SetOpenedState(IWindowSlot parentSlot) public void SetOpenedState(IWindowSlot parentSlot, object data)
{ {
gameObject.SetActive(true); gameObject.SetActive(true);
Parent = parentSlot; Parent = parentSlot;
EventSystem.current.SetSelectedGameObject(defaultSelectedItem); EventSystem.current.SetSelectedGameObject(defaultSelectedItem);
if (parentSlot.OpenWindowAsMain) if (parentSlot.OpenWindowAsMain)
UiManager.Instance.SetMainWindow(this); UiManager.Instance.SetMainWindow(this);
OnOpened?.Invoke(data);
} }
public void SetClosedState() public void SetClosedState()
@ -46,8 +45,7 @@ namespace NEG.UI.UnityUi.Window
Parent = null; Parent = null;
((ISlotsHolder)this).CloseAllWindows(); ((ISlotsHolder)this).CloseAllWindows();
UiManager.Instance.OnWindowClosed(this); UiManager.Instance.OnWindowClosed(this);
if(monoController != null) OnClosed?.Invoke();
monoController.OnClosed();
} }
public void SetHiddenState() => gameObject.SetActive(false); public void SetHiddenState() => gameObject.SetActive(false);
@ -58,8 +56,6 @@ namespace NEG.UI.UnityUi.Window
private void OnValidate() private void OnValidate()
{ {
if (monoController == null)
monoController = GetComponent<MonoController>();
#if !NEG_UI_DISABLE_WARNING_DEFAULT_SELECTION #if !NEG_UI_DISABLE_WARNING_DEFAULT_SELECTION
if(defaultSelectedItem == null) if(defaultSelectedItem == null)
Debug.LogWarning($"Window {name} should have default selected item set"); 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 SetDefaultSelectedItem(GameObject item) => defaultSelectedItem = item;
public void FireOpenCallback(object data) => OnOpened?.Invoke(data); public void TryUseBack(ref IControllable.BackUsed backUsed) => UseBack?.Invoke(backUsed);
public void FireOnClosed() => OnClosed?.Invoke();
public void FireUseBack(ref IControllable.BackUsed backUsed) => UseBack?.Invoke(backUsed);
} }
} }

View File

@ -9,11 +9,10 @@ namespace NEG.UI.WindowSlot
public IWindow CurrentWindow public IWindow CurrentWindow
{ {
get => currentWindow; get => currentWindow;
set protected set
{ {
currentWindow?.SetClosedState(); currentWindow?.SetClosedState();
currentWindow = value; currentWindow = value;
currentWindow?.SetOpenedState(this);
} }
} }
@ -22,7 +21,7 @@ namespace NEG.UI.WindowSlot
public override void AttachWindow(IWindow window, object data) public override void AttachWindow(IWindow window, object data)
{ {
CurrentWindow = window; CurrentWindow = window;
window.FireOpenCallback(data); window.SetOpenedState(this, data);
} }
public override void DetachWindow(IWindow window) => CurrentWindow = null; public override void DetachWindow(IWindow window) => CurrentWindow = null;

View File

@ -21,7 +21,6 @@ namespace NegUtils.NEG.UI.UnityUi.WindowSlot
currentWindow?.SetHiddenState(); currentWindow?.SetHiddenState();
currentWindow = value; currentWindow = value;
windowsHistory.Add(currentWindow); windowsHistory.Add(currentWindow);
currentWindow?.SetOpenedState(this);
} }
} }
@ -32,7 +31,7 @@ namespace NegUtils.NEG.UI.UnityUi.WindowSlot
public override void AttachWindow(IWindow window, object data) public override void AttachWindow(IWindow window, object data)
{ {
CurrentWindow = window; CurrentWindow = window;
CurrentWindow.FireOpenCallback(data); CurrentWindow.SetOpenedState(this, data);
} }
public override void DetachWindow(IWindow window) public override void DetachWindow(IWindow window)

View File

@ -13,13 +13,12 @@ namespace NEG.UI.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); /// <param name="data">data</param>
void SetOpenedState(IWindowSlot parentSlot, object data);
/// <summary> /// <summary>
/// Called internally to close window by slot. /// Called internally to close window by slot.