145 lines
4.3 KiB
C#
145 lines
4.3 KiB
C#
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<BaseButton>());
|
|
|
|
private void Awake()
|
|
{
|
|
button = GetComponent<BaseButton>();
|
|
if (buttonImage == null)
|
|
{
|
|
buttonImage = GetComponent<Image>();
|
|
}
|
|
|
|
if (labelLocalize == null && labelText != null)
|
|
{
|
|
labelLocalize = labelText.GetComponent<LocalizeStringEvent>();
|
|
}
|
|
|
|
button.onStateTransition.AddListener(OnStateTransition);
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
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)
|
|
{
|
|
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 static bool IsLocalizationKey(string value)
|
|
{
|
|
return !string.IsNullOrEmpty(value) && value.Contains("_", StringComparison.Ordinal);
|
|
}
|
|
}
|
|
}
|