/
Layer.cs
940 lines (774 loc) · 30.4 KB
/
Layer.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
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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Artemis.Core.LayerBrushes;
using Artemis.Core.LayerEffects;
using Artemis.Storage.Entities.Profile;
using Artemis.Storage.Entities.Profile.Abstract;
using RGB.NET.Core;
using SkiaSharp;
namespace Artemis.Core;
/// <summary>
/// Represents a layer in a <see cref="Profile" />
/// </summary>
public sealed class Layer : RenderProfileElement
{
private const string BROKEN_STATE_BRUSH_NOT_FOUND = "Failed to load layer brush, ensure the plugin is enabled";
private const string BROKEN_STATE_INIT_FAILED = "Failed to initialize layer brush";
private readonly List<Layer> _renderCopies = new();
private LayerGeneralProperties _general = new();
private LayerTransformProperties _transform = new();
private BaseLayerBrush? _layerBrush;
private LayerShape? _layerShape;
private List<ArtemisLed> _leds = new();
private List<LedEntity> _missingLeds = new();
/// <summary>
/// Creates a new instance of the <see cref="Layer" /> class and adds itself to the child collection of the provided
/// <paramref name="parent" />
/// </summary>
/// <param name="parent">The parent of the layer</param>
/// <param name="name">The name of the layer</param>
public Layer(ProfileElement parent, string name) : base(parent, parent.Profile)
{
LayerEntity = new LayerEntity();
EntityId = Guid.NewGuid();
Profile = Parent.Profile;
Name = name;
Suspended = false;
Leds = new ReadOnlyCollection<ArtemisLed>(_leds);
Adapter = new LayerAdapter(this);
Initialize();
}
/// <summary>
/// Creates a new instance of the <see cref="Layer" /> class based on the provided layer entity
/// </summary>
/// <param name="profile">The profile the layer belongs to</param>
/// <param name="parent">The parent of the layer</param>
/// <param name="layerEntity">The entity of the layer</param>
/// <param name="loadNodeScript">A boolean indicating whether or not to attempt to load the node script straight away</param>
public Layer(Profile profile, ProfileElement parent, LayerEntity layerEntity, bool loadNodeScript = false) : base(parent, parent.Profile)
{
LayerEntity = layerEntity;
EntityId = layerEntity.Id;
Profile = profile;
Parent = parent;
Leds = new ReadOnlyCollection<ArtemisLed>(_leds);
Adapter = new LayerAdapter(this);
Load();
Initialize();
if (loadNodeScript)
LoadNodeScript();
}
/// <summary>
/// Creates a new instance of the <see cref="Layer" /> class by copying the provided <paramref name="source" />.
/// </summary>
/// <param name="source">The layer to copy</param>
private Layer(Layer source) : base(source, source.Profile)
{
LayerEntity = source.LayerEntity;
Profile = source.Profile;
Parent = source;
// TODO: move to top
_renderCopies = new List<Layer>();
_general = new LayerGeneralProperties();
_transform = new LayerTransformProperties();
_leds = new List<ArtemisLed>();
Leds = new ReadOnlyCollection<ArtemisLed>(_leds);
Adapter = new LayerAdapter(this);
Load();
Initialize();
Timeline.JumpToStart();
AddLeds(source.Leds);
Enable();
// After loading using the source entity create a new entity so the next call to Save won't mess with the source, just in case.
LayerEntity = new LayerEntity();
}
/// <summary>
/// A collection of all the LEDs this layer is assigned to.
/// </summary>
public ReadOnlyCollection<ArtemisLed> Leds { get; private set; }
/// <summary>
/// Defines the shape that is rendered by the <see cref="LayerBrush" />.
/// </summary>
public LayerShape? LayerShape
{
get => _layerShape;
set
{
SetAndNotify(ref _layerShape, value);
if (Path != null)
CalculateRenderProperties();
}
}
/// <summary>
/// Gets the general properties of the layer
/// </summary>
[PropertyGroupDescription(Identifier = "General", Name = "General", Description = "A collection of general properties")]
public LayerGeneralProperties General
{
get => _general;
private set => SetAndNotify(ref _general, value);
}
/// <summary>
/// Gets the transform properties of the layer
/// </summary>
[PropertyGroupDescription(Identifier = "Transform", Name = "Transform", Description = "A collection of transformation properties")]
public LayerTransformProperties Transform
{
get => _transform;
private set => SetAndNotify(ref _transform, value);
}
/// <summary>
/// The brush that will fill the <see cref="LayerShape" />.
/// </summary>
public BaseLayerBrush? LayerBrush
{
get => _layerBrush;
internal set => SetAndNotify(ref _layerBrush, value);
}
/// <summary>
/// Gets the layer entity this layer uses for persistent storage
/// </summary>
public LayerEntity LayerEntity { get; internal set; }
/// <summary>
/// Gets the layer adapter that can be used to adapt this layer to a different set of devices
/// </summary>
public LayerAdapter Adapter { get; }
/// <inheritdoc />
public override bool ShouldBeEnabled => !Suspended && DisplayConditionMet && HasBounds;
internal override RenderElementEntity RenderElementEntity => LayerEntity;
private bool HasBounds => Bounds.Width > 0 && Bounds.Height > 0;
/// <inheritdoc />
public override List<ILayerProperty> GetAllLayerProperties()
{
List<ILayerProperty> result = new();
result.AddRange(General.GetAllLayerProperties());
result.AddRange(Transform.GetAllLayerProperties());
if (LayerBrush?.BaseProperties != null)
result.AddRange(LayerBrush.BaseProperties.GetAllLayerProperties());
foreach (BaseLayerEffect layerEffect in LayerEffects)
{
if (layerEffect.BaseProperties != null)
result.AddRange(layerEffect.BaseProperties.GetAllLayerProperties());
}
return result;
}
/// <inheritdoc />
public override string ToString()
{
return $"[Layer] {nameof(Name)}: {Name}, {nameof(Order)}: {Order}";
}
/// <inheritdoc />
public override IEnumerable<PluginFeature> GetFeatureDependencies()
{
return LayerEffects.SelectMany(e => e.GetFeatureDependencies())
.Concat(LayerBrush?.GetFeatureDependencies() ?? [])
.Concat(General.GetFeatureDependencies())
.Concat(Transform.GetFeatureDependencies())
.Concat(DisplayCondition.GetFeatureDependencies());
}
/// <summary>
/// Occurs when a property affecting the rendering properties of this layer has been updated
/// </summary>
public event EventHandler? RenderPropertiesUpdated;
/// <summary>
/// Occurs when the layer brush of this layer has been updated
/// </summary>
public event EventHandler? LayerBrushUpdated;
#region Overrides of BreakableModel
/// <inheritdoc />
public override IEnumerable<IBreakableModel> GetBrokenHierarchy()
{
if (LayerBrush?.BrokenState != null)
yield return LayerBrush;
foreach (BaseLayerEffect baseLayerEffect in LayerEffects.Where(e => e.BrokenState != null))
yield return baseLayerEffect;
}
#endregion
/// <inheritdoc />
protected override void Dispose(bool disposing)
{
Disable();
Disposed = true;
LayerBrushStore.LayerBrushAdded -= LayerBrushStoreOnLayerBrushAdded;
LayerBrushStore.LayerBrushRemoved -= LayerBrushStoreOnLayerBrushRemoved;
// Brush first in case it depends on any of the other disposables during it's own disposal
_layerBrush?.Dispose();
_general.Dispose();
_transform.Dispose();
base.Dispose(disposing);
}
internal void OnLayerBrushUpdated()
{
LayerBrushUpdated?.Invoke(this, EventArgs.Empty);
}
private void Initialize()
{
LayerBrushStore.LayerBrushAdded += LayerBrushStoreOnLayerBrushAdded;
LayerBrushStore.LayerBrushRemoved += LayerBrushStoreOnLayerBrushRemoved;
// Layers have two hardcoded property groups, instantiate them
PropertyGroupDescriptionAttribute generalAttribute = (PropertyGroupDescriptionAttribute) Attribute.GetCustomAttribute(
GetType().GetProperty(nameof(General))!,
typeof(PropertyGroupDescriptionAttribute)
)!;
PropertyGroupDescriptionAttribute transformAttribute = (PropertyGroupDescriptionAttribute) Attribute.GetCustomAttribute(
GetType().GetProperty(nameof(Transform))!,
typeof(PropertyGroupDescriptionAttribute)
)!;
LayerEntity.GeneralPropertyGroup ??= new PropertyGroupEntity {Identifier = generalAttribute.Identifier!};
LayerEntity.TransformPropertyGroup ??= new PropertyGroupEntity {Identifier = transformAttribute.Identifier!};
General.Initialize(this, null, generalAttribute, LayerEntity.GeneralPropertyGroup);
Transform.Initialize(this, null, transformAttribute, LayerEntity.TransformPropertyGroup);
General.ShapeType.CurrentValueSet += ShapeTypeOnCurrentValueSet;
ApplyShapeType();
ActivateLayerBrush();
Reset();
}
private void LayerBrushStoreOnLayerBrushRemoved(object? sender, LayerBrushStoreEvent e)
{
if (LayerBrush?.Descriptor == e.Registration.LayerBrushDescriptor)
{
DeactivateLayerBrush();
SetBrokenState(BROKEN_STATE_BRUSH_NOT_FOUND);
}
}
private void LayerBrushStoreOnLayerBrushAdded(object? sender, LayerBrushStoreEvent e)
{
if (LayerBrush != null || !General.PropertiesInitialized)
return;
LayerBrushReference? current = General.BrushReference.CurrentValue;
if (e.Registration.PluginFeature.Id == current?.LayerBrushProviderId && e.Registration.LayerBrushDescriptor.LayerBrushType.Name == current.BrushType)
ActivateLayerBrush();
}
private void OnRenderPropertiesUpdated()
{
RenderPropertiesUpdated?.Invoke(this, EventArgs.Empty);
}
#region Storage
internal override void Load()
{
EntityId = LayerEntity.Id;
Name = LayerEntity.Name;
Suspended = LayerEntity.Suspended;
Order = LayerEntity.Order;
LoadRenderElement();
Adapter.Load();
}
internal override void Save()
{
if (Disposed)
throw new ObjectDisposedException("Layer");
// Properties
LayerEntity.Id = EntityId;
LayerEntity.ParentId = Parent?.EntityId ?? new Guid();
LayerEntity.Order = Order;
LayerEntity.Suspended = Suspended;
LayerEntity.Name = Name;
General.ApplyToEntity();
Transform.ApplyToEntity();
// Don't override the old value of LayerBrush if the current value is null, this avoid losing settings of an unavailable brush
if (LayerBrush != null)
{
LayerBrush.Save();
LayerEntity.LayerBrush = LayerBrush.LayerBrushEntity;
}
// LEDs
LayerEntity.Leds.Clear();
SaveMissingLeds();
foreach (ArtemisLed artemisLed in Leds)
{
LedEntity ledEntity = new()
{
DeviceIdentifier = artemisLed.Device.Identifier,
LedName = artemisLed.RgbLed.Id.ToString(),
PhysicalLayout = artemisLed.Device.DeviceType == RGBDeviceType.Keyboard ? (int) artemisLed.Device.PhysicalLayout : null
};
LayerEntity.Leds.Add(ledEntity);
}
// Adaption hints
Adapter.Save();
SaveRenderElement();
}
#endregion
#region Shape management
private void ShapeTypeOnCurrentValueSet(object? sender, EventArgs e)
{
ApplyShapeType();
}
private void ApplyShapeType()
{
LayerShape = General.ShapeType.CurrentValue switch
{
LayerShapeType.Ellipse => new EllipseShape(this),
LayerShapeType.Rectangle => new RectangleShape(this),
_ => throw new ArgumentOutOfRangeException()
};
}
#endregion
#region Rendering
/// <inheritdoc />
public override void Update(double deltaTime)
{
if (Disposed)
throw new ObjectDisposedException("Layer");
if (Timeline.IsOverridden)
{
Timeline.ClearOverride();
return;
}
try
{
UpdateDisplayCondition();
UpdateTimeline(deltaTime);
if (ShouldBeEnabled)
Enable();
else if (Suspended || !HasBounds || (Timeline.IsFinished && !_renderCopies.Any()))
Disable();
if (!Enabled || Timeline.Delta == TimeSpan.Zero)
return;
General.Update(Timeline);
Transform.Update(Timeline);
LayerBrush?.InternalUpdate(Timeline);
foreach (BaseLayerEffect baseLayerEffect in LayerEffects)
{
if (!baseLayerEffect.Suspended)
baseLayerEffect.InternalUpdate(Timeline);
}
// Remove render copies that finished their timeline and update the rest
for (int index = 0; index < _renderCopies.Count; index++)
{
Layer child = _renderCopies[index];
if (!child.Timeline.IsFinished)
{
child.Update(deltaTime);
}
else
{
_renderCopies.Remove(child);
child.Dispose();
index--;
}
}
}
finally
{
Timeline.ClearDelta();
}
}
/// <inheritdoc />
public override void Render(SKCanvas canvas, SKPointI basePosition, ProfileElement? editorFocus)
{
if (Disposed)
throw new ObjectDisposedException("Layer");
if (editorFocus != null && editorFocus != this)
return;
RenderLayer(canvas, basePosition);
RenderCopies(canvas, basePosition);
}
private void RenderLayer(SKCanvas canvas, SKPointI basePosition)
{
// Ensure the layer is ready
if (!Enabled || Path == null || LayerShape?.Path == null || !General.PropertiesInitialized || !Transform.PropertiesInitialized || !Leds.Any())
return;
// Ensure the brush is ready
if (LayerBrush == null || LayerBrush?.BaseProperties?.PropertiesInitialized == false)
return;
if (Timeline.IsFinished || LayerBrush?.BrushType != LayerBrushType.Regular)
return;
SKPaint layerPaint = new() {FilterQuality = SKFilterQuality.Low};
try
{
using SKAutoCanvasRestore _ = new(canvas);
canvas.Translate(Bounds.Left - basePosition.X, Bounds.Top - basePosition.Y);
using SKPath clipPath = new(Path);
clipPath.Transform(SKMatrix.CreateTranslation(Bounds.Left * -1, Bounds.Top * -1));
canvas.ClipPath(clipPath, SKClipOperation.Intersect, true);
SKRectI layerBounds = SKRectI.Create(0, 0, Bounds.Width, Bounds.Height);
// Apply blend mode and color
layerPaint.BlendMode = General.BlendMode.CurrentValue;
layerPaint.Color = new SKColor(0, 0, 0, (byte) (Transform.Opacity.CurrentValue * 2.55f));
using SKPath renderPath = new();
if (General.ShapeType.CurrentValue == LayerShapeType.Rectangle)
renderPath.AddRect(layerBounds);
else
renderPath.AddOval(layerBounds);
if (General.TransformMode.CurrentValue == LayerTransformMode.Normal)
{
// Apply transformation except rotation to the render path
if (LayerBrush.SupportsTransformation)
{
SKMatrix renderPathMatrix = GetTransformMatrix(true, true, true, false);
renderPath.Transform(renderPathMatrix);
}
// Apply rotation to the canvas
if (LayerBrush.SupportsTransformation)
{
SKMatrix rotationMatrix = GetTransformMatrix(true, false, false, true);
canvas.SetMatrix(canvas.TotalMatrix.PreConcat(rotationMatrix));
}
DelegateRendering(canvas, renderPath, renderPath.Bounds, layerPaint);
}
else if (General.TransformMode.CurrentValue == LayerTransformMode.Clip)
{
SKMatrix renderPathMatrix = GetTransformMatrix(true, true, true, true);
renderPath.Transform(renderPathMatrix);
DelegateRendering(canvas, renderPath, layerBounds, layerPaint);
}
}
finally
{
layerPaint.DisposeSelfAndProperties();
}
}
private void RenderCopies(SKCanvas canvas, SKPointI basePosition)
{
for (int i = _renderCopies.Count - 1; i >= 0; i--)
_renderCopies[i].Render(canvas, basePosition, null);
}
/// <inheritdoc />
public override void Enable()
{
if (Enabled)
return;
bool tryOrBreak = TryOrBreak(() => LayerBrush?.InternalEnable(), "Failed to enable layer brush");
if (!tryOrBreak)
return;
tryOrBreak = TryOrBreak(() =>
{
foreach (BaseLayerEffect baseLayerEffect in LayerEffects)
baseLayerEffect.InternalEnable();
}, "Failed to enable one or more effects");
if (!tryOrBreak)
return;
Enabled = true;
}
/// <inheritdoc />
public override void Disable()
{
if (!Enabled)
return;
LayerBrush?.InternalDisable();
foreach (BaseLayerEffect baseLayerEffect in LayerEffects)
baseLayerEffect.InternalDisable();
Enabled = false;
}
/// <inheritdoc />
public override void OverrideTimelineAndApply(TimeSpan position)
{
DisplayCondition.OverrideTimeline(position);
General.Update(Timeline);
Transform.Update(Timeline);
LayerBrush?.InternalUpdate(Timeline);
foreach (BaseLayerEffect baseLayerEffect in LayerEffects.Where(e => !e.Suspended))
baseLayerEffect.InternalUpdate(Timeline);
}
/// <inheritdoc />
public override void Reset()
{
UpdateDisplayCondition();
if (DisplayConditionMet)
Timeline.JumpToStart();
else
Timeline.JumpToEnd();
while (_renderCopies.Any())
{
_renderCopies[0].Dispose();
_renderCopies.RemoveAt(0);
}
}
/// <summary>
/// Creates a copy of this layer and renders it alongside this layer for as long as its timeline lasts.
/// </summary>
/// <param name="max">The total maximum of render copies to keep</param>
public void CreateRenderCopy(int max)
{
if (_renderCopies.Count >= max)
return;
Layer copy = new(this);
_renderCopies.Add(copy);
}
internal void CalculateRenderProperties()
{
if (Disposed)
throw new ObjectDisposedException("Layer");
if (!Leds.Any())
{
Path = new SKPath();
}
else
{
SKPath path = new() {FillType = SKPathFillType.Winding};
foreach (ArtemisLed artemisLed in Leds)
path.AddRect(artemisLed.AbsoluteRectangle);
Path = path;
}
// This is called here so that the shape's render properties are up to date when other code
// responds to OnRenderPropertiesUpdated
LayerShape?.CalculateRenderProperties();
// Folder render properties are based on child paths and thus require an update
if (Parent is Folder folder)
folder.CalculateRenderProperties();
OnRenderPropertiesUpdated();
}
internal SKPoint GetLayerAnchorPosition(bool applyTranslation, bool zeroBased, SKRect? customBounds = null)
{
if (Disposed)
throw new ObjectDisposedException("Layer");
SKRect bounds = customBounds ?? Bounds;
SKPoint positionProperty = Transform.Position.CurrentValue;
// Start at the top left of the shape
SKPoint position = zeroBased ? new SKPoint(0, 0) : new SKPoint(bounds.Left, bounds.Top);
// Apply translation
if (applyTranslation)
{
position.X += positionProperty.X * bounds.Width;
position.Y += positionProperty.Y * bounds.Height;
}
return position;
}
private void DelegateRendering(SKCanvas canvas, SKPath renderPath, SKRect bounds, SKPaint layerPaint)
{
if (LayerBrush == null)
throw new ArtemisCoreException("The layer is not yet ready for rendering");
foreach (BaseLayerEffect baseLayerEffect in LayerEffects)
{
if (!baseLayerEffect.Suspended)
baseLayerEffect.InternalPreProcess(canvas, bounds, layerPaint);
}
try
{
canvas.SaveLayer(layerPaint);
canvas.ClipPath(renderPath);
// Restore the blend mode before doing the actual render
layerPaint.BlendMode = SKBlendMode.SrcOver;
LayerBrush.InternalRender(canvas, bounds, layerPaint);
foreach (BaseLayerEffect baseLayerEffect in LayerEffects)
{
if (!baseLayerEffect.Suspended)
baseLayerEffect.InternalPostProcess(canvas, bounds, layerPaint);
}
}
finally
{
canvas.Restore();
}
}
/// <summary>
/// Creates a transformation matrix that applies the current transformation settings
/// </summary>
/// <param name="zeroBased">
/// If true, treats the layer as if it is located at 0,0 instead of its actual position on the
/// surface
/// </param>
/// <param name="includeTranslation">Whether translation should be included</param>
/// <param name="includeScale">Whether the scale should be included</param>
/// <param name="includeRotation">Whether the rotation should be included</param>
/// <param name="customBounds">Optional custom bounds to base the anchor on</param>
/// <returns>The transformation matrix containing the current transformation settings</returns>
public SKMatrix GetTransformMatrix(bool zeroBased, bool includeTranslation, bool includeScale, bool includeRotation, SKRect? customBounds = null)
{
if (Disposed)
throw new ObjectDisposedException("Layer");
if (Path == null)
return SKMatrix.Empty;
SKRect bounds = customBounds ?? Bounds;
SKSize sizeProperty = Transform.Scale.CurrentValue;
float rotationProperty = Transform.Rotation.CurrentValue;
SKPoint anchorPosition = GetLayerAnchorPosition(true, zeroBased, bounds);
SKPoint anchorProperty = Transform.AnchorPoint.CurrentValue;
// Translation originates from the top left of the shape and is tied to the anchor
float x = anchorPosition.X - (zeroBased ? 0 : bounds.Left) - anchorProperty.X * bounds.Width;
float y = anchorPosition.Y - (zeroBased ? 0 : bounds.Top) - anchorProperty.Y * bounds.Height;
SKMatrix transform = SKMatrix.Empty;
if (includeTranslation)
// transform is always SKMatrix.Empty here...
transform = SKMatrix.CreateTranslation(x, y);
if (includeScale)
{
if (transform == SKMatrix.Empty)
transform = SKMatrix.CreateScale(sizeProperty.Width / 100f, sizeProperty.Height / 100f, anchorPosition.X, anchorPosition.Y);
else
transform = transform.PostConcat(SKMatrix.CreateScale(sizeProperty.Width / 100f, sizeProperty.Height / 100f, anchorPosition.X, anchorPosition.Y));
}
if (includeRotation)
{
if (transform == SKMatrix.Empty)
transform = SKMatrix.CreateRotationDegrees(rotationProperty, anchorPosition.X, anchorPosition.Y);
else
transform = transform.PostConcat(SKMatrix.CreateRotationDegrees(rotationProperty, anchorPosition.X, anchorPosition.Y));
}
return transform;
}
#endregion
#region LED management
/// <summary>
/// Adds a new <see cref="ArtemisLed" /> to the layer and updates the render properties.
/// </summary>
/// <param name="led">The LED to add</param>
public void AddLed(ArtemisLed led)
{
if (Disposed)
throw new ObjectDisposedException("Layer");
if (_leds.Contains(led))
return;
_leds.Add(led);
CalculateRenderProperties();
}
/// <summary>
/// Adds a collection of new <see cref="ArtemisLed" />s to the layer and updates the render properties.
/// </summary>
/// <param name="leds">The LEDs to add</param>
public void AddLeds(IEnumerable<ArtemisLed> leds)
{
if (Disposed)
throw new ObjectDisposedException("Layer");
_leds.AddRange(leds.Except(_leds));
CalculateRenderProperties();
}
/// <summary>
/// Removes a <see cref="ArtemisLed" /> from the layer and updates the render properties.
/// </summary>
/// <param name="led">The LED to remove</param>
public void RemoveLed(ArtemisLed led)
{
if (Disposed)
throw new ObjectDisposedException("Layer");
if (!_leds.Remove(led))
return;
CalculateRenderProperties();
}
/// <summary>
/// Removes all <see cref="ArtemisLed" />s from the layer and updates the render properties.
/// </summary>
public void ClearLeds()
{
if (Disposed)
throw new ObjectDisposedException("Layer");
if (!_leds.Any())
return;
_leds.Clear();
CalculateRenderProperties();
}
internal void PopulateLeds(IEnumerable<ArtemisDevice> devices)
{
if (Disposed)
throw new ObjectDisposedException("Layer");
List<ArtemisLed> leds = new();
// Get the surface LEDs for this layer
List<ArtemisLed> availableLeds = devices.SelectMany(d => d.Leds).ToList();
foreach (LedEntity ledEntity in LayerEntity.Leds)
{
ArtemisLed? match = availableLeds.FirstOrDefault(a => a.Device.Identifier == ledEntity.DeviceIdentifier &&
a.RgbLed.Id.ToString() == ledEntity.LedName);
if (match != null && !leds.Contains(match))
leds.Add(match);
else
_missingLeds.Add(ledEntity);
}
_leds = leds;
Leds = new ReadOnlyCollection<ArtemisLed>(_leds);
CalculateRenderProperties();
}
private void SaveMissingLeds()
{
LayerEntity.Leds.AddRange(_missingLeds.Except(LayerEntity.Leds, LedEntity.LedEntityComparer));
}
#endregion
#region Brush management
/// <summary>
/// Changes the current layer brush to the provided layer brush and activates it
/// </summary>
public void ChangeLayerBrush(BaseLayerBrush layerBrush)
{
if (layerBrush == null)
throw new ArgumentNullException(nameof(layerBrush));
BaseLayerBrush? oldLayerBrush = LayerBrush;
General.BrushReference.SetCurrentValue(new LayerBrushReference(layerBrush.Descriptor));
LayerBrush = layerBrush;
// Don't dispose the brush, only disable it
// That way an undo-action does not need to worry about disposed brushes
oldLayerBrush?.InternalDisable();
ActivateLayerBrush();
}
private void ActivateLayerBrush()
{
try
{
// If the brush is null, try to instantiate it
if (LayerBrush == null)
{
// Ensure the provider is loaded and still provides the expected brush
LayerBrushReference? brushReference = General.BrushReference.CurrentValue;
if (brushReference?.LayerBrushProviderId != null && brushReference.BrushType != null)
{
// Only apply the brush if it is successfully retrieved from the store and instantiated
BaseLayerBrush? layerBrush = LayerBrushStore.Get(brushReference.LayerBrushProviderId, brushReference.BrushType)?.LayerBrushDescriptor.CreateInstance(this, LayerEntity.LayerBrush);
if (layerBrush != null)
{
ClearBrokenState(BROKEN_STATE_BRUSH_NOT_FOUND);
ChangeLayerBrush(layerBrush);
}
// If that's not possible there's nothing to do
else
{
SetBrokenState(BROKEN_STATE_BRUSH_NOT_FOUND);
return;
}
}
}
General.ShapeType.IsHidden = LayerBrush != null && !LayerBrush.SupportsTransformation;
General.BlendMode.IsHidden = LayerBrush != null && !LayerBrush.SupportsTransformation;
Transform.IsHidden = LayerBrush != null && !LayerBrush.SupportsTransformation;
if (LayerBrush != null && Enabled)
{
LayerBrush.InternalEnable();
LayerBrush.Update(0);
}
OnLayerBrushUpdated();
ClearBrokenState(BROKEN_STATE_INIT_FAILED);
}
catch (Exception e)
{
SetBrokenState(BROKEN_STATE_INIT_FAILED, e);
}
}
private void DeactivateLayerBrush()
{
if (LayerBrush == null)
return;
BaseLayerBrush? brush = LayerBrush;
LayerBrush = null;
brush?.Dispose();
OnLayerBrushUpdated();
}
#endregion
}
/// <summary>
/// Represents a type of layer shape
/// </summary>
public enum LayerShapeType
{
/// <summary>
/// A circular layer shape
/// </summary>
Ellipse,
/// <summary>
/// A rectangular layer shape
/// </summary>
Rectangle
}
/// <summary>
/// Represents a layer transform mode
/// </summary>
public enum LayerTransformMode
{
/// <summary>
/// Normal transformation
/// </summary>
Normal,
/// <summary>
/// Transforms only a clip
/// </summary>
Clip
}