61 lines
2.2 KiB
C#
61 lines
2.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Ichni.RhythmGame;
|
|
using Michsky.MUIP;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
|
|
namespace Ichni.Editor
|
|
{
|
|
public class DynamicUIEnumDropdown : DynamicUIElement
|
|
{
|
|
// 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);
|
|
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)
|
|
{
|
|
realValues.Clear();
|
|
dropdownComponent.items.Clear();
|
|
List<string> enumNameList = System.Enum.GetNames(enumType).ToList();
|
|
realValues = System.Enum.GetValues(enumType).Cast<int>().ToList();
|
|
foreach (var name in enumNameList)
|
|
{
|
|
dropdownComponent.CreateNewItem(name);
|
|
}
|
|
dropdownComponent.selectedItemIndex = 0;
|
|
dropdownComponent.SetupDropdown();
|
|
}
|
|
|
|
private void OnDropdownChanged(int index)
|
|
{
|
|
if (index < 0 || index >= realValues.Count) return;
|
|
ApplyParameters(realValues[index]);
|
|
}
|
|
|
|
private void ApplyParameters(int value)
|
|
{
|
|
Ichni.Editor.Commands.CommandManager.ExecuteCommand(new Ichni.Editor.Commands.ChangeValueCommand(connectedBaseElement, parameterName, value));
|
|
connectedBaseElement.Refresh();
|
|
}
|
|
|
|
public override DynamicUIElement AddListenerFunction(UnityAction action)
|
|
{
|
|
dropdownComponent.onValueChanged.AddListener(_ => action());
|
|
return this;
|
|
}
|
|
}
|
|
} |