69 lines
1.8 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.UI;
public class StringView : MonoBehaviour, ISettingView
{
[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 void Initialize(SettingDTO setting)
{
CachedSetting = setting;
currentIndex = CalculateCurrentIndex();
UpdateUi();
}
public void TakeInput(int direction)
{
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();
}
}