SettingsDrawers.cs
Editor/Inspectors/SettingsDrawers.cs
1 using System;
2 using System.Text;
3 using Unity.Collections;
4 using UnityEditor;
5 using UnityEditor.UIElements;
6 using UnityEngine.UIElements;
7
8 namespace MeatMotion.Editor
9 {
10 static class SettingsFields
11 {
12 internal static Foldout Root(SerializedProperty property)
13 {
14 var foldout = new Foldout { text = property.displayName, value = property.isExpanded };
15 foldout.RegisterValueChangedCallback(evt =>
16 {
17 if (evt.target == foldout) property.isExpanded = evt.newValue;
18 });
19 return foldout;
20 }
21
22 internal static void ShowWhen(VisualElement field, SerializedProperty mode, int value)
23 {
24 void Update(SerializedProperty p) =>
25 field.style.display = p.hasMultipleDifferentValues || p.enumValueIndex == value ? DisplayStyle.Flex : DisplayStyle.None;
26 Update(mode);
27 field.TrackPropertyValue(mode, Update);
28 }
29
30 internal static VisualElement Value(SerializedProperty property)
31 {
32 switch (property.type)
33 {
34 case nameof(FixedString32Bytes): return Text(property, FixedString32Bytes.UTF8MaxLengthInBytes, s => new FixedString32Bytes(s));
35 case nameof(FixedString64Bytes): return Text(property, FixedString64Bytes.UTF8MaxLengthInBytes, s => new FixedString64Bytes(s));
36 case nameof(FixedString128Bytes): return Text(property, FixedString128Bytes.UTF8MaxLengthInBytes, s => new FixedString128Bytes(s));
37 case nameof(FixedString512Bytes): return Text(property, FixedString512Bytes.UTF8MaxLengthInBytes, s => new FixedString512Bytes(s));
38 case nameof(FixedString4096Bytes): return Text(property, FixedString4096Bytes.UTF8MaxLengthInBytes, s => new FixedString4096Bytes(s));
39 default: return new PropertyField(property);
40 }
41 }
42
43 static TextField Text(SerializedProperty property, int capacity, Func<string, object> fromString)
44 {
45 var field = new TextField(property.displayName) { isDelayed = true, name = property.name };
46 void Refresh(SerializedProperty p)
47 {
48 field.SetValueWithoutNotify(p.boxedValue.ToString());
49 field.showMixedValue = p.hasMultipleDifferentValues;
50 }
51 Refresh(property);
52 field.TrackPropertyValue(property, Refresh);
53 field.RegisterValueChangedCallback(evt =>
54 {
55 string text = evt.newValue ?? "";
56 if (Encoding.UTF8.GetByteCount(text) > capacity)
57 {
58 var encoder = Encoding.UTF8.GetEncoder();
59 encoder.Convert(text.ToCharArray(), 0, text.Length, new byte[capacity], 0, capacity, true,
60 out int chars, out _, out _);
61 text = text.Substring(0, chars);
62 }
63 property.boxedValue = fromString(text);
64 property.serializedObject.ApplyModifiedProperties();
65 Refresh(property);
66 });
67 return field;
68 }
69 }
70
71 [CustomPropertyDrawer(typeof(SerializableMotionSettings<,>))]
72 public class SerializableMotionSettingsDrawer : PropertyDrawer
73 {
74 public override VisualElement CreatePropertyGUI(SerializedProperty property)
75 {
76 var root = SettingsFields.Root(property);
77 SerializedProperty Child(string name) => property.FindPropertyRelative(name);
78 root.Add(SettingsFields.Value(Child("startValue")));
79 root.Add(SettingsFields.Value(Child("endValue")));
80 root.Add(new PropertyField(Child("duration")));
81 var ease = Child("ease");
82 root.Add(new PropertyField(ease));
83 var curve = new PropertyField(Child("customEaseCurve"));
84 SettingsFields.ShowWhen(curve, ease, (int)Ease.CustomAnimationCurve);
85 root.Add(curve);
86 foreach (var name in new[] { "delay", "delayType", "loops", "loopType" }) root.Add(new PropertyField(Child(name)));
87 var options = Child("options");
88 if (options.hasVisibleChildren) root.Add(new PropertyField(options));
89 var more = new Foldout { text = "Additional Settings" };
90 more.BindProperty(Child("additionalSettings"));
91 foreach (var name in new[] { "cancelOnError", "skipValuesDuringDelay", "immediateBind" }) more.Add(new PropertyField(Child(name)));
92 more.Add(new PropertyField(Child("schedulerType"), "Scheduler"));
93 root.Add(more);
94 return root;
95 }
96 }
97
98 [CustomPropertyDrawer(typeof(StringOptions))]
99 public sealed class StringOptionsDrawer : PropertyDrawer
100 {
101 public override VisualElement CreatePropertyGUI(SerializedProperty property)
102 {
103 var root = SettingsFields.Root(property);
104 var mode = property.FindPropertyRelative("ScrambleMode");
105 root.Add(new PropertyField(mode));
106 var chars = SettingsFields.Value(property.FindPropertyRelative("CustomScrambleChars"));
107 SettingsFields.ShowWhen(chars, mode, (int)ScrambleMode.Custom);
108 root.Add(chars);
109 root.Add(new PropertyField(property.FindPropertyRelative("RichTextEnabled")));
110 root.Add(new PropertyField(property.FindPropertyRelative("RandomSeed")));
111 return root;
112 }
113 }
114 }