103 lines
2.8 KiB
C#
103 lines
2.8 KiB
C#
using System;
|
|
using System.Globalization;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace AibisDream.UI.Terminal
|
|
{
|
|
public class TerminalSliderView : MonoBehaviour
|
|
{
|
|
[SerializeField] private TMP_Text labelText;
|
|
[SerializeField] private TMP_Text valueText;
|
|
[SerializeField] private Slider slider;
|
|
[SerializeField] private RectTransform fillRect;
|
|
|
|
private string _propName;
|
|
private Action<string, string> _onValueChanged;
|
|
|
|
private void Awake()
|
|
{
|
|
if (slider != null)
|
|
{
|
|
slider.onValueChanged.AddListener(OnSliderChanged);
|
|
}
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (slider != null)
|
|
{
|
|
slider.onValueChanged.RemoveListener(OnSliderChanged);
|
|
}
|
|
}
|
|
|
|
public void Init(string propName, string label, float minValue, float maxValue, Action<string, string> onValueChanged)
|
|
{
|
|
_propName = propName;
|
|
_onValueChanged = onValueChanged;
|
|
|
|
if (labelText != null)
|
|
{
|
|
labelText.text = label;
|
|
}
|
|
|
|
if (slider != null)
|
|
{
|
|
slider.minValue = minValue;
|
|
slider.maxValue = maxValue;
|
|
}
|
|
}
|
|
|
|
public void SetValue(string value)
|
|
{
|
|
if (!float.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out var parsed))
|
|
{
|
|
parsed = slider != null ? slider.minValue : 0f;
|
|
}
|
|
|
|
if (slider != null)
|
|
{
|
|
slider.SetValueWithoutNotify(parsed);
|
|
}
|
|
|
|
RefreshValue(parsed);
|
|
}
|
|
|
|
private void OnSliderChanged(float value)
|
|
{
|
|
RefreshValue(value);
|
|
_onValueChanged?.Invoke(_propName, value.ToString("0.##", CultureInfo.InvariantCulture));
|
|
}
|
|
|
|
private void RefreshValue(float value)
|
|
{
|
|
if (valueText != null)
|
|
{
|
|
if (_propName == "Volume")
|
|
{
|
|
valueText.text = $"{Mathf.RoundToInt(value * 100)}/100";
|
|
}
|
|
else if (_propName == "TextSpeed")
|
|
{
|
|
valueText.text = value switch
|
|
{
|
|
< 1.5f => "<慢速>",
|
|
< 4.5f => "<正常>",
|
|
_ => "<快速>"
|
|
};
|
|
}
|
|
else
|
|
{
|
|
valueText.text = value.ToString("0.##", CultureInfo.InvariantCulture);
|
|
}
|
|
}
|
|
|
|
if (fillRect != null && slider != null)
|
|
{
|
|
fillRect.anchorMax = new Vector2(slider.normalizedValue, fillRect.anchorMax.y);
|
|
}
|
|
}
|
|
}
|
|
}
|