Files
2026-06-26 16:23:20 +02:00

88 lines
2.8 KiB
C#

using System;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using Ashwild.Player;
namespace Ashwild.Settings
{
/// <summary>
/// One rebindable-key row: an action label, the current key (shown as a glyph when an icon is
/// available, otherwise as text), a Rebind button and a Reset button. Pure UI — it holds the
/// config entry's action name + binding index and raises callbacks; it knows nothing about the
/// Input System. Mirrors SlotUI/RecipeButtonUI: the panel calls Initialize once, then RefreshDisplay.
/// </summary>
public class KeybindRowUI : MonoBehaviour
{
#region Serialized Fields
[Header("References")]
[SerializeField] private TMP_Text labelText;
[SerializeField] private TMP_Text keyText;
[SerializeField] private Image keyIcon;
[SerializeField] private Button rebindButton;
[SerializeField] private Button resetButton;
#endregion
#region State
private string actionName;
private int bindingIndex;
private Action<KeybindRowUI> onRebind;
private Action<KeybindRowUI> onReset;
public string ActionName => actionName;
public int BindingIndex => bindingIndex;
#endregion
#region Public API
/// <summary>
/// Binds this row to its config entry and the panel callbacks. Call once after Instantiate.
/// </summary>
public void Initialize(RebindableEntry entry, Action<KeybindRowUI> onRebind, Action<KeybindRowUI> onReset)
{
actionName = entry.ActionName;
bindingIndex = entry.BindingIndex;
this.onRebind = onRebind;
this.onReset = onReset;
if (labelText != null) labelText.text = entry.DisplayLabel;
if (rebindButton != null)
{
rebindButton.onClick.RemoveAllListeners();
rebindButton.onClick.AddListener(() => this.onRebind?.Invoke(this));
}
if (resetButton != null)
{
resetButton.onClick.RemoveAllListeners();
resetButton.onClick.AddListener(() => this.onReset?.Invoke(this));
}
}
/// <summary>
/// Shows the current key as a glyph when an icon is supplied, otherwise as text.
/// </summary>
public void RefreshDisplay(string keyDisplay, Sprite icon)
{
bool hasIcon = icon != null;
if (keyIcon != null)
{
keyIcon.enabled = hasIcon;
if (hasIcon) keyIcon.sprite = icon;
}
if (keyText != null)
{
keyText.gameObject.SetActive(!hasIcon);
keyText.text = keyDisplay;
}
}
#endregion
}
}