Danro/Assets/Scripts/Settings/UI/ValueSelectorRow.cs
Danil Iarosh 009152ac5b
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Has been cancelled
Added ValueSelectorRow option to int settings
- Добавил новый тип отображения настроек с целочисленным значением
- У этого типа можно настраивать шаг, выглядит приятнее
2026-02-06 16:14:39 +03:00

100 lines
3.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace RR.Common.Settings.UI
{
/// <summary>
/// Компонент-Selectable для элемента настроек с выбором значения стрелками влево/вправо.
/// Работает на одном префабе вместе с SettingUIItem.
/// </summary>
public class ValueSelectorRow : Selectable, IMoveHandler
{
[Header("References")]
[SerializeField] private TMP_Text valueText;
private SettingEntry _entry;
private int _currentValue;
private int _step = 1;
public void Setup(SettingEntry entry, int currentValue)
{
_entry = entry;
_currentValue = Mathf.Clamp(currentValue, entry.minInt, entry.maxInt);
_step = Mathf.Max(1, entry.intStep);
UpdateUI();
}
public new void OnMove(AxisEventData eventData)
{
if (_entry == null || !IsInteractable())
{
// Пробрасываем событие к базовому классу для стандартной навигации
base.OnMove(eventData);
return;
}
switch (eventData.moveDir)
{
case MoveDirection.Left:
DecreaseValue();
eventData.Use();
break;
case MoveDirection.Right:
IncreaseValue();
eventData.Use();
break;
case MoveDirection.Up:
case MoveDirection.Down:
// Пробрасываем вертикальную навигацию к базовому классу
base.OnMove(eventData);
break;
}
}
private void DecreaseValue()
{
int newValue = Mathf.Max(_entry.minInt, _currentValue - _step);
if (newValue != _currentValue)
{
_currentValue = newValue;
UpdateUI();
SettingsManager.Instance?.SetInt(_entry.key, _currentValue);
}
}
private void IncreaseValue()
{
int newValue = Mathf.Min(_entry.maxInt, _currentValue + _step);
if (newValue != _currentValue)
{
_currentValue = newValue;
UpdateUI();
SettingsManager.Instance?.SetInt(_entry.key, _currentValue);
}
}
private void UpdateUI()
{
if (valueText != null)
valueText.text = _currentValue.ToString();
}
public void SetValue(int value)
{
if (_entry == null)
return;
_currentValue = Mathf.Clamp(value, _entry.minInt, _entry.maxInt);
UpdateUI();
}
public string Key => _entry?.key;
}
}