Files
ichni_Creator_Studio/Assets/Scripts/DynamicUI/DynamicUIElements/Simple/DynamicUIInputField.cs

125 lines
4.1 KiB
C#
Raw Normal View History

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;
using UnityEngine.Events;
2025-02-11 22:58:56 -05:00
namespace Ichni.Editor
{
public class DynamicUIInputField : DynamicUIElement, IHaveAutoUpdate
2025-02-11 22:58:56 -05:00
{
public TMP_InputField inputField;
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-28 06:30:33 -04:00
// 精确重置可交互状态,防止从对象池取回到被禁用的废弃输入框
if (inputField != null) inputField.interactable = true;
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);
if (parameterName != string.Empty)
{
ApplyContent();
2026-03-22 12:05:32 -04:00
inputField.onEndEdit.AddListener(OnEndEditNode);
}
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; }
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;
2026-03-22 12:05:32 -04:00
inputField.onSelect.RemoveListener(OnSelectNode);
inputField.onSelect.AddListener(OnSelectNode);
inputField.onDeselect.RemoveListener(OnDeselectNode);
inputField.onDeselect.AddListener(OnDeselectNode);
}
public void ApplyContent()
{
object value = ReflectionHelper.GetDeepValue(connectedBaseElement, parameterName);
inputField.text = value != null ? value.ToString() : string.Empty;
}
2025-02-11 22:58:56 -05:00
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)
{
2026-03-22 12:05:32 -04:00
Ichni.Editor.Commands.CommandManager.ExecuteCommand(new Ichni.Editor.Commands.ChangeValueCommand(connectedBaseElement, parameterName, value));
}
2025-02-12 18:46:46 -05:00
connectedBaseElement.Refresh();
2025-02-11 22:58:56 -05:00
}
2025-04-14 17:49:47 -04:00
public override DynamicUIElement AddListenerFunction(UnityAction action)
{
2026-03-22 12:05:32 -04:00
customAction += _ => action();
2025-04-14 17:49:47 -04:00
return this;
}
2025-02-11 22:58:56 -05:00
}
}