Neg_Utils/NEG/UI/UnityUi/Area/MonoArea.cs
2024-02-12 21:26:24 +01:00

58 lines
1.5 KiB
C#

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