分离具体属性组件,加入样式表系统

This commit is contained in:
2026-07-25 22:47:14 +08:00
parent b1636d5932
commit 3fbb82f40e
38 changed files with 742 additions and 38 deletions
@@ -1,6 +1,7 @@
using System;
using UnityEngine;
using UnityEngine.UI;
using XericLibrary.Runtime.SuperStyleSheet;
namespace XericUI.ReflectionUIGenerator
{
@@ -15,15 +16,49 @@ namespace XericUI.ReflectionUIGenerator
[Tooltip("关联的 Slider 组件(未设置则自动查找)")]
private Slider _slider;
[Header("Slider 专属插槽")]
[SerializeField, Tooltip("增加按钮(点击按步进值 +)")]
private Button _incrementButton;
[SerializeField, Tooltip("减少按钮(点击按步进值 -)")]
private Button _decrementButton;
#if dUI_TextMeshPro
[SerializeField]
[Tooltip("值显示标签(可选,未设置则不更新)")]
private TMPro.TMP_Text _valueLabel;
#endif
// ============ 样式表参数 ============
private readonly StyleMember _styleDefaultStep = new() { StylePath = "xuiavrg/slider/defaultStep" };
private readonly StyleMember _styleMinStep = new() { StylePath = "xuiavrg/slider/minStep" };
private readonly StyleMember _styleMaxStep = new() { StylePath = "xuiavrg/slider/maxStep" };
// ============ 生命周期 ============
private void Awake()
{
if (_slider == null) _slider = GetComponent<Slider>();
}
protected override void OnEnable()
{
base.OnEnable();
_styleDefaultStep.Register();
_styleMinStep.Register();
_styleMaxStep.Register();
}
protected override void OnDisable()
{
base.OnDisable();
_styleDefaultStep.Unregister();
_styleMinStep.Unregister();
_styleMaxStep.Unregister();
}
// ============ 核心绑定 ============
public override void BindToAccessor(
IAvrgValueAccessor accessor,
AvrgMvvmBinding binding,
@@ -45,6 +80,12 @@ namespace XericUI.ReflectionUIGenerator
_slider.maxValue = BoundMetaData.RangeMax.Value;
}
// 增减按钮
if (_incrementButton != null)
_incrementButton.onClick.AddListener(OnIncrementClicked);
if (_decrementButton != null)
_decrementButton.onClick.AddListener(OnDecrementClicked);
// UI → 数据
_slider.onValueChanged.AddListener(val =>
{
@@ -52,8 +93,10 @@ namespace XericUI.ReflectionUIGenerator
binding.WriteFromUI(convertedVal);
scheduler?.MarkUIDirty(binding);
#if dUI_TextMeshPro
if (_valueLabel != null)
_valueLabel.text = accessor.FormatDisplay(convertedVal);
#endif
});
// 数据 → UI
@@ -62,8 +105,10 @@ namespace XericUI.ReflectionUIGenerator
float sliderVal = Convert.ToSingle(value);
_slider.SetValueWithoutNotify(sliderVal);
#if dUI_TextMeshPro
if (_valueLabel != null)
_valueLabel.text = accessor.FormatDisplay(value);
#endif
};
// 首次同步
@@ -71,9 +116,46 @@ namespace XericUI.ReflectionUIGenerator
if (initialValue != null)
{
_slider.SetValueWithoutNotify(Convert.ToSingle(initialValue));
#if dUI_TextMeshPro
if (_valueLabel != null)
_valueLabel.text = accessor.FormatDisplay(initialValue);
#endif
}
RefreshCommonNodes();
}
// ============ 步进值 ============
private float GetStepValue()
{
var val = GetStyleValue(_styleDefaultStep);
if (val != null)
{
float step = val.GetValueAsFloat();
float min = GetStyleValue(_styleMinStep)?.GetValueAsFloat() ?? 0.001f;
float max = GetStyleValue(_styleMaxStep)?.GetValueAsFloat() ?? 1000f;
return Mathf.Clamp(step, min, max);
}
return _slider.wholeNumbers ? 1f : 0.1f;
}
private void OnIncrementClicked()
{
if (CurrentAccessor?.IsReadOnly == true || _slider == null) return;
float step = GetStepValue();
float current = Convert.ToSingle(CurrentAccessor.GetValue());
float newVal = Mathf.Clamp(current + step, _slider.minValue, _slider.maxValue);
_slider.value = newVal;
}
private void OnDecrementClicked()
{
if (CurrentAccessor?.IsReadOnly == true || _slider == null) return;
float step = GetStepValue();
float current = Convert.ToSingle(CurrentAccessor.GetValue());
float newVal = Mathf.Clamp(current - step, _slider.minValue, _slider.maxValue);
_slider.value = newVal;
}
private static object ConvertValueForAccessor(float val, Type targetType)