using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UIElements; using UnityEditor; using UnityEditor.UIElements; using System.Reflection; using System.Linq; namespace BITKit { [CustomPropertyDrawer(typeof(TempValue))] public sealed class TempValueFloatPropertyDrawer : TempValuePropertyDrawer { protected override void OnCreate(VisualElement root, TempValue tempValue, MethodInfo actionMethod, Slider element) { element.showInputField = true; element.lowValue = tempValue.lowValue; element.highValue = tempValue.highValue; } } [CustomPropertyDrawer(typeof(TempValue))] public sealed class TempValueVector3PropertyDrawer : TempValuePropertyDrawer { protected override void OnCreate(VisualElement root, TempValue tempValue, MethodInfo actionMethod, Vector3Field element) { } } [CustomPropertyDrawer(typeof(TempValue))] public sealed class TempValueStringPropertyDrawer : TempValuePropertyDrawer { protected override void OnCreate(VisualElement root, TempValue tempValue, MethodInfo actionMethod, TextField element) { base.OnCreate(root, tempValue, actionMethod, element); if (tempValue.optionals.IsValid()) { DropdownField dropdown = new(); var value = tempValue.Get(); dropdown.choices = tempValue.optionals.ToList(); if (value is not null) dropdown.SetValueWithoutNotify(value); dropdown.RegisterValueChangedCallback(x => { if (EditorApplication.isPlaying && x.previousValue?.Equals(x.newValue) is false) { actionMethod.Invoke(tempValue, new object[] { x.newValue }); } }); root.Add(dropdown); } } } public abstract class TempValuePropertyDrawer : PropertyDrawer where V : BindableElement, INotifyValueChanged, new() { public override VisualElement CreatePropertyGUI(SerializedProperty property) { VisualElement root = new(); V v = new(); var value = property.FindPropertyRelative("value"); var locked = property.FindPropertyRelative("locked"); var field = new V(); var monoBehaviour = property.serializedObject.targetObject as MonoBehaviour; var type = monoBehaviour.GetType(); var tempValue = type.GetField(property.name).GetValue(monoBehaviour) as TempValue; var actionMethod = typeof(TempValue).GetMethod("Set"); var toggle = new Toggle("Lock"); toggle.BindProperty(locked); root.Add(field); OnCreate(root, tempValue, actionMethod, field); root.Add(toggle); field.BindProperty(value); field.RegisterValueChangedCallback(x => { if (EditorApplication.isPlaying && x.previousValue.Equals(x.newValue) is false) { actionMethod.Invoke(tempValue, new object[] { x.newValue }); } }); field.style.flexGrow = 1; root.style.flexDirection = new(FlexDirection.Row); return root; } protected virtual void OnCreate(VisualElement root, TempValue tempValue, MethodInfo actionMethod, V element) { } } }