-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEditorUI_Load.cs
846 lines (689 loc) · 35.5 KB
/
EditorUI_Load.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
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Digi.ParticleEditor.UIControls;
using Sandbox.Game.Screens;
using Sandbox.Game.Screens.Helpers;
using Sandbox.Game.World;
using Sandbox.Graphics.GUI;
using Sandbox.ModAPI;
using VRage.Game;
using VRage.ObjectBuilders;
using VRage.ObjectBuilders.Private;
using VRage.Render.Particles;
using VRageMath;
using VRageRender.Animations;
namespace Digi.ParticleEditor
{
public partial class EditorUI // to allow use of the protected methods
{
int CreatedParticles = 1;
MyGuiControlListbox ParticlesListbox;
ValueGetScreenWithCaption NamePrompt;
bool ShowParticlePicker = true;
string SearchQuery = null;
CheckStateEnum FilterLoop = CheckStateEnum.Unchecked;
bool FilterSeen = false;
bool Controls_LoadParticle()
{
MyGuiControlLabel labelTitle = Host.CreateLabel($"Particle Editor v{ParticleEditorPlugin.Version.ToString()}");
labelTitle.SetToolTip(@"Some notes about the behavior of this tool:
It sees all particles loaded into the game, including mod-added ones (but it cannot identify the mod unfortunately).
Changes to particles are only in memory and will not reset when changing between particles.
Therefore you need to save your changes to an SBC file (button at the bottom right) if you wish to keep them.
Also, particles of the same name share the same data so changes will affect it when spawned by blocks or whatever else.");
if(!ShowParticlePicker && SelectedParticle.Name != null)
{
MyGuiControlButton buttonChangeParticle = Host.CreateButton("Change Particle",
"Shows the particle browser." +
"\nChanges to this particle remain in memory regardless, do not forget to export if you wish to keep them.", clicked: (b) =>
{
ShowParticlePicker = true;
RefreshUI();
});
Host.PositionControls(labelTitle, Host.CreateLabel(""), buttonChangeParticle);
Host.PositionAndFillWidth(Host.CreateLabel(SelectedParticle.Name));
return true; // show the rest of controls
}
else
{
MyGuiControlButton buttonCreateParticle = Host.CreateButton("New particle",
"Creates a new particle effect with the name you'll be asked for." +
"\nRemember that all particle changes, including new ones, are only in-memory. You need to export them to .sbc to keep them.",
clicked: (b) =>
{
string generatedName = $"New Particle {CreatedParticles}";
NamePrompt = new ValueGetScreenWithCaption($"Create particle", generatedName, (newName) =>
{
return CreateParticle(newName);
});
MyGuiSandbox.AddScreen(NamePrompt);
});
MyGuiControlButton buttonLoadFromFile = Host.CreateButton("Load from SBC",
"Opens file explorer to pick one or more .sbc file(s) to load particles from." +
"\nWill prompt for override and for picking the particles to load if multiple are found." +
"\nRemember that all particle changes, including loaded ones, are only in-memory. You need to export them to .sbc to keep them.",
clicked: (b) => LoadParticleDialog());
Host.PositionControls(labelTitle, Host.CreateLabel(""), buttonCreateParticle, buttonLoadFromFile);
MyGuiControlButton buttonBack = Host.CreateButton($"< Back to {SelectedParticle.Name ?? "particle"}", "", clicked: (button) =>
{
ShowParticlePicker = false;
RefreshUI();
});
buttonBack.ColorMask = (SelectedParticle.Name != null ? Color.Lime : Color.Gray);
buttonBack.Enabled = (SelectedParticle.Name != null);
Host.PositionAndFillWidth(buttonBack);
MyGuiControlSearchBox searchBar = new MyGuiControlSearchBox();
searchBar.Size = new Vector2(Host.PanelSize.X, searchBar.Size.Y * 2);
searchBar.SearchText = SearchQuery;
searchBar.OnTextChanged += (text) =>
{
SearchQuery = text;
RefreshParticlesList();
};
// FIXME: doesn't "glow" like after clicking on it...
FocusedControl = searchBar.TextBox;
Host.Add(searchBar);
Host.PositionAndFillWidth(searchBar);
// HACK fix this control being weird
searchBar.Position = new Vector2(searchBar.Position.X + (searchBar.Size.X / 2f), searchBar.Position.Y + (searchBar.Size.Y / 2f));
(MyGuiControlParent cbFilterLoop, _, _) = Host.Insert3StateCheckbox("Loop", "Filter in/out looping particles.\nEmpty = don't filter.", FilterLoop, (state) =>
{
FilterLoop = state;
RefreshParticlesList();
});
Host.UndoLastVerticalShift();
(MyGuiControlParent cbFilterLastSeen, _, _) = Host.InsertCheckbox("Last Seen", "Show only last seen particles in game world.", FilterSeen, (state) =>
{
FilterSeen = state;
RefreshParticlesList();
});
Host.UndoLastVerticalShift();
Host.PositionControlsNoSize(cbFilterLoop, cbFilterLastSeen);
ParticlesListbox = Host.CreateListBox(36, Host.PanelSize.X * 0.95f);
Host.PositionAndFillWidth(ParticlesListbox, ParticlesListbox.TextScale);
ParticlesListbox.ItemDoubleClicked += (c) => LoadSelected();
RefreshParticlesList();
MyGuiControlButton buttonLoadSelected = Host.CreateButton("Load selected",
"You can also doubleclick a particle in the list to cause this action.",
clicked: (b) => LoadSelected());
MyGuiControlButton buttonCreateFromSelected = Host.CreateButton("Duplicate selection",
"Creates a new particle with the data from the selected particle.",
clicked: (b) =>
{
if(ParticlesListbox.SelectedItems.Count != 1)
return;
string copyFrom = (string)ParticlesListbox.SelectedItems[0].UserData;
string generatedName = $"Copy of {copyFrom}";
NamePrompt = new ValueGetScreenWithCaption($"Duplicate particle", generatedName, (newName) =>
{
return CreateParticle(newName, copyFrom);
});
MyGuiSandbox.AddScreen(NamePrompt);
});
buttonLoadSelected.Enabled = false;
buttonCreateFromSelected.Enabled = false;
ParticlesListbox.ItemsSelected += (c) =>
{
buttonLoadSelected.Enabled = true;
buttonCreateFromSelected.Enabled = true;
// gets too wide
//string copyFrom = (string)ParticlesListbox.SelectedItems[0].UserData;
//buttonLoadSelected.Text = $"Load {copyFrom}";
};
Host.PositionControls(buttonLoadSelected, buttonCreateFromSelected);
return false; // hide the rest of controls
}
}
void LoadSelected()
{
if(ParticlesListbox.SelectedItems.Count != 1)
return;
string subtypeId = (string)ParticlesListbox.SelectedItems[0].UserData;
LoadParticle(subtypeId);
}
void UpdateLoadParticleUI()
{
// update every second to see new particles pop up and such
if(FilterSeen && MySession.Static.GameplayFrameCounter % 60 == 0)
{
RefreshParticlesList();
}
}
void RefreshParticlesList()
{
ParticlesListbox.ClearItems();
ParticlesListbox.SelectedItems.Clear();
List<(MyParticleEffectData, int)> showParticles = new List<(MyParticleEffectData, int)>();
int tick = MySession.Static.GameplayFrameCounter;
int minTickSeen = tick - (60 * 60); // at most 1 minute ago
foreach(KeyValuePair<string, MyParticleEffectData> kv in MyParticleEffectsLibrary.GetByName())
{
MyParticleEffectData data = kv.Value;
int order = 0;
if(FilterSeen)
{
int lastSeenTick = Editor.LastSeenParticles.Recent.GetValueOrDefault(data.Name, -1);
if(lastSeenTick < 0 || lastSeenTick < minTickSeen)
continue;
order = lastSeenTick;
}
if(FilterLoop == CheckStateEnum.Checked && !data.Loop)
continue;
if(FilterLoop == CheckStateEnum.Indeterminate && data.Loop)
continue;
if(SearchQuery != null)
{
if(data.Name.IndexOf(SearchQuery, StringComparison.OrdinalIgnoreCase) == -1)
continue;
}
showParticles.Add((data, order));
}
if(FilterSeen)
{
showParticles.Sort((a, b) => a.Item2.CompareTo(b.Item2));
}
else
{
showParticles.Sort((a, b) => a.Item1.Name.CompareTo(b.Item1.Name));
}
foreach((MyParticleEffectData, int) kv in showParticles)
{
MyParticleEffectData data = kv.Item1;
int tickSeen = kv.Item2;
StringBuilder label = new StringBuilder(data.Name);
if(data.Loop)
label.Append(" (loop)");
if(FilterSeen)
{
label.Append(" Last seen ").Append(TimeSpan.FromSeconds((tick - tickSeen) / 60.0).TotalSeconds.ToString("0")).Append("s ago");
}
//if(data.DurationMin > 0 || data.DurationMax > 0)
//{
// label.Append(" (");
// if(data.DurationMax > data.DurationMin)
// label.Append(data.DurationMin).Append("s to ").Append(data.DurationMax).Append("s");
// else
// label.Append(data.DurationMin).Append("s");
// label.Append(")");
//}
string tooltip = $@"Integer Id: {data.ID}
Loop: {data.Loop}
Duration: {(data.DurationMin < data.DurationMax ? $"{data.DurationMin}s to {data.DurationMax}s" : data.DurationMin.ToString())}
Emitters: {data.GetGenerations().Count}
Lights: {data.GetParticleLights().Count}
Priority: {data.Priority}
Tag: {data.Tag}";
MyGuiControlListbox.Item item = new MyGuiControlListbox.Item(label, tooltip, userData: data.Name);
ParticlesListbox.Add(item);
}
}
bool CreateParticle(string name, string cloneFromName = null)
{
if(MyParticleEffectsLibrary.Exists(name))
{
PopupInfoAction("ERROR", $"Particle name '{name}' already exists", "Select that particle", "Pick a different name", (result) =>
{
if(result == MyGuiScreenMessageBox.ResultEnum.YES)
{
if(NamePrompt != null)
MyGuiSandbox.RemoveScreen(NamePrompt);
LoadParticle(name);
}
}, MyMessageBoxStyleEnum.Error);
return false;
}
if(cloneFromName != null && !MyParticleEffectsLibrary.Exists(cloneFromName))
{
PopupInfo("Error", $"Couldn't find '{cloneFromName}' to clone from!", MyMessageBoxStyleEnum.Error);
return false;
}
// TODO: need a better way to deal with the particle ID
if(MyParticleEffectsLibrary.GetById().TryGetValue(name.GetHashCode(), out MyParticleEffectData existingData))
{
PopupInfo("ERROR", $"The hashcode of name '{name}' used for the integer Id ({name.GetHashCode()}) already exists, used by: '{existingData.Name}'", MyMessageBoxStyleEnum.Error);
return false;
}
MyParticleEffectData data = new MyParticleEffectData();
data.Start(name.GetHashCode(), name);
if(cloneFromName != null)
{
MyParticleEffectData cloneFromData = MyParticleEffectsLibrary.Get(cloneFromName);
MyObjectBuilder_ParticleEffect dataOB = MyParticleEffectDataSerializer.SerializeToObjectBuilder(cloneFromData);
MyParticleEffectDataSerializer.DeserializeFromObjectBuilder(data, dataOB);
data.ID = name.GetHashCode();
data.Name = name;
}
MyParticleEffectsLibrary.Add(data);
CreatedParticles++;
if(SelectedParticle.SpawnedEffect != null)
{
if(Editor.Backup.ShouldBackup)
{
Editor.Backup.BackupCurrentParticle();
}
if(DrawOnlySelected)
{
RestoreEnabled();
DrawOnlySelected = false;
}
SelectedParticle.Despawn();
}
SelectedParticle.ParentDistance = 2f;
SelectedParticle.Spawn(data.Name);
SelectedParticle.HasChanges = false;
ShowParticlePicker = false;
Editor.Backup.ShouldBackup = false;
RefreshUI();
return true;
}
class LoadParticleInfo
{
public readonly string FileName;
public readonly MyObjectBuilder_ParticleEffect OB;
public bool Collides = false;
public LoadParticleInfo(string fileName, MyObjectBuilder_ParticleEffect ob)
{
FileName = fileName;
OB = ob;
}
}
public void LoadParticleDialog(string overrideFolder = null)
{
MyParticleEffectData lastLoadedData = null;
int loadedCount = 0;
OpenMultipleFilesDialog("Load particle(s)", overrideFolder, FileDialogFilterSBC, (filePaths) =>
{
try
{
List<LoadParticleInfo> foundParticles = new List<LoadParticleInfo>();
foreach(string filePath in filePaths)
{
string text = File.ReadAllText(filePath);
if(string.IsNullOrWhiteSpace(text))
{
Log.Error($"File is empty: '{filePath}'");
continue;
}
string fileName = Path.GetFileName(filePath);
if(text.Contains("<ParticleEffects>"))
{
MyObjectBuilder_Definitions definitionsOB;
if(!MyObjectBuilderSerializerKeen.DeserializeXML<MyObjectBuilder_Definitions>(filePath, out definitionsOB) || definitionsOB == null)
{
Log.Error($"'{fileName}': Couldn't deserialize, see SE log maybe there's something.");
continue;
}
if(definitionsOB.ParticleEffects.Length == 0)
{
Log.Error($"'{fileName}': Does not contain any valid particle effects.");
continue;
}
foreach(MyObjectBuilder_ParticleEffect particleOB in definitionsOB.ParticleEffects)
{
foundParticles.Add(new LoadParticleInfo(fileName, particleOB));
}
}
else
{
MyObjectBuilder_ParticleEffect particleOB = MyAPIGateway.Utilities.SerializeFromXML<MyObjectBuilder_ParticleEffect>(text);
if(particleOB == null)
throw new Exception("It got deserialized as ParticleEffect but returned null.");
foundParticles.Add(new LoadParticleInfo(filePath, particleOB));
}
}
if(foundParticles.Count <= 0)
{
Notifications.Show("Found 0 particle effects.", 5, Color.Yellow);
return;
}
else if(foundParticles.Count == 1)
{
LoadSingleParticleFromOB(foundParticles[0].OB);
}
else
{
foundParticles.Sort((a, b) => a.OB.Id.SubtypeName.CompareTo(b.OB.Id.SubtypeName));
// check duplicates which are always next to eachother because of the above sorting
for(int i = 1; i < foundParticles.Count; i++)
{
LoadParticleInfo piA = foundParticles[i - 1];
LoadParticleInfo piB = foundParticles[i];
if(piA.OB.Id.SubtypeName == piB.OB.Id.SubtypeName)
{
piA.Collides = true;
piB.Collides = true;
}
}
UICustomizablePopup screen = new UICustomizablePopup(closeButtonTooltip: "Cancels loading.", screenSize: new Vector2(0.5f, 0.8f));
HashSet<string> selectedParticles = new HashSet<string>();
List<MyGuiControlCheckbox> checkboxes = new List<MyGuiControlCheckbox>(foundParticles.Count);
screen.ControlGetter = (innerHost) =>
{
const string CollidingNamesTitle = "Same particle declared multiple times!";
const string CollidingNamesText = "At least one particle name is used by multiple definitions (the yellow colored ones)." +
"\nRecommended to check only one of them and use 'Load only selected'.";
var loadOverride = innerHost.CreateButton("Load all + override", "Loads all found particles and overrides if they match any existing ones.",
clicked: (b) =>
{
if(foundParticles.Any(pi => pi.Collides))
{
PopupInfoAction(CollidingNamesTitle, CollidingNamesText,
"Continue, load whichever", "Go back", (result) =>
{
if(result == MyGuiScreenMessageBox.ResultEnum.YES)
DoTheThing();
});
}
else
{
DoTheThing();
}
void DoTheThing()
{
try
{
foreach(LoadParticleInfo pi in foundParticles)
{
AddOrReplaceParticleOB(pi.OB);
}
FinalizeLoading();
}
catch(Exception e)
{
Log.Error(e);
}
screen.CloseScreen();
}
});
var loadNew = innerHost.CreateButton("Load only new", "Loads only particles that don't already exist in memory (no override).",
clicked: (b) =>
{
if(foundParticles.Any(pi => pi.Collides))
{
PopupInfoAction(CollidingNamesTitle, CollidingNamesText,
"Continue, load whichever", "Go back", (result) =>
{
if(result == MyGuiScreenMessageBox.ResultEnum.YES)
DoTheThing();
});
}
else
{
DoTheThing();
}
void DoTheThing()
{
try
{
foreach(LoadParticleInfo pi in foundParticles)
{
if(!MyParticleEffectsLibrary.Exists(pi.OB.Id.SubtypeName))
AddOrReplaceParticleOB(pi.OB);
}
FinalizeLoading();
}
catch(Exception e)
{
Log.Error(e);
}
screen.CloseScreen();
}
});
innerHost.PositionControls(loadOverride, loadNew);
innerHost.InsertSeparator();
var invertSelection = innerHost.CreateButton("Invert selection", "Uncheck all checked and check all unchecked.",
clicked: (b) =>
{
HashSet<string> inverted = new HashSet<string>();
foreach(LoadParticleInfo pi in foundParticles)
{
if(!selectedParticles.Contains(pi.OB.Id.SubtypeName))
inverted.Add(pi.OB.Id.SubtypeName);
}
selectedParticles = inverted;
foreach(MyGuiControlCheckbox cb in checkboxes)
{
cb.IsChecked = selectedParticles.Contains((string)cb.UserData);
}
});
var loadSelected = innerHost.CreateButton("Load only selected", "Loads only the below selected particles, and overrides if any already exist.",
clicked: (b) =>
{
if(selectedParticles.Count == 0)
{
PopupInfoAction("Nothing selected", "No particles were selected...",
"Yes, load nothing", "Go back", (result) =>
{
if(result == MyGuiScreenMessageBox.ResultEnum.YES)
screen.CloseScreen();
});
}
else
{
try
{
foreach(LoadParticleInfo pi in foundParticles)
{
if(selectedParticles.Contains(pi.OB.Id.SubtypeName))
{
AddOrReplaceParticleOB(pi.OB);
}
}
FinalizeLoading();
}
catch(Exception e)
{
Log.Error(e);
}
screen.CloseScreen();
}
});
innerHost.PositionControls(invertSelection, loadSelected);
Vector2 size = new Vector2(innerHost.PanelSize.X - innerHost.Padding.X * 2 - EyeballedScrollbarWidth, 0f);
var scrollHost = new VerticalControlsHost(null, Vector2.Zero, size);
var scrollPanel = innerHost.CreateScrollableArea(scrollHost.Panel, new Vector2(innerHost.PanelSize.X - innerHost.Padding.X * 2, 0.62f));
innerHost.Add(scrollPanel);
innerHost.PositionAndFillWidth(scrollPanel);
scrollHost.Reset();
foreach(LoadParticleInfo pi in foundParticles)
{
MyObjectBuilder_ParticleEffect particleOB = pi.OB;
string name = particleOB.Id.SubtypeName;
bool exists = MyParticleEffectsLibrary.Exists(name);
(_, MyGuiControlCheckbox cb, MyGuiControlLabel label) = scrollHost.InsertCheckbox($"{name}{(exists ? " (exists)" : "")}", $"From file: {pi.FileName}", false, (v) =>
{
if(v)
selectedParticles.Add(name);
else
selectedParticles.Remove(name);
});
cb.UserData = name;
checkboxes.Add(cb);
if(pi.Collides)
label.ColorMask = Color.Yellow;
else if(exists)
label.ColorMask = Color.Gray;
}
EditorUI.FinalizeScrollable(scrollPanel, scrollHost.Panel, scrollHost);
};
screen.FinishSetup();
MyGuiSandbox.AddScreen(screen);
}
}
catch(Exception e)
{
Log.Error(e);
}
});
void LoadSingleParticleFromOB(MyObjectBuilder_ParticleEffect particleOB)
{
string name = particleOB.Id.SubtypeName;
if(MyParticleEffectsLibrary.Exists(name))
{
PopupInfoAction("ERROR", $"Particle name '{name}' already exists", "Overwrite", "Cancel", (result) =>
{
if(result == MyGuiScreenMessageBox.ResultEnum.YES)
{
AddOrReplaceParticleOB(particleOB);
}
}, MyMessageBoxStyleEnum.Error);
return;
}
else
{
AddOrReplaceParticleOB(particleOB);
}
FinalizeLoading();
}
void FinalizeLoading()
{
if(lastLoadedData != null)
{
SelectedParticle.Spawn(lastLoadedData.Name);
SelectedParticle.ParentDistance = ParentingDistanceOnLoad(lastLoadedData);
SelectedParticle.HasChanges = false;
CheckForZeroViewDistance(lastLoadedData);
ShowParticlePicker = false;
Editor.Backup.ShouldBackup = false;
}
if(loadedCount > 0)
{
Notifications.Show($"Loaded {loadedCount} particle effects. Selected: {lastLoadedData.Name}", 5);
}
else
{
Notifications.Show("Loaded 0 particle effects.", 5, Color.Yellow);
}
RefreshUI();
}
void AddOrReplaceParticleOB(MyObjectBuilder_ParticleEffect particleOB)
{
string name = particleOB.Id.SubtypeName;
bool existed = MyParticleEffectsLibrary.Exists(name);
if(existed)
MyParticleEffectsLibrary.Remove(name);
if(MyParticleEffectsLibrary.GetById().TryGetValue(name.GetHashCode(), out MyParticleEffectData existingData))
{
PopupInfo("ERROR", $"The hashcode of name '{name}' used for the integer Id already exists, used by: '{existingData.Name}'", MyMessageBoxStyleEnum.Error);
return;
}
if(SelectedParticle != null)
{
if(Editor.Backup.ShouldBackup)
{
Editor.Backup.BackupCurrentParticle();
}
if(DrawOnlySelected)
{
RestoreEnabled();
DrawOnlySelected = false;
}
SelectedParticle.Despawn();
}
MyParticleEffectData data = new MyParticleEffectData();
data.Start(name.GetHashCode(), name);
MyParticleEffectDataSerializer.DeserializeFromObjectBuilder(data, particleOB);
MyParticleEffectsLibrary.Add(data);
lastLoadedData = data;
loadedCount++;
}
}
void LoadParticle(string name)
{
if(SelectedParticle.SpawnedEffect != null)
{
if(Editor.Backup.ShouldBackup)
{
Editor.Backup.BackupCurrentParticle();
}
if(DrawOnlySelected)
{
RestoreEnabled();
DrawOnlySelected = false;
}
SelectedParticle.Despawn();
}
SelectedParticle.Spawn(name, true);
if(SelectedParticle.SpawnedEffect == null)
return;
if(!OriginalParticleData.ContainsKey(name))
{
OriginalParticleData.Add(name, MyParticleEffectDataSerializer.SerializeToObjectBuilder(SelectedParticle.Data));
}
SelectedParticle.HasChanges = false;
SelectedParticle.ParentDistance = ParentingDistanceOnLoad(SelectedParticle.Data);
SelectedParticle.OriginalData = OriginalParticleData.GetValueOrDefault(name); // HACK: fixing original data not being there in first load because it gets assigned in Spawn()
CheckForZeroViewDistance(SelectedParticle.Data);
ShowParticlePicker = false;
Editor.Backup.ShouldBackup = false;
RefreshUI();
}
static float ParentingDistanceOnLoad(MyParticleEffectData data)
{
return Math.Max(1f, EstimateParticleTotalRadius(data) * 0.25f);
}
static void CheckForZeroViewDistance(MyParticleEffectData data)
{
if(data.DistanceMax <= 0)
{
data.DistanceMax = DefaultData.DistanceMax;
Notifications.Show($"Particle '{data.Name}' had ViewDistance 0 or negative. Was reset to default: {data.DistanceMax}.", 5, Color.Yellow);
}
}
static float EstimateParticleTotalRadius(MyParticleEffectData data)
{
BoundingSphere sphere = new BoundingSphere(Vector3.Zero, 0f);
foreach(MyParticleGPUGenerationData emitter in data.GetGenerations())
{
float highestRadius = 0f;
float radiusVar = Math.Max(0, emitter.RadiusVar.GetValue());
for(float time = 0f; time <= 1f; time += 0.1f)
{
CollectKeys(time, emitter.Radius, out MyAnimatedPropertyFloat keys);
float radius;
keys.GetKey(0, out _, out radius);
highestRadius = Math.Max(highestRadius, radius + radiusVar);
keys.GetKey(1, out _, out radius);
highestRadius = Math.Max(highestRadius, radius + radiusVar);
keys.GetKey(2, out _, out radius);
highestRadius = Math.Max(highestRadius, radius + radiusVar);
keys.GetKey(3, out _, out radius);
highestRadius = Math.Max(highestRadius, radius + radiusVar);
}
float highestEmitterSize = 0f;
for(float time = 0f; time <= 1f; time += 0.1f)
{
emitter.EmitterSize.GetInterpolatedValue(time, out Vector3 vec);
highestEmitterSize = Math.Max(highestEmitterSize, vec.AbsMax());
}
float highestVelocity = 0f;
for(float time = 0f; time <= 1f; time += 0.1f)
{
emitter.Velocity.GetInterpolatedValue(time, out float vel);
emitter.VelocityVar.GetInterpolatedValue(time, out float velVar);
highestVelocity = Math.Max(highestVelocity, vel + Math.Max(0, velVar));
}
float maxLife = emitter.Life.GetValue() + Math.Max(0, emitter.LifeVar.GetValue());
float size = highestEmitterSize + highestRadius + (highestVelocity * maxLife);
BoundingSphere emitterSphere = new BoundingSphere(emitter.Offset.GetValue(), size);
sphere.Include(emitterSphere);
}
return sphere.Radius;
}
static void CollectKeys(float time, MyAnimatedProperty2DFloat prop, out MyAnimatedPropertyFloat keys)
{
if(prop.GetKeysCount() == 1)
{
prop.GetKey(0, out float _, out keys);
return;
}
_tempFloatProperty.ClearKeys();
prop.GetInterpolatedKeys(time, 1f, _tempFloatProperty);
keys = _tempFloatProperty;
}
static readonly MyAnimatedPropertyFloat _tempFloatProperty = new MyAnimatedPropertyFloat();
}
}