-
Notifications
You must be signed in to change notification settings - Fork 877
Expand file tree
/
Copy pathVFXComponentBoard.cs
More file actions
1019 lines (869 loc) · 34.5 KB
/
Copy pathVFXComponentBoard.cs
File metadata and controls
1019 lines (869 loc) · 34.5 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
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Linq;
using System.Collections.Generic;
using System.Globalization;
using UnityEditor.Experimental;
using UnityEditor.Experimental.GraphView;
using UnityEditor.SceneManagement;
using UnityEditor.VFX.UIElements;
using UnityEngine;
using UnityEngine.VFX;
using UnityEngine.UIElements;
using PositionType = UnityEngine.UIElements.Position;
namespace UnityEditor.VFX.UI
{
static class BoardPreferenceHelper
{
public enum Board
{
blackboard,
componentBoard,
profilingBoard,
}
const string rectPreferenceFormat = "vfx-{0}-rect";
const string visiblePreferenceFormat = "vfx-{0}-visible";
public static bool IsVisible(Board board, bool defaultState)
{
return EditorPrefs.GetBool(string.Format(visiblePreferenceFormat, board), defaultState);
}
public static void SetVisible(Board board, bool value)
{
EditorPrefs.SetBool(string.Format(visiblePreferenceFormat, board), value);
}
public static Rect LoadPosition(Board board, Rect defaultPosition)
{
string str = EditorPrefs.GetString(string.Format(rectPreferenceFormat, board));
Rect blackBoardPosition = defaultPosition;
if (!string.IsNullOrEmpty(str))
{
var rectValues = str.Split(',');
if (rectValues.Length == 4)
{
float x, y, width, height;
if (float.TryParse(rectValues[0], NumberStyles.Float, CultureInfo.InvariantCulture, out x) &&
float.TryParse(rectValues[1], NumberStyles.Float, CultureInfo.InvariantCulture, out y) &&
float.TryParse(rectValues[2], NumberStyles.Float, CultureInfo.InvariantCulture, out width) &&
float.TryParse(rectValues[3], NumberStyles.Float, CultureInfo.InvariantCulture, out height))
{
blackBoardPosition = new Rect(x, y, width, height);
}
}
}
return blackBoardPosition;
}
public static void SavePosition(Board board, Rect r)
{
EditorPrefs.SetString(string.Format(rectPreferenceFormat, board), string.Format(CultureInfo.InvariantCulture, "{0},{1},{2},{3}", r.x, r.y, r.width, r.height));
}
public static void ValidatePosition(GraphElement element, VFXView view, Rect defaultPosition)
{
Rect viewrect = view.contentRect;
Rect rect = element.GetPosition();
bool changed = false;
Vector2 maxSizeInView = viewrect.max;
float newWidth = Mathf.Max(defaultPosition.width, Mathf.Min(rect.width, maxSizeInView.x));
float newHeight = Mathf.Max(defaultPosition.height, Mathf.Min(rect.height, maxSizeInView.y));
if (Mathf.Abs(newWidth - rect.width) > 1)
{
rect.width = newWidth;
changed = true;
}
if (Mathf.Abs(newHeight - rect.height) > 1)
{
rect.height = newHeight;
changed = true;
}
var xDiff = viewrect.xMax - rect.xMax;
if (xDiff < 0)
{
if (rect.x + xDiff >= 0)
{
rect.x += xDiff;
}
else
{
rect.width = Math.Max(defaultPosition.width, rect.width + xDiff);
}
changed = true;
}
var yDiff = viewrect.yMax - rect.yMax;
if (yDiff < 0)
{
if (rect.y + yDiff >= 0)
{
rect.y += yDiff;
}
else
{
rect.height = Math.Max(defaultPosition.height, rect.height + yDiff);
}
changed = true;
}
if (changed)
{
element.SetPosition(rect);
}
}
}
class VFXComponentBoard : GraphElement, IControlledElement<VFXViewController>, IVFXMovable, IVFXResizable
{
VFXViewController m_Controller;
Controller IControlledElement.controller
{
get { return m_Controller; }
}
public VFXViewController controller
{
get { return m_Controller; }
set
{
if (m_Controller != value)
{
if (m_Controller != null)
{
m_Controller.UnregisterHandler(this);
}
Clear();
m_Controller = value;
if (m_Controller != null)
{
m_Controller.RegisterHandler(this);
}
}
}
}
VFXView m_View;
VFXUIDebug m_DebugUI;
public VFXComponentBoard(VFXView view)
{
m_View = view;
var tpl = VFXView.LoadUXML("VFXComponentBoard");
tpl.CloneTree(contentContainer);
contentContainer.AddStyleSheetPath("VFXComponentBoard");
m_RootElement = this.Query<VisualElement>("component-container");
m_SubtitleIcon = this.Query<Image>("subTitle-icon");
m_Subtitle = this.Query<Label>("subTitleLabel");
m_SubtitleIcon.image = EditorGUIUtility.LoadIcon(EditorResources.iconsPath + "console.warnicon.sml.png");
m_Stop = this.Query<Button>("stop");
m_Stop.clickable.clicked += EffectStop;
m_Play = this.Query<Button>("play");
m_Play.clickable.clicked += EffectPlay;
m_Step = this.Query<Button>("step");
m_Step.clickable.clicked += EffectStep;
m_Restart = this.Query<Button>("restart");
m_Restart.clickable.clicked += EffectRestart;
m_PlayIcon = m_Play.Q<Image>("icon");
m_PlayRateSlider = this.Query<Slider>("play-rate-slider");
m_PlayRateSlider.lowValue = Mathf.Pow(VisualEffectControl.minSlider, 1 / VisualEffectControl.sliderPower);
m_PlayRateSlider.highValue = Mathf.Pow(VisualEffectControl.maxSlider, 1 / VisualEffectControl.sliderPower);
m_PlayRateSlider.RegisterValueChangedCallback(evt => OnEffectSlider(evt.newValue));
m_PlayRateField = this.Query<IntegerField>("play-rate-field");
m_PlayRateField.RegisterCallback<ChangeEvent<int>>(OnPlayRateField);
m_PlayRateMenu = this.Query<Button>("play-rate-menu");
m_PlayRateMenu.AddStyleSheetPathWithSkinVariant("VFXControls");
m_PlayRateMenu.clickable.clicked += OnPlayRateMenu;
m_ParticleCount = this.Query<Label>("particle-count");
Button button = this.Query<Button>("on-play-button");
button.clickable.clicked += () => SendEvent(VisualEffectAsset.PlayEventName);
button = this.Query<Button>("on-stop-button");
button.clickable.clicked += () => SendEvent(VisualEffectAsset.StopEventName);
m_EventsContainer = this.Query("events-container");
m_DebugModes = this.Query<Button>("debug-modes");
m_DebugModes.clickable.clicked += OnDebugModes;
m_RecordIcon = VFXView.LoadImage("d_Record");
m_RecordBoundsButton = this.Query<Button>("record");
m_RecordBoundsImage = m_RecordBoundsButton.Query<Image>("record-icon");
m_RecordBoundsImage.style.backgroundImage = m_RecordIcon;
m_RecordBoundsButton.clickable.clicked += OnRecordBoundsButton;
m_BoundsActionLabel = this.Query<Label>("bounds-label");
m_BoundsToolContainer = this.Query("bounds-tool-container");
m_BackgroundDefaultColor = m_BoundsToolContainer.style.backgroundColor;
m_SystemBoundsContainer = this.Query<VFXBoundsSelector>("system-bounds-container");
m_SystemBoundsContainer.RegisterCallback<MouseDownEvent>(OnMouseClickBoundsContainer);
m_ApplyBoundsButton = this.Query<Button>("apply-bounds-button");
m_ApplyBoundsButton.clickable.clicked += ApplyCurrentBounds;
Detach();
this.AddManipulator(new Dragger { clampToParentEdges = true });
capabilities |= Capabilities.Movable;
RegisterCallback<MouseDownEvent>(OnMouseClick);
// Prevent graphview from zooming in/out when using the mouse wheel over the component board
RegisterCallback<WheelEvent>(e => e.StopPropagation());
style.position = PositionType.Absolute;
SetPosition(BoardPreferenceHelper.LoadPosition(BoardPreferenceHelper.Board.componentBoard, defaultRect));
}
public void ValidatePosition()
{
BoardPreferenceHelper.ValidatePosition(this, m_View, defaultRect);
}
static readonly Rect defaultRect = new Rect(200, 100, 300, 300);
public override Rect GetPosition()
{
return new Rect(resolvedStyle.left, resolvedStyle.top, resolvedStyle.width, resolvedStyle.height);
}
public override void SetPosition(Rect newPos)
{
style.left = newPos.xMin;
style.top = newPos.yMin;
style.width = newPos.width;
style.height = newPos.height;
}
public void SetDebugMode(VFXUIDebug.Modes mode)
{
m_DebugUI?.SetDebugMode(mode, this);
}
void OnMouseClick(MouseDownEvent e)
{
m_View.SetBoardToFront(this);
}
void OnMouseClickBoundsContainer(MouseDownEvent e)
{
if (e.button == (int)MouseButton.LeftMouse)
{
foreach (var elem in m_SystemBoundsContainer.Children())
{
if (elem is VFXComponentBoardBoundsSystemUI systemBound)
systemBound.Unselect();
}
}
}
void OnPlayRateMenu()
{
GenericMenu menu = new GenericMenu();
foreach (var value in VisualEffectControl.setPlaybackValues)
{
menu.AddItem(EditorGUIUtility.TextContent(string.Format("{0}%", value)), false, SetPlayRate, value);
}
menu.DropDown(m_PlayRateMenu.worldBound);
}
void OnPlayRateField(ChangeEvent<int> e)
{
SetPlayRate(e.newValue);
}
void SetPlayRate(object value)
{
if (m_AttachedComponent == null)
return;
float rate = (float)((int)value) * VisualEffectControl.valueToPlayRate;
m_AttachedComponent.playRate = rate;
UpdatePlayRate();
}
void OnDebugModes()
{
GenericMenu menu = new GenericMenu();
foreach (VFXUIDebug.Modes mode in Enum.GetValues(typeof(VFXUIDebug.Modes)))
{
menu.AddItem(EditorGUIUtility.TextContent(mode.ToString()), false, SetDebugModeCallback, mode);
}
menu.DropDown(m_DebugModes.worldBound);
}
private void SetDebugModeCallback(object mode)
{
m_DebugUI.SetDebugMode((VFXUIDebug.Modes)mode, this);
}
private VFXBoundsRecorder m_BoundsRecorder;
void OnRecordBoundsButton()
{
if (m_BoundsRecorder != null)
{
m_BoundsRecorder.ToggleRecording();
}
UpdateRecordingButton();
}
void UpdateRecordingButton()
{
bool hasSomethingToRecord = m_BoundsRecorder != null && m_BoundsRecorder.NeedsAnyToBeRecorded();
m_RecordBoundsButton.SetEnabled(hasSomethingToRecord);
if (hasSomethingToRecord && m_BoundsRecorder.isRecording)
{
float remainder = Time.realtimeSinceStartup % 1.0f;
if (remainder < 0.22f)
{
m_RecordBoundsImage.style.backgroundImage = null;
}
else
{
m_RecordBoundsImage.style.backgroundImage = m_RecordIcon;
}
m_BoundsToolContainer.style.backgroundColor = m_BackgroundRecordingColor;
m_BoundsActionLabel.text = "Recording in progress...";
}
else
{
m_RecordBoundsImage.style.backgroundImage = m_RecordIcon;
m_BoundsToolContainer.style.backgroundColor = m_BackgroundDefaultColor;
m_BoundsActionLabel.text = "Bounds Recording";
}
if (!hasSomethingToRecord && m_BoundsRecorder.isRecording)
m_BoundsRecorder.ToggleRecording();
}
public void DeactivateBoundsRecordingIfNeeded()
{
if (m_BoundsRecorder != null && m_BoundsRecorder.isRecording)
m_BoundsRecorder.ToggleRecording();
}
void ApplyCurrentBounds()
{
if (m_View.IsAssetEditable())
m_BoundsRecorder.ApplyCurrentBounds();
}
void DeleteBoundsRecorder()
{
if (m_BoundsRecorder != null)
{
m_BoundsRecorder.isRecording = false;
m_BoundsRecorder.CleanUp();
m_BoundsRecorder = null;
}
}
void UpdateBoundsRecorder()
{
if (controller != null && m_AttachedComponent != null && m_View.controller.graph != null)
{
bool wasRecording = false;
if (m_BoundsRecorder != null)
{
wasRecording = m_BoundsRecorder.isRecording;
m_BoundsRecorder.CleanUp();
}
m_BoundsRecorder = new VFXBoundsRecorder(m_AttachedComponent, m_View);
if (wasRecording && !m_View.controller.isReentrant) //If this is called during an Undo/Redo, toggling the recording will cause a reentrant invalidation
{
m_BoundsRecorder.ToggleRecording();
}
var systemNames = m_BoundsRecorder.systemNames;
if (m_SystemBoundsContainer != null)
{
foreach (var elem in m_SystemBoundsContainer.Children())
{
if (elem is VFXComponentBoardBoundsSystemUI ui)
{
ui.ReleaseBoundsRecorder();
}
}
m_SystemBoundsContainer.Clear();
m_SystemBoundsContainer.AddStyleSheetPath("VFXComponentBoard-bounds-list");
}
foreach (var system in systemNames)
{
var tpl = VFXView.LoadUXML("VFXComponentBoard-bounds-list");
tpl.CloneTree(m_SystemBoundsContainer);
if (m_SystemBoundsContainer.Children().Last() is VFXComponentBoardBoundsSystemUI newUI)
{
newUI.Setup(m_View, system, m_BoundsRecorder);
}
}
}
}
void OnEffectSlider(float f)
{
if (m_AttachedComponent != null)
{
m_AttachedComponent.playRate = VisualEffectControl.valueToPlayRate * Mathf.Pow(f, VisualEffectControl.sliderPower);
UpdatePlayRate();
}
}
void EffectStop()
{
if (m_AttachedComponent != null)
m_AttachedComponent.ControlStop();
if (m_DebugUI != null)
m_DebugUI.Notify(VFXUIDebug.Events.VFXStop);
}
void EffectPlay()
{
if (m_AttachedComponent != null)
m_AttachedComponent.ControlPlayPause();
if (m_DebugUI != null)
m_DebugUI.Notify(VFXUIDebug.Events.VFXPlayPause);
}
void EffectStep()
{
if (m_AttachedComponent != null)
m_AttachedComponent.ControlStep();
if (m_DebugUI != null)
m_DebugUI.Notify(VFXUIDebug.Events.VFXStep);
}
void EffectRestart()
{
if (m_AttachedComponent != null)
m_AttachedComponent.ControlRestart();
if (m_DebugUI != null)
m_DebugUI.Notify(VFXUIDebug.Events.VFXReset);
}
public void OnVisualEffectComponentChanged(IEnumerable<VisualEffect> visualEffects)
{
if (m_AttachedComponent != null
&& visualEffects.Contains(m_AttachedComponent)
&& m_AttachedComponent.visualEffectAsset != controller.graph.visualEffectResource.asset)
{
//The Visual Effect Asset has been changed and is no longer valid, we don't want to modify capacity on the wrong graph. We have to detach.
m_View.attachedComponent = null;
}
}
VisualEffect m_AttachedComponent;
public VisualEffect GetAttachedComponent()
{
return m_AttachedComponent;
}
bool m_LastKnownPauseState;
void UpdatePlayButton()
{
if (m_AttachedComponent == null)
return;
if (m_LastKnownPauseState != m_AttachedComponent.pause)
{
m_LastKnownPauseState = m_AttachedComponent.pause;
if (m_LastKnownPauseState)
{
m_Play.AddToClassList("paused");
}
else
{
m_Play.RemoveFromClassList("paused");
}
m_PlayIcon.MarkDirtyRepaint();
}
}
public void Detach()
{
m_RootElement.SetEnabled(false);
m_Subtitle.text = "Select a Game Object running this VFX";
m_SubtitleIcon.style.display = DisplayStyle.Flex;
if (m_AttachedComponent != null)
{
m_AttachedComponent.playRate = 1;
m_AttachedComponent.pause = false;
}
m_AttachedComponent = null;
if (m_UpdateItem != null)
{
m_UpdateItem.Pause();
}
if (m_EventsContainer != null)
m_EventsContainer.Clear();
m_Events.Clear();
if (m_DebugUI != null)
{
m_DebugUI.SetDebugMode(VFXUIDebug.Modes.None, this, true);
}
DeleteBoundsRecorder();
RefreshInitializeErrors();
}
public void RefreshInitializeErrors()
{
var viewContexts = m_View.GetAllContexts();
List<VFXContextUI> contextsToRefresh = new List<VFXContextUI>();
foreach (var context in viewContexts)
{
if (context.controller.model is VFXBasicInitialize)
{
contextsToRefresh.Add(context);
}
}
foreach (var context in contextsToRefresh)
{
context.controller.model.RefreshErrors();
}
}
public void LockUI()
{
m_BoundsToolContainer.SetEnabled(false);
}
public void UnlockUI()
{
m_BoundsToolContainer.SetEnabled(true);
}
public bool Attach(VisualEffect effect = null)
{
VisualEffect target = effect != null ? effect : Selection.activeGameObject?.GetComponent<VisualEffect>();
if (target != null && m_View.controller?.graph != null && m_AttachedComponent != target)
{
if (m_AttachedComponent != null)
{
m_AttachedComponent.playRate = 1;
}
m_AttachedComponent = target;
m_Subtitle.text = m_AttachedComponent.name;
m_LastKnownPauseState = !m_AttachedComponent.pause;
m_AttachedComponent.playRate = m_LastKnownPlayRate >= 0 ? m_LastKnownPlayRate : 1;
UpdatePlayButton();
if (m_UpdateItem == null)
m_UpdateItem = schedule.Execute(Update).Every(100);
else
m_UpdateItem.Resume();
UpdateEventList();
var debugMode = VFXUIDebug.Modes.None;
if (m_DebugUI != null)
{
debugMode = m_DebugUI.GetDebugMode();
m_DebugUI.Clear();
}
m_DebugUI = new VFXUIDebug(m_View);
m_DebugUI.SetVisualEffect(m_AttachedComponent);
m_DebugUI.SetDebugMode(debugMode, this, true);
m_RootElement.SetEnabled(true);
m_SubtitleIcon.style.display = DisplayStyle.None;
UpdateBoundsRecorder();
UpdateRecordingButton();
RefreshInitializeErrors();
return true;
}
return false;
}
public void SendEvent(string name)
{
if (m_AttachedComponent != null)
{
m_AttachedComponent.SendEvent(name);
}
}
IVisualElementScheduledItem m_UpdateItem;
float m_LastKnownPlayRate = -1;
int m_LastKnownParticleCount = -1;
void Update()
{
if (m_AttachedComponent == null || controller == null)
{
Detach();
return;
}
string path = m_AttachedComponent.name;
UnityEngine.Transform current = m_AttachedComponent.transform.parent;
while (current != null)
{
path = current.name + " > " + path;
current = current.parent;
}
if (UnityEngine.SceneManagement.SceneManager.loadedSceneCount > 1)
{
path = m_AttachedComponent.gameObject.scene.name + " : " + path;
}
if (m_Subtitle.text != path)
m_Subtitle.text = path;
if (m_ParticleCount != null)
{
int newParticleCount = 0;//m_AttachedComponent.aliveParticleCount
if (m_LastKnownParticleCount != newParticleCount)
{
m_LastKnownParticleCount = newParticleCount;
m_ParticleCount.text = m_LastKnownParticleCount.ToString();
}
}
UpdatePlayRate();
UpdatePlayButton();
UpdateBoundsModes();
UpdateRecordingButton();
}
void UpdatePlayRate()
{
if (Math.Abs(m_LastKnownPlayRate - m_AttachedComponent.playRate) > 1e-4)
{
m_LastKnownPlayRate = m_AttachedComponent.playRate;
SetPlayrateSlider(m_AttachedComponent.playRate);
}
}
void SetPlayrateSlider(float value)
{
float playRateValue = value * VisualEffectControl.playRateToValue;
m_PlayRateSlider.value = Mathf.Pow(playRateValue, 1 / VisualEffectControl.sliderPower);
if (m_PlayRateField != null && !m_PlayRateField.HasFocus())
m_PlayRateField.value = Mathf.RoundToInt(playRateValue);
}
VisualElement m_EventsContainer;
VisualElement m_RootElement;
Label m_Subtitle;
Image m_SubtitleIcon;
Button m_Stop;
Button m_Play;
Image m_PlayIcon;
Button m_Step;
Button m_Restart;
Slider m_PlayRateSlider;
IntegerField m_PlayRateField;
Button m_PlayRateMenu;
Button m_DebugModes;
Button m_RecordBoundsButton;
Image m_RecordBoundsImage;
Texture2D m_RecordIcon;
Button m_ApplyBoundsButton;
VFXBoundsSelector m_SystemBoundsContainer;
VisualElement m_BoundsToolContainer;
Label m_BoundsActionLabel;
StyleColor m_BackgroundRecordingColor = new StyleColor(new Color(0.325f, 0.125f, 0.125f));
StyleColor m_BackgroundDefaultColor;
Label m_ParticleCount;
public new void Clear()
{
Detach();
}
void IControlledElement.OnControllerChanged(ref ControllerChangedEvent e)
{
UpdateEventList();
if (e.change != VFXViewController.Change.ui)
UpdateBoundsRecorder();
}
static readonly string[] staticEventNames = new string[] { VisualEffectAsset.PlayEventName, VisualEffectAsset.StopEventName };
static bool IsDefaultEvent(string evt)
{
return evt == VisualEffectAsset.PlayEventName || evt == VisualEffectAsset.StopEventName;
}
IEnumerable<string> GetEventNames()
{
return controller?.contexts.SelectMany(x => this.RecurseGetEventNames(x.model)) ?? Enumerable.Empty<string>();
}
IEnumerable<string> RecurseGetEventNames(VFXContext context)
{
switch (context)
{
case VFXBasicEvent basicEvent when !IsDefaultEvent(name):
yield return basicEvent.eventName;
break;
case VFXSubgraphContext subgraphContext when subgraphContext.subChildren != null:
{
foreach (var eventName in subgraphContext.subChildren.OfType<VFXContext>().SelectMany(RecurseGetEventNames))
{
yield return eventName;
}
break;
}
}
}
public void UpdateEventList()
{
if (m_AttachedComponent == null)
{
if (m_EventsContainer != null)
m_EventsContainer.Clear();
m_Events.Clear();
}
else
{
var eventNames = GetEventNames().ToArray();
foreach (var removed in m_Events.Keys.Except(eventNames).ToArray())
{
var ui = m_Events[removed];
m_EventsContainer.Remove(ui);
m_Events.Remove(removed);
}
foreach (var added in eventNames.Except(m_Events.Keys).ToArray())
{
var tpl = VFXView.LoadUXML("VFXComponentBoard-event");
tpl.CloneTree(m_EventsContainer);
VFXComponentBoardEventUI newUI = m_EventsContainer.Children().Last() as VFXComponentBoardEventUI;
if (newUI != null)
{
newUI.Setup();
newUI.name = added;
m_Events.Add(added, newUI);
}
}
if (!m_Events.Values.Any(t => t.nameHasFocus))
{
SortEventList();
}
}
}
internal void ResetPlayRate()
{
m_LastKnownPlayRate = -1f;
SetPlayrateSlider(1f);
}
void SortEventList()
{
var eventNames = m_Events.Keys.OrderBy(t => t);
//Sort events
VFXComponentBoardEventUI prev = null;
foreach (var eventName in eventNames)
{
VFXComponentBoardEventUI current = m_Events[eventName];
if (current != null)
{
if (prev == null)
{
current.SendToBack();
}
else
{
current.PlaceInFront(prev);
}
prev = current;
}
}
}
void UpdateBoundsModes()
{
bool systemNamesChanged = false;
foreach (var elem in m_SystemBoundsContainer.Children())
{
if (elem is VFXComponentBoardBoundsSystemUI boundsModeElem)
{
if (boundsModeElem.HasSystemBeenRenamed())
{
systemNamesChanged = true;
break;
}
boundsModeElem.UpdateLabel();
}
}
if (systemNamesChanged)
UpdateBoundsRecorder();
}
Dictionary<string, VFXComponentBoardEventUI> m_Events = new Dictionary<string, VFXComponentBoardEventUI>();
public override void UpdatePresenterPosition()
{
BoardPreferenceHelper.SavePosition(BoardPreferenceHelper.Board.componentBoard, GetPosition());
}
public void OnMoved()
{
BoardPreferenceHelper.SavePosition(BoardPreferenceHelper.Board.componentBoard, GetPosition());
}
void IVFXResizable.OnStartResize() { }
public void OnResized()
{
BoardPreferenceHelper.SavePosition(BoardPreferenceHelper.Board.componentBoard, GetPosition());
}
}
[System.Obsolete("VFXComponentBoardEventUIFactory is deprecated and will be removed. Use UxmlElementAttribute instead.", false)]
class VFXComponentBoardEventUIFactory : UxmlFactory<VFXComponentBoardEventUI>
{ }
class VFXComponentBoardEventUI : VisualElement
{
public VFXComponentBoardEventUI()
{
}
public void Setup()
{
m_EventName = this.Query<TextField>("event-name");
m_EventName.isDelayed = true;
m_EventName.RegisterCallback<ChangeEvent<string>>(OnChangeName);
m_EventSend = this.Query<Button>("event-send");
m_EventSend.clickable.clicked += OnSend;
}
void OnChangeName(ChangeEvent<string> e)
{
var board = GetFirstAncestorOfType<VFXComponentBoard>();
if (board != null)
{
board.controller.ChangeEventName(m_Name, e.newValue);
}
}
public bool nameHasFocus
{
get { return m_EventName.HasFocus(); }
}
public new string name
{
get
{
return m_Name;
}
set
{
m_Name = value;
if (m_EventName != null)
{
if (!m_EventName.HasFocus())
m_EventName.SetValueWithoutNotify(m_Name);
}
}
}
string m_Name;
TextField m_EventName;
Button m_EventSend;
void OnSend()
{
var board = GetFirstAncestorOfType<VFXComponentBoard>();
if (board != null)
{
board.SendEvent(m_Name);
}
}
}
[System.Obsolete("VFXComponentBoardBoundsSystemUIFactory is deprecated and will be removed. Use UxmlElementAttribute instead.", false)]
class VFXComponentBoardBoundsSystemUIFactory : UxmlFactory<VFXComponentBoardBoundsSystemUI>
{ }
class VFXComponentBoardBoundsSystemUI : VisualElement
{
public void Setup(VFXView vfxView, string systemName, VFXBoundsRecorder boundsRecorder)
{
m_BoundsRecorder = boundsRecorder;
m_CurrentMode = m_BoundsRecorder.GetSystemBoundsSettingMode(systemName);
m_SystemName = systemName;
m_SystemNameButton = this.Query<VFXBoundsRecorderField>("system-field");
var initContextUI = m_BoundsRecorder.GetInitializeContextUI(m_SystemName);
m_SystemNameButton.Setup(initContextUI, vfxView);
m_SystemNameButton.text = m_SystemName;
InitBoundsModeElement();
m_Colors = new Dictionary<string, StyleColor>()
{
{"included", m_SystemNameButton.style.color},
{"excluded", new StyleColor(Color.gray * 0.8f) }
};
if (!m_BoundsRecorder.NeedsToBeRecorded(m_SystemName, out VFXBoundsRecorder.ExclusionCause cause))
{
m_SystemNameButton.text = $"{m_SystemName} {VFXBoundsRecorder.exclusionCauseString[cause]}";
m_SystemNameButton.tooltip =
$"This system will not be taken into account in the recording because {VFXBoundsRecorder.exclusionCauseTooltip[cause]}";
m_SystemNameButton.style.color = m_Colors["excluded"];
m_SystemNameButton.SetEnabled(false);
}
}
void InitBoundsModeElement()
{
m_BoundsMode = new VFXEnumField(s_EmptyEnumLabel, typeof(BoundsSettingMode));
m_BoundsMode.OnValueChanged += OnValueChanged;
m_BoundsMode.SetValue((int)m_CurrentMode);
m_BoundsMode.AddToClassList("bounds-mode");
Add(m_BoundsMode);
}
public void UpdateLabel()
{
m_CurrentMode = m_BoundsRecorder.GetSystemBoundsSettingMode(m_SystemName);
m_BoundsMode.SetValue((int)m_CurrentMode);
OnValueChanged();
if (!m_BoundsRecorder.NeedsToBeRecorded(m_SystemName, out VFXBoundsRecorder.ExclusionCause cause))
{
m_SystemNameButton.text = $"{m_SystemName} {VFXBoundsRecorder.exclusionCauseString[cause]}";
m_SystemNameButton.tooltip =
$"This system will not be taken into account in the recording because {VFXBoundsRecorder.exclusionCauseTooltip[cause]}";
m_SystemNameButton.style.color = m_Colors["excluded"];
m_SystemNameButton.SetEnabled(false);
}
else
{
m_SystemNameButton.text = m_SystemName;
m_SystemNameButton.tooltip = "";
m_SystemNameButton.SetEnabled(true);
m_SystemNameButton.style.color = m_Colors["included"];
}
}
public bool HasSystemBeenRenamed()
{
return !m_BoundsRecorder.systemNames.Contains(m_SystemName);
}
void OnValueChanged()
{
if (m_CurrentMode != (BoundsSettingMode)m_BoundsMode.value)
{
m_CurrentMode = (BoundsSettingMode)m_BoundsMode.value;
m_BoundsRecorder.ModifyMode(m_SystemName, m_CurrentMode);
}
}
public void ReleaseBoundsRecorder()
{