using System;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
namespace Ashwild.Settings
{
///
/// A reusable dropdown. Pure UI: it knows nothing about which setting it drives. The panel
/// calls once to register the selection action, supplies the option list via
/// (resolutions, monitors… are all built by the panel) and selects the
/// current entry through .
///
[DisallowMultipleComponent]
public class SettingDropdownWidget : MonoBehaviour
{
#region Serialized Fields
[Header("References")]
[SerializeField] private TMP_Dropdown dropdown;
#endregion
#region State
private Action onChanged;
#endregion
#region Public API
///
/// Registers the action to run when the user picks an option. Call once.
///
public void Init(Action onChanged)
{
if (dropdown == null)
{
Debug.LogError($"[SettingDropdownWidget] '{name}' has no Dropdown assigned.", this);
return;
}
this.onChanged = onChanged;
dropdown.onValueChanged.RemoveListener(HandleChanged);
dropdown.onValueChanged.AddListener(HandleChanged);
}
///
/// Replaces the dropdown's options. Call before on refresh.
///
public void SetOptions(IEnumerable options)
{
if (dropdown == null) return;
dropdown.ClearOptions();
dropdown.AddOptions(new List(options));
}
///
/// Selects the given index (clamped to the current options) without invoking the action.
///
public void SetValue(int index)
{
if (dropdown == null) return;
dropdown.SetValueWithoutNotify(Mathf.Clamp(index, 0, dropdown.options.Count - 1));
dropdown.RefreshShownValue();
}
#endregion
#region Event Handlers
///
/// Runs the apply action with the chosen index.
///
private void HandleChanged(int index) => onChanged?.Invoke(index);
#endregion
}
}