86 lines
2.1 KiB
C#
86 lines
2.1 KiB
C#
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.UI;
|
|
|
|
public class FloatView : MonoBehaviour, IView
|
|
{
|
|
[SerializeField] private Graphic Bar;
|
|
[SerializeField] private RectTransform MaskRect;
|
|
|
|
[SerializeField] private Gradient gradient;
|
|
|
|
[SerializeField] private float minMaskWidth = 0;
|
|
[SerializeField] private float maxMaskWidth = 150;
|
|
|
|
[SerializeField] private float minValue = 0;
|
|
[SerializeField] private float maxValue = 1;
|
|
|
|
|
|
[SerializeField] private AudioSource source;
|
|
|
|
[SerializeField] TextMeshProUGUI label;
|
|
|
|
bool useSound;
|
|
public UnityEvent OnChangeReady { get; }
|
|
|
|
float lastSoundStart;
|
|
|
|
public void Initialize(string settingId)
|
|
{
|
|
lastSoundStart = Time.time;
|
|
Setting setting = SettingsManager.Container.TryGetSetting(settingId);
|
|
if (setting.Type != SettingType.Float)
|
|
{
|
|
Debug.LogError("Настройка не того типа (float)");
|
|
return;
|
|
}
|
|
|
|
string value = setting.FloatValue.ToString();
|
|
useSound = false;
|
|
ChangeValue(settingId);
|
|
useSound = true;
|
|
}
|
|
|
|
public void ChangeValue(string settingId)
|
|
{
|
|
Setting setting = SettingsManager.Container.TryGetSetting(settingId);
|
|
if (setting.Type != SettingType.Float)
|
|
{
|
|
Debug.LogError("Настройка не того типа (float)");
|
|
return;
|
|
}
|
|
float val = setting.FloatValue;
|
|
label.text = (int)(val * 100) + "%";
|
|
|
|
if (useSound)
|
|
{
|
|
needSound(val);
|
|
}
|
|
|
|
|
|
float width = minMaskWidth + (maxMaskWidth - minValue) * (maxValue-minValue) * val;
|
|
MaskRect.sizeDelta = new Vector2(width, MaskRect.sizeDelta.y);
|
|
|
|
Bar.color = gradient.Evaluate(val);
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (Time.time - lastSoundStart > 0.0001f)
|
|
{
|
|
source.Stop();
|
|
}
|
|
}
|
|
void needSound(float val)
|
|
{
|
|
lastSoundStart = Time.time;
|
|
source.pitch = val * 1.5f + 0.5f;
|
|
source.volume = val;
|
|
if (!source.isPlaying)
|
|
source.Play();
|
|
}
|
|
|
|
|
|
}
|