84 lines
1.9 KiB
C#
84 lines
1.9 KiB
C#
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class FloatView : MonoBehaviour, ISettingView
|
|
{
|
|
[Header("Layout ссылки")]
|
|
[SerializeField] private Image Bar;
|
|
[SerializeField] TextMeshProUGUI PercentLabel;
|
|
|
|
[Header("Анимация")]
|
|
[SerializeField] private Gradient gradient;
|
|
|
|
[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 Setting CachedSetting { get; private set; }
|
|
|
|
public void Initialize(Setting setting)
|
|
{
|
|
CachedSetting = setting;
|
|
CacheSettingValues(setting);
|
|
|
|
Source.clip = SwitchClip;
|
|
|
|
Display();
|
|
}
|
|
|
|
public void TakeInput(int direction)
|
|
{
|
|
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;
|
|
Bar.color = gradient.Evaluate(percent);
|
|
|
|
PercentLabel.text = $"{percent:P0}";
|
|
}
|
|
|
|
private void CacheSettingValues(Setting 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();
|
|
}
|
|
}
|