using System; using AibisDream; using AibisDream.Utility; using TMPro; using UnityEngine; using UnityEngine.Localization.Components; using UnityEngine.UI; namespace AibisDream.UI { [RequireComponent(typeof(BaseButton))] public class TerminalButton : MonoBehaviour { [SerializeField] private BaseButton button; [SerializeField] private Image buttonImage; [SerializeField] private Image backgroundImage; [SerializeField] private TMP_Text labelText; [SerializeField] private LocalizeStringEvent labelLocalize; [Header("文字")] [SerializeField] private string defaultLabel = string.Empty; [Header("前景色彩(文字 + 按钮图)")] [SerializeField] private Color normalForegroundColor = Color.white; [SerializeField] private Color hoverForegroundColor = new Color(0.02f, 0.02f, 0.04f); [Header("底图色彩")] [SerializeField] private Color normalBackgroundColor = new Color(1f, 1f, 1f, 0f); [SerializeField] private Color hoverBackgroundColor = Color.white; [Header("禁用")] [SerializeField] private Color disabledForegroundColor = new Color(0.78431374f, 0.78431374f, 0.78431374f, 0.5f); [SerializeField] private Color disabledBackgroundColor = new Color(1f, 1f, 1f, 0.3f); public BaseButton Button => button != null ? button : (button = GetComponent()); private SelectionType _prevSelectionState = SelectionType.Normal; private void Awake() { button = GetComponent(); if (buttonImage == null) { buttonImage = GetComponent(); } if (labelLocalize == null && labelText != null) { labelLocalize = labelText.GetComponent(); } button.onStateTransition.AddListener(OnStateTransition); button.onClick.AddListener(OnClick); if (!string.IsNullOrEmpty(defaultLabel)) { SetLabel(defaultLabel); } } private void OnEnable() { if (button != null) { OnStateTransition(button.CurSelectionState, true); } } private void OnDestroy() { if (button != null) { button.onStateTransition.RemoveListener(OnStateTransition); button.onClick.RemoveListener(OnClick); } } public void SetLabel(string value) { if (labelLocalize != null && IsLocalizationKey(value)) { labelLocalize.enabled = true; labelLocalize.SetTable(ConstRef.UITextTable); labelLocalize.SetEntry(value); return; } if (labelLocalize != null) { labelLocalize.enabled = false; } if (labelText != null) { labelText.text = value; } } private void OnStateTransition(SelectionType state, bool instant) { if (button.interactable && state == SelectionType.Highlighted && _prevSelectionState == SelectionType.Normal) { TerminalUIAudio.NotifyHover(); } _prevSelectionState = state; switch (state) { case SelectionType.Disabled: ApplyForegroundColor(disabledForegroundColor); ApplyBackgroundColor(disabledBackgroundColor); break; case SelectionType.Highlighted: case SelectionType.Pressed: case SelectionType.Selected: ApplyForegroundColor(hoverForegroundColor); ApplyBackgroundColor(hoverBackgroundColor); break; default: ApplyForegroundColor(normalForegroundColor); ApplyBackgroundColor(normalBackgroundColor); break; } } private void ApplyForegroundColor(Color color) { if (buttonImage != null) { buttonImage.color = color; } if (labelText != null) { labelText.color = color; } } private void ApplyBackgroundColor(Color color) { if (backgroundImage != null) { backgroundImage.color = color; } } private void OnClick() { if (button != null && button.interactable) { TerminalUIAudio.NotifyClick(); } } private static bool IsLocalizationKey(string value) { return !string.IsNullOrEmpty(value) && value.Contains("_", StringComparison.Ordinal); } } }