85 lines
1.9 KiB
C#

using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class FloatView : BaseSettingView
{
[Header("Layout ссылки")]
[SerializeField] private Image Bar;
[SerializeField] List<TextMeshProUGUI> PercentLabels;
[Header("Звук")]
[SerializeField] private bool UseSound;
[SerializeField] private AudioClip SwitchClip;
[SerializeField] private AudioSource Source;
//Кэш значений настройки
private float minValue;
private float maxValue;
private float value;
private float delta;
private float step;
public SettingDTO CachedSetting { get; private set; }
public override void Initialize(SettingDTO setting)
{
base.Initialize(setting);
CachedSetting = setting;
CacheSettingValues(setting);
Source.clip = SwitchClip;
Display();
}
public override void TakeInput(int direction, bool _)
{
if (direction == 0)
{
Source.Stop();
return;
}
value += step * Mathf.Sign(direction);
value = Mathf.Clamp(value, minValue, maxValue);
CachedSetting.FloatValue = value;
if (UseSound)
PlaySound(value);
Display();
}
private void Display()
{
float percent = (value - minValue) / delta;
Bar.fillAmount = percent;
foreach (var label in PercentLabels)
{
label.text = $"{percent * 100:0}";
}
}
private void CacheSettingValues(SettingDTO setting)
{
minValue = setting.FloatMin;
maxValue = setting.FloatMax;
step = setting.FloatStep;
value = setting.FloatValue;
delta = maxValue - minValue;
}
private void PlaySound(float val)
{
Source.pitch = val * 1.5f + 0.5f;
Source.volume = val;
if (!Source.isPlaying)
Source.Play();
}
}