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

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
{
@@ -11,14 +12,48 @@ namespace XericUI.ReflectionUIGenerator
public class XUIAvrgMDInputFieldValue : XUIAvrgMDValue
{
[SerializeField]
[Tooltip("关联的 InputField 组件(未设置则自动查找)")]
[Tooltip("关联的 InputField 组件")]
#if dUI_TextMeshPro
private TMPro.TMP_InputField _inputField;
#else
private InputField _inputField;
#endif
[Header("InputField 专属插槽")]
[SerializeField, Tooltip("增加按钮(点击按步进值 +)")]
private Button _incrementButton;
[SerializeField, Tooltip("减少按钮(点击按步进值 -)")]
private Button _decrementButton;
// ============ 样式表参数 ============
private readonly StyleMember _styleStepValue = new() { StylePath = "xuiavrg/input/stepValue" };
// ============ 生命周期 ============
private void Awake()
{
#if dUI_TextMeshPro
if (_inputField == null) _inputField = GetComponent<TMPro.TMP_InputField>();
#else
if (_inputField == null) _inputField = GetComponent<InputField>();
#endif
}
protected override void OnEnable()
{
base.OnEnable();
_styleStepValue.Register();
}
protected override void OnDisable()
{
base.OnDisable();
_styleStepValue.Unregister();
}
// ============ 核心绑定 ============
public override void BindToAccessor(
IAvrgValueAccessor accessor,
AvrgMvvmBinding binding,
@@ -29,12 +64,25 @@ namespace XericUI.ReflectionUIGenerator
if (binding == null || accessor == null || _inputField == null) return;
// 设置输入类型
#if dUI_TextMeshPro
if (accessor.ValueType == typeof(float) || accessor.ValueType == typeof(int)
|| accessor.ValueType == typeof(double))
{
_inputField.contentType = TMPro.TMP_InputField.ContentType.DecimalNumber;
}
#else
if (accessor.ValueType == typeof(float) || accessor.ValueType == typeof(int)
|| accessor.ValueType == typeof(double))
{
_inputField.contentType = InputField.ContentType.DecimalNumber;
}
#endif
// 增减按钮
if (_incrementButton != null)
_incrementButton.onClick.AddListener(OnIncrementClicked);
if (_decrementButton != null)
_decrementButton.onClick.AddListener(OnDecrementClicked);
// UI → 数据
_inputField.onEndEdit.AddListener(val =>
@@ -53,6 +101,43 @@ namespace XericUI.ReflectionUIGenerator
// 首次同步
var initialValue = accessor.GetValue();
_inputField.SetTextWithoutNotify(accessor.FormatDisplay(initialValue));
RefreshCommonNodes();
}
// ============ 步进 ============
private float GetStepValue()
{
var val = GetStyleValue(_styleStepValue);
return val?.GetValueAsFloat() ?? 1f;
}
private void OnIncrementClicked()
{
if (CurrentAccessor?.IsReadOnly == true || _inputField == null) return;
if (!float.TryParse(_inputField.text, out float current)) return;
float step = GetStepValue();
float newVal = current + step;
_inputField.text = CurrentAccessor.FormatDisplay(
ConvertValueForAccessor(newVal, CurrentAccessor.ValueType));
}
private void OnDecrementClicked()
{
if (CurrentAccessor?.IsReadOnly == true || _inputField == null) return;
if (!float.TryParse(_inputField.text, out float current)) return;
float step = GetStepValue();
float newVal = current - step;
_inputField.text = CurrentAccessor.FormatDisplay(
ConvertValueForAccessor(newVal, CurrentAccessor.ValueType));
}
private static object ConvertValueForAccessor(float val, Type targetType)
{
if (targetType == typeof(int)) return (int)val;
if (targetType == typeof(double)) return (double)val;
return val;
}
private static object ConvertStringToValue(string s, Type targetType)