using System.Collections.Generic; using RR.Common.Settings; using TMPro; using UnityEngine; using UnityEngine.UI; namespace RR.Common.UI { /// /// Биндер для выбора из списка опций (как enum или список строк). /// Использует Dropdown или кнопки влево/вправо для переключения. /// public class SettingsOptionPickerBinder : MonoBehaviour { [SerializeField] private string key; [SerializeField] private TMP_Dropdown dropdown; [SerializeField] private TMP_Text label; // Альтернативный вариант: кнопки влево/вправо + текст текущего значения [Header("Alternative: Buttons")] [SerializeField] private Button prevButton; [SerializeField] private Button nextButton; [SerializeField] private TMP_Text valueText; private SettingEntry def; private bool useDropdown = true; private void Start() { if (!SettingsManager.Instance) { Debug.LogError("[SettingsOptionPickerBinder] SettingsManager.Instance not found!"); gameObject.SetActive(false); return; } def = SettingsManager.Instance.Define(key); if (def == null) { Debug.LogWarning($"[SettingsOptionPickerBinder] Setting '{key}' not found in database."); gameObject.SetActive(false); return; } if (def.valueType != SettingValueType.OptionPicker) { Debug.LogWarning($"[SettingsOptionPickerBinder] Setting '{key}' is not an OptionPicker type!"); gameObject.SetActive(false); return; } if (def.options == null || def.options.Count == 0) { Debug.LogWarning($"[SettingsOptionPickerBinder] Setting '{key}' has no options!"); gameObject.SetActive(false); return; } // Определяем режим работы useDropdown = dropdown != null; if (label) label.text = def.displayName ?? key; if (useDropdown) InitDropdown(); else InitButtons(); } private void InitDropdown() { if (!dropdown) return; // Заполняем dropdown опциями dropdown.ClearOptions(); dropdown.AddOptions(def.options); // Устанавливаем текущее значение int currentIndex = SettingsManager.Instance.Get(key).i; dropdown.SetValueWithoutNotify(currentIndex); // Подписываемся на изменения dropdown.onValueChanged.AddListener(OnDropdownChanged); } private void InitButtons() { if (!prevButton || !nextButton) return; prevButton.onClick.AddListener(OnPrevClicked); nextButton.onClick.AddListener(OnNextClicked); UpdateButtonsDisplay(); } private void OnDropdownChanged(int newIndex) { SettingsManager.Instance.Set(key, SettingValue.Int(newIndex)); } private void OnPrevClicked() { int current = SettingsManager.Instance.Get(key).i; int newIndex = current - 1; if (newIndex < 0) newIndex = def.options.Count - 1; // Wrap around SettingsManager.Instance.Set(key, SettingValue.Int(newIndex)); UpdateButtonsDisplay(); } private void OnNextClicked() { int current = SettingsManager.Instance.Get(key).i; int newIndex = (current + 1) % def.options.Count; // Wrap around SettingsManager.Instance.Set(key, SettingValue.Int(newIndex)); UpdateButtonsDisplay(); } private void UpdateButtonsDisplay() { if (!valueText) return; int currentIndex = SettingsManager.Instance.Get(key).i; if (currentIndex >= 0 && currentIndex < def.options.Count) valueText.text = def.options[currentIndex]; } } }