2025-02-16 11:15:42 -05:00
|
|
|
|
using System;
|
2025-02-11 22:58:56 -05:00
|
|
|
|
using System.Collections;
|
|
|
|
|
|
using System.Collections.Generic;
|
2025-02-12 18:46:46 -05:00
|
|
|
|
using Ichni.RhythmGame;
|
2025-02-11 22:58:56 -05:00
|
|
|
|
using TMPro;
|
|
|
|
|
|
using UnityEngine;
|
2025-02-18 10:30:11 -05:00
|
|
|
|
using UnityEngine.Events;
|
2025-02-11 22:58:56 -05:00
|
|
|
|
|
|
|
|
|
|
namespace Ichni.Editor
|
|
|
|
|
|
{
|
2025-02-26 00:52:08 -05:00
|
|
|
|
public class DynamicUIInputField : DynamicUIElement, IHaveAutoUpdate
|
2025-02-11 22:58:56 -05:00
|
|
|
|
{
|
|
|
|
|
|
public TMP_InputField inputField;
|
2025-02-26 00:52:08 -05:00
|
|
|
|
public bool isAutoUpdate { get; set; }
|
|
|
|
|
|
public bool isReceiving { get; set; }
|
2026-03-22 12:05:32 -04:00
|
|
|
|
private UnityAction<string> customAction;
|
|
|
|
|
|
|
2025-02-12 18:46:46 -05:00
|
|
|
|
public override void Initialize(IBaseElement baseElement, string title, string parameterName)
|
2025-02-11 22:58:56 -05:00
|
|
|
|
{
|
2026-03-22 12:05:32 -04:00
|
|
|
|
// [对象池安全]
|
|
|
|
|
|
inputField.onEndEdit.RemoveListener(OnEndEditNode);
|
|
|
|
|
|
inputField.onSelect.RemoveListener(OnSelectNode);
|
|
|
|
|
|
inputField.onDeselect.RemoveListener(OnDeselectNode);
|
|
|
|
|
|
customAction = null;
|
|
|
|
|
|
|
2025-02-12 18:46:46 -05:00
|
|
|
|
base.Initialize(baseElement, title, parameterName);
|
2026-01-07 23:48:28 +08:00
|
|
|
|
|
2025-02-26 00:52:08 -05:00
|
|
|
|
if (parameterName != string.Empty)
|
|
|
|
|
|
{
|
|
|
|
|
|
ApplyContent();
|
2026-03-22 12:05:32 -04:00
|
|
|
|
inputField.onEndEdit.AddListener(OnEndEditNode);
|
2025-02-26 00:52:08 -05:00
|
|
|
|
}
|
2025-02-11 22:58:56 -05:00
|
|
|
|
}
|
2026-03-22 12:05:32 -04:00
|
|
|
|
|
|
|
|
|
|
private void OnEndEditNode(string str)
|
|
|
|
|
|
{
|
|
|
|
|
|
ApplyParameters(str);
|
|
|
|
|
|
customAction?.Invoke(str);
|
|
|
|
|
|
}
|
|
|
|
|
|
private void OnSelectNode(string str) { isReceiving = false; }
|
|
|
|
|
|
private void OnDeselectNode(string str) { isReceiving = true; }
|
2026-01-07 23:48:28 +08:00
|
|
|
|
|
2025-02-26 00:52:08 -05:00
|
|
|
|
private void Update()
|
|
|
|
|
|
{
|
|
|
|
|
|
(this as IHaveAutoUpdate).UpdateContent();
|
|
|
|
|
|
}
|
2026-01-07 23:48:28 +08:00
|
|
|
|
|
2025-02-26 00:52:08 -05:00
|
|
|
|
public void SetDefaultValue(string text)
|
|
|
|
|
|
{
|
|
|
|
|
|
inputField.text = text;
|
|
|
|
|
|
}
|
2026-01-07 23:48:28 +08:00
|
|
|
|
|
2025-02-26 00:52:08 -05:00
|
|
|
|
public T GetValue<T>()
|
|
|
|
|
|
{
|
|
|
|
|
|
return (T)Convert.ChangeType(inputField.text, typeof(T));
|
|
|
|
|
|
}
|
2026-01-07 23:48:28 +08:00
|
|
|
|
|
2025-02-26 00:52:08 -05:00
|
|
|
|
public void SetAutoUpdate(bool enable)
|
|
|
|
|
|
{
|
|
|
|
|
|
isAutoUpdate = enable;
|
|
|
|
|
|
isReceiving = true;
|
2026-01-07 23:48:28 +08:00
|
|
|
|
|
2026-03-22 12:05:32 -04:00
|
|
|
|
inputField.onSelect.RemoveListener(OnSelectNode);
|
|
|
|
|
|
inputField.onSelect.AddListener(OnSelectNode);
|
|
|
|
|
|
|
|
|
|
|
|
inputField.onDeselect.RemoveListener(OnDeselectNode);
|
|
|
|
|
|
inputField.onDeselect.AddListener(OnDeselectNode);
|
2025-02-26 00:52:08 -05:00
|
|
|
|
}
|
2026-01-07 23:48:28 +08:00
|
|
|
|
|
2025-02-26 00:52:08 -05:00
|
|
|
|
public void ApplyContent()
|
|
|
|
|
|
{
|
2026-01-07 23:48:28 +08:00
|
|
|
|
object value = ReflectionHelper.GetDeepValue(connectedBaseElement, parameterName);
|
|
|
|
|
|
inputField.text = value != null ? value.ToString() : string.Empty;
|
2025-02-26 00:52:08 -05:00
|
|
|
|
}
|
2026-01-07 23:48:28 +08:00
|
|
|
|
|
2025-02-11 22:58:56 -05:00
|
|
|
|
private void ApplyParameters(string text)
|
|
|
|
|
|
{
|
2026-01-07 23:48:28 +08:00
|
|
|
|
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;
|
2025-02-16 11:15:42 -05:00
|
|
|
|
if (type == typeof(int))
|
|
|
|
|
|
{
|
2026-01-07 23:48:28 +08:00
|
|
|
|
value = int.Parse(text);
|
2025-02-16 11:15:42 -05:00
|
|
|
|
}
|
|
|
|
|
|
else if (type == typeof(float))
|
|
|
|
|
|
{
|
2026-01-07 23:48:28 +08:00
|
|
|
|
value = float.Parse(text);
|
2025-02-16 11:15:42 -05:00
|
|
|
|
}
|
|
|
|
|
|
else if (type == typeof(string))
|
|
|
|
|
|
{
|
2026-01-07 23:48:28 +08:00
|
|
|
|
value = text;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (value != null)
|
|
|
|
|
|
{
|
2026-03-22 12:05:32 -04:00
|
|
|
|
Ichni.Editor.Commands.CommandManager.ExecuteCommand(new Ichni.Editor.Commands.ChangeValueCommand(connectedBaseElement, parameterName, value));
|
2025-02-16 11:15:42 -05:00
|
|
|
|
}
|
2025-02-12 18:46:46 -05:00
|
|
|
|
connectedBaseElement.Refresh();
|
2025-02-11 22:58:56 -05:00
|
|
|
|
}
|
2026-01-07 23:48:28 +08:00
|
|
|
|
|
2025-04-14 17:49:47 -04:00
|
|
|
|
public override DynamicUIElement AddListenerFunction(UnityAction action)
|
2025-02-18 10:30:11 -05:00
|
|
|
|
{
|
2026-03-22 12:05:32 -04:00
|
|
|
|
customAction += _ => action();
|
2025-04-14 17:49:47 -04:00
|
|
|
|
return this;
|
2025-02-18 10:30:11 -05:00
|
|
|
|
}
|
2025-02-11 22:58:56 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|