mirror of
https://github.com/Artemis-RGB/Artemis
synced 2026-11-11 19:36:28 +00:00
Added layer shapes, editor is still WIP
This commit is contained in:
1 parent
8ba43ecec1
commit
580db3185e
115 files changed
+1949
-1189
No files matched your search
@@ -1,6 +1,5 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Artemis.Core.Models.Profile.Abstract;
|
||||
using Artemis.Core.Models.Surface;
|
||||
using Artemis.Storage.Entities.Profile;
|
||||
using SkiaSharp;
|
||||
@@ -92,7 +91,7 @@ namespace Artemis.Core.Models.Profile
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"Folder - {nameof(Name)}: {Name}, {nameof(Order)}: {Order}";
|
||||
return $"[Folder] {nameof(Name)}: {Name}, {nameof(Order)}: {Order}";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,9 +3,9 @@ using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using Artemis.Core.Extensions;
|
||||
using Artemis.Core.Models.Profile.Abstract;
|
||||
using Artemis.Core.Models.Profile.LayerShapes;
|
||||
using Artemis.Core.Models.Surface;
|
||||
using Artemis.Core.Plugins.LayerElement;
|
||||
using Artemis.Core.Plugins.LayerBrush;
|
||||
using Artemis.Storage.Entities.Profile;
|
||||
using Newtonsoft.Json;
|
||||
using SkiaSharp;
|
||||
@@ -14,7 +14,7 @@ namespace Artemis.Core.Models.Profile
|
||||
{
|
||||
public sealed class Layer : ProfileElement
|
||||
{
|
||||
private readonly List<LayerElement> _layerElements;
|
||||
private LayerShape _layerShape;
|
||||
private List<ArtemisLed> _leds;
|
||||
|
||||
public Layer(Profile profile, ProfileElement parent, string name)
|
||||
@@ -27,7 +27,6 @@ namespace Artemis.Core.Models.Profile
|
||||
Name = name;
|
||||
|
||||
_leds = new List<ArtemisLed>();
|
||||
_layerElements = new List<LayerElement>();
|
||||
}
|
||||
|
||||
internal Layer(Profile profile, ProfileElement parent, LayerEntity layerEntity)
|
||||
@@ -40,61 +39,98 @@ namespace Artemis.Core.Models.Profile
|
||||
Name = layerEntity.Name;
|
||||
Order = layerEntity.Order;
|
||||
|
||||
switch (layerEntity.ShapeEntity?.Type)
|
||||
{
|
||||
case ShapeEntityType.Ellipse:
|
||||
LayerShape = new Ellipse(this, layerEntity.ShapeEntity);
|
||||
break;
|
||||
case ShapeEntityType.Fill:
|
||||
LayerShape = new Fill(this, layerEntity.ShapeEntity);
|
||||
break;
|
||||
case ShapeEntityType.Polygon:
|
||||
LayerShape = new Polygon(this, layerEntity.ShapeEntity);
|
||||
break;
|
||||
case ShapeEntityType.Rectangle:
|
||||
LayerShape = new Rectangle(this, layerEntity.ShapeEntity);
|
||||
break;
|
||||
case null:
|
||||
LayerShape = null;
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
|
||||
_leds = new List<ArtemisLed>();
|
||||
_layerElements = new List<LayerElement>();
|
||||
}
|
||||
|
||||
internal LayerEntity LayerEntity { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// A collection of all the LEDs this layer is assigned to.
|
||||
/// </summary>
|
||||
public ReadOnlyCollection<ArtemisLed> Leds => _leds.AsReadOnly();
|
||||
public ReadOnlyCollection<LayerElement> LayerElements => _layerElements.AsReadOnly();
|
||||
|
||||
public SKRect RenderRectangle { get; set; }
|
||||
public SKRect AbsoluteRenderRectangle { get; set; }
|
||||
public SKPath RenderPath { get; set; }
|
||||
/// <summary>
|
||||
/// A rectangle relative to the surface that contains all the LEDs in this layer.
|
||||
/// <para>For rendering, use the RenderRectangle on <see cref="LayerShape" />.</para>
|
||||
/// </summary>
|
||||
public SKRect Rectangle { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// A zero-based absolute rectangle that contains all the LEDs in this layer.
|
||||
/// <para>For rendering, use the RenderRectangle on <see cref="LayerShape" />.</para>
|
||||
/// </summary>
|
||||
public SKRect AbsoluteRectangle { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// A path containing all the LEDs this layer is applied to.
|
||||
/// <para>For rendering, use the RenderPath on <see cref="LayerShape" />.</para>
|
||||
/// </summary>
|
||||
public SKPath Path { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Defines the shape that is rendered by the <see cref="LayerBrush"/>.
|
||||
/// </summary>
|
||||
public LayerShape LayerShape
|
||||
{
|
||||
get => _layerShape;
|
||||
set
|
||||
{
|
||||
_layerShape = value;
|
||||
_layerShape.CalculateRenderProperties();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The brush that will fill the <see cref="LayerShape"/>.
|
||||
/// </summary>
|
||||
public LayerBrush LayerBrush { get; internal set; }
|
||||
|
||||
public override void Update(double deltaTime)
|
||||
{
|
||||
lock (_layerElements)
|
||||
{
|
||||
foreach (var layerElement in LayerElements)
|
||||
layerElement.Update(deltaTime);
|
||||
}
|
||||
LayerBrush?.Update(deltaTime);
|
||||
}
|
||||
|
||||
public override void Render(double deltaTime, SKCanvas canvas)
|
||||
{
|
||||
if (RenderPath == null)
|
||||
if (Path == null)
|
||||
return;
|
||||
|
||||
lock (_layerElements)
|
||||
{
|
||||
canvas.Save();
|
||||
using (var framePath = new SKPath(RenderPath))
|
||||
{
|
||||
canvas.ClipPath(framePath);
|
||||
|
||||
foreach (var layerElement in LayerElements)
|
||||
layerElement.RenderPreProcess(framePath, canvas);
|
||||
foreach (var layerElement in LayerElements)
|
||||
layerElement.Render(framePath, canvas);
|
||||
foreach (var layerElement in LayerElements)
|
||||
layerElement.RenderPostProcess(framePath, canvas);
|
||||
}
|
||||
canvas.Restore();
|
||||
}
|
||||
canvas.Save();
|
||||
LayerBrush?.Render(canvas);
|
||||
canvas.Restore();
|
||||
}
|
||||
|
||||
internal override void ApplyToEntity()
|
||||
{
|
||||
// Properties
|
||||
LayerEntity.Id = EntityId;
|
||||
LayerEntity.ParentId = Parent?.EntityId ?? new Guid();
|
||||
|
||||
LayerEntity.Order = Order;
|
||||
LayerEntity.Name = Name;
|
||||
|
||||
LayerEntity.ProfileId = Profile.EntityId;
|
||||
|
||||
// LEDs
|
||||
LayerEntity.Leds.Clear();
|
||||
foreach (var artemisLed in Leds)
|
||||
{
|
||||
@@ -106,62 +142,60 @@ namespace Artemis.Core.Models.Profile
|
||||
LayerEntity.Leds.Add(ledEntity);
|
||||
}
|
||||
|
||||
// Conditions TODO
|
||||
LayerEntity.Condition.Clear();
|
||||
|
||||
LayerEntity.Elements.Clear();
|
||||
foreach (var layerElement in LayerElements)
|
||||
// Brush
|
||||
LayerEntity.BrushEntity = new BrushEntity
|
||||
{
|
||||
var layerElementEntity = new LayerElementEntity
|
||||
{
|
||||
Id = layerElement.Guid,
|
||||
PluginGuid = layerElement.Descriptor.LayerElementProvider.PluginInfo.Guid,
|
||||
LayerElementType = layerElement.GetType().Name,
|
||||
Configuration = JsonConvert.SerializeObject(layerElement.Settings)
|
||||
};
|
||||
LayerEntity.Elements.Add(layerElementEntity);
|
||||
}
|
||||
BrushPluginGuid = LayerBrush.Descriptor.LayerBrushProvider.PluginInfo.Guid,
|
||||
BrushType = LayerBrush.GetType().Name,
|
||||
Configuration = JsonConvert.SerializeObject(LayerBrush.Settings)
|
||||
};
|
||||
|
||||
// Shape
|
||||
LayerShape.ApplyToEntity();
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
_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)
|
||||
{
|
||||
_leds.AddRange(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)
|
||||
{
|
||||
_leds.Remove(led);
|
||||
CalculateRenderProperties();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes all <see cref="ArtemisLed" />s from the layer and updates the render properties.
|
||||
/// </summary>
|
||||
public void ClearLeds()
|
||||
{
|
||||
_leds.Clear();
|
||||
CalculateRenderProperties();
|
||||
}
|
||||
|
||||
internal void AddLayerElement(LayerElement layerElement)
|
||||
{
|
||||
lock (_layerElements)
|
||||
{
|
||||
_layerElements.Add(layerElement);
|
||||
}
|
||||
}
|
||||
|
||||
internal void RemoveLayerElement(LayerElement layerElement)
|
||||
{
|
||||
lock (_layerElements)
|
||||
{
|
||||
_layerElements.Remove(layerElement);
|
||||
}
|
||||
}
|
||||
|
||||
internal void PopulateLeds(ArtemisSurface surface)
|
||||
{
|
||||
var leds = new List<ArtemisLed>();
|
||||
@@ -191,20 +225,20 @@ namespace Artemis.Core.Models.Profile
|
||||
var maxX = Leds.Max(l => l.AbsoluteRenderRectangle.Right);
|
||||
var maxY = Leds.Max(l => l.AbsoluteRenderRectangle.Bottom);
|
||||
|
||||
RenderRectangle = SKRect.Create(minX, minY, maxX - minX, maxY - minY);
|
||||
AbsoluteRenderRectangle = SKRect.Create(0, 0, maxX - minX, maxY - minY);
|
||||
Rectangle = SKRect.Create(minX, minY, maxX - minX, maxY - minY);
|
||||
AbsoluteRectangle = SKRect.Create(0, 0, maxX - minX, maxY - minY);
|
||||
|
||||
var path = new SKPath {FillType = SKPathFillType.Winding};
|
||||
foreach (var artemisLed in Leds)
|
||||
path.AddRect(artemisLed.AbsoluteRenderRectangle);
|
||||
|
||||
RenderPath = path;
|
||||
Path = path;
|
||||
OnRenderPropertiesUpdated();
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"Layer - {nameof(Name)}: {Name}, {nameof(Order)}: {Order}";
|
||||
return $"[Layer] {nameof(Name)}: {Name}, {nameof(Order)}: {Order}";
|
||||
}
|
||||
|
||||
#region Events
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
using Artemis.Storage.Entities.Profile;
|
||||
using SkiaSharp;
|
||||
|
||||
namespace Artemis.Core.Models.Profile.LayerShapes
|
||||
{
|
||||
public class Ellipse : LayerShape
|
||||
{
|
||||
public Ellipse(Layer layer) : base(layer)
|
||||
{
|
||||
}
|
||||
|
||||
internal Ellipse(Layer layer, ShapeEntity shapeEntity) : base(layer, shapeEntity)
|
||||
{
|
||||
}
|
||||
|
||||
public override void CalculateRenderProperties()
|
||||
{
|
||||
var width = Layer.AbsoluteRectangle.Width;
|
||||
var height = Layer.AbsoluteRectangle.Height;
|
||||
var rect = SKRect.Create(Position.X * width, Position.Y * height, Size.Width * width, Size.Height * height);
|
||||
|
||||
var path = new SKPath();
|
||||
path.AddOval(rect);
|
||||
|
||||
RenderPath = path;
|
||||
RenderRectangle = path.GetRect();
|
||||
}
|
||||
|
||||
public override void ApplyToEntity()
|
||||
{
|
||||
base.ApplyToEntity();
|
||||
Layer.LayerEntity.ShapeEntity.Type = ShapeEntityType.Ellipse;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using Artemis.Storage.Entities.Profile;
|
||||
|
||||
namespace Artemis.Core.Models.Profile.LayerShapes
|
||||
{
|
||||
public class Fill : LayerShape
|
||||
{
|
||||
public Fill(Layer layer) : base(layer)
|
||||
{
|
||||
}
|
||||
internal Fill(Layer layer, ShapeEntity shapeEntity) : base(layer, shapeEntity)
|
||||
{
|
||||
}
|
||||
|
||||
public override void CalculateRenderProperties()
|
||||
{
|
||||
RenderPath = Layer.Path;
|
||||
RenderRectangle = Layer.Path.GetRect();
|
||||
}
|
||||
|
||||
public override void ApplyToEntity()
|
||||
{
|
||||
base.ApplyToEntity();
|
||||
Layer.LayerEntity.ShapeEntity.Type = ShapeEntityType.Fill;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
using System;
|
||||
using Artemis.Storage.Entities.Profile;
|
||||
using SkiaSharp;
|
||||
|
||||
namespace Artemis.Core.Models.Profile.LayerShapes
|
||||
{
|
||||
public abstract class LayerShape
|
||||
{
|
||||
protected LayerShape(Layer layer)
|
||||
{
|
||||
Layer = layer;
|
||||
|
||||
Layer.RenderPropertiesUpdated += LayerOnRenderPropertiesUpdated;
|
||||
}
|
||||
|
||||
protected LayerShape(Layer layer, ShapeEntity shapeEntity)
|
||||
{
|
||||
Layer = layer;
|
||||
Anchor = new SKPoint(shapeEntity.Anchor?.X ?? 0, shapeEntity.Anchor?.Y ?? 0);
|
||||
Position = new SKPoint(shapeEntity.Position?.X ?? 0, shapeEntity.Position?.Y ?? 0);
|
||||
Size = new SKSize(shapeEntity.Width, shapeEntity.Height);
|
||||
|
||||
Layer.RenderPropertiesUpdated += LayerOnRenderPropertiesUpdated;
|
||||
}
|
||||
|
||||
private void LayerOnRenderPropertiesUpdated(object sender, EventArgs e)
|
||||
{
|
||||
CalculateRenderProperties();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The layer this shape is attached to
|
||||
/// </summary>
|
||||
public Layer Layer { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// At which position the shape is attached to the layer
|
||||
/// </summary>
|
||||
public SKPoint Anchor { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The position of the shape
|
||||
/// </summary>
|
||||
public SKPoint Position { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The size of the shape
|
||||
/// </summary>
|
||||
public SKSize Size { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// A render rectangle relative to the layer
|
||||
/// </summary>
|
||||
public SKRect RenderRectangle { get; protected set; }
|
||||
|
||||
/// <summary>
|
||||
/// A path relative to the layer
|
||||
/// </summary>
|
||||
public SKPath RenderPath { get; protected set; }
|
||||
|
||||
public abstract void CalculateRenderProperties();
|
||||
|
||||
public virtual void ApplyToEntity()
|
||||
{
|
||||
Layer.LayerEntity.ShapeEntity = new ShapeEntity
|
||||
{
|
||||
Anchor = new ShapePointEntity { X = Anchor.X, Y = Anchor.Y },
|
||||
Position = new ShapePointEntity { X = Position.X, Y = Position.Y },
|
||||
Width = Size.Width,
|
||||
Height = Size.Height
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Artemis.Storage.Entities.Profile;
|
||||
using SkiaSharp;
|
||||
|
||||
namespace Artemis.Core.Models.Profile.LayerShapes
|
||||
{
|
||||
public class Polygon : LayerShape
|
||||
{
|
||||
|
||||
public Polygon(Layer layer) : base(layer)
|
||||
{
|
||||
}
|
||||
|
||||
internal Polygon(Layer layer, ShapeEntity shapeEntity) : base(layer, shapeEntity)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The points of this polygon
|
||||
/// </summary>
|
||||
public List<SKPoint> Points { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The points of this polygon where they need to be rendered inside the layer
|
||||
/// </summary>
|
||||
public List<SKPoint> RenderPoints => Points.Select(p => new SKPoint(p.X * Layer.AbsoluteRectangle.Width, p.Y * Layer.AbsoluteRectangle.Height)).ToList();
|
||||
|
||||
public override void CalculateRenderProperties()
|
||||
{
|
||||
var path = new SKPath();
|
||||
path.AddPoly(RenderPoints.ToArray());
|
||||
|
||||
RenderPath = path;
|
||||
RenderRectangle = path.GetRect();
|
||||
}
|
||||
|
||||
public override void ApplyToEntity()
|
||||
{
|
||||
base.ApplyToEntity();
|
||||
Layer.LayerEntity.ShapeEntity.Type = ShapeEntityType.Polygon;
|
||||
Layer.LayerEntity.ShapeEntity.Points = Points.Select(p => new ShapePointEntity { X = p.X, Y = p.Y }).ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using Artemis.Storage.Entities.Profile;
|
||||
using SkiaSharp;
|
||||
|
||||
namespace Artemis.Core.Models.Profile.LayerShapes
|
||||
{
|
||||
public class Rectangle : LayerShape
|
||||
{
|
||||
public Rectangle(Layer layer) : base(layer)
|
||||
{
|
||||
}
|
||||
|
||||
internal Rectangle(Layer layer, ShapeEntity shapeEntity) : base(layer, shapeEntity)
|
||||
{
|
||||
}
|
||||
|
||||
public override void CalculateRenderProperties()
|
||||
{
|
||||
var width = Layer.AbsoluteRectangle.Width;
|
||||
var height = Layer.AbsoluteRectangle.Height;
|
||||
var rect = SKRect.Create(Position.X * width, Position.Y * height, Size.Width * width, Size.Height * height);
|
||||
var path = new SKPath();
|
||||
path.AddRect(rect);
|
||||
|
||||
RenderPath = path;
|
||||
RenderRectangle = path.GetRect();
|
||||
}
|
||||
|
||||
public override void ApplyToEntity()
|
||||
{
|
||||
base.ApplyToEntity();
|
||||
Layer.LayerEntity.ShapeEntity.Type = ShapeEntityType.Rectangle;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Artemis.Core.Exceptions;
|
||||
using Artemis.Core.Models.Profile.Abstract;
|
||||
using Artemis.Core.Models.Surface;
|
||||
using Artemis.Core.Plugins.Models;
|
||||
using Artemis.Storage.Entities.Profile;
|
||||
@@ -109,17 +108,17 @@ namespace Artemis.Core.Models.Profile
|
||||
}
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{nameof(Order)}: {Order}, {nameof(Name)}: {Name}, {nameof(PluginInfo)}: {PluginInfo}";
|
||||
}
|
||||
|
||||
internal void PopulateLeds(ArtemisSurface surface)
|
||||
{
|
||||
foreach (var layer in GetAllLayers())
|
||||
layer.PopulateLeds(surface);
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"[Profile] {nameof(Name)}: {Name}, {nameof(IsActivated)}: {IsActivated}, {nameof(PluginInfo)}: {PluginInfo}";
|
||||
}
|
||||
|
||||
#region Events
|
||||
|
||||
/// <summary>
|
||||
|
||||
+3
-2
@@ -2,11 +2,10 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using Artemis.Core.Models.Surface;
|
||||
using SkiaSharp;
|
||||
using Stylet;
|
||||
|
||||
namespace Artemis.Core.Models.Profile.Abstract
|
||||
namespace Artemis.Core.Models.Profile
|
||||
{
|
||||
public abstract class ProfileElement : PropertyChangedBase
|
||||
{
|
||||
@@ -132,5 +131,7 @@ namespace Artemis.Core.Models.Profile.Abstract
|
||||
child.Parent = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user