-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathVRCFTGenerator_ParameterUtility.cs
211 lines (177 loc) · 8.33 KB
/
VRCFTGenerator_ParameterUtility.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
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
// Small Helper Class for VRC Parameters
// SPDX-License-Identifier: MIT
// Copyright 2022 Razgriz/Cam
// Cam — 08/02/2022
// it took me like 5 minutes
// i dont even want credit its w/e
// just yeet it wherever you want
#if UNITY_EDITOR
#if VRC_SDK_VRCSDK3
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.Linq;
using VRCFTGenerator.AnimatorAsCode.V0;
using VRC.SDK3.Avatars.Components;
using VRC.SDK3.Avatars.ScriptableObjects;
namespace Raz.VRCFTGenerator
{
public static class VRCFTGenerator_ParameterUtility
{
public static bool ParameterExists(VRCExpressionParameters.Parameter parameter, VRCAvatarDescriptor descriptor, bool matchType = true)
{
VRCExpressionParameters.Parameter found = descriptor.expressionParameters.FindParameter(parameter.name);
if (found == null)
return false;
return matchType ? found.valueType == parameter.valueType : true;
}
public static bool ParameterExists(string parameterName, VRCAvatarDescriptor descriptor)
{
return descriptor.expressionParameters.FindParameter(parameterName) != null;
}
public static VRCExpressionParameters.Parameter GetParameterIfExists(VRCExpressionParameters.Parameter parameter, VRCAvatarDescriptor descriptor, bool matchType = true)
{
VRCExpressionParameters.Parameter found = descriptor.expressionParameters.FindParameter(parameter.name);
if (found == null)
return null;
if (matchType && found.valueType == parameter.valueType)
return found;
return null;
}
public static VRCExpressionParameters.Parameter GetParameterIfExists(string parameterName, VRCAvatarDescriptor descriptor)
{
return descriptor.expressionParameters.FindParameter(parameterName);
}
public static void SetDefaultParameterValue(string parameterName, float defaultFloat, VRCAvatarDescriptor descriptor)
{
if (descriptor.expressionParameters.FindParameter(parameterName) == null)
return;
VRCExpressionParameters.Parameter[] parameters = descriptor.expressionParameters.parameters;
for (int i = 0; i < parameters.Length; i++)
{
if (parameters[i].name.Equals(parameterName))
{
parameters[i].defaultValue = Mathf.Clamp(defaultFloat, -1f, 1f);
break;
}
}
EditorUtility.SetDirty(descriptor.expressionParameters);
descriptor.expressionParameters.parameters = parameters;
}
public static void SetDefaultParameterValue(string parameterName, int defaultInt, VRCAvatarDescriptor descriptor)
{
if (descriptor.expressionParameters.FindParameter(parameterName) == null)
return;
VRCExpressionParameters.Parameter[] parameters = descriptor.expressionParameters.parameters;
for (int i = 0; i < parameters.Length; i++)
{
if (parameters[i].name.Equals(parameterName)) {
parameters[i].defaultValue = Mathf.Clamp(defaultInt, 0, 255);
break;
}
}
EditorUtility.SetDirty(descriptor.expressionParameters);
descriptor.expressionParameters.parameters = parameters;
}
public static void SetDefaultParameterValue(string parameterName, bool defaultBool, VRCAvatarDescriptor descriptor)
{
if (descriptor.expressionParameters.FindParameter(parameterName) == null)
return;
VRCExpressionParameters.Parameter[] parameters = descriptor.expressionParameters.parameters;
for (int i = 0; i < parameters.Length; i++)
{
if (parameters[i].name.Equals(parameterName))
{
parameters[i].defaultValue = defaultBool ? 1f : 0f;
break;
}
}
EditorUtility.SetDirty(descriptor.expressionParameters);
descriptor.expressionParameters.parameters = parameters;
}
public static void ChangeParameterType(string parameterName, VRCExpressionParameters.ValueType newType, VRCAvatarDescriptor descriptor)
{
VRCExpressionParameters.Parameter parameter = descriptor.expressionParameters.FindParameter(parameterName);
if (parameter == null)
return;
VRCExpressionParameters.Parameter[] parameters = descriptor.expressionParameters.parameters;
for (int i = 0; i < parameters.Length; i++) {
if (parameters[i].name.Equals(parameterName)) {
parameters[i].valueType = newType;
break;
}
}
EditorUtility.SetDirty(descriptor.expressionParameters);
descriptor.expressionParameters.parameters = parameters;
}
public static void SetSaved(string parameterName, bool saved, VRCAvatarDescriptor descriptor)
{
if (descriptor.expressionParameters.FindParameter(parameterName) == null)
return;
VRCExpressionParameters.Parameter[] parameters = descriptor.expressionParameters.parameters;
for (int i = 0; i < parameters.Length; i++)
{
if (parameters[i].name.Equals(parameterName))
{
parameters[i].saved = saved;
break;
}
}
EditorUtility.SetDirty(descriptor.expressionParameters);
descriptor.expressionParameters.parameters = parameters;
}
public static void RemoveParameter(string parameterName, VRCAvatarDescriptor descriptor)
{
if (descriptor.expressionParameters.FindParameter(parameterName) == null)
return;
EditorUtility.SetDirty(descriptor.expressionParameters);
descriptor.expressionParameters.parameters = descriptor.expressionParameters.parameters
.Where(p => !p.name.Equals(parameterName))
.ToArray();
}
public static void AddParameter(string parameterName, VRCExpressionParameters.ValueType valueType, int defaultInt, bool saved, VRCAvatarDescriptor descriptor)
{
VRCExpressionParameters.Parameter newParameter = new VRCExpressionParameters.Parameter() {
name = parameterName,
defaultValue = Mathf.Clamp(defaultInt, 0, 255),
saved = saved,
valueType = valueType
};
EditorUtility.SetDirty(descriptor.expressionParameters);
descriptor.expressionParameters.parameters = descriptor.expressionParameters.parameters
.Append(newParameter)
.ToArray();
}
public static void AddParameter(string parameterName, VRCExpressionParameters.ValueType valueType, float defaultFloat, bool saved, VRCAvatarDescriptor descriptor)
{
VRCExpressionParameters.Parameter newParameter = new VRCExpressionParameters.Parameter()
{
name = parameterName,
defaultValue = Mathf.Clamp(defaultFloat, -1f, 1f),
saved = saved,
valueType = valueType
};
EditorUtility.SetDirty(descriptor.expressionParameters);
descriptor.expressionParameters.parameters = descriptor.expressionParameters.parameters
.Append(newParameter)
.ToArray();
}
public static void AddParameter(string parameterName, VRCExpressionParameters.ValueType valueType, bool defaultBool, bool saved, VRCAvatarDescriptor descriptor)
{
VRCExpressionParameters.Parameter newParameter = new VRCExpressionParameters.Parameter()
{
name = parameterName,
defaultValue = defaultBool ? 1f : 0f,
saved = saved,
valueType = valueType
};
EditorUtility.SetDirty(descriptor.expressionParameters);
descriptor.expressionParameters.parameters = descriptor.expressionParameters.parameters
.Append(newParameter)
.ToArray();
}
}
}
#endif
#endif