forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParticleSystemCurveEditor.cs
More file actions
817 lines (715 loc) · 28.7 KB
/
Copy pathParticleSystemCurveEditor.cs
File metadata and controls
817 lines (715 loc) · 28.7 KB
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
internal class ParticleSystemCurveEditor
{
private List<CurveData> m_AddedCurves;
private CurveEditor m_CurveEditor;
static CurveEditorSettings m_CurveEditorSettings = new CurveEditorSettings();
private Color[] m_Colors;
private List<Color> m_AvailableColors;
private DoubleCurvePresetsContentsForPopupWindow m_DoubleCurvePresets;
// Presets
public const float k_PresetsHeight = 30f;
internal class Styles
{
public GUIStyle curveEditorBackground = "CurveEditorBackground";
public GUIStyle curveSwatch = "PopupCurveEditorSwatch";
public GUIStyle curveSwatchArea = "PopupCurveSwatchBackground";
public GUIStyle yAxisHeader = new GUIStyle(ParticleSystemStyles.Get().label);
public GUIContent optimizeCurveText = EditorGUIUtility.TrTextContent("Optimize", "Click to optimize curve. Optimized curves are defined by having at most 3 keys, with a key at both ends, and do not support loop or ping pong wrapping.");
public GUIContent removeCurveText = EditorGUIUtility.TrTextContent("Remove", "Remove selected curve(s)");
public GUIContent curveLibraryPopup = EditorGUIUtility.TrTextContent("", "Open curve library");
public GUIContent presetTooltip = new GUIContent();
}
internal static Styles s_Styles;
public class CurveData
{
// Can be single curve (using m_Max) or a region (using m_Min and m_Max)
public SerializedProperty m_Max, m_Min;
public bool m_SignedRange;
public Color m_Color;
public string m_UniqueName;
public GUIContent m_DisplayName;
public CurveWrapper.GetAxisScalarsCallback m_GetAxisScalarsCallback;
public CurveWrapper.SetAxisScalarsCallback m_SetAxisScalarsCallback;
public int m_MaxId, m_MinId;
public bool m_Visible;
private static int s_IdCounter;
// Region if min and max is valid, Curve if min is null
public CurveData(string name, GUIContent displayName, SerializedProperty min, SerializedProperty max, Color color, bool signedRange,
CurveWrapper.GetAxisScalarsCallback getAxisScalars, CurveWrapper.SetAxisScalarsCallback setAxisScalars, bool visible)
{
m_UniqueName = name;
m_DisplayName = displayName;
m_SignedRange = signedRange;
m_Min = min;
m_Max = max;
if (m_Min != null)
m_MinId = ++s_IdCounter;
if (m_Max != null)
m_MaxId = ++s_IdCounter;
m_Color = color;
m_GetAxisScalarsCallback = getAxisScalars;
m_SetAxisScalarsCallback = setAxisScalars;
m_Visible = visible;
if (m_Max == null || m_MaxId == 0)
{
Debug.LogError("Max curve should always be valid! (Min curve can be null)");
}
}
public bool IsRegion()
{
return m_Min != null;
}
}
public void OnDisable()
{
m_CurveEditor.OnDisable();
Undo.undoRedoPerformed -= UndoRedoPerformed;
}
public void OnDestroy()
{
m_DoubleCurvePresets.GetPresetLibraryEditor().UnloadUsedLibraries();
}
public void Refresh()
{
ContentChanged();
UnityEditorInternal.AnimationCurvePreviewCache.ClearCache();
}
public void Init()
{
if (m_AddedCurves != null)
return;
m_AddedCurves = new List<CurveData>();
// Colors
m_Colors = new Color[]
{
new Color(255 / 255f, 158 / 255f, 33 / 255f), // orange
new Color(223 / 255f, 54 / 255f, 148 / 255f), // purple
new Color(0f, 175 / 255f, 255 / 255f), // blue
new Color(255 / 255f, 235 / 255f, 0), // yellow
new Color(50 / 255f, 255 / 255f, 68 / 255f), // green
new Color(250 / 255f, 0f, 0f), // red (this is the first color used)
};
m_AvailableColors = new List<Color>(m_Colors);
// Curve Editor
m_CurveEditorSettings.useFocusColors = true;
m_CurveEditorSettings.showAxisLabels = false;
m_CurveEditorSettings.hRangeMin = 0.0f;
m_CurveEditorSettings.vRangeMin = 0.0F;
m_CurveEditorSettings.vRangeMax = 1.0f;
m_CurveEditorSettings.hRangeMax = 1.0F;
m_CurveEditorSettings.vSlider = false;
m_CurveEditorSettings.hSlider = false;
m_CurveEditorSettings.showWrapperPopups = true;
m_CurveEditorSettings.rectangleToolFlags = CurveEditorSettings.RectangleToolFlags.MiniRectangleTool;
m_CurveEditorSettings.hTickLabelOffset = 5;
m_CurveEditorSettings.allowDraggingCurvesAndRegions = true;
m_CurveEditorSettings.allowDeleteLastKeyInCurve = false;
TickStyle hTS = new TickStyle();
hTS.tickColor.color = new Color(0.0f, 0.0f, 0.0f, 0.2f);
hTS.distLabel = 30;
hTS.stubs = false;
hTS.centerLabel = true;
m_CurveEditorSettings.hTickStyle = hTS;
TickStyle vTS = new TickStyle();
vTS.tickColor.color = new Color(0.0f, 0.0f, 0.0f, 0.2f);
vTS.distLabel = 20;
vTS.stubs = false;
vTS.centerLabel = true;
m_CurveEditorSettings.vTickStyle = vTS;
m_CurveEditor = new CurveEditor(new Rect(0, 0, 1000, 100), CreateCurveWrapperArray(), false);
m_CurveEditor.settings = m_CurveEditorSettings;
m_CurveEditor.leftmargin = 40;
m_CurveEditor.rightmargin = m_CurveEditor.topmargin = m_CurveEditor.bottommargin = 25;
m_CurveEditor.SetShownHRangeInsideMargins(m_CurveEditorSettings.hRangeMin, m_CurveEditorSettings.hRangeMax);
m_CurveEditor.SetShownVRangeInsideMargins(m_CurveEditorSettings.vRangeMin, m_CurveEditorSettings.hRangeMax);
m_CurveEditor.ignoreScrollWheelUntilClicked = false;
Undo.undoRedoPerformed += UndoRedoPerformed;
}
void UndoRedoPerformed()
{
ContentChanged();
}
void UpdateRangeBasedOnShownCurves()
{
bool hasSignedRange = false;
for (int i = 0; i < m_AddedCurves.Count; i++)
hasSignedRange |= m_AddedCurves[i].m_SignedRange;
float newMinRange = hasSignedRange ? -1.0F : 0.0F;
if (newMinRange != m_CurveEditorSettings.vRangeMin)
{
m_CurveEditorSettings.vRangeMin = newMinRange;
m_CurveEditor.settings = m_CurveEditorSettings;
m_CurveEditor.SetShownVRangeInsideMargins(m_CurveEditorSettings.vRangeMin, m_CurveEditorSettings.hRangeMax);
}
}
// Public interface
public bool IsAdded(SerializedProperty min, SerializedProperty max)
{
return FindIndex(min, max) != -1;
}
public bool IsAdded(SerializedProperty max)
{
return FindIndex(null, max) != -1;
}
public void AddCurve(CurveData curveData)
{
Add(curveData);
}
public void RemoveCurve(SerializedProperty max)
{
RemoveCurve(null, max);
}
public void RemoveCurve(SerializedProperty min, SerializedProperty max)
{
if (Remove(FindIndex(min, max)))
{
ContentChanged();
UpdateRangeBasedOnShownCurves();
}
}
public Color GetCurveColor(SerializedProperty max)
{
int index = FindIndex(max);
if (index >= 0 && index < m_AddedCurves.Count)
{
return m_AddedCurves[index].m_Color;
}
return new Color(0.8f, 0.8f, 0.8f, 0.7f);
}
public void AddCurveDataIfNeeded(string curveName, CurveData curveData)
{
// We check if a curve was added by checking our inspector state if a color was set for curveName
Vector3 c = SessionState.GetVector3(curveName, Vector3.zero);
if (c != Vector3.zero)
{
Color color = new Color(c.x, c.y, c.z);
curveData.m_Color = color;
AddCurve(curveData);
// Remove color from available list
for (int i = 0; i < m_AvailableColors.Count; ++i)
{
if (SameColor(m_AvailableColors[i], color))
{
m_AvailableColors.RemoveAt(i);
break;
}
}
}
}
public void SetVisible(SerializedProperty curveProp, bool visible)
{
int index = FindIndex(curveProp);
if (index >= 0)
{
m_AddedCurves[index].m_Visible = visible;
}
}
static bool SameColor(Color c1, Color c2)
{
return Mathf.Abs(c1.r - c2.r) < 0.01f &&
Mathf.Abs(c1.g - c2.g) < 0.01f &&
Mathf.Abs(c1.b - c2.b) < 0.01f;
}
// Private
//---------
private int FindIndex(SerializedProperty prop)
{
return FindIndex(null, prop);
}
private int FindIndex(SerializedProperty min, SerializedProperty max)
{
if (max == null)
return -1;
if (min == null)
{
// Just check max
for (int i = 0; i < m_AddedCurves.Count; ++i)
if (m_AddedCurves[i].m_Max == max)
return i;
}
else
{
for (int i = 0; i < m_AddedCurves.Count; ++i)
if (m_AddedCurves[i].m_Max == max && m_AddedCurves[i].m_Min == min)
return i;
}
return -1;
}
private void Add(CurveData cd)
{
m_CurveEditor.SelectNone(); // ensures that newly added curves are moved to front (selected keys will stay on top)
m_AddedCurves.Add(cd);
ContentChanged();
// Cache info
SessionState.SetVector3(cd.m_UniqueName, new Vector3(cd.m_Color.r, cd.m_Color.g, cd.m_Color.b));
UpdateRangeBasedOnShownCurves();
}
private bool Remove(int index)
{
if (index >= 0 && index < m_AddedCurves.Count)
{
// Make color available again
Color color = m_AddedCurves[index].m_Color;
m_AvailableColors.Add(color);
// Remove from inspector state
string curveName = m_AddedCurves[index].m_UniqueName;
SessionState.EraseVector3(curveName);
// Remove from list
m_AddedCurves.RemoveAt(index);
// When no added curves reset available colors
if (m_AddedCurves.Count == 0)
{
//if (m_AvailableColors.Count != m_Colors.Length)
// Debug.LogError ("Color count mismatch : " + m_AvailableColors.Count + " != " + m_Colors.Length);
m_AvailableColors = new List<Color>(m_Colors);
}
return true;
}
Debug.Log("Invalid index in ParticleSystemCurveEditor::Remove");
return false;
}
private void RemoveTopMost()
{
int topMostCurveID;
if (m_CurveEditor.GetTopMostCurveID(out topMostCurveID))
{
for (int j = 0; j < m_AddedCurves.Count; ++j)
{
CurveData cd = m_AddedCurves[j];
if (cd.m_MaxId == topMostCurveID || cd.m_MinId == topMostCurveID)
{
Remove(j);
ContentChanged();
UpdateRangeBasedOnShownCurves();
return;
}
}
}
}
private void RemoveSelected()
{
bool anyRemoved = false;
List<CurveSelection> selection = m_CurveEditor.selectedCurves;
for (int i = 0; i < selection.Count; ++i)
{
int curveId = selection[i].curveID;
for (int j = 0; j < m_AddedCurves.Count; ++j)
{
CurveData cd = m_AddedCurves[j];
if (cd.m_MaxId == curveId || cd.m_MinId == curveId)
{
anyRemoved |= Remove(j);
break;
}
}
}
if (anyRemoved)
{
ContentChanged();
UpdateRangeBasedOnShownCurves();
}
m_CurveEditor.SelectNone();
}
public Color GetAvailableColor()
{
// If no available colors left just use same colors again...
if (m_AvailableColors.Count == 0)
m_AvailableColors = new List<Color>(m_Colors);
int i = m_AvailableColors.Count - 1;
Color color = m_AvailableColors[i];
m_AvailableColors.RemoveAt(i);
return color;
}
public void OnGUI(Rect rect)
{
Init();
if (s_Styles == null)
s_Styles = new Styles();
// Sizes
Rect curveEditorRect = new Rect(rect.x, rect.y, rect.width, rect.height - k_PresetsHeight);
Rect presetRect = new Rect(rect.x, rect.y + curveEditorRect.height, rect.width, k_PresetsHeight);
// Background
GUI.Label(curveEditorRect, GUIContent.none, s_Styles.curveEditorBackground);
// Setup curve editor
if (Event.current.type == EventType.Repaint)
m_CurveEditor.rect = curveEditorRect;
// Clamp the y axis to 1,000,000 for the particle curve.
foreach (CurveWrapper cw in m_CurveEditor.animationCurves)
{
if (cw.getAxisUiScalarsCallback != null && cw.setAxisUiScalarsCallback != null)
{
Vector2 axisUiScalar = cw.getAxisUiScalarsCallback();
if (axisUiScalar.y > 1000000)
{
axisUiScalar.y = 1000000;
cw.setAxisUiScalarsCallback(axisUiScalar);
}
}
}
DoLabelForTopMostCurve(new Rect(rect.x + 4, rect.y, rect.width - 160, 20));
DoRemoveSelectedButton(new Rect(curveEditorRect.x, curveEditorRect.y, curveEditorRect.width, 24));
DoOptimizeCurveButton(new Rect(curveEditorRect.x, curveEditorRect.y, curveEditorRect.width, 24));
presetRect.x += 30;
presetRect.width -= 2 * 30;
PresetCurveButtons(presetRect, rect);
m_CurveEditor.OnGUI();
SaveChangedCurves();
}
void DoLabelForTopMostCurve(Rect rect)
{
if (m_CurveEditor.IsDraggingCurveOrRegion() || m_CurveEditor.selectedCurves.Count <= 1)
{
int curveID;
if (m_CurveEditor.GetTopMostCurveID(out curveID))
{
for (int i = 0; i < m_AddedCurves.Count; ++i)
{
if (m_AddedCurves[i].m_MaxId == curveID || m_AddedCurves[i].m_MinId == curveID)
{
s_Styles.yAxisHeader.normal.textColor = m_AddedCurves[i].m_Color;
GUI.Label(rect, m_AddedCurves[i].m_DisplayName, s_Styles.yAxisHeader);
return;
}
}
}
}
}
void SetCurve(CurveWrapper cw, AnimationCurve curve)
{
Keyframe[] keys = new Keyframe[curve.keys.Length];
System.Array.Copy(curve.keys, keys, keys.Length);
cw.curve.keys = keys;
cw.changed = true; // Used in SaveChangedCurves () later in OnGUI
}
void SetTopMostCurve(DoubleCurve doubleCurve)
{
int topMostCurveID;
if (m_CurveEditor.GetTopMostCurveID(out topMostCurveID))
{
for (int j = 0; j < m_AddedCurves.Count; ++j)
{
CurveData cd = m_AddedCurves[j];
if (cd.m_MaxId == topMostCurveID || cd.m_MinId == topMostCurveID)
{
if (doubleCurve.signedRange == cd.m_SignedRange)
{
if (cd.m_MaxId > 0)
{
SetCurve(m_CurveEditor.GetCurveWrapperFromID(cd.m_MaxId), doubleCurve.maxCurve);
}
if (cd.m_MinId > 0)
{
SetCurve(m_CurveEditor.GetCurveWrapperFromID(cd.m_MinId), doubleCurve.minCurve);
}
}
else
{
Debug.LogWarning("Cannot assign a curves with different signed range");
}
}
}
}
}
DoubleCurve CreateDoubleCurveFromTopMostCurve()
{
int topMostCurveID;
if (m_CurveEditor.GetTopMostCurveID(out topMostCurveID))
{
for (int j = 0; j < m_AddedCurves.Count; ++j)
{
CurveData cd = m_AddedCurves[j];
if (cd.m_MaxId == topMostCurveID || cd.m_MinId == topMostCurveID)
{
AnimationCurve maxCurve = null, minCurve = null;
if (cd.m_Min != null)
minCurve = cd.m_Min.animationCurveValue;
if (cd.m_Max != null)
maxCurve = cd.m_Max.animationCurveValue;
return new DoubleCurve(minCurve, maxCurve, cd.m_SignedRange);
}
}
}
return null;
}
void PresetDropDown(Rect rect)
{
if (EditorGUI.DropdownButton(rect, EditorGUI.GUIContents.titleSettingsIcon, FocusType.Passive, EditorStyles.inspectorTitlebarText))
{
DoubleCurve doubleCurveToSaveAsPreset = CreateDoubleCurveFromTopMostCurve();
if (doubleCurveToSaveAsPreset != null)
{
InitDoubleCurvePresets();
if (m_DoubleCurvePresets != null)
{
m_DoubleCurvePresets.doubleCurveToSave = CreateDoubleCurveFromTopMostCurve();
// We prioritize left first for normal inspector layout (where the inspector is on the right)
PopupWindow.Show(rect, m_DoubleCurvePresets);
}
}
}
}
int m_LastTopMostCurveID = -1;
void InitDoubleCurvePresets()
{
int topMostCurveID;
if (m_CurveEditor.GetTopMostCurveID(out topMostCurveID))
{
if (m_DoubleCurvePresets == null || m_LastTopMostCurveID != topMostCurveID)
{
m_LastTopMostCurveID = topMostCurveID;
// Selection callback for library window
System.Action<DoubleCurve> presetSelectedCallback = delegate(DoubleCurve presetDoubleCurve)
{
SetTopMostCurve(presetDoubleCurve);
UnityEditorInternal.InternalEditorUtility.RepaintAllViews();
};
DoubleCurve doubleCurveToSaveAsPreset = CreateDoubleCurveFromTopMostCurve();
m_DoubleCurvePresets = new DoubleCurvePresetsContentsForPopupWindow(doubleCurveToSaveAsPreset, presetSelectedCallback);
m_DoubleCurvePresets.InitIfNeeded();
}
}
}
void PresetCurveButtons(Rect position, Rect curveEditorRect)
{
if (m_CurveEditor.animationCurves.Length == 0)
return;
InitDoubleCurvePresets();
if (m_DoubleCurvePresets == null)
return;
DoubleCurvePresetLibrary curveLibrary = m_DoubleCurvePresets.GetPresetLibraryEditor().GetCurrentLib();
const int maxNumPresets = 9;
int numPresets = (curveLibrary != null) ? curveLibrary.Count() : 0;
int showNumPresets = Mathf.Min(numPresets, maxNumPresets);
float swatchWidth = 30;
float swatchHeight = 15;
float spaceBetweenSwatches = 10;
float presetButtonsWidth = showNumPresets * swatchWidth + (showNumPresets - 1) * spaceBetweenSwatches;
float flexWidth = (position.width - presetButtonsWidth) * 0.5f;
// Preset swatch area
float curY = (position.height - swatchHeight) * 0.5f;
float curX = 3.0f;
if (flexWidth > 0)
curX = flexWidth;
PresetDropDown(new Rect(curX - 20 + position.x, curY + position.y, 16, 16));
GUI.BeginGroup(position);
Color curveColor = Color.white;
curveColor.a *= 0.6f;
for (int i = 0; i < showNumPresets; i++)
{
if (i > 0)
curX += spaceBetweenSwatches;
Rect swatchRect = new Rect(curX, curY, swatchWidth, swatchHeight);
if (swatchRect.xMax > position.width)
break;
s_Styles.presetTooltip.tooltip = curveLibrary.GetName(i);
if (GUI.Button(swatchRect, s_Styles.presetTooltip, GUIStyle.none))
{
DoubleCurve presetDoubleCurve = curveLibrary.GetPreset(i) as DoubleCurve;
if (presetDoubleCurve != null)
{
SetTopMostCurve(presetDoubleCurve);
m_CurveEditor.ClearSelection();
}
}
if (Event.current.type == EventType.Repaint)
curveLibrary.Draw(swatchRect, i);
curX += swatchWidth;
}
GUI.EndGroup();
}
// Polynomial curves have limitations on how they have to be authored.
// Since we don't enforce the layout, we have a button that enforces the curve layout instead.
void DoOptimizeCurveButton(Rect rect)
{
bool optimizeButtonShown = false;
Vector2 buttonSize = new Vector2(64, 18);
Rect buttonRect = new Rect(rect.xMax - 80 - buttonSize.x, rect.y + (rect.height - buttonSize.y) * 0.5f, buttonSize.x, buttonSize.y);
if (!m_CurveEditor.IsDraggingCurveOrRegion())
{
int numValidPolynomialCurve = 0;
List<CurveSelection> selection = m_CurveEditor.selectedCurves;
if (selection.Count > 0)
{
for (int j = 0; j < selection.Count; ++j)
{
CurveWrapper cw = m_CurveEditor.GetCurveWrapperFromSelection(selection[j]);
numValidPolynomialCurve += AnimationUtility.IsValidOptimizedPolynomialCurve(cw.curve) ? 1 : 0;
}
if (selection.Count != numValidPolynomialCurve)
{
optimizeButtonShown = true;
if (GUI.Button(buttonRect, s_Styles.optimizeCurveText))
{
for (int j = 0; j < selection.Count; ++j)
{
CurveWrapper cw = m_CurveEditor.GetCurveWrapperFromSelection(selection[j]);
if (!AnimationUtility.IsValidOptimizedPolynomialCurve(cw.curve))
{
// Reset wrap mode
cw.curve.preWrapMode = WrapMode.Clamp;
cw.curve.postWrapMode = WrapMode.Clamp;
cw.renderer.SetWrap(WrapMode.Clamp, WrapMode.Clamp);
AnimationUtility.ConstrainToPolynomialCurve(cw.curve);
cw.changed = true; // Used in SaveChangedCurves () later in OnGUI
}
}
m_CurveEditor.SelectNone();
}
}
}
else
{
// Check if top most curve can be optimized
int topMostCurveID;
if (m_CurveEditor.GetTopMostCurveID(out topMostCurveID))
{
CurveWrapper cw = m_CurveEditor.GetCurveWrapperFromID(topMostCurveID);
if (!AnimationUtility.IsValidOptimizedPolynomialCurve(cw.curve))
{
optimizeButtonShown = true;
if (GUI.Button(buttonRect, s_Styles.optimizeCurveText))
{
// Reset wrap mode
cw.curve.preWrapMode = WrapMode.Clamp;
cw.curve.postWrapMode = WrapMode.Clamp;
cw.renderer.SetWrap(WrapMode.Clamp, WrapMode.Clamp);
AnimationUtility.ConstrainToPolynomialCurve(cw.curve);
cw.changed = true; // Used in SaveChangedCurves () later in OnGUI
}
}
}
}
}
if (!optimizeButtonShown)
{
using (new EditorGUI.DisabledScope(true))
{
GUI.Button(buttonRect, s_Styles.optimizeCurveText);
}
}
}
void DoRemoveSelectedButton(Rect rect)
{
using (new EditorGUI.DisabledScope(m_CurveEditor.animationCurves.Length == 0))
{
Vector2 buttonSize = new Vector2(64, 18);
Rect clearCurvesRect = new Rect(rect.x + rect.width - buttonSize.x - 10, rect.y + (rect.height - buttonSize.y) * 0.5f, buttonSize.x, buttonSize.y);
if (GUI.Button(clearCurvesRect, s_Styles.removeCurveText))
{
if (m_CurveEditor.selectedCurves.Count > 0)
RemoveSelected();
else
RemoveTopMost();
}
}
}
void SaveCurve(SerializedProperty prop, CurveWrapper cw)
{
if (cw.curve.keys.Length == 1)
{
cw.renderer.SetCustomRange(0.0f, 1.0f);
cw.wrapColorMultiplier = Color.clear; // Hide wrapping when we only have 1 key
}
else
{
cw.renderer.SetCustomRange(0.0f, 0.0f);
cw.wrapColorMultiplier = cw.color;
}
prop.animationCurveValue = cw.curve;
cw.changed = false;
}
void SaveChangedCurves()
{
CurveWrapper[] curves = m_CurveEditor.animationCurves;
bool refreshPreviews = false;
for (int i = 0; i < curves.Length; ++i)
{
CurveWrapper cw = curves[i];
if (cw.changed)
{
for (int j = 0; j < m_AddedCurves.Count; ++j)
{
if (m_AddedCurves[j].m_MaxId == cw.id)
{
SaveCurve(m_AddedCurves[j].m_Max, cw);
break;
}
if (m_AddedCurves[j].IsRegion() && m_AddedCurves[j].m_MinId == cw.id)
{
SaveCurve(m_AddedCurves[j].m_Min, cw);
break;
}
}
refreshPreviews = true;
}
}
if (refreshPreviews)
{
UnityEditorInternal.AnimationCurvePreviewCache.ClearCache();
HandleUtility.Repaint();
}
}
CurveWrapper CreateCurveWrapper(SerializedProperty curve, int id, int regionId, Color color, bool signedRange,
CurveWrapper.GetAxisScalarsCallback getAxisScalarsCallback, CurveWrapper.SetAxisScalarsCallback setAxisScalarsCallback)
{
CurveWrapper cw = new CurveWrapper();
cw.id = id;
cw.regionId = regionId;
cw.color = color;
cw.renderer = new NormalCurveRenderer(curve.animationCurveValue);
cw.renderer.SetWrap(curve.animationCurveValue.preWrapMode, curve.animationCurveValue.postWrapMode);
if (cw.curve.keys.Length == 1)
{
cw.renderer.SetCustomRange(0.0f, 1.0f);
cw.wrapColorMultiplier = Color.clear; // Hide wrapping when we only have 1 key
}
else
{
cw.renderer.SetCustomRange(0.0f, 0.0f);
cw.wrapColorMultiplier = color;
}
cw.vRangeMin = signedRange ? -1.0F : 0.0F;
cw.getAxisUiScalarsCallback = getAxisScalarsCallback;
cw.setAxisUiScalarsCallback = setAxisScalarsCallback;
return cw;
}
CurveWrapper[] CreateCurveWrapperArray()
{
List<CurveWrapper> curveWrappers = new List<CurveWrapper>();
int regionCounter = 0;
for (int i = 0; i < m_AddedCurves.Count; i++)
{
CurveData cd = m_AddedCurves[i];
if (cd.m_Visible)
{
if (cd.m_Max != null && cd.m_Max.hasMultipleDifferentValues)
continue;
if (cd.m_Min != null && cd.m_Min.hasMultipleDifferentValues)
continue;
int regionId = -1;
if (cd.IsRegion())
regionId = ++regionCounter;
if (cd.m_Max != null)
{
curveWrappers.Add(CreateCurveWrapper(cd.m_Max, cd.m_MaxId, regionId, cd.m_Color, cd.m_SignedRange, cd.m_GetAxisScalarsCallback, cd.m_SetAxisScalarsCallback));
}
if (cd.m_Min != null)
{
curveWrappers.Add(CreateCurveWrapper(cd.m_Min, cd.m_MinId, regionId, cd.m_Color, cd.m_SignedRange, cd.m_GetAxisScalarsCallback, cd.m_SetAxisScalarsCallback));
}
}
}
return curveWrappers.ToArray();
}
void ContentChanged()
{
m_CurveEditor.animationCurves = CreateCurveWrapperArray();
m_CurveEditorSettings.showAxisLabels = m_CurveEditor.animationCurves.Length > 0;
}
}