Files
Emberwild/Assets/GAME/Script/Settings/Widgets/RebindPromptUI.cs
T
2026-06-26 16:23:20 +02:00

91 lines
3.1 KiB
C#

using System;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace Ashwild.Settings
{
/// <summary>
/// Standalone modal overlay for rebinding, living as its own object under the SettingsPanel (a
/// sibling of the category sub-panels) so it renders in front of everything. One message label
/// plus two buttons: while capturing it reads "press a key" with the buttons hidden; on a clash
/// it shows "X already uses this key" with Replace / Cancel. Pure UI driven by the keybindings
/// sub-panel, which holds a reference and calls these methods. Buttons are wired per-conflict.
/// </summary>
public class RebindPromptUI : MonoBehaviour
{
#region Serialized Fields
[Header("Root")]
[Tooltip("The overlay object toggled on/off. Leave empty to use this GameObject.")]
[SerializeField] private GameObject root;
[Header("References")]
[SerializeField] private TMP_Text messageText;
[SerializeField] private Button replaceButton;
[SerializeField] private Button cancelButton;
[Header("Text")]
[SerializeField] private string listeningText = "Appuyez sur une touche…";
[Tooltip("{0} = the action that already uses the key.")]
[SerializeField] private string conflictFormat = "« {0} » utilise déjà cette touche.";
#endregion
#region Public API
/// <summary>
/// Shows the overlay in "press a key" mode (buttons hidden).
/// </summary>
public void ShowListening()
{
Target.SetActive(true);
if (messageText != null) messageText.text = listeningText;
SetButtonsActive(false);
}
/// <summary>
/// Switches the open overlay to the conflict prompt, showing and wiring the two buttons.
/// </summary>
public void ShowConflict(string conflictLabel, Action onReplace, Action onCancel)
{
Target.SetActive(true);
if (messageText != null) messageText.text = string.Format(conflictFormat, conflictLabel);
SetButtonsActive(true);
if (replaceButton != null)
{
replaceButton.onClick.RemoveAllListeners();
replaceButton.onClick.AddListener(() => onReplace?.Invoke());
}
if (cancelButton != null)
{
cancelButton.onClick.RemoveAllListeners();
cancelButton.onClick.AddListener(() => onCancel?.Invoke());
}
}
/// <summary>
/// Hides the overlay.
/// </summary>
public void Hide() => Target.SetActive(false);
#endregion
#region Internal Helpers
/// <summary>
/// Shows/hides the Replace and Cancel buttons (only relevant in the conflict state).
/// </summary>
private void SetButtonsActive(bool active)
{
if (replaceButton != null) replaceButton.gameObject.SetActive(active);
if (cancelButton != null) cancelButton.gameObject.SetActive(active);
}
private GameObject Target => root != null ? root : gameObject;
#endregion
}
}