Passion & UI
This commit is contained in:
77
Assets/Scripts/Settings/UI/SettingsEntryButton.cs
Normal file
77
Assets/Scripts/Settings/UI/SettingsEntryButton.cs
Normal file
@@ -0,0 +1,77 @@
|
||||
using System;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Cielonos.Settings.UI
|
||||
{
|
||||
/// <summary>
|
||||
/// 按钮型设置条目。不绑定到数据字段,而是执行指定的点击动作。
|
||||
/// <para>
|
||||
/// 典型用途:打开二级 UIPage(如 Key Binding 页面)、触发重置操作等。
|
||||
/// 支持鼠标悬停说明面板。
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public class SettingsEntryButton : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
|
||||
{
|
||||
[SerializeField] private TMP_Text labelText;
|
||||
[SerializeField] private Button button;
|
||||
|
||||
private Action onClickAction;
|
||||
|
||||
/// <summary>条目的显示标题。</summary>
|
||||
public string DisplayLabel { get; private set; }
|
||||
|
||||
/// <summary>条目的说明文本(可为空)。</summary>
|
||||
public string Description { get; private set; }
|
||||
|
||||
/// <summary>鼠标悬停进入时触发。由 SettingsUIPage 设置。</summary>
|
||||
public Action<string, string> OnHoverEnter { get; set; }
|
||||
|
||||
/// <summary>鼠标悬停离开时触发。由 SettingsUIPage 设置。</summary>
|
||||
public Action OnHoverExit { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 初始化按钮条目。
|
||||
/// </summary>
|
||||
/// <param name="label">显示标题。</param>
|
||||
/// <param name="description">悬停说明文本(可为空)。</param>
|
||||
/// <param name="onClick">点击按钮时执行的动作。</param>
|
||||
public void Initialize(string label, string description, Action onClick)
|
||||
{
|
||||
DisplayLabel = label;
|
||||
Description = description;
|
||||
onClickAction = onClick;
|
||||
|
||||
if (labelText != null)
|
||||
labelText.text = label;
|
||||
|
||||
if (button != null)
|
||||
button.onClick.AddListener(OnButtonClicked);
|
||||
}
|
||||
|
||||
private void OnButtonClicked()
|
||||
{
|
||||
onClickAction?.Invoke();
|
||||
}
|
||||
|
||||
// ──────────────────── 悬停事件 ────────────────────
|
||||
|
||||
public void OnPointerEnter(PointerEventData eventData)
|
||||
{
|
||||
OnHoverEnter?.Invoke(DisplayLabel, Description);
|
||||
}
|
||||
|
||||
public void OnPointerExit(PointerEventData eventData)
|
||||
{
|
||||
OnHoverExit?.Invoke();
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
if (button != null)
|
||||
button.onClick.RemoveListener(OnButtonClicked);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user