summaryrefslogtreecommitdiff
path: root/VRCSDK3AvatarsQuestLegacy/Assets/VRCSDK/SDK3A/Editor/Components3/VRCExpressionParametersEditor.cs
blob: 2f51b6c73a426ac2080beea271007bbfbd35eb62 (plain)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
using UnityEngine;
using UnityEditor;
using ExpressionParameters = VRC.SDK3.Avatars.ScriptableObjects.VRCExpressionParameters;
using ExpressionParameter = VRC.SDK3.Avatars.ScriptableObjects.VRCExpressionParameters.Parameter;
using UnityEngine.UI;

[CustomEditor(typeof(VRC.SDK3.Avatars.ScriptableObjects.VRCExpressionParameters))]
public class VRCExpressionParametersEditor : Editor
{
	int selected = -1;
	GUIStyle boxNormal;
	GUIStyle boxSelected;

	void InitStyles()
	{
		//Normal
		if(boxNormal == null)
			boxNormal = new GUIStyle(GUI.skin.box);

		//Selected
		if(boxSelected == null)
		{
			boxSelected = new GUIStyle(GUI.skin.box);
			boxSelected.normal.background = MakeStyleBackground(new Color(0.0f, 0.5f, 1f, 0.5f));
		}
	}
	Texture2D MakeStyleBackground(Color color)
	{
		var texture = new Texture2D(1, 1);
		texture.SetPixel(0, 0, color);
		texture.Apply();
		return texture;
	}

	void SelectParam(int value)
	{
		selected = value;
		Repaint();
	}
	public void OnEnable()
	{
		//Init parameters
		var expressionParameters = target as ExpressionParameters;
		if (expressionParameters.parameters == null)
			InitExpressionParameters(true);

		SelectParam(-1);
	}
	public override void OnInspectorGUI()
	{
		InitStyles();

		serializedObject.Update();
		{
			EditorGUILayout.LabelField("Parameters");
			var parameters = serializedObject.FindProperty("parameters");

			//Controls
			EditorGUILayout.BeginHorizontal();
			{
				//Add
				if (GUILayout.Button("Add"))
					parameters.arraySize = parameters.arraySize + 1;

				EditorGUI.BeginDisabledGroup(selected < 0);
				{
					//Move Up
					if (GUILayout.Button("Up"))
					{
						if(selected > 0)
						{
							SwapParams(selected, selected - 1);
							selected = selected - 1;
							Repaint();
						}
					}

					//Move Down
					if (GUILayout.Button("Down"))
					{
						if (selected < parameters.arraySize-1)
						{
							SwapParams(selected, selected + 1);
							selected = selected + 1;
							Repaint();
						}
					}

					void SwapParams(int indexA, int indexB)
					{
						var script = (ExpressionParameters)target;
						var itemA = script.parameters[indexA];
						var itemB = script.parameters[indexB];
						script.parameters[indexA] = itemB;
						script.parameters[indexB] = itemA;

						serializedObject.Update();
					}

					//Delete
					if (GUILayout.Button("Delete"))
					{
						parameters.DeleteArrayElementAtIndex(selected);
						SelectParam(-1);
					}
				}
				EditorGUI.EndDisabledGroup();
			}
			EditorGUILayout.EndHorizontal();
			

			//Labels
			EditorGUILayout.BeginHorizontal();
			{
				EditorGUILayout.LabelField("    Name", GUILayout.MinWidth(100));
				EditorGUILayout.LabelField("    Type", GUILayout.Width(100));
				EditorGUILayout.LabelField("Default", GUILayout.Width(64));
				EditorGUILayout.LabelField("Saved", GUILayout.Width(64));
			}
			EditorGUILayout.EndHorizontal();

			//Parameters
			int count = parameters.arraySize;
			for(int paramIter=0; paramIter< parameters.arraySize; paramIter++)
			{
				DrawExpressionParameter(parameters, paramIter);
					
				/*var item = parameters.GetArrayElementAtIndex(paramIter);
				var name = item.FindPropertyRelative("name");
				var valueType = item.FindPropertyRelative("valueType");

				//Draw
				EditorGUI.indentLevel += 1;
				EditorGUILayout.BeginHorizontal();
				{
					EditorGUILayout.PropertyField(name, new GUIContent(""));
					EditorGUILayout.PropertyField(valueType, new GUIContent(""));
					if(GUILayout.Button("X", GUILayout.Width(32)))
					{
						parameters.DeleteArrayElementAtIndex(paramIter);
						paramIter -= 1;
					}
				}
				EditorGUILayout.EndHorizontal();
				EditorGUI.indentLevel -= 1;*/
			}

			//Cost
			int cost = (target as ExpressionParameters).CalcTotalCost();
			if(cost <= ExpressionParameters.MAX_PARAMETER_COST)
				EditorGUILayout.HelpBox($"Total Memory: {cost}/{ExpressionParameters.MAX_PARAMETER_COST}", MessageType.Info);
			else
				EditorGUILayout.HelpBox($"Total Memory: {cost}/{ExpressionParameters.MAX_PARAMETER_COST}\nParameters use too much memory.  Remove parameters or use bools which use less memory.", MessageType.Error);

			//Info
			EditorGUILayout.HelpBox("Only parameters defined here can be used by expression menus, sync between all playable layers and sync across the network to remote clients.", MessageType.Info);
			EditorGUILayout.HelpBox("The parameter name and type should match a parameter defined on one or more of your animation controllers.", MessageType.Info);
			EditorGUILayout.HelpBox("Parameters used by the default animation controllers (Optional)\nVRCEmote, Int\nVRCFaceBlendH, Float\nVRCFaceBlendV, Float", MessageType.Info);

			//Clear
			if (GUILayout.Button("Clear Parameters"))
			{
				if (EditorUtility.DisplayDialogComplex("Warning", "Are you sure you want to clear all expression parameters?", "Clear", "Cancel", "") == 0)
				{
					InitExpressionParameters(false);
				}
			}
			if (GUILayout.Button("Default Parameters"))
			{
				if (EditorUtility.DisplayDialogComplex("Warning", "Are you sure you want to reset all expression parameters to default?", "Reset", "Cancel", "") == 0)
				{
					InitExpressionParameters(true);
				}
			}
		}
		serializedObject.ApplyModifiedProperties();
	}
	void DrawExpressionParameter(SerializedProperty parameters, int index)
	{
		if (parameters.arraySize < index + 1)
			parameters.InsertArrayElementAtIndex(index);
		var item = parameters.GetArrayElementAtIndex(index);

		var name = item.FindPropertyRelative("name");
		var valueType = item.FindPropertyRelative("valueType");
		var defaultValue = item.FindPropertyRelative("defaultValue");
		var saved = item.FindPropertyRelative("saved");

		bool isSelected = selected == index;

		EditorGUI.indentLevel += 1;
		var rect = EditorGUILayout.BeginHorizontal(isSelected ? boxSelected : boxNormal);
		{
			EditorGUILayout.PropertyField(name, new GUIContent(""), GUILayout.MinWidth(100));
			EditorGUILayout.PropertyField(valueType, new GUIContent(""), GUILayout.Width(100));
			var type = (ExpressionParameters.ValueType)valueType.intValue;
			switch(type)
			{
				case ExpressionParameters.ValueType.Int:
					defaultValue.floatValue = Mathf.Clamp(EditorGUILayout.IntField((int)defaultValue.floatValue, GUILayout.Width(64)), 0, 255);
					break;
				case ExpressionParameters.ValueType.Float:
					defaultValue.floatValue = Mathf.Clamp(EditorGUILayout.FloatField(defaultValue.floatValue, GUILayout.Width(64)), -1f, 1f);
					break;
				case ExpressionParameters.ValueType.Bool:
					defaultValue.floatValue = EditorGUILayout.Toggle(defaultValue.floatValue != 0 ? true : false, GUILayout.Width(64)) ? 1f : 0f;
					break;
			}
			EditorGUILayout.PropertyField(saved, new GUIContent(""), GUILayout.Width(64));
		}
		EditorGUILayout.EndHorizontal();
		EditorGUI.indentLevel -= 1;

		//Select
		if(Event.current.type == EventType.MouseDown)
		{
			if(rect.Contains(Event.current.mousePosition))
			{
				SelectParam(index);
				Event.current.Use();
			}
		}
	}
	void InitExpressionParameters(bool populateWithDefault)
	{
		var expressionParameters = target as ExpressionParameters;
		serializedObject.Update();
		{
			if (populateWithDefault)
			{
				expressionParameters.parameters = new ExpressionParameter[3];

				expressionParameters.parameters[0] = new ExpressionParameter();
				expressionParameters.parameters[0].name = "VRCEmote";
				expressionParameters.parameters[0].valueType = ExpressionParameters.ValueType.Int;

				expressionParameters.parameters[1] = new ExpressionParameter();
				expressionParameters.parameters[1].name = "VRCFaceBlendH";
				expressionParameters.parameters[1].valueType = ExpressionParameters.ValueType.Float;

				expressionParameters.parameters[2] = new ExpressionParameter();
				expressionParameters.parameters[2].name = "VRCFaceBlendV";
				expressionParameters.parameters[2].valueType = ExpressionParameters.ValueType.Float;
			}
			else
			{
				//Empty
				expressionParameters.parameters = new ExpressionParameter[0];
			}
		}
		serializedObject.ApplyModifiedProperties();
	}
}