94 lines
3.6 KiB
C#
94 lines
3.6 KiB
C#
|
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<float>))]
|
||
|
public sealed class TempValueFloatPropertyDrawer : TempValuePropertyDrawer<float, Slider>
|
||
|
{
|
||
|
protected override void OnCreate(VisualElement root, TempValue<float> tempValue, MethodInfo actionMethod, Slider element)
|
||
|
{
|
||
|
element.showInputField = true;
|
||
|
element.lowValue = tempValue.lowValue;
|
||
|
element.highValue = tempValue.highValue;
|
||
|
}
|
||
|
}
|
||
|
[CustomPropertyDrawer(typeof(TempValue<Vector3>))]
|
||
|
public sealed class TempValueVector3PropertyDrawer : TempValuePropertyDrawer<Vector3, Vector3Field>
|
||
|
{
|
||
|
protected override void OnCreate(VisualElement root, TempValue<Vector3> tempValue, MethodInfo actionMethod, Vector3Field element)
|
||
|
{
|
||
|
}
|
||
|
}
|
||
|
[CustomPropertyDrawer(typeof(TempValue<string>))]
|
||
|
public sealed class TempValueStringPropertyDrawer : TempValuePropertyDrawer<string, TextField>
|
||
|
{
|
||
|
protected override void OnCreate(VisualElement root, TempValue<string> 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<T, V> : PropertyDrawer where V : BindableElement, INotifyValueChanged<T>, 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<T>;
|
||
|
var actionMethod = typeof(TempValue<T>).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<T> tempValue, MethodInfo actionMethod, V element)
|
||
|
{
|
||
|
|
||
|
}
|
||
|
}
|
||
|
}
|