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

61 lines
2.2 KiB
C#
Raw Normal View History

2025-02-12 18:46:46 -05:00
using System;
using System.Collections.Generic;
using System.Linq;
using Ichni.RhythmGame;
using Michsky.MUIP;
2025-02-12 18:46:46 -05:00
using TMPro;
using UnityEngine;
2025-02-17 14:46:14 -05:00
using UnityEngine.Events;
2025-02-12 18:46:46 -05:00
namespace Ichni.Editor
{
2025-02-17 14:46:14 -05:00
public class DynamicUIEnumDropdown : DynamicUIElement
2025-02-12 18:46:46 -05:00
{
// public TMP_Dropdown dropdown;
public CustomDropdown dropdownComponent;
public List<int> realValues = new List<int>();
2025-02-12 18:46:46 -05:00
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);
2025-02-12 18:46:46 -05:00
}
public void SetUpEnum(Type enumType)
{
realValues.Clear();
dropdownComponent.items.Clear();
2025-02-12 18:46:46 -05:00
List<string> enumNameList = System.Enum.GetNames(enumType).ToList();
2025-07-26 10:52:52 -04:00
realValues = System.Enum.GetValues(enumType).Cast<int>().ToList();
foreach (var name in enumNameList)
{
dropdownComponent.CreateNewItem(name);
}
2026-03-26 14:48:04 -04:00
dropdownComponent.selectedItemIndex = 0;
dropdownComponent.SetupDropdown();
}
private void OnDropdownChanged(int index)
{
if (index < 0 || index >= realValues.Count) return;
ApplyParameters(realValues[index]);
2025-02-12 18:46:46 -05:00
}
private void ApplyParameters(int value)
{
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-04-19 23:21:27 +08:00
2025-04-14 17:49:47 -04:00
public override DynamicUIElement AddListenerFunction(UnityAction action)
2025-02-17 14:46:14 -05:00
{
dropdownComponent.onValueChanged.AddListener(_ => action());
2025-04-14 17:49:47 -04:00
return this;
2025-02-17 14:46:14 -05:00
}
2025-02-12 18:46:46 -05:00
}
}