-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathImporterFeaturesWindow.cs
359 lines (309 loc) · 15.5 KB
/
ImporterFeaturesWindow.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
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
/*
* Copyright (C) 2021 Victor Soupday
* This file is part of CC_Unity_Tools <https://github.com/soupday/CC_Unity_Tools>
*
* CC_Unity_Tools is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CC_Unity_Tools is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CC_Unity_Tools. If not, see <https://www.gnu.org/licenses/>.
*/
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEditor.Sprites;
using UnityEngine;
using UnityEngine.UIElements;
namespace Reallusion.Import
{
public class ImporterFeaturesWindow : EditorWindow
{
static ImporterFeaturesWindow importerFeaturesWindow = null;
static long lastClosedTime;
private CharacterInfo contextCharacter;
private CharacterInfo originalCharacter;
bool massProcessingValidate = false;
bool flagChanged = false;
private ImporterWindow importerWindow;
private Styles windowStyles;
private float DROPDOWN_WIDTH = 260f;
private float INITIAL_DROPDOWN_HEIGHT = 160f;
private float LABEL_WIDTH = 200f;
private float SECTION_INDENT = 8f;
private float SUB_SECTION_INDENT = 18f;
void OnEnable()
{
AssemblyReloadEvents.beforeAssemblyReload += Close;
hideFlags = HideFlags.DontSave;
}
void OnDisable()
{
AssemblyReloadEvents.beforeAssemblyReload -= Close;
importerFeaturesWindow = null;
}
void MassProcessingWindowValidate()
{
if (MassProcessingWindow.massProcessingWindow != null)
{
if (originalCharacter != null)
{
CharacterInfo workingCharacter = MassProcessingWindow.massProcessingWindow.workingList.Where(x => x.guid == contextCharacter.guid).FirstOrDefault();
workingCharacter.settingsChanged = contextCharacter.ShaderFlags != originalCharacter.ShaderFlags;
MassProcessingWindow.massProcessingWindow.FilterDisplayedList();
}
}
}
public static bool ShowAtPosition(Rect buttonRect, CharacterInfo contextChar = null)
{
long nowMilliSeconds = System.DateTime.Now.Ticks / System.TimeSpan.TicksPerMillisecond;
bool justClosed = nowMilliSeconds < lastClosedTime + 50;
if (!justClosed)
{
Event.current.Use();
if (importerFeaturesWindow == null)
importerFeaturesWindow = ScriptableObject.CreateInstance<ImporterFeaturesWindow>();
else
{
importerFeaturesWindow.Cancel();
return false;
}
importerFeaturesWindow.Init(buttonRect, contextChar);
return true;
}
return false;
}
void Init(Rect buttonRect, CharacterInfo contextChar = null)
{
// Has to be done before calling Show / ShowWithMode
buttonRect = GUIUtility.GUIToScreenRect(buttonRect);
importerWindow = ImporterWindow.Current;
if (contextChar != null)
{
contextCharacter = contextChar;
originalCharacter = ImporterWindow.ValidCharacters.Where(x => x.guid == contextChar.guid).FirstOrDefault();
massProcessingValidate = true;
}
else
{
contextCharacter = importerWindow.Character;
}
Vector2 windowSize = new Vector2(DROPDOWN_WIDTH, INITIAL_DROPDOWN_HEIGHT);
ShowAsDropDown(buttonRect, windowSize);
}
void Cancel()
{
Close();
GUI.changed = true;
GUIUtility.ExitGUI();
}
public class Styles
{
public GUIStyle listEvenBg;
public GUIStyle listOddBg;
public GUIStyle listLabel;
public Styles()
{
listEvenBg = new GUIStyle("ObjectPickerResultsOdd");
listEvenBg.fontStyle = FontStyle.Normal;
listOddBg = new GUIStyle("ObjectPickerResultsEven");
listOddBg.fontStyle = FontStyle.Normal;
listLabel = new GUIStyle("label");
listLabel.fontSize = 12;
listLabel.fontStyle = FontStyle.Italic;
}
}
void OnGUI()
{
if (windowStyles == null) windowStyles = new Styles();
int line = 0; // used to determine the background tint of alternate lines to avoid a block of solid color
flagChanged = false;
GUILayout.BeginVertical();
// manipulate the "[Flags]enum ShaderFeatures" with condidions on what flags are available
// due to pipleine version and available add-ons such as magica cloth or dynamic bone
// much more flexible than EditorGUILayout.EnumFlagsField
DrawLabelLine(line++, "Material Shader Features:");
if (DrawFlagSelectionLine(line++, CharacterInfo.ShaderFeatureFlags.Tessellation, "", SECTION_INDENT))
flagChanged = true;
if (DrawFlagSelectionLine(line++, CharacterInfo.ShaderFeatureFlags.WrinkleMaps, "", SECTION_INDENT))
flagChanged = true;
DrawLabelLine(line++, "Character Physics:");
// Cloth Physics
if (DrawFlagSelectionLine(line++, CharacterInfo.ShaderFeatureFlags.ClothPhysics, "Enable Cloth Physics", SECTION_INDENT))
flagChanged = true;
if (importerWindow.MagicaCloth2Available) // cloth alternatives available so enable non default selections
{
if (contextCharacter.ShaderFlags.HasFlag(CharacterInfo.ShaderFeatureFlags.ClothPhysics))
{
if (DrawFlagSelectionLine(line++, CharacterInfo.ShaderFeatureFlags.UnityClothPhysics, "Unity Cloth", SUB_SECTION_INDENT, CharacterInfo.clothGroup))
flagChanged = true;
if (importerWindow.MagicaCloth2Available)
{
if (DrawFlagSelectionLine(line++, CharacterInfo.ShaderFeatureFlags.MagicaCloth, "Magica Cloth 2", SUB_SECTION_INDENT, CharacterInfo.clothGroup))
flagChanged = true;
}
}
}
// Hair Physics
if (DrawFlagSelectionLine(line++, CharacterInfo.ShaderFeatureFlags.HairPhysics, "Enable Hair Physics", SECTION_INDENT))
flagChanged = true;
if (importerWindow.DynamicBoneAvailable || importerWindow.MagicaCloth2Available) // cloth/bone alternatives available so enable non default selections
{
if (contextCharacter.ShaderFlags.HasFlag(CharacterInfo.ShaderFeatureFlags.HairPhysics))
{
if (DrawFlagSelectionLine(line++, CharacterInfo.ShaderFeatureFlags.UnityClothHairPhysics, "Unity Hair Physics", SUB_SECTION_INDENT, CharacterInfo.hairGroup))
flagChanged = true;
if (DrawFlagSelectionLine(line++, CharacterInfo.ShaderFeatureFlags.MagicaClothHairPhysics, "Magica Cloth 2 Hair Physics", SUB_SECTION_INDENT, CharacterInfo.hairGroup))
flagChanged = true;
/*
if (importerWindow.DynamicBoneAvailable)
{
if (DrawFlagSelectionLine(line++, CharacterInfo.ShaderFeatureFlags.SpringBoneHair, "Dynamic Bone Springbones", SUB_SECTION_INDENT, CharacterInfo.hairGroup))
flagChanged = true;
}
if (importerWindow.MagicaCloth2Available)
{
if (DrawFlagSelectionLine(line++, CharacterInfo.ShaderFeatureFlags.MagicaBone, "Magica Bone Springbones", SUB_SECTION_INDENT, CharacterInfo.hairGroup))
flagChanged = true;
}
*/
}
}
// Spring Bone Physics
if (importerWindow.DynamicBoneAvailable || importerWindow.MagicaCloth2Available)
{
if (DrawFlagSelectionLine(line++, CharacterInfo.ShaderFeatureFlags.SpringBonePhysics, "Enable Spring Bone Physics", SECTION_INDENT))
flagChanged = true;
if (contextCharacter.ShaderFlags.HasFlag(CharacterInfo.ShaderFeatureFlags.SpringBonePhysics))
{
if (importerWindow.MagicaCloth2Available)
{
if (DrawFlagSelectionLine(line++, CharacterInfo.ShaderFeatureFlags.MagicaBone, "Magica Bone Springbones", SUB_SECTION_INDENT, CharacterInfo.springGroup))
flagChanged = true;
}
if (importerWindow.DynamicBoneAvailable)
{
if (DrawFlagSelectionLine(line++, CharacterInfo.ShaderFeatureFlags.SpringBoneHair, "Dynamic Bone Springbones", SUB_SECTION_INDENT, CharacterInfo.springGroup))
flagChanged = true;
}
}
}
DrawLabelLine(line++, "");
if (Event.current.type == EventType.Repaint)
{
minSize = new Vector2(DROPDOWN_WIDTH, GUILayoutUtility.GetLastRect().yMax);
}
if (massProcessingValidate && flagChanged)
{
MassProcessingWindowValidate();
}
}
private bool DrawFlagSelectionLine(int line, CharacterInfo.ShaderFeatureFlags flag, string overrideLabel = "", float indent = 0f, CharacterInfo.ShaderFeatureFlags [] radioGroup = null)
{
GUILayout.BeginHorizontal(GetLineStyle(line));
GUILayout.Space(indent);
bool flagVal = contextCharacter.ShaderFlags.HasFlag(flag);
GUILayout.Label(new GUIContent(string.IsNullOrEmpty(overrideLabel) ? flag.ToString() : overrideLabel, ""), GUILayout.Width(LABEL_WIDTH));
EditorGUI.BeginChangeCheck();
flagVal = GUILayout.Toggle(flagVal, "");
if (EditorGUI.EndChangeCheck())
{
if (radioGroup != null)
SetFeatureFlagInGroup(flag, radioGroup);
else
SetFeatureFlag(flag, flagVal);
return true;
}
GUILayout.EndHorizontal();
return false;
}
private void DrawLabelLine(int line, string label)
{
GUILayout.BeginHorizontal(GetLineStyle(line));
GUILayout.Label(label, windowStyles.listLabel);
GUILayout.EndHorizontal();
}
private GUIStyle GetLineStyle(int itemIndex)
{
if (windowStyles == null) windowStyles = new Styles();
return itemIndex % 2 > 0 ? windowStyles.listEvenBg : windowStyles.listOddBg;
}
private void SetFeatureFlag(CharacterInfo.ShaderFeatureFlags flag, bool value)
{
if (value)
{
if (!contextCharacter.ShaderFlags.HasFlag(flag))
{
contextCharacter.ShaderFlags |= flag; // toggle changed to ON => bitwise OR to add flag
contextCharacter.EnsureDefaultsAreSet(flag);
}
}
else
{
if (contextCharacter.ShaderFlags.HasFlag(flag))
{
contextCharacter.ShaderFlags ^= flag; // toggle changed to OFF => bitwise XOR to remove flag
// if the group flag is being unset then all the 'radio group' entries should be unset too
switch (flag)
{
case CharacterInfo.ShaderFeatureFlags.ClothPhysics:
{
foreach (CharacterInfo.ShaderFeatureFlags groupFlag in CharacterInfo.clothGroup)
{
if (contextCharacter.ShaderFlags.HasFlag(groupFlag))
contextCharacter.ShaderFlags ^= groupFlag;
}
/*
if (contextCharacter.ShaderFlags.HasFlag(CharacterInfo.ShaderFeatureFlags.MagicaCloth))
contextCharacter.ShaderFlags ^= CharacterInfo.ShaderFeatureFlags.MagicaCloth;
if (contextCharacter.ShaderFlags.HasFlag(CharacterInfo.ShaderFeatureFlags.UnityClothPhysics))
contextCharacter.ShaderFlags ^= CharacterInfo.ShaderFeatureFlags.UnityClothPhysics;
*/
break;
}
case CharacterInfo.ShaderFeatureFlags.HairPhysics:
{
foreach (CharacterInfo.ShaderFeatureFlags groupFlag in CharacterInfo.hairGroup)
{
if (contextCharacter.ShaderFlags.HasFlag(groupFlag))
contextCharacter.ShaderFlags ^= groupFlag;
}
/*
if (contextCharacter.ShaderFlags.HasFlag(CharacterInfo.ShaderFeatureFlags.MagicaBone))
contextCharacter.ShaderFlags ^= CharacterInfo.ShaderFeatureFlags.MagicaBone;
if (contextCharacter.ShaderFlags.HasFlag(CharacterInfo.ShaderFeatureFlags.SpringBoneHair))
contextCharacter.ShaderFlags ^= CharacterInfo.ShaderFeatureFlags.SpringBoneHair;
if (contextCharacter.ShaderFlags.HasFlag(CharacterInfo.ShaderFeatureFlags.UnityClothHairPhysics))
contextCharacter.ShaderFlags ^= CharacterInfo.ShaderFeatureFlags.UnityClothHairPhysics;
*/
break;
}
case CharacterInfo.ShaderFeatureFlags.SpringBonePhysics:
{
foreach (CharacterInfo.ShaderFeatureFlags groupFlag in CharacterInfo.springGroup)
{
if (contextCharacter.ShaderFlags.HasFlag(groupFlag))
contextCharacter.ShaderFlags ^= groupFlag;
}
break;
}
}
}
}
}
private void SetFeatureFlagInGroup(CharacterInfo.ShaderFeatureFlags flag, CharacterInfo.ShaderFeatureFlags[] radioGroup)
{
foreach (CharacterInfo.ShaderFeatureFlags groupFlag in radioGroup)
{
SetFeatureFlag(groupFlag, groupFlag.Equals(flag));
}
}
}
}