Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Has been cancelled
- Добавил новый тип отображения настроек с целочисленным значением - У этого типа можно настраивать шаг, выглядит приятнее
100 lines
3.0 KiB
C#
100 lines
3.0 KiB
C#
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;
|
||
}
|
||
}
|
||
|