Added layer shapes, editor is still WIP

This commit is contained in:
SpoinkyNL committed 2019-12-15 22:52:04 +01:00
1 parent 70bf0e1c43
commit ddce69e8f0
115 files changed
+1949 -1189

No files matched your search

+11 -6
View File
@@ -166,6 +166,11 @@
<Compile Include="Extensions\TypeExtensions.cs" />
<Compile Include="JsonConverters\SKColorConverter.cs" />
<Compile Include="Models\DataModelDescription.cs" />
<Compile Include="Models\Profile\LayerShapes\Ellipse.cs" />
<Compile Include="Models\Profile\LayerShapes\Fill.cs" />
<Compile Include="Models\Profile\LayerShapes\Polygon.cs" />
<Compile Include="Models\Profile\LayerShapes\Rectangle.cs" />
<Compile Include="Models\Profile\LayerShapes\LayerShape.cs" />
<Compile Include="Models\Surface\ArtemisLed.cs" />
<Compile Include="Models\Surface\ArtemisSurface.cs" />
<Compile Include="Models\Surface\ArtemisDevice.cs" />
@@ -180,16 +185,16 @@
<Compile Include="Plugins\Abstract\DeviceProvider.cs" />
<Compile Include="Plugins\Abstract\Module.cs" />
<Compile Include="Plugins\Abstract\Plugin.cs" />
<Compile Include="Plugins\LayerElement\LayerElement.cs" />
<Compile Include="Plugins\LayerElement\LayerElementDescriptor.cs" />
<Compile Include="Plugins\LayerElement\LayerElementProvider.cs" />
<Compile Include="Plugins\LayerElement\LayerElementSettings.cs" />
<Compile Include="Plugins\LayerElement\LayerElementViewModel.cs" />
<Compile Include="Plugins\LayerBrush\LayerBrush.cs" />
<Compile Include="Plugins\LayerBrush\LayerBrushDescriptor.cs" />
<Compile Include="Plugins\LayerBrush\LayerBrushProvider.cs" />
<Compile Include="Plugins\LayerBrush\LayerBrushSettings.cs" />
<Compile Include="Plugins\LayerBrush\LayerBrushViewModel.cs" />
<Compile Include="Plugins\Models\PluginInfo.cs" />
<Compile Include="Plugins\Models\PluginSetting.cs" />
<Compile Include="Plugins\Models\PluginSettings.cs" />
<Compile Include="Models\Profile\Folder.cs" />
<Compile Include="Models\Profile\Abstract\ProfileElement.cs" />
<Compile Include="Models\Profile\ProfileElement.cs" />
<Compile Include="Models\Profile\Layer.cs" />
<Compile Include="Models\Profile\Profile.cs" />
<Compile Include="Ninject\CoreModule.cs" />
+1 -2
View File
@@ -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}";
}
}
}
+98 -64
View File
@@ -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;
}
}
}
+5 -6
View File
@@ -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>
@@ -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;
}
}
}
}
@@ -0,0 +1,48 @@
using System;
using Artemis.Core.Models.Profile;
using SkiaSharp;
namespace Artemis.Core.Plugins.LayerBrush
{
public abstract class LayerBrush : IDisposable
{
protected LayerBrush(Layer layer, LayerBrushSettings settings, LayerBrushDescriptor descriptor)
{
Layer = layer;
Settings = settings;
Descriptor = descriptor;
}
public Layer Layer { get; }
public LayerBrushSettings Settings { get; }
public LayerBrushDescriptor Descriptor { get; }
public virtual void Dispose()
{
}
/// <summary>
/// Called by the profile editor to populate the brush properties panel
/// </summary>
/// <returns></returns>
public abstract LayerBrushViewModel GetViewModel();
/// <summary>
/// Called before rendering every frame, write your update logic here
/// </summary>
/// <param name="deltaTime"></param>
public virtual void Update(double deltaTime)
{
}
/// <summary>
/// The main method of rendering anything to the layer. The provided <see cref="SKCanvas" /> is specific to the layer
/// and matches it's width and height.
/// <para>Called during rendering, in the order configured on the layer</para>
/// </summary>
/// <param name="canvas">The layer canvas</param>
public virtual void Render(SKCanvas canvas)
{
}
}
}
@@ -0,0 +1,22 @@
using System;
namespace Artemis.Core.Plugins.LayerBrush
{
public class LayerBrushDescriptor
{
internal LayerBrushDescriptor(string displayName, string description, string icon, Type layerBrushType, LayerBrushProvider layerBrushProvider)
{
DisplayName = displayName;
Description = description;
Icon = icon;
LayerBrushType = layerBrushType;
LayerBrushProvider = layerBrushProvider;
}
public string DisplayName { get; }
public string Description { get; }
public string Icon { get; }
public Type LayerBrushType { get; }
public LayerBrushProvider LayerBrushProvider { get; }
}
}
@@ -0,0 +1,28 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Artemis.Core.Plugins.Abstract;
using Artemis.Core.Plugins.Models;
namespace Artemis.Core.Plugins.LayerBrush
{
/// <inheritdoc />
/// <summary>
/// Allows you to create one or more <see cref="LayerBrush" />s usable by profile layers.
/// </summary>
public abstract class LayerBrushProvider : Plugin
{
private readonly List<LayerBrushDescriptor> _layerBrushDescriptors;
protected LayerBrushProvider(PluginInfo pluginInfo) : base(pluginInfo)
{
_layerBrushDescriptors = new List<LayerBrushDescriptor>();
}
public ReadOnlyCollection<LayerBrushDescriptor> LayerBrushDescriptors => _layerBrushDescriptors.AsReadOnly();
protected void AddLayerBrushDescriptor<T>(string displayName, string description, string icon) where T : LayerBrush
{
_layerBrushDescriptors.Add(new LayerBrushDescriptor(displayName, description, icon, typeof(T), this));
}
}
}
@@ -1,9 +1,9 @@
using System;
using Stylet;
namespace Artemis.Core.Plugins.LayerElement
namespace Artemis.Core.Plugins.LayerBrush
{
public abstract class LayerElementSettings : PropertyChangedBase
public abstract class LayerBrushSettings : PropertyChangedBase
{
private Action<Action> _propertyChangedDispatcher = Execute.DefaultPropertyChangedDispatcher;
@@ -0,0 +1,14 @@
using Stylet;
namespace Artemis.Core.Plugins.LayerBrush
{
public abstract class LayerBrushViewModel : PropertyChangedBase
{
protected LayerBrushViewModel(LayerBrush brush)
{
Brush = brush;
}
public LayerBrush Brush { get; }
}
}
@@ -1,75 +0,0 @@
using System;
using Artemis.Core.Models.Profile;
using SkiaSharp;
namespace Artemis.Core.Plugins.LayerElement
{
public abstract class LayerElement : IDisposable
{
protected LayerElement(Layer layer, Guid guid, LayerElementSettings settings, LayerElementDescriptor descriptor)
{
Layer = layer;
Guid = guid;
Settings = settings;
Descriptor = descriptor;
}
public Layer Layer { get; }
public Guid Guid { get; }
public LayerElementSettings Settings { get; }
public LayerElementDescriptor Descriptor { get; }
public virtual void Dispose()
{
}
/// <summary>
/// Called by the profile editor to populate the layer element properties panel
/// </summary>
/// <returns></returns>
public abstract LayerElementViewModel GetViewModel();
/// <summary>
/// Called before rendering every frame, write your update logic here
/// </summary>
/// <param name="deltaTime"></param>
public virtual void Update(double deltaTime)
{
}
/// <summary>
/// Allows you to perform rendering on the surface <see cref="SKCanvas" /> before any layer-clipping is applied
/// <para>Called before rendering, in the order configured on the layer</para>
/// </summary>
/// <param name="framePath"></param>
/// <param name="canvas">The entire surface canvas</param>
public virtual void RenderPreProcess(SKPath framePath, SKCanvas canvas)
{
}
/// <summary>
/// The main method of rendering anything to the layer. The provided <see cref="SKCanvas" /> is specific to the layer
/// and matches it's width and height.
/// <para>Called during rendering, in the order configured on the layer</para>
/// </summary>
/// <param name="framePath"></param>
/// <param name="canvas">The layer canvas</param>
public virtual void Render(SKPath framePath, SKCanvas canvas)
{
}
/// <summary>
/// Allows you to modify the <see cref="SKShader" /> used to draw the layer's <see cref="SKBitmap" /> on the
/// <see cref="SKCanvas" />.
/// <para>Called after rendering, in the order configured on the layer.</para>
/// </summary>
/// <param name="framePath"></param>
/// <param name="canvas"></param>
/// <param name="bitmap">The bitmap created from the layer canvas</param>
/// <param name="shader">The current shader used to draw the bitmap on the surface canvas</param>
/// <returns>The resulting shader used to draw the bitmap on the surface canvas</returns>
public virtual void RenderPostProcess(SKPath framePath, SKCanvas canvas)
{
}
}
}
@@ -1,22 +0,0 @@
using System;
namespace Artemis.Core.Plugins.LayerElement
{
public class LayerElementDescriptor
{
internal LayerElementDescriptor(string displayName, string description, string icon, Type layerElementType, LayerElementProvider layerElementProvider)
{
DisplayName = displayName;
Description = description;
Icon = icon;
LayerElementType = layerElementType;
LayerElementProvider = layerElementProvider;
}
public string DisplayName { get; }
public string Description { get; }
public string Icon { get; }
public Type LayerElementType { get; }
public LayerElementProvider LayerElementProvider { get; }
}
}
@@ -1,28 +0,0 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Artemis.Core.Plugins.Abstract;
using Artemis.Core.Plugins.Models;
namespace Artemis.Core.Plugins.LayerElement
{
/// <inheritdoc />
/// <summary>
/// Allows you to create one or more <see cref="LayerElement" />s usable by profile layers.
/// </summary>
public abstract class LayerElementProvider : Plugin
{
private readonly List<LayerElementDescriptor> _layerElementDescriptors;
protected LayerElementProvider(PluginInfo pluginInfo) : base(pluginInfo)
{
_layerElementDescriptors = new List<LayerElementDescriptor>();
}
public ReadOnlyCollection<LayerElementDescriptor> LayerElementDescriptors => _layerElementDescriptors.AsReadOnly();
protected void AddLayerElementDescriptor<T>(string displayName, string description, string icon) where T : LayerElement
{
_layerElementDescriptors.Add(new LayerElementDescriptor(displayName, description, icon, typeof(T), this));
}
}
}
@@ -1,14 +0,0 @@
using Stylet;
namespace Artemis.Core.Plugins.LayerElement
{
public abstract class LayerElementViewModel : PropertyChangedBase
{
protected LayerElementViewModel(LayerElement layerElement)
{
LayerElement = layerElement;
}
public LayerElement LayerElement { get; }
}
}
@@ -1,21 +1,18 @@
using System;
using Artemis.Core.Models.Profile;
using Artemis.Core.Plugins.LayerElement;
using Artemis.Core.Models.Profile;
using Artemis.Core.Plugins.LayerBrush;
namespace Artemis.Core.Services.Interfaces
{
public interface ILayerService : IArtemisService
{
/// <summary>
/// Instantiates and adds the <see cref="LayerElement" /> described by the provided
/// <see cref="LayerElementDescriptor" /> to the provided <see cref="Layer" />.
/// Instantiates and adds the <see cref="LayerBrush" /> described by the provided
/// <see cref="LayerBrushDescriptor" /> to the provided <see cref="Layer" />.
/// </summary>
/// <param name="layer">The layer to add the new layer element to</param>
/// <param name="layerElementDescriptor">The descriptor of the new layer element</param>
/// <param name="settings">JSON settings to be deserialized and injected into the layer element</param>
/// <param name="brushDescriptor">The descriptor of the new layer brush</param>
/// <param name="settings">JSON settings to be deserialized and injected into the layer brush</param>
/// <returns></returns>
LayerElement InstantiateLayerElement(Layer layer, LayerElementDescriptor layerElementDescriptor, string settings = null, Guid? guid = null);
void RemoveLayerElement(Layer layer, LayerElement layerElement);
LayerBrush InstantiateLayerBrush(Layer layer, LayerBrushDescriptor brushDescriptor, string settings = null);
}
}
+15 -18
View File
@@ -3,7 +3,7 @@ using System.Linq;
using System.Reflection;
using Artemis.Core.Models.Profile;
using Artemis.Core.Plugins.Exceptions;
using Artemis.Core.Plugins.LayerElement;
using Artemis.Core.Plugins.LayerBrush;
using Artemis.Core.Services.Interfaces;
using Newtonsoft.Json;
using Ninject;
@@ -23,16 +23,13 @@ namespace Artemis.Core.Services
_logger = logger;
}
public LayerElement InstantiateLayerElement(Layer layer, LayerElementDescriptor layerElementDescriptor, string settings, Guid? guid)
public LayerBrush InstantiateLayerBrush(Layer layer, LayerBrushDescriptor brushDescriptor, string settings)
{
if (guid == null)
guid = Guid.NewGuid();
// Determine the settings type declared by the layer element
object settingsInstance = null;
var properties = layerElementDescriptor.LayerElementType.GetProperties();
var properties = brushDescriptor.LayerBrushType.GetProperties();
var settingsType = properties.FirstOrDefault(p => p.Name == "Settings" &&
p.DeclaringType == layerElementDescriptor.LayerElementType)?.PropertyType;
p.DeclaringType == brushDescriptor.LayerBrushType)?.PropertyType;
// Deserialize the settings if provided, check for null in JSON as well
if (settings != null && settings != "null")
@@ -41,8 +38,8 @@ namespace Artemis.Core.Services
if (settingsType == null)
{
throw new ArtemisPluginException(
layerElementDescriptor.LayerElementProvider.PluginInfo,
$"Settings where provided but layer element of type {layerElementDescriptor.LayerElementType.Name} has no Settings property."
brushDescriptor.LayerBrushProvider.PluginInfo,
$"Settings where provided but layer element of type {brushDescriptor.LayerBrushType.Name} has no Settings property."
);
}
@@ -53,8 +50,8 @@ namespace Artemis.Core.Services
catch (JsonSerializationException e)
{
_logger.Warning(e, "Failed to deserialize settings for layer type {type}, resetting element settings - Plugin info: {pluginInfo}",
layerElementDescriptor.LayerElementType.Name,
layerElementDescriptor.LayerElementProvider.PluginInfo);
brushDescriptor.LayerBrushType.Name,
brushDescriptor.LayerBrushProvider.PluginInfo);
settingsInstance = Activator.CreateInstance(settingsType);
}
@@ -68,20 +65,20 @@ namespace Artemis.Core.Services
var arguments = new IParameter[]
{
new ConstructorArgument("layer", layer),
new ConstructorArgument("guid", guid.Value),
new ConstructorArgument("settings", settingsInstance),
new ConstructorArgument("descriptor", layerElementDescriptor)
new ConstructorArgument("descriptor", brushDescriptor)
};
var layerElement = (LayerElement) _kernel.Get(layerElementDescriptor.LayerElementType, arguments);
layer.AddLayerElement(layerElement);
var layerElement = (LayerBrush) _kernel.Get(brushDescriptor.LayerBrushType, arguments);
layer.LayerBrush = (layerElement);
return layerElement;
}
public void RemoveLayerElement(Layer layer, LayerElement layerElement)
public void RemoveLayerBrush(Layer layer, LayerBrush layerElement)
{
layer.RemoveLayerElement(layerElement);
layerElement.Dispose();
var brush = layer.LayerBrush;
layer.LayerBrush = null;
brush.Dispose();
}
}
}
@@ -4,7 +4,7 @@ using Artemis.Core.Events;
using Artemis.Core.Models.Profile;
using Artemis.Core.Models.Surface;
using Artemis.Core.Plugins.Abstract;
using Artemis.Core.Plugins.LayerElement;
using Artemis.Core.Plugins.LayerBrush;
using Artemis.Core.Services.Interfaces;
using Artemis.Core.Services.Storage.Interfaces;
using Artemis.Storage.Repositories.Interfaces;
@@ -16,10 +16,10 @@ namespace Artemis.Core.Services.Storage
/// </summary>
public class ProfileService : IProfileService
{
private readonly ILayerService _layerService;
private readonly IPluginService _pluginService;
private readonly IProfileRepository _profileRepository;
private readonly ISurfaceService _surfaceService;
private readonly ILayerService _layerService;
internal ProfileService(IPluginService pluginService, ISurfaceService surfaceService, ILayerService layerService, IProfileRepository profileRepository)
{
@@ -78,7 +78,7 @@ namespace Artemis.Core.Services.Storage
{
module.ChangeActiveProfile(profile, _surfaceService.ActiveSurface);
if (profile != null)
InstantiateProfileLayerElements(profile);
InstantiateProfileLayerBrushes(profile);
}
public void DeleteProfile(Profile profile)
@@ -102,27 +102,22 @@ namespace Artemis.Core.Services.Storage
_profileRepository.Save(profile.ProfileEntity);
}
private void InstantiateProfileLayerElements(Profile profile)
private void InstantiateProfileLayerBrushes(Profile profile)
{
var layerElementProviders = _pluginService.GetPluginsOfType<LayerElementProvider>();
var descriptors = layerElementProviders.SelectMany(l => l.LayerElementDescriptors).ToList();
var layerBrushProviders = _pluginService.GetPluginsOfType<LayerBrushProvider>();
var descriptors = layerBrushProviders.SelectMany(l => l.LayerBrushDescriptors).ToList();
foreach (var layer in profile.GetAllLayers())
// Only instantiate brushes for layers without an existing brush instance
foreach (var layer in profile.GetAllLayers().Where(l => l.LayerBrush == null && l.LayerEntity.BrushEntity != null))
{
foreach (var elementEntity in layer.LayerEntity.Elements)
{
// Skip already instantiated layer elements
if (layer.LayerElements.Any(e => e.Guid == elementEntity.Id))
continue;
// Get a matching descriptor
var descriptor = descriptors.FirstOrDefault(d => d.LayerBrushProvider.PluginInfo.Guid == layer.LayerEntity.BrushEntity.BrushPluginGuid &&
d.LayerBrushType.Name == layer.LayerEntity.BrushEntity.BrushType);
// Get a matching descriptor
var descriptor = descriptors.FirstOrDefault(d => d.LayerElementProvider.PluginInfo.Guid == elementEntity.PluginGuid &&
d.LayerElementType.Name == elementEntity.LayerElementType);
// If a descriptor that matches if found, instantiate it with the GUID of the element entity
if (descriptor != null)
_layerService.InstantiateLayerElement(layer, descriptor, elementEntity.Configuration, elementEntity.Id);
}
// If a descriptor that matches if found, instantiate it with the GUID of the element entity
if (descriptor != null)
_layerService.InstantiateLayerBrush(layer, descriptor, layer.LayerEntity.BrushEntity.Configuration);
}
}
@@ -133,11 +128,11 @@ namespace Artemis.Core.Services.Storage
profileModule.ActiveProfile.PopulateLeds(surface);
}
private void ActiveProfilesInstantiateProfileLayerElements()
private void ActiveProfilesInstantiateProfileLayerBrushes()
{
var profileModules = _pluginService.GetPluginsOfType<ProfileModule>();
foreach (var profileModule in profileModules.Where(p => p.ActiveProfile != null).ToList())
InstantiateProfileLayerElements(profileModule.ActiveProfile);
InstantiateProfileLayerBrushes(profileModule.ActiveProfile);
}
#region Event handlers
@@ -155,8 +150,8 @@ namespace Artemis.Core.Services.Storage
private void OnPluginLoaded(object sender, PluginEventArgs e)
{
if (e.PluginInfo.Instance is LayerElementProvider)
ActiveProfilesInstantiateProfileLayerElements();
if (e.PluginInfo.Instance is LayerBrushProvider)
ActiveProfilesInstantiateProfileLayerBrushes();
}
#endregion