Files
aibis-dream/Assets/Scripts/UI/Terminal/TerminalMenuItemView.cs
T

140 lines
3.9 KiB
C#

using System;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace AibisDream.UI.Terminal
{
[RequireComponent(typeof(BaseButton))]
public class TerminalMenuItemView : MonoBehaviour
{
[SerializeField] private TMP_Text labelText;
[SerializeField] private TMP_Text statusText;
[SerializeField] private TMP_Text detailText;
[SerializeField] private TMP_Text cursor;
[SerializeField] private Image selectionBar;
[SerializeField] private Color normalTextColor = Color.white;
[SerializeField] private Color selectedTextColor = new Color(0.02f, 0.02f, 0.04f);
[SerializeField] private Color dangerTextColor = new Color(1f, 0.18f, 0.2f);
[SerializeField] private float disabledAlpha = 0.35f;
private BaseButton _button;
private CanvasGroup _canvasGroup;
private bool _isDanger;
private bool _isSelected;
public event Action<TerminalMenuItemView> Clicked;
public event Action<TerminalMenuItemView> Hovered;
private void Awake()
{
_button = GetComponent<BaseButton>();
_canvasGroup = GetComponent<CanvasGroup>();
if (_canvasGroup == null)
{
_canvasGroup = gameObject.AddComponent<CanvasGroup>();
}
_button.onClick.AddListener(HandleClick);
_button.onStateTransition.AddListener(OnButtonStateTransition);
SetSelected(false);
}
private void OnDestroy()
{
if (_button != null)
{
_button.onClick.RemoveListener(HandleClick);
_button.onStateTransition.RemoveListener(OnButtonStateTransition);
}
}
public void SetContent(string label, string status = "", string detail = "", bool isDanger = false)
{
_isDanger = isDanger;
if (labelText != null)
{
labelText.text = label;
}
if (statusText != null)
{
statusText.text = status;
}
if (detailText != null)
{
detailText.text = detail;
}
RefreshVisual();
}
public void SetInteractable(bool interactable)
{
if (_button != null)
{
_button.interactable = interactable;
}
if (_canvasGroup != null)
{
_canvasGroup.alpha = interactable ? 1f : disabledAlpha;
}
}
public void SetSelected(bool selected)
{
_isSelected = selected;
RefreshVisual();
}
private void HandleClick()
{
Clicked?.Invoke(this);
}
private void OnButtonStateTransition(SelectionType state, bool instant)
{
if (state is SelectionType.Highlighted or SelectionType.Selected or SelectionType.Pressed)
{
Hovered?.Invoke(this);
}
RefreshVisual();
}
private void RefreshVisual()
{
if (cursor != null)
{
cursor.gameObject.SetActive(_isSelected);
}
if (selectionBar != null)
{
selectionBar.gameObject.SetActive(_isSelected);
}
var color = _isDanger ? dangerTextColor : normalTextColor;
if (_isSelected && !_isDanger)
{
color = selectedTextColor;
}
SetColor(labelText, color);
SetColor(statusText, color);
SetColor(detailText, color);
SetColor(cursor, color);
}
private static void SetColor(TMP_Text text, Color color)
{
if (text != null)
{
text.color = color;
}
}
}
}