-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLayerConfigFile.cs
More file actions
687 lines (627 loc) · 28.7 KB
/
LayerConfigFile.cs
File metadata and controls
687 lines (627 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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
namespace lifeviz;
internal sealed class LayerConfigFile
{
public int Version { get; set; } = 7;
public DateTime SavedUtc { get; set; } = DateTime.UtcNow;
public LayerConfigProjectSettings ProjectSettings { get; set; } = new();
public List<LayerConfigSimulationLayer> SimulationLayers { get; set; } = new();
public bool PositiveLayerEnabled { get; set; } = true;
public string PositiveLayerBlendMode { get; set; } = "Additive";
public bool NegativeLayerEnabled { get; set; } = true;
public string NegativeLayerBlendMode { get; set; } = "Subtractive";
public List<string> SimulationLayerOrder { get; set; } = new() { "Positive", "Negative" };
public List<LayerConfigSource> Sources { get; set; } = new();
public static LayerConfigFile FromEditorSources(
IEnumerable<LayerEditorSource> sources,
IEnumerable<LayerEditorSimulationLayer> simulationLayers,
LayerEditorProjectSettings projectSettings)
{
var sourceList = sources.ToList();
bool hasEmbeddedSimulationGroups = EnumerateSources(sourceList).Any(source => source.Kind == LayerEditorSourceKind.SimGroup);
var file = new LayerConfigFile
{
ProjectSettings = new LayerConfigProjectSettings
{
Height = Math.Max(1, projectSettings.Height),
Depth = Math.Max(1, projectSettings.Depth),
Framerate = projectSettings.Framerate,
LifeOpacity = Math.Clamp(projectSettings.LifeOpacity, 0, 1),
// Legacy global hue lives on project settings for backward compatibility only.
RgbHueShiftDegrees = 0,
RgbHueShiftSpeedDegreesPerSecond = 0,
InvertComposite = projectSettings.InvertComposite,
Passthrough = projectSettings.Passthrough,
CompositeBlendMode = string.IsNullOrWhiteSpace(projectSettings.CompositeBlendMode) ? "Additive" : projectSettings.CompositeBlendMode
},
SimulationLayers = hasEmbeddedSimulationGroups
? new List<LayerConfigSimulationLayer>()
: simulationLayers.Select(FromEditorSimulationLayer).ToList()
};
if (!hasEmbeddedSimulationGroups && file.SimulationLayers.Count == 0)
{
file.SimulationLayers = new List<LayerConfigSimulationLayer>
{
new()
{
Id = Guid.NewGuid(),
LayerType = nameof(LayerEditorSimulationLayerType.Life),
Name = "Life Sim",
Enabled = true,
InputFunction = "Direct",
BlendMode = "Additive",
InjectionMode = "Threshold",
LifeMode = "NaiveGrayscale",
BinningMode = "Fill",
InjectionNoise = 0,
LifeOpacity = 1.0,
RgbHueShiftDegrees = 0,
RgbHueShiftSpeedDegreesPerSecond = 0,
AudioFrequencyHueShiftDegrees = 0,
ReactiveMappings = new List<LayerConfigReactiveMapping>(),
ThresholdMin = 0.35,
ThresholdMax = 0.75,
InvertThreshold = false,
PixelSortCellWidth = 12,
PixelSortCellHeight = 8
}
};
}
foreach (var source in sourceList)
{
file.Sources.Add(FromEditorSource(source));
}
return file;
}
public List<LayerEditorSource> ToEditorSources()
{
var list = new List<LayerEditorSource>(Sources.Count);
foreach (var source in Sources)
{
list.Add(ToEditorSource(source, null));
}
if (!list.Any(source => source.Kind == LayerEditorSourceKind.SimGroup) &&
SimulationLayers != null &&
SimulationLayers.Count > 0)
{
var simGroup = new LayerEditorSource
{
Id = Guid.NewGuid(),
Kind = LayerEditorSourceKind.SimGroup,
DisplayName = "Simulation",
Enabled = true,
BlendMode = "Additive",
FitMode = "Fill",
Opacity = 1.0,
KeyColorHex = "#000000",
KeyTolerance = 0.1
};
foreach (var simulationLayer in ToEditorSimulationLayers())
{
simGroup.SimulationLayers.Add(simulationLayer);
}
list.Add(simGroup);
}
return list;
}
public List<LayerEditorSimulationLayer> ToEditorSimulationLayers()
{
if (SimulationLayers != null && SimulationLayers.Count > 0)
{
return FlattenSimulationLayers(SimulationLayers)
.Select(layer => ToEditorSimulationLayer(layer, null))
.ToList();
}
return BuildLegacyEditorSimulationLayers();
}
public LayerEditorProjectSettings ToEditorProjectSettings()
{
var settings = ProjectSettings ?? new LayerConfigProjectSettings();
return new LayerEditorProjectSettings
{
Height = Math.Max(1, settings.Height),
Depth = Math.Max(1, settings.Depth),
Framerate = settings.Framerate,
LifeOpacity = Math.Clamp(settings.LifeOpacity, 0, 1),
RgbHueShiftDegrees = settings.RgbHueShiftDegrees,
RgbHueShiftSpeedDegreesPerSecond = settings.RgbHueShiftSpeedDegreesPerSecond,
InvertComposite = settings.InvertComposite,
Passthrough = settings.Passthrough,
CompositeBlendMode = string.IsNullOrWhiteSpace(settings.CompositeBlendMode) ? "Additive" : settings.CompositeBlendMode
};
}
private static LayerConfigSource FromEditorSource(LayerEditorSource source)
{
var config = new LayerConfigSource
{
Type = source.Kind.ToString(),
Enabled = source.Enabled,
WindowTitle = source.WindowTitle,
WebcamId = source.WebcamId,
FilePath = source.FilePath,
DisplayName = source.DisplayName,
BlendMode = source.BlendMode,
FitMode = source.FitMode,
Opacity = source.Opacity,
VideoAudioEnabled = source.VideoAudioEnabled,
VideoAudioVolume = source.VideoAudioVolume,
Mirror = source.Mirror,
KeyEnabled = source.KeyEnabled,
KeyColor = source.KeyColorHex,
KeyTolerance = source.KeyTolerance
};
if (source.FilePaths.Count > 0)
{
config.FilePaths = new List<string>(source.FilePaths);
}
foreach (var animation in source.Animations)
{
config.Animations.Add(new LayerConfigAnimation
{
Type = animation.Type,
Loop = animation.Loop,
Speed = animation.Speed,
TranslateDirection = animation.TranslateDirection,
RotationDirection = animation.RotationDirection,
RotationDegrees = animation.RotationDegrees,
DvdScale = animation.DvdScale,
BeatShakeIntensity = animation.BeatShakeIntensity,
AudioGranularLowGain = animation.AudioGranularLowGain,
AudioGranularMidGain = animation.AudioGranularMidGain,
AudioGranularHighGain = animation.AudioGranularHighGain,
BeatsPerCycle = animation.BeatsPerCycle
});
}
foreach (var child in source.Children)
{
config.Children.Add(FromEditorSource(child));
}
foreach (var simulationLayer in source.SimulationLayers)
{
config.SimulationLayers.Add(FromEditorSimulationLayer(simulationLayer));
}
return config;
}
private static LayerConfigSimulationLayer FromEditorSimulationLayer(LayerEditorSimulationLayer layer)
{
var config = new LayerConfigSimulationLayer
{
Id = layer.Id,
Kind = layer.Kind.ToString(),
LayerType = layer.LayerType.ToString(),
Name = string.IsNullOrWhiteSpace(layer.Name)
? (layer.IsGroup ? "Sim Group" : layer.TypeLabel)
: layer.Name,
Enabled = layer.Enabled,
InputFunction = string.IsNullOrWhiteSpace(layer.InputFunction) ? "Direct" : layer.InputFunction,
BlendMode = string.IsNullOrWhiteSpace(layer.BlendMode) ? "Subtractive" : layer.BlendMode,
InjectionMode = string.IsNullOrWhiteSpace(layer.InjectionMode) ? "Threshold" : layer.InjectionMode,
LifeMode = string.IsNullOrWhiteSpace(layer.LifeMode) ? "NaiveGrayscale" : layer.LifeMode,
BinningMode = string.IsNullOrWhiteSpace(layer.BinningMode) ? "Fill" : layer.BinningMode,
InjectionNoise = Math.Clamp(layer.InjectionNoise, 0, 1),
LifeOpacity = Math.Clamp(layer.LifeOpacity, 0, 1),
RgbHueShiftDegrees = layer.RgbHueShiftDegrees,
RgbHueShiftSpeedDegreesPerSecond = layer.RgbHueShiftSpeedDegreesPerSecond,
AudioFrequencyHueShiftDegrees = 0,
ReactiveMappings = layer.ReactiveMappings
.Select(mapping => new LayerConfigReactiveMapping
{
Id = mapping.Id,
Input = string.IsNullOrWhiteSpace(mapping.Input) ? nameof(SimulationReactiveInput.Level) : mapping.Input,
Output = string.IsNullOrWhiteSpace(mapping.Output) ? nameof(SimulationReactiveOutput.Opacity) : mapping.Output,
Amount = mapping.Amount,
ThresholdMin = mapping.ThresholdMin,
ThresholdMax = mapping.ThresholdMax
})
.ToList(),
ThresholdMin = Math.Clamp(layer.ThresholdMin, 0, 1),
ThresholdMax = Math.Clamp(layer.ThresholdMax, 0, 1),
InvertThreshold = layer.InvertThreshold,
PixelSortCellWidth = Math.Clamp(layer.PixelSortCellWidth, 1, 4096),
PixelSortCellHeight = Math.Clamp(layer.PixelSortCellHeight, 1, 4096)
};
foreach (var child in layer.Children)
{
config.Children.Add(FromEditorSimulationLayer(child));
}
return config;
}
private static LayerEditorSource ToEditorSource(LayerConfigSource config, LayerEditorSource? parent)
{
var kind = ResolveKind(config);
var model = new LayerEditorSource
{
Id = Guid.NewGuid(),
Kind = kind,
Enabled = config.Enabled,
DisplayName = config.DisplayName ?? string.Empty,
WindowTitle = config.WindowTitle,
WebcamId = config.WebcamId,
FilePath = config.FilePath,
BlendMode = string.IsNullOrWhiteSpace(config.BlendMode) ? "Additive" : config.BlendMode,
FitMode = string.IsNullOrWhiteSpace(config.FitMode) ? "Fill" : config.FitMode,
Opacity = Math.Clamp(config.Opacity, 0, 1),
VideoAudioEnabled = config.VideoAudioEnabled,
VideoAudioVolume = Math.Clamp(config.VideoAudioVolume, 0, 1),
Mirror = config.Mirror,
KeyEnabled = config.KeyEnabled,
KeyColorHex = string.IsNullOrWhiteSpace(config.KeyColor) ? "#000000" : config.KeyColor,
KeyTolerance = Math.Clamp(config.KeyTolerance, 0, 1),
Parent = parent
};
if (config.FilePaths.Count > 0)
{
model.FilePaths.AddRange(config.FilePaths);
}
if (kind == LayerEditorSourceKind.Window &&
string.IsNullOrWhiteSpace(model.WindowTitle) &&
!string.IsNullOrWhiteSpace(model.DisplayName))
{
model.WindowTitle = model.DisplayName;
}
foreach (var animation in config.Animations)
{
model.Animations.Add(new LayerEditorAnimation
{
Id = Guid.NewGuid(),
Type = string.IsNullOrWhiteSpace(animation.Type) ? "ZoomIn" : animation.Type,
Loop = string.IsNullOrWhiteSpace(animation.Loop) ? "Forward" : animation.Loop,
Speed = string.IsNullOrWhiteSpace(animation.Speed) ? "Normal" : animation.Speed,
TranslateDirection = string.IsNullOrWhiteSpace(animation.TranslateDirection) ? "Right" : animation.TranslateDirection,
RotationDirection = string.IsNullOrWhiteSpace(animation.RotationDirection) ? "Clockwise" : animation.RotationDirection,
RotationDegrees = animation.RotationDegrees,
DvdScale = animation.DvdScale,
BeatShakeIntensity = animation.BeatShakeIntensity,
AudioGranularLowGain = animation.AudioGranularLowGain,
AudioGranularMidGain = animation.AudioGranularMidGain,
AudioGranularHighGain = animation.AudioGranularHighGain,
BeatsPerCycle = animation.BeatsPerCycle,
Parent = model
});
}
foreach (var child in config.Children)
{
model.Children.Add(ToEditorSource(child, model));
}
foreach (var simulationLayer in FlattenSimulationLayers(config.SimulationLayers))
{
model.SimulationLayers.Add(ToEditorSimulationLayer(simulationLayer, null));
}
return model;
}
private static LayerEditorSimulationLayer ToEditorSimulationLayer(LayerConfigSimulationLayer layer, LayerEditorSimulationLayer? parent)
{
var reactiveMappings = (layer.ReactiveMappings ?? new List<LayerConfigReactiveMapping>())
.Select(mapping => new LayerEditorSimulationReactiveMapping
{
Id = mapping.Id == Guid.Empty ? Guid.NewGuid() : mapping.Id,
Input = string.IsNullOrWhiteSpace(mapping.Input) ? nameof(SimulationReactiveInput.Level) : mapping.Input,
Output = string.IsNullOrWhiteSpace(mapping.Output) ? nameof(SimulationReactiveOutput.Opacity) : mapping.Output,
Amount = SimulationReactivity.ClampAmount(
ParseReactiveOutputOrDefault(mapping.Output, SimulationReactiveOutput.Opacity),
mapping.Amount),
ThresholdMin = Math.Clamp(mapping.ThresholdMin, 0, 1),
ThresholdMax = Math.Clamp(mapping.ThresholdMax, 0, 1)
})
.ToList();
if (reactiveMappings.Count == 0 && layer.AudioFrequencyHueShiftDegrees > 0.001)
{
reactiveMappings.Add(new LayerEditorSimulationReactiveMapping
{
Id = Guid.NewGuid(),
Input = nameof(SimulationReactiveInput.Frequency),
Output = nameof(SimulationReactiveOutput.HueShift),
Amount = Math.Clamp(layer.AudioFrequencyHueShiftDegrees, 0, 360),
ThresholdMin = 0,
ThresholdMax = 1
});
}
var model = new LayerEditorSimulationLayer
{
Id = layer.Id == Guid.Empty ? Guid.NewGuid() : layer.Id,
Kind = ParseSimulationItemKind(layer.Kind),
LayerType = ParseSimulationLayerType(layer.LayerType),
Name = string.IsNullOrWhiteSpace(layer.Name)
? (string.Equals(layer.Kind, nameof(LayerEditorSimulationItemKind.Group), StringComparison.OrdinalIgnoreCase)
? "Sim Group"
: ResolveDefaultSimulationLayerName(ParseSimulationLayerType(layer.LayerType)))
: layer.Name,
Enabled = layer.Enabled,
InputFunction = string.IsNullOrWhiteSpace(layer.InputFunction) ? "Direct" : layer.InputFunction,
BlendMode = string.IsNullOrWhiteSpace(layer.BlendMode) ? "Subtractive" : layer.BlendMode,
InjectionMode = string.IsNullOrWhiteSpace(layer.InjectionMode) ? "Threshold" : layer.InjectionMode,
LifeMode = string.IsNullOrWhiteSpace(layer.LifeMode) ? "NaiveGrayscale" : layer.LifeMode,
BinningMode = string.IsNullOrWhiteSpace(layer.BinningMode) ? "Fill" : layer.BinningMode,
InjectionNoise = Math.Clamp(layer.InjectionNoise, 0, 1),
LifeOpacity = Math.Clamp(layer.LifeOpacity, 0, 1),
RgbHueShiftDegrees = layer.RgbHueShiftDegrees,
RgbHueShiftSpeedDegreesPerSecond = layer.RgbHueShiftSpeedDegreesPerSecond,
AudioFrequencyHueShiftDegrees = 0,
ReactiveMappings = new ObservableCollection<LayerEditorSimulationReactiveMapping>(reactiveMappings),
ThresholdMin = Math.Clamp(layer.ThresholdMin, 0, 1),
ThresholdMax = Math.Clamp(layer.ThresholdMax, 0, 1),
InvertThreshold = layer.InvertThreshold,
PixelSortCellWidth = Math.Clamp(layer.PixelSortCellWidth > 0 ? layer.PixelSortCellWidth : layer.PixelSortGridColumns, 1, 4096),
PixelSortCellHeight = Math.Clamp(layer.PixelSortCellHeight > 0 ? layer.PixelSortCellHeight : layer.PixelSortGridRows, 1, 4096),
Parent = parent
};
foreach (var child in layer.Children)
{
model.Children.Add(ToEditorSimulationLayer(child, model));
}
return model;
}
private static IEnumerable<LayerEditorSource> EnumerateSources(IEnumerable<LayerEditorSource> roots)
{
foreach (var source in roots)
{
yield return source;
foreach (var child in EnumerateSources(source.Children))
{
yield return child;
}
}
}
private static List<LayerConfigSimulationLayer> FlattenSimulationLayers(IEnumerable<LayerConfigSimulationLayer> layers)
{
var flattened = new List<LayerConfigSimulationLayer>();
foreach (var layer in layers)
{
FlattenSimulationLayer(layer, flattened, ancestorEnabled: true);
}
return flattened;
}
private static void FlattenSimulationLayer(LayerConfigSimulationLayer layer, ICollection<LayerConfigSimulationLayer> target, bool ancestorEnabled)
{
bool enabled = ancestorEnabled && layer.Enabled;
if (ParseSimulationItemKind(layer.Kind) == LayerEditorSimulationItemKind.Group)
{
foreach (var child in layer.Children)
{
FlattenSimulationLayer(child, target, enabled);
}
return;
}
var flattened = new LayerConfigSimulationLayer
{
Id = layer.Id,
Kind = nameof(LayerEditorSimulationItemKind.Layer),
LayerType = layer.LayerType,
Name = layer.Name,
Enabled = enabled,
InputFunction = layer.InputFunction,
BlendMode = layer.BlendMode,
InjectionMode = layer.InjectionMode,
LifeMode = layer.LifeMode,
BinningMode = layer.BinningMode,
InjectionNoise = layer.InjectionNoise,
LifeOpacity = layer.LifeOpacity,
RgbHueShiftDegrees = layer.RgbHueShiftDegrees,
RgbHueShiftSpeedDegreesPerSecond = layer.RgbHueShiftSpeedDegreesPerSecond,
AudioFrequencyHueShiftDegrees = 0,
ReactiveMappings = layer.ReactiveMappings.Select(mapping => new LayerConfigReactiveMapping
{
Id = mapping.Id,
Input = mapping.Input,
Output = mapping.Output,
Amount = mapping.Amount,
ThresholdMin = mapping.ThresholdMin,
ThresholdMax = mapping.ThresholdMax
}).ToList(),
ThresholdMin = layer.ThresholdMin,
ThresholdMax = layer.ThresholdMax,
InvertThreshold = layer.InvertThreshold,
PixelSortCellWidth = layer.PixelSortCellWidth,
PixelSortCellHeight = layer.PixelSortCellHeight
};
target.Add(flattened);
}
private static LayerEditorSourceKind ResolveKind(LayerConfigSource config)
{
if (Enum.TryParse<LayerEditorSourceKind>(config.Type, true, out var parsed))
{
return parsed;
}
if (!string.IsNullOrWhiteSpace(config.FilePath) &&
config.FilePath.StartsWith("youtube:", StringComparison.OrdinalIgnoreCase))
{
return LayerEditorSourceKind.Youtube;
}
return LayerEditorSourceKind.File;
}
private List<LayerEditorSimulationLayer> BuildLegacyEditorSimulationLayers()
{
string positiveBlend = string.IsNullOrWhiteSpace(PositiveLayerBlendMode) ? "Additive" : PositiveLayerBlendMode;
string negativeBlend = string.IsNullOrWhiteSpace(NegativeLayerBlendMode) ? "Subtractive" : NegativeLayerBlendMode;
var order = BuildLegacyOrder(SimulationLayerOrder);
var byKey = new Dictionary<string, LayerEditorSimulationLayer>(StringComparer.OrdinalIgnoreCase)
{
["Positive"] = new LayerEditorSimulationLayer
{
Id = Guid.NewGuid(),
LayerType = LayerEditorSimulationLayerType.Life,
Name = "Life Sim",
Enabled = PositiveLayerEnabled,
InputFunction = "Direct",
BlendMode = positiveBlend,
InjectionMode = string.IsNullOrWhiteSpace(ProjectSettings?.InjectionMode) ? "Threshold" : ProjectSettings.InjectionMode,
LifeMode = string.IsNullOrWhiteSpace(ProjectSettings?.LifeMode) ? "NaiveGrayscale" : ProjectSettings.LifeMode,
BinningMode = string.IsNullOrWhiteSpace(ProjectSettings?.BinningMode) ? "Fill" : ProjectSettings.BinningMode,
InjectionNoise = Math.Clamp(ProjectSettings?.InjectionNoise ?? 0, 0, 1),
LifeOpacity = 1.0,
RgbHueShiftDegrees = ProjectSettings?.RgbHueShiftDegrees ?? 0,
RgbHueShiftSpeedDegreesPerSecond = ProjectSettings?.RgbHueShiftSpeedDegreesPerSecond ?? 0,
AudioFrequencyHueShiftDegrees = 0,
ReactiveMappings = new ObservableCollection<LayerEditorSimulationReactiveMapping>(),
ThresholdMin = 0.35,
ThresholdMax = 0.75,
InvertThreshold = false,
PixelSortCellWidth = 12,
PixelSortCellHeight = 8
}
};
var list = new List<LayerEditorSimulationLayer>(2);
foreach (var key in order)
{
if (byKey.TryGetValue(key, out var layer))
{
list.Add(layer);
}
}
if (list.Count == 0)
{
list.AddRange(byKey.Values);
}
return list;
}
private static List<string> BuildLegacyOrder(IReadOnlyList<string>? order)
{
var normalized = new List<string>();
if (order != null)
{
foreach (var value in order)
{
if (string.IsNullOrWhiteSpace(value))
{
continue;
}
if (value.Equals("Positive", StringComparison.OrdinalIgnoreCase) &&
!normalized.Contains("Positive", StringComparer.OrdinalIgnoreCase))
{
normalized.Add("Positive");
}
else if (value.Equals("Negative", StringComparison.OrdinalIgnoreCase) &&
!normalized.Contains("Negative", StringComparer.OrdinalIgnoreCase))
{
normalized.Add("Negative");
}
}
}
if (!normalized.Contains("Positive", StringComparer.OrdinalIgnoreCase))
{
normalized.Add("Positive");
}
if (!normalized.Contains("Negative", StringComparer.OrdinalIgnoreCase))
{
normalized.Add("Negative");
}
return normalized;
}
private static SimulationReactiveOutput ParseReactiveOutputOrDefault(string? value, SimulationReactiveOutput fallback)
{
if (!string.IsNullOrWhiteSpace(value) &&
Enum.TryParse<SimulationReactiveOutput>(value, true, out var parsed))
{
return parsed;
}
return fallback;
}
private static LayerEditorSimulationItemKind ParseSimulationItemKind(string? value)
{
return Enum.TryParse<LayerEditorSimulationItemKind>(value, true, out var parsed)
? parsed
: LayerEditorSimulationItemKind.Layer;
}
private static LayerEditorSimulationLayerType ParseSimulationLayerType(string? value)
{
return Enum.TryParse<LayerEditorSimulationLayerType>(value, true, out var parsed)
? parsed
: LayerEditorSimulationLayerType.Life;
}
private static string ResolveDefaultSimulationLayerName(LayerEditorSimulationLayerType layerType)
{
return layerType == LayerEditorSimulationLayerType.PixelSort ? "Pixel Sort" : "Life Sim";
}
}
internal sealed class LayerConfigSource
{
public string? Type { get; set; }
public bool Enabled { get; set; } = true;
public string? WindowTitle { get; set; }
public string? WebcamId { get; set; }
public string? FilePath { get; set; }
public List<string> FilePaths { get; set; } = new();
public string? DisplayName { get; set; }
public string? BlendMode { get; set; }
public string? FitMode { get; set; }
public double Opacity { get; set; } = 1.0;
public bool VideoAudioEnabled { get; set; }
public double VideoAudioVolume { get; set; } = 1.0;
public bool Mirror { get; set; }
public bool KeyEnabled { get; set; }
public string? KeyColor { get; set; }
public double KeyTolerance { get; set; } = 0.1;
public List<LayerConfigAnimation> Animations { get; set; } = new();
public List<LayerConfigSimulationLayer> SimulationLayers { get; set; } = new();
public List<LayerConfigSource> Children { get; set; } = new();
}
internal sealed class LayerConfigAnimation
{
public string? Type { get; set; }
public string? Loop { get; set; }
public string? Speed { get; set; }
public string? TranslateDirection { get; set; }
public string? RotationDirection { get; set; }
public double RotationDegrees { get; set; } = 12.0;
public double DvdScale { get; set; } = 0.2;
public double BeatShakeIntensity { get; set; } = 1.0;
public double AudioGranularLowGain { get; set; } = 1.0;
public double AudioGranularMidGain { get; set; } = 1.0;
public double AudioGranularHighGain { get; set; } = 1.0;
public double BeatsPerCycle { get; set; } = 1.0;
}
internal sealed class LayerConfigSimulationLayer
{
public Guid Id { get; set; }
public string Kind { get; set; } = nameof(LayerEditorSimulationItemKind.Layer);
public string LayerType { get; set; } = nameof(LayerEditorSimulationLayerType.Life);
public string Name { get; set; } = "Life Sim";
public bool Enabled { get; set; } = true;
public string InputFunction { get; set; } = "Direct";
public string BlendMode { get; set; } = "Subtractive";
public string InjectionMode { get; set; } = "Threshold";
public string LifeMode { get; set; } = "NaiveGrayscale";
public string BinningMode { get; set; } = "Fill";
public double InjectionNoise { get; set; }
public double LifeOpacity { get; set; } = 1.0;
public double RgbHueShiftDegrees { get; set; }
public double RgbHueShiftSpeedDegreesPerSecond { get; set; }
public double AudioFrequencyHueShiftDegrees { get; set; }
public List<LayerConfigReactiveMapping> ReactiveMappings { get; set; } = new();
public double ThresholdMin { get; set; } = 0.35;
public double ThresholdMax { get; set; } = 0.75;
public bool InvertThreshold { get; set; }
public int PixelSortCellWidth { get; set; } = 12;
public int PixelSortCellHeight { get; set; } = 8;
public int PixelSortGridColumns { get; set; }
public int PixelSortGridRows { get; set; }
public List<LayerConfigSimulationLayer> Children { get; set; } = new();
}
internal sealed class LayerConfigReactiveMapping
{
public Guid Id { get; set; }
public string Input { get; set; } = nameof(SimulationReactiveInput.Level);
public string Output { get; set; } = nameof(SimulationReactiveOutput.Opacity);
public double Amount { get; set; } = 1.0;
public double ThresholdMin { get; set; }
public double ThresholdMax { get; set; } = 1.0;
}
internal sealed class LayerConfigProjectSettings
{
public int Height { get; set; } = 144;
public int Depth { get; set; } = 24;
public double Framerate { get; set; } = 60;
public string LifeMode { get; set; } = "NaiveGrayscale";
public string BinningMode { get; set; } = "Fill";
public string InjectionMode { get; set; } = "Threshold";
public double InjectionNoise { get; set; }
public double LifeOpacity { get; set; } = 1.0;
public double RgbHueShiftDegrees { get; set; }
public double RgbHueShiftSpeedDegreesPerSecond { get; set; }
public bool InvertComposite { get; set; }
public bool Passthrough { get; set; }
public string CompositeBlendMode { get; set; } = "Additive";
}