geometry-tower-defense/Assets/GameMain/Scripts/UI/Game/View/EventOptionItem.cs

86 lines
2.4 KiB
C#

using GeometryTD.CustomEvent;
using TMPro;
using UnityEngine;
namespace GeometryTD.UI
{
public class EventOptionItem : MonoBehaviour
{
[SerializeField] private TMP_Text _optionText;
private static readonly Color SelectableTextColor = Color.white;
private static readonly Color BlockedTextColor = new Color(0.75f, 0.75f, 0.75f, 1f);
private int _optionIndex;
private bool _isSelectable;
private CommonButton _commonButton;
private void Awake()
{
_commonButton = GetComponent<CommonButton>();
}
public void OnInit(EventOptionItemContext context)
{
if (context == null)
{
OnReset();
gameObject.SetActive(false);
return;
}
_optionIndex = context.OptionIndex;
_isSelectable = context.IsSelectable;
if (_optionText != null)
{
_optionText.text = BuildDisplayText(context);
_optionText.color = _isSelectable ? SelectableTextColor : BlockedTextColor;
}
if (_commonButton != null)
{
_commonButton.Interactive = _isSelectable;
}
gameObject.SetActive(!string.IsNullOrEmpty(context.OptionText));
}
public void OnReset()
{
_optionIndex = -1;
_isSelectable = false;
if (_optionText != null)
{
_optionText.text = string.Empty;
_optionText.color = SelectableTextColor;
}
if (_commonButton != null)
{
_commonButton.Interactive = true;
}
}
public void OnClick()
{
if (_optionIndex < 0 || !_isSelectable)
{
return;
}
GameEntry.Event.Fire(this, EventOptionItemSelectedEventArgs.Create(_optionIndex));
}
private static string BuildDisplayText(EventOptionItemContext context)
{
string optionText = context.OptionText ?? string.Empty;
if (context.IsSelectable || string.IsNullOrEmpty(context.BlockedReason))
{
return optionText;
}
return $"{optionText}\n<size=75%>{context.BlockedReason}</size>";
}
}
}