Danro/Assets/Scripts/Settings/UI/SettingsOptionPickerBinder.cs
Danil Iarosh 4d05d1adbb
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Has been cancelled
FEAT: Added new settings system
- Добавил новую систему настроек, возможности:
    - Динамически создает список настроек
    - Сохраняет настройки в JSON-файл
    - Возможность группировать настройки
    - Широкие возможности по кастомизации
    - Применение настроек в реальном времени (частично)
- Добавил префабы для новых элементов настроек
- Изменил поведение игры исходя из нового формата настроек
2026-02-04 14:32:15 +03:00

131 lines
4.3 KiB
C#

using System.Collections.Generic;
using RR.Common.Settings;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace RR.Common.UI
{
/// <summary>
/// Биндер для выбора из списка опций (как enum или список строк).
/// Использует Dropdown или кнопки влево/вправо для переключения.
/// </summary>
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];
}
}
}