/
MinMaxGradientPropertyDrawer.cs
143 lines (126 loc) · 5.81 KB
/
MinMaxGradientPropertyDrawer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEngine.UIElements;
using UnityEditor.UIElements;
namespace UnityEditorInternal
{
[CustomPropertyDrawer(typeof(ParticleSystem.MinMaxGradient))]
public class MinMaxGradientPropertyDrawer : PropertyDrawer
{
internal class PropertyData
{
public SerializedProperty mode;
public SerializedProperty gradientMin;
public SerializedProperty gradientMax;
public SerializedProperty colorMin;
public SerializedProperty colorMax;
}
// Its possible that the PropertyDrawer may be used to draw more than one MinMaxCurve property(arrays, lists)
Dictionary<string, PropertyData> m_PropertyDataPerPropertyPath = new Dictionary<string, PropertyData>();
PropertyData m_Property;
class Styles
{
public readonly float stateButtonWidth = 18;
public readonly GUIContent[] modes = new[]
{
EditorGUIUtility.TrTextContent("Color"),
EditorGUIUtility.TrTextContent("Gradient"),
EditorGUIUtility.TrTextContent("Random Between Two Colors"),
EditorGUIUtility.TrTextContent("Random Between Two Gradients"),
EditorGUIUtility.TrTextContent("Random Color")
};
}
static Styles s_Styles;
void Init(SerializedProperty property)
{
if (s_Styles == null)
s_Styles = new Styles();
if (m_PropertyDataPerPropertyPath.TryGetValue(property.propertyPath, out m_Property))
return;
m_Property = new PropertyData()
{
mode = property.FindPropertyRelative("m_Mode"),
gradientMin = property.FindPropertyRelative("m_GradientMin"),
gradientMax = property.FindPropertyRelative("m_GradientMax"),
colorMin = property.FindPropertyRelative("m_ColorMin"),
colorMax = property.FindPropertyRelative("m_ColorMax")
};
m_PropertyDataPerPropertyPath.Add(property.propertyPath, m_Property);
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
Init(property);
return m_Property.mode.intValue == (int)MinMaxGradientState.k_RandomBetweenTwoGradients ? EditorGUI.kSingleLineHeight * 2.0f : EditorGUI.kSingleLineHeight;
}
static void DrawTwoPropertyFields(Rect rect, GUIContent label, SerializedProperty prop1, SerializedProperty prop2, bool singleLine)
{
rect = EditorGUI.PrefixLabel(rect, label);
var property1Rect = rect;
var property2Rect = rect;
if (singleLine)
{
float wideModeWidth = rect.width * 0.5f;
property1Rect.width = wideModeWidth;
property2Rect.x += wideModeWidth;
property2Rect.width = wideModeWidth;
}
else
{
property2Rect.y += EditorGUI.kSingleLineHeight;
}
EditorGUI.PropertyField(property1Rect, prop1, GUIContent.none);
EditorGUI.PropertyField(property2Rect, prop2, GUIContent.none);
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
Init(property);
Rect fieldRect = position;
fieldRect.height = EditorGUI.kSingleLineHeight;
var mode = (MinMaxGradientState)m_Property.mode.intValue;
// Mode
fieldRect.xMax -= s_Styles.stateButtonWidth;
var modeRect = new Rect(fieldRect.xMax, fieldRect.y, s_Styles.stateButtonWidth, fieldRect.height);
EditorGUI.BeginProperty(modeRect, GUIContent.none, m_Property.mode);
EditorGUI.BeginChangeCheck();
int prevIndentLevel = EditorGUI.indentLevel;
EditorGUI.indentLevel = 0;
int newSelection = EditorGUI.Popup(modeRect, GUIContent.none, m_Property.mode.intValue, s_Styles.modes, EditorStyles.minMaxStateDropdown);
EditorGUI.indentLevel = prevIndentLevel;
if (EditorGUI.EndChangeCheck())
m_Property.mode.intValue = newSelection;
EditorGUI.EndProperty();
if (m_Property.mode.hasMultipleDifferentValues)
{
EditorGUI.LabelField(fieldRect, GUIContent.Temp("-"));
return;
}
switch (mode)
{
case MinMaxGradientState.k_Color:
EditorGUI.PropertyField(fieldRect, m_Property.colorMax, label);
break;
case MinMaxGradientState.k_Gradient:
case MinMaxGradientState.k_RandomColor:
EditorGUI.PropertyField(fieldRect, m_Property.gradientMax, label);
break;
case MinMaxGradientState.k_RandomBetweenTwoColors:
DrawTwoPropertyFields(fieldRect, label, m_Property.colorMin, m_Property.colorMax, true);
break;
case MinMaxGradientState.k_RandomBetweenTwoGradients:
DrawTwoPropertyFields(fieldRect, label, m_Property.gradientMin, m_Property.gradientMax, false);
break;
}
}
public override VisualElement CreatePropertyGUI(SerializedProperty property)
{
Init(property);
var field = new MinMaxGradientField(m_Property, preferredLabel);
PropertyField.ConfigureFieldStyles<MinMaxGradientField, ParticleSystem.MinMaxGradient>(field);
return field;
}
}
}