Files
aibis-dream/Assets/Scripts/UI/Form/SliderItem.cs
T

161 lines
4.7 KiB
C#

using System;
using System.Globalization;
using AibisDream.Utility;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Localization.Components;
using UnityEngine.UI;
namespace AibisDream
{
public class SliderItem : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
[SerializeField] private Slider slider;
[SerializeField] private TMP_Text labelText;
[SerializeField] private LocalizeStringEvent labelLocalize;
[SerializeField] private TMP_Text sliderText;
[SerializeField] private Image selectionBar;
[SerializeField] private float selectionBarHiddenAlpha;
[SerializeField] private float selectionBarVisibleAlpha = 1f;
[SerializeField] private Color normalTextColor = Color.white;
[SerializeField] private Color hoveredTextColor = new Color(0.02f, 0.02f, 0.04f);
private string _propName;
private Action<string, string> _onValueChanged;
private bool _isListening;
private void Awake()
{
SetHovered(false);
if (slider != null && !_isListening)
{
slider.onValueChanged.AddListener(OnSliderChanged);
_isListening = true;
}
}
public void OnPointerEnter(PointerEventData eventData)
{
SetHovered(true);
}
public void OnPointerExit(PointerEventData eventData)
{
SetHovered(false);
}
private void SetHovered(bool hovered)
{
SetSelectionBarAlpha(hovered ? selectionBarVisibleAlpha : selectionBarHiddenAlpha);
var color = hovered ? hoveredTextColor : normalTextColor;
SetTextColor(labelText, color);
SetTextColor(sliderText, color);
}
private void SetSelectionBarAlpha(float alpha)
{
if (selectionBar == null)
{
return;
}
var color = selectionBar.color;
color.a = alpha;
selectionBar.color = color;
}
private static void SetTextColor(TMP_Text text, Color color)
{
if (text != null)
{
text.color = color;
}
}
private void OnDestroy()
{
if (slider != null && _isListening)
{
slider.onValueChanged.RemoveListener(OnSliderChanged);
_isListening = false;
}
}
public void Init(string propName, string label, float minValue, float maxValue, Action<string, string> onValueChanged)
{
_propName = propName;
_onValueChanged = onValueChanged;
SetDisplayText(labelText, labelLocalize, label);
if (slider == null)
{
Debug.LogError($"[SliderItem] Slider not assigned on {name}.", this);
return;
}
slider.minValue = minValue;
slider.maxValue = maxValue;
}
public void SetValue(string value)
{
if (!float.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out var floatValue))
{
Debug.LogError($"SliderItem: 解析数值{value}失败");
return;
}
if (slider != null)
{
slider.SetValueWithoutNotify(floatValue);
}
RefreshValue(floatValue);
}
private void OnSliderChanged(float value)
{
RefreshValue(value);
_onValueChanged?.Invoke(_propName, value.ToString("0.##", CultureInfo.InvariantCulture));
}
private void RefreshValue(float value)
{
if (sliderText != null)
{
var curVolume = Mathf.RoundToInt(value * 100).ToString(CultureInfo.InvariantCulture);
sliderText.text = $"{curVolume}/100";
}
}
private static void SetDisplayText(TMP_Text text, LocalizeStringEvent localize, string value)
{
if (localize != null && IsLocalizationKey(value))
{
localize.enabled = true;
localize.SetTable(ConstRef.UITextTable);
localize.SetEntry(value);
return;
}
if (localize != null)
{
localize.enabled = false;
}
if (text != null)
{
text.text = value;
}
}
private static bool IsLocalizationKey(string value)
{
return !string.IsNullOrEmpty(value) && value.StartsWith("setting_", StringComparison.Ordinal);
}
}
}