Signed-off-by: TRAfoer <lhf190@outlook.com>
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Ichni.RhythmGame;
|
||||
using Michsky.MUIP;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
@@ -11,34 +11,49 @@ namespace Ichni.Editor
|
||||
{
|
||||
public class DynamicUIEnumDropdown : DynamicUIElement
|
||||
{
|
||||
public TMP_Dropdown dropdown;
|
||||
public List<int> realValues;
|
||||
// public TMP_Dropdown dropdown;
|
||||
public CustomDropdown dropdownComponent;
|
||||
public List<int> realValues = new List<int>();
|
||||
|
||||
public override void Initialize(IBaseElement baseElement, string title, string parameterName)
|
||||
{
|
||||
base.Initialize(baseElement, title, parameterName);
|
||||
object value = ReflectionHelper.GetDeepValue(connectedBaseElement, parameterName);
|
||||
dropdown.value = value != null ? (int)value : 0;
|
||||
dropdown.onValueChanged.AddListener(ApplyParameters);
|
||||
int targetIndex = realValues.IndexOf(value != null ? (int)value : 0);
|
||||
dropdownComponent.selectedItemIndex = (targetIndex != -1) ? targetIndex : 0;
|
||||
dropdownComponent.SetupDropdown();
|
||||
dropdownComponent.onValueChanged.RemoveAllListeners();
|
||||
dropdownComponent.onValueChanged.AddListener(OnDropdownChanged);
|
||||
}
|
||||
|
||||
public void SetUpEnum(Type enumType)
|
||||
{
|
||||
dropdown.ClearOptions();
|
||||
realValues.Clear();
|
||||
dropdownComponent.items.Clear();
|
||||
List<string> enumNameList = System.Enum.GetNames(enumType).ToList();
|
||||
realValues = System.Enum.GetValues(enumType).Cast<int>().ToList();
|
||||
dropdown.AddOptions(enumNameList);
|
||||
foreach (var name in enumNameList)
|
||||
{
|
||||
dropdownComponent.CreateNewItem(name);
|
||||
}
|
||||
dropdownComponent.SetupDropdown();
|
||||
}
|
||||
|
||||
private void OnDropdownChanged(int index)
|
||||
{
|
||||
if (index < 0 || index >= realValues.Count) return;
|
||||
ApplyParameters(realValues[index]);
|
||||
}
|
||||
|
||||
private void ApplyParameters(int value)
|
||||
{
|
||||
ReflectionHelper.SetDeepValue(connectedBaseElement, parameterName, realValues[value]);
|
||||
ReflectionHelper.SetDeepValue(connectedBaseElement, parameterName, value);
|
||||
connectedBaseElement.Refresh();
|
||||
}
|
||||
|
||||
public override DynamicUIElement AddListenerFunction(UnityAction action)
|
||||
{
|
||||
dropdown.onValueChanged.AddListener(_ => action());
|
||||
dropdownComponent.onValueChanged.AddListener(_ => action());
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,32 +1,75 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Ichni.RhythmGame;
|
||||
using Michsky.MUIP;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Ichni.Editor
|
||||
{
|
||||
public class DynamicUIStringListDropdown : DynamicUIElement
|
||||
{
|
||||
[Header("Resources")]
|
||||
public TMP_Dropdown dropdown;
|
||||
public List<string> stringList;
|
||||
public CustomDropdown dropdownComponent;
|
||||
|
||||
[Header("Settings")]
|
||||
public List<string> stringList = new List<string>();
|
||||
public bool addPlaceholder = true;
|
||||
public string placeholderText = "Please Select...";
|
||||
|
||||
public override void Initialize(IBaseElement baseElement, string title, string parameterName)
|
||||
{
|
||||
base.Initialize(baseElement, title, parameterName);
|
||||
|
||||
// 获取当前反射值
|
||||
string connectedValue = (string)ReflectionHelper.GetDeepValue(connectedBaseElement, parameterName);
|
||||
dropdown.value = stringList.Contains(connectedValue) ? stringList.IndexOf(connectedValue) : 0;
|
||||
dropdown.onValueChanged.AddListener((value) => ApplyParameters(dropdown.options[value].text));
|
||||
|
||||
// 查找索引并设置给组件
|
||||
int targetIndex = stringList.IndexOf(connectedValue);
|
||||
dropdownComponent.selectedItemIndex = (targetIndex != -1) ? targetIndex : 0;
|
||||
|
||||
// 初始化 UI 显示
|
||||
dropdownComponent.SetupDropdown();
|
||||
|
||||
// 移除旧监听防止重复触发,并添加新监听
|
||||
dropdownComponent.onValueChanged.RemoveAllListeners();
|
||||
dropdownComponent.onValueChanged.AddListener(OnDropdownChanged);
|
||||
}
|
||||
|
||||
public void SetUpStringList(List<string> originalList)
|
||||
{
|
||||
this.stringList.AddRange(originalList);
|
||||
this.stringList.Insert(0, "Please Select..."); // Add a default value "Please Select...
|
||||
dropdown.ClearOptions();
|
||||
dropdown.AddOptions(this.stringList);
|
||||
// Bug 修复:清空本地列表防止重复叠加
|
||||
stringList.Clear();
|
||||
|
||||
if (addPlaceholder) { stringList.Add(placeholderText); }
|
||||
stringList.AddRange(originalList);
|
||||
|
||||
// 清空组件内的旧项目
|
||||
// 优化:直接清除 items 列表,而不是循环调用 RemoveItem 以避免多次 UI 刷新
|
||||
dropdownComponent.items.Clear();
|
||||
|
||||
foreach (var item in stringList)
|
||||
{
|
||||
// 使用不触发 SetupDropdown 的重载
|
||||
dropdownComponent.CreateNewItem(item);
|
||||
}
|
||||
|
||||
// 统一刷新 UI
|
||||
dropdownComponent.SetupDropdown();
|
||||
}
|
||||
|
||||
private void OnDropdownChanged(int index)
|
||||
{
|
||||
if (index < 0 || index >= stringList.Count) return;
|
||||
|
||||
string value = stringList[index];
|
||||
|
||||
// 逻辑优化:如果是占位符,通常不希望写入反射模型
|
||||
if (addPlaceholder && index == 0 && value == placeholderText) return;
|
||||
|
||||
ApplyParameters(value);
|
||||
}
|
||||
|
||||
private void ApplyParameters(string value)
|
||||
@@ -37,7 +80,7 @@ namespace Ichni.Editor
|
||||
|
||||
public override DynamicUIElement AddListenerFunction(UnityAction action)
|
||||
{
|
||||
dropdown.onValueChanged.AddListener(_ => action());
|
||||
dropdownComponent.onValueChanged.AddListener(_ => action());
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -238,7 +238,7 @@ namespace Ichni.Editor
|
||||
enumDropdown.SetUpEnum(enumType);
|
||||
enumDropdown.Initialize(baseElement, title, parameterName);
|
||||
var nav = new Navigation { mode = Navigation.Mode.None };
|
||||
enumDropdown.dropdown.navigation = nav;
|
||||
//enumDropdown.dropdownComponent.GetComponent<Button>().navigation = nav;
|
||||
subcontainer.dynamicUIElements.Add(enumDropdown);
|
||||
return enumDropdown;
|
||||
}
|
||||
@@ -252,7 +252,7 @@ namespace Ichni.Editor
|
||||
stringListDropdown.SetUpStringList(stringList);
|
||||
stringListDropdown.Initialize(baseElement, title, parameterName);
|
||||
var nav = new Navigation { mode = Navigation.Mode.None };
|
||||
stringListDropdown.dropdown.navigation = nav;
|
||||
//stringListDropdown.dropdownComponent.GetComponent<Button>().navigation = nav;
|
||||
subcontainer.dynamicUIElements.Add(stringListDropdown);
|
||||
return stringListDropdown;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user