103 lines
3.2 KiB
C#
103 lines
3.2 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using Ichni.RhythmGame;
|
||
using TMPro;
|
||
using UnityEngine;
|
||
using UnityEngine.Events;
|
||
|
||
namespace Ichni.Editor
|
||
{
|
||
public class DynamicUIInputField : DynamicUIElement, IHaveAutoUpdate
|
||
{
|
||
public TMP_InputField inputField;
|
||
public bool isAutoUpdate { get; set; }
|
||
public bool isReceiving { get; set; }
|
||
public override void Initialize(IBaseElement baseElement, string title, string parameterName)
|
||
{
|
||
base.Initialize(baseElement, title, parameterName);
|
||
|
||
if (parameterName != string.Empty)
|
||
{
|
||
ApplyContent();
|
||
inputField.onEndEdit.AddListener(ApplyParameters);
|
||
}
|
||
}
|
||
|
||
private void Update()
|
||
{
|
||
(this as IHaveAutoUpdate).UpdateContent();
|
||
}
|
||
|
||
public void SetDefaultValue(string text)
|
||
{
|
||
inputField.text = text;
|
||
}
|
||
|
||
public T GetValue<T>()
|
||
{
|
||
return (T)Convert.ChangeType(inputField.text, typeof(T));
|
||
}
|
||
|
||
public void SetAutoUpdate(bool enable)
|
||
{
|
||
isAutoUpdate = enable;
|
||
isReceiving = true;
|
||
|
||
inputField.onSelect.AddListener(_ => isReceiving = false);
|
||
inputField.onDeselect.AddListener(_ => isReceiving = true);
|
||
}
|
||
|
||
public void ApplyContent()
|
||
{
|
||
object value = ReflectionHelper.GetDeepValue(connectedBaseElement, parameterName);
|
||
inputField.text = value != null ? value.ToString() : string.Empty;
|
||
}
|
||
|
||
private void ApplyParameters(string text)
|
||
{
|
||
Type type = null;
|
||
// 优先获取字段/属性类型
|
||
var baseType = connectedBaseElement.GetType();
|
||
var field = baseType.GetField(parameterName);
|
||
if (field != null)
|
||
type = field.FieldType;
|
||
else
|
||
{
|
||
var prop = baseType.GetProperty(parameterName);
|
||
if (prop != null)
|
||
type = prop.PropertyType;
|
||
}
|
||
// 如果都没有,fallback 到当前值类型
|
||
if (type == null)
|
||
{
|
||
var val = ReflectionHelper.GetDeepValue(connectedBaseElement, parameterName);
|
||
type = val != null ? val.GetType() : typeof(string);
|
||
}
|
||
object value = null;
|
||
if (type == typeof(int))
|
||
{
|
||
value = int.Parse(text);
|
||
}
|
||
else if (type == typeof(float))
|
||
{
|
||
value = float.Parse(text);
|
||
}
|
||
else if (type == typeof(string))
|
||
{
|
||
value = text;
|
||
}
|
||
if (value != null)
|
||
{
|
||
ReflectionHelper.SetDeepValue(connectedBaseElement, parameterName, value);
|
||
}
|
||
connectedBaseElement.Refresh();
|
||
}
|
||
|
||
public override DynamicUIElement AddListenerFunction(UnityAction action)
|
||
{
|
||
inputField.onEndEdit.AddListener(_ => action());
|
||
return this;
|
||
}
|
||
}
|
||
} |