70 lines
1.9 KiB
C#
70 lines
1.9 KiB
C#
using TMPro;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
|
||
public class StringView : BaseSettingView
|
||
{
|
||
[Header("Настройки Layout'а")]
|
||
[SerializeField] private TextMeshProUGUI ValueLabel;
|
||
[SerializeField] private Image LeftPointer;
|
||
[SerializeField] private Image RightPointer;
|
||
|
||
[SerializeField] private Color NormalColor;
|
||
[SerializeField] private Color InactiveColor;
|
||
|
||
[Header("Звуки")]
|
||
[SerializeField] private AudioClip SwitchClip;
|
||
|
||
public SettingDTO CachedSetting { get; private set; }
|
||
private int MaxIndex => CachedSetting.AllowedValues.Count - 1;
|
||
private int currentIndex = 0;
|
||
private bool pressed = false;
|
||
|
||
public override void Initialize(SettingDTO setting)
|
||
{
|
||
base.Initialize(setting);
|
||
CachedSetting = setting;
|
||
currentIndex = CalculateCurrentIndex();
|
||
UpdateUi();
|
||
}
|
||
|
||
public override void TakeInput(int direction, bool _)
|
||
{
|
||
if (direction == 0)
|
||
{
|
||
pressed = false;
|
||
return;
|
||
}
|
||
|
||
if (pressed) return;
|
||
|
||
pressed = true;
|
||
int newIndex = Mathf.Clamp(currentIndex + direction, 0, MaxIndex);
|
||
if (newIndex == currentIndex) return;
|
||
|
||
|
||
currentIndex = newIndex;
|
||
|
||
CachedSetting.StringValue = CachedSetting.AllowedValues[currentIndex];
|
||
SoundManager.instance.playClip(SwitchClip);
|
||
UpdateUi();
|
||
}
|
||
|
||
private int CalculateCurrentIndex()
|
||
{
|
||
int index = CachedSetting.AllowedValues.IndexOf(CachedSetting.StringValue);
|
||
return index < 0 ? 0 : index;
|
||
}
|
||
|
||
private void UpdatePointers()
|
||
{
|
||
LeftPointer.color = currentIndex > 0 ? NormalColor : InactiveColor;
|
||
RightPointer.color = currentIndex < MaxIndex ? NormalColor : InactiveColor;
|
||
}
|
||
|
||
private void UpdateUi()
|
||
{
|
||
ValueLabel.text = CachedSetting.StringValue;
|
||
UpdatePointers();
|
||
}
|
||
} |