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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using ExpressionsMenu = VRC.SDK3.Avatars.ScriptableObjects.VRCExpressionsMenu;
using ExpressionControl = VRC.SDK3.Avatars.ScriptableObjects.VRCExpressionsMenu.Control;
using ExpressionParameters = VRC.SDK3.Avatars.ScriptableObjects.VRCExpressionParameters;
using VRC.SDK3.Avatars.ScriptableObjects;
using System.Reflection.Emit;
[CustomEditor(typeof(VRC.SDK3.Avatars.ScriptableObjects.VRCExpressionsMenu))]
public class VRCExpressionsMenuEditor : Editor
{
static string[] ToggleStyles = { "Pip-Slot", "Animation" };
List<UnityEngine.Object> foldoutList = new List<UnityEngine.Object>();
public void Start()
{
}
public void OnDisable()
{
SelectAvatarDescriptor(null);
}
public override void OnInspectorGUI()
{
serializedObject.Update();
SelectAvatarDescriptor();
if(activeDescriptor == null)
{
EditorGUILayout.HelpBox("No active avatar descriptor found in scene.", MessageType.Error);
}
EditorGUILayout.Space();
//Controls
EditorGUI.BeginDisabledGroup(activeDescriptor == null);
EditorGUILayout.LabelField("Controls");
EditorGUI.indentLevel += 1;
{
var controls = serializedObject.FindProperty("controls");
for (int i = 0; i < controls.arraySize; i++)
{
var control = controls.GetArrayElementAtIndex(i);
DrawControl(controls, control as SerializedProperty, i);
}
//Add
EditorGUI.BeginDisabledGroup(controls.arraySize >= ExpressionsMenu.MAX_CONTROLS);
if (GUILayout.Button("Add Control"))
{
var menu = serializedObject.targetObject as ExpressionsMenu;
var control = new ExpressionControl();
control.name = "New Control";
menu.controls.Add(control);
}
EditorGUI.EndDisabledGroup();
}
EditorGUI.indentLevel -= 1;
EditorGUI.EndDisabledGroup();
serializedObject.ApplyModifiedProperties();
}
void DrawControl(SerializedProperty controls, SerializedProperty control, int index)
{
var name = control.FindPropertyRelative("name");
var icon = control.FindPropertyRelative("icon");
var type = control.FindPropertyRelative("type");
var parameter = control.FindPropertyRelative("parameter");
var value = control.FindPropertyRelative("value");
var subMenu = control.FindPropertyRelative("subMenu");
var subParameters = control.FindPropertyRelative("subParameters");
var labels = control.FindPropertyRelative("labels");
//Foldout
EditorGUI.BeginChangeCheck();
control.isExpanded = EditorGUILayout.Foldout(control.isExpanded, name.stringValue);
if (!control.isExpanded)
return;
//Box
GUILayout.BeginVertical(GUI.skin.box);
{
//Up, Down, Delete
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
if (GUILayout.Button("Up", GUILayout.Width(64)))
{
if (index > 0)
controls.MoveArrayElement(index, index - 1);
}
if (GUILayout.Button("Down", GUILayout.Width(64)))
{
if (index < controls.arraySize - 1)
controls.MoveArrayElement(index, index + 1);
}
if (GUILayout.Button("Delete", GUILayout.Width(64)))
{
controls.DeleteArrayElementAtIndex(index);
return;
}
GUILayout.EndHorizontal();
//Generic params
EditorGUI.indentLevel += 1;
{
EditorGUILayout.PropertyField(name);
EditorGUILayout.PropertyField(icon);
EditorGUILayout.PropertyField(type);
//Type Info
var controlType = (ExpressionControl.ControlType)type.intValue;
switch (controlType)
{
case ExpressionControl.ControlType.Button:
EditorGUILayout.HelpBox("Click or hold to activate. The button remains active for a minimum 0.2s.\nWhile active the (Parameter) is set to (Value).\nWhen inactive the (Parameter) is reset to zero.", MessageType.Info);
break;
case ExpressionControl.ControlType.Toggle:
EditorGUILayout.HelpBox("Click to toggle on or off.\nWhen turned on the (Parameter) is set to (Value).\nWhen turned off the (Parameter) is reset to zero.", MessageType.Info);
break;
case ExpressionControl.ControlType.SubMenu:
EditorGUILayout.HelpBox("Opens another expression menu.\nWhen opened the (Parameter) is set to (Value).\nWhen closed (Parameter) is reset to zero.", MessageType.Info);
break;
case ExpressionControl.ControlType.TwoAxisPuppet:
EditorGUILayout.HelpBox("Puppet menu that maps the joystick to two parameters (-1 to +1).\nWhen opened the (Parameter) is set to (Value).\nWhen closed (Parameter) is reset to zero.", MessageType.Info);
break;
case ExpressionControl.ControlType.FourAxisPuppet:
EditorGUILayout.HelpBox("Puppet menu that maps the joystick to four parameters (0 to 1).\nWhen opened the (Parameter) is set to (Value).\nWhen closed (Parameter) is reset to zero.", MessageType.Info);
break;
case ExpressionControl.ControlType.RadialPuppet:
EditorGUILayout.HelpBox("Puppet menu that sets a value based on joystick rotation. (0 to 1)\nWhen opened the (Parameter) is set to (Value).\nWhen closed (Parameter) is reset to zero.", MessageType.Info);
break;
}
//Param
switch (controlType)
{
case ExpressionControl.ControlType.Button:
case ExpressionControl.ControlType.Toggle:
case ExpressionControl.ControlType.SubMenu:
case ExpressionControl.ControlType.TwoAxisPuppet:
case ExpressionControl.ControlType.FourAxisPuppet:
case ExpressionControl.ControlType.RadialPuppet:
DrawParameterDropDown(parameter, "Parameter");
DrawParameterValue(parameter, value);
break;
}
EditorGUILayout.LabelField("", GUI.skin.horizontalSlider);
//Style
/*if (controlType == ExpressionsControl.ControlType.Toggle)
{
style.intValue = EditorGUILayout.Popup("Visual Style", style.intValue, ToggleStyles);
}*/
//Sub menu
if (controlType == ExpressionControl.ControlType.SubMenu)
{
EditorGUILayout.PropertyField(subMenu);
}
//Puppet Parameter Set
switch (controlType)
{
case ExpressionControl.ControlType.TwoAxisPuppet:
subParameters.arraySize = 2;
labels.arraySize = 4;
DrawParameterDropDown(subParameters.GetArrayElementAtIndex(0), "Parameter Horizontal", false);
DrawParameterDropDown(subParameters.GetArrayElementAtIndex(1), "Parameter Vertical", false);
DrawLabel(labels.GetArrayElementAtIndex(0), "Label Up");
DrawLabel(labels.GetArrayElementAtIndex(1), "Label Right");
DrawLabel(labels.GetArrayElementAtIndex(2), "Label Down");
DrawLabel(labels.GetArrayElementAtIndex(3), "Label Left");
break;
case ExpressionControl.ControlType.FourAxisPuppet:
subParameters.arraySize = 4;
labels.arraySize = 4;
DrawParameterDropDown(subParameters.GetArrayElementAtIndex(0), "Parameter Up", false);
DrawParameterDropDown(subParameters.GetArrayElementAtIndex(1), "Parameter Right", false);
DrawParameterDropDown(subParameters.GetArrayElementAtIndex(2), "Parameter Down", false);
DrawParameterDropDown(subParameters.GetArrayElementAtIndex(3), "Parameter Left", false);
DrawLabel(labels.GetArrayElementAtIndex(0), "Label Up");
DrawLabel(labels.GetArrayElementAtIndex(1), "Label Right");
DrawLabel(labels.GetArrayElementAtIndex(2), "Label Down");
DrawLabel(labels.GetArrayElementAtIndex(3), "Label Left");
break;
case ExpressionControl.ControlType.RadialPuppet:
subParameters.arraySize = 1;
labels.arraySize = 0;
DrawParameterDropDown(subParameters.GetArrayElementAtIndex(0), "Paramater Rotation", false);
break;
default:
subParameters.arraySize = 0;
labels.arraySize = 0;
break;
}
}
EditorGUI.indentLevel -= 1;
}
GUILayout.EndVertical();
}
void DrawLabel(SerializedProperty subControl, string name)
{
var nameProp = subControl.FindPropertyRelative("name");
var icon = subControl.FindPropertyRelative("icon");
EditorGUILayout.LabelField(name);
EditorGUI.indentLevel += 2;
EditorGUILayout.PropertyField(nameProp);
EditorGUILayout.PropertyField(icon);
EditorGUI.indentLevel -= 2;
}
void DrawInfoHover(string text)
{
GUILayout.Button(new GUIContent("?", text), GUILayout.MaxWidth(32));
}
void DrawInfo(string text)
{
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
GUILayout.Label(text, GUI.skin.textArea, GUILayout.MaxWidth(400));
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
}
VRC.SDK3.Avatars.Components.VRCAvatarDescriptor activeDescriptor = null;
string[] parameterNames;
void SelectAvatarDescriptor()
{
var descriptors = GameObject.FindObjectsOfType<VRC.SDK3.Avatars.Components.VRCAvatarDescriptor>();
if (descriptors.Length > 0)
{
//Compile list of names
string[] names = new string[descriptors.Length];
for(int i=0; i<descriptors.Length; i++)
names[i] = descriptors[i].gameObject.name;
//Select
var currentIndex = System.Array.IndexOf(descriptors, activeDescriptor);
var nextIndex = EditorGUILayout.Popup("Active Avatar", currentIndex, names);
if(nextIndex < 0)
nextIndex = 0;
if (nextIndex != currentIndex)
SelectAvatarDescriptor(descriptors[nextIndex]);
}
else
SelectAvatarDescriptor(null);
}
void SelectAvatarDescriptor(VRC.SDK3.Avatars.Components.VRCAvatarDescriptor desc)
{
if (desc == activeDescriptor)
return;
activeDescriptor = desc;
if(activeDescriptor != null)
{
//Init stage parameters
int paramCount = desc.GetExpressionParameterCount();
parameterNames = new string[paramCount + 1];
parameterNames[0] = "[None]";
for (int i = 0; i < paramCount; i++)
{
var param = desc.GetExpressionParameter(i);
string name = "[None]";
if (param != null && !string.IsNullOrEmpty(param.name))
name = string.Format("{0}, {1}", param.name, param.valueType.ToString(), i + 1);
parameterNames[i + 1] = name;
}
}
else
{
parameterNames = null;
}
}
int GetExpressionParametersCount()
{
if (activeDescriptor != null && activeDescriptor.expressionParameters != null && activeDescriptor.expressionParameters.parameters != null)
return activeDescriptor.expressionParameters.parameters.Length;
return 0;
}
ExpressionParameters.Parameter GetExpressionParameter(int i)
{
if (activeDescriptor != null)
return activeDescriptor.GetExpressionParameter(i);
return null;
}
void DrawParameterDropDown(SerializedProperty parameter, string name, bool allowBool=true)
{
var parameterName = parameter.FindPropertyRelative("name");
VRCExpressionParameters.Parameter param = null;
string value = parameterName.stringValue;
bool parameterFound = false;
EditorGUILayout.BeginHorizontal();
{
if(activeDescriptor != null)
{
//Dropdown
int currentIndex;
if (string.IsNullOrEmpty(value))
{
currentIndex = -1;
parameterFound = true;
}
else
{
currentIndex = -2;
for (int i = 0; i < GetExpressionParametersCount(); i++)
{
var item = activeDescriptor.GetExpressionParameter(i);
if (item.name == value)
{
param = item;
parameterFound = true;
currentIndex = i;
break;
}
}
}
//Dropdown
EditorGUI.BeginChangeCheck();
currentIndex = EditorGUILayout.Popup(name, currentIndex + 1, parameterNames);
if (EditorGUI.EndChangeCheck())
{
if (currentIndex == 0)
parameterName.stringValue = "";
else
parameterName.stringValue = GetExpressionParameter(currentIndex - 1).name;
}
}
else
{
EditorGUI.BeginDisabledGroup(true);
EditorGUILayout.Popup(0, new string[0]);
EditorGUI.EndDisabledGroup();
}
//Text field
parameterName.stringValue = EditorGUILayout.TextField(parameterName.stringValue, GUILayout.MaxWidth(200));
}
EditorGUILayout.EndHorizontal();
if (!parameterFound)
{
EditorGUILayout.HelpBox("Parameter not found on the active avatar descriptor.", MessageType.Warning);
}
if(!allowBool && param != null && param.valueType == ExpressionParameters.ValueType.Bool)
{
EditorGUILayout.HelpBox("Bool parameters not valid for this choice.", MessageType.Error);
}
}
void DrawParameterValue(SerializedProperty parameter, SerializedProperty value)
{
string paramName = parameter.FindPropertyRelative("name").stringValue;
if (!string.IsNullOrEmpty(paramName))
{
var paramDef = FindExpressionParameterDef(paramName);
if (paramDef != null)
{
if (paramDef.valueType == ExpressionParameters.ValueType.Int)
{
value.floatValue = EditorGUILayout.IntField("Value", Mathf.Clamp((int)value.floatValue, 0, 255));
}
else if (paramDef.valueType == ExpressionParameters.ValueType.Float)
{
value.floatValue = EditorGUILayout.FloatField("Value", Mathf.Clamp(value.floatValue, -1f, 1f));
}
else if(paramDef.valueType == ExpressionParameters.ValueType.Bool)
{
value.floatValue = 1f;
}
}
else
{
EditorGUI.BeginDisabledGroup(true);
value.floatValue = EditorGUILayout.FloatField("Value", value.floatValue);
EditorGUI.EndDisabledGroup();
}
}
}
ExpressionParameters.Parameter FindExpressionParameterDef(string name)
{
if (activeDescriptor == null || string.IsNullOrEmpty(name))
return null;
//Find
int length = GetExpressionParametersCount();
for(int i=0; i<length; i++)
{
var item = GetExpressionParameter(i);
if (item != null && item.name == name)
return item;
}
return null;
}
}
|