using System.Collections.Generic;
using UnityEngine;
using UnityAtoms.BaseAtoms;
namespace UnityAtoms.UI
{
///
/// A MonoBehaviour that you can add to a `CanvasGroup` and makes it transition based on a `StringVariable` value.
///
/// **TODO**: Add support for differnt transitions. Maybe integrate with DOTween?
///
[AddComponentMenu("Unity Atoms/UI/Container")]
public class UIContainer : MonoBehaviour, IAtomListener
{
///
/// Variable that we listens to.
///
[SerializeField]
private StringVariable _currentUIState = null;
///
/// A list of states that this `UIContainer` will be visible for.
///
[SerializeField]
private List _visibleForStates = null;
private void Start()
{
StateNameChanged(_currentUIState.Value);
}
///
/// Handler for when the state is changed.
///
///
public void OnEventRaised(string stateName)
{
StateNameChanged(stateName);
}
private void StateNameChanged(string stateName)
{
if (_visibleForStates.Exists((state) => state.Value == stateName))
{
GetComponent().alpha = 1f;
GetComponent().blocksRaycasts = true;
GetComponent().interactable = true;
}
else
{
GetComponent().alpha = 0f;
GetComponent().blocksRaycasts = false;
GetComponent().interactable = false;
}
}
private void Awake()
{
if (_currentUIState.Changed != null)
{
_currentUIState.Changed.RegisterListener(this);
}
}
private void OnDestroy()
{
if (_currentUIState.Changed != null)
{
_currentUIState.Changed.UnregisterListener(this);
}
}
}
}