Layer brushes - Added RGB.NET-based layer brushes

Layer brushes - Added sample RGB.NET-based color brush
Layer properties - Save on gradient edit dialog close
Layer properties - Fix layer brush keyframes not working after changing layer brush
This commit is contained in:
SpoinkyNL committed 2020-05-31 21:58:06 +02:00
1 parent 493790f6bd
commit cdb91021a2
31 files changed
+550 -146

No files matched your search

+10 -4
View File
@@ -204,7 +204,7 @@ namespace Artemis.Core.Models.Profile
/// <inheritdoc />
public override void Update(double deltaTime)
{
if (LayerBrush == null || !LayerBrush.BaseProperties.PropertiesInitialized)
if (LayerBrush?.BaseProperties == null || !LayerBrush.BaseProperties.PropertiesInitialized)
return;
var properties = new List<BaseLayerProperty>(General.GetAllLayerProperties().Where(p => p.BaseKeyframes.Any()));
@@ -269,6 +269,9 @@ namespace Artemis.Core.Models.Profile
private void StretchRender(SKCanvas canvas, SKImageInfo canvasInfo, SKPaint paint)
{
if (LayerBrush == null || !LayerBrush.BaseProperties.PropertiesInitialized || LayerBrush.BrushType != LayerBrushType.Regular)
return;
// Apply transformations
var sizeProperty = Transform.Scale.CurrentValue;
var rotationProperty = Transform.Rotation.CurrentValue;
@@ -285,12 +288,14 @@ namespace Artemis.Core.Models.Profile
canvas.Scale(sizeProperty.Width / 100f, sizeProperty.Height / 100f, anchorPosition.X, anchorPosition.Y);
canvas.Translate(x, y);
if (LayerBrush != null && LayerBrush.BaseProperties.PropertiesInitialized)
LayerBrush.Render(canvas, canvasInfo, new SKPath(LayerShape.Path), paint);
LayerBrush.InternalRender(canvas, canvasInfo, new SKPath(LayerShape.Path), paint);
}
private void ClipRender(SKCanvas canvas, SKImageInfo canvasInfo, SKPaint paint)
{
if (LayerBrush == null || !LayerBrush.BaseProperties.PropertiesInitialized || LayerBrush.BrushType != LayerBrushType.Regular)
return;
// Apply transformations
var sizeProperty = Transform.Scale.CurrentValue;
var rotationProperty = Transform.Rotation.CurrentValue;
@@ -321,7 +326,8 @@ namespace Artemis.Core.Models.Profile
);
var renderPath = new SKPath();
renderPath.AddRect(boundsRect);
LayerBrush?.Render(canvas, canvasInfo, renderPath, paint);
LayerBrush.InternalRender(canvas, canvasInfo, renderPath, paint);
}
internal void CalculateRenderProperties()
@@ -165,6 +165,9 @@ namespace Artemis.Core.Models.Profile
internal void ApplyToEntity()
{
if (!PropertiesInitialized)
return;
// Get all properties with a PropertyDescriptionAttribute
foreach (var propertyInfo in GetType().GetProperties())
{
@@ -1,17 +1,25 @@
using System;
using System.Linq;
using Artemis.Core.Models.Profile;
using Artemis.Core.Plugins.Models;
using Artemis.Core.Services.Interfaces;
using RGB.NET.Core;
using RGB.NET.Groups;
using SkiaSharp;
namespace Artemis.Core.Plugins.LayerBrush
{
/// <summary>
/// A basic layer brush that does not implement any layer property, to use properties with persistent storage,
/// implement <see cref="LayerBrush{T}" /> instead
/// For internal use only, please use <see cref="LayerBrush{T}" /> or <see cref="RgbNetLayerBrush{T}" /> or instead
/// </summary>
public abstract class BaseLayerBrush : IDisposable
{
protected BaseLayerBrush(Layer layer, LayerBrushDescriptor descriptor)
{
Layer = layer;
Descriptor = descriptor;
}
/// <summary>
/// Gets the layer this brush is applied to
/// </summary>
@@ -27,38 +35,43 @@ namespace Artemis.Core.Plugins.LayerBrush
/// </summary>
public PluginInfo PluginInfo => Descriptor.LayerBrushProvider.PluginInfo;
public virtual LayerPropertyGroup BaseProperties => null;
/// <summary>
/// Gets the type of layer brush
/// </summary>
public LayerBrushType BrushType { get; internal set; }
/// <summary>
/// Gets a reference to the layer property group without knowing it's type
/// </summary>
public virtual LayerPropertyGroup BaseProperties => null;
/// <summary>
/// Called when the brush is being removed from the layer
/// </summary>
public virtual void Dispose()
{
}
public abstract void Dispose();
/// <summary>
/// Called before rendering every frame, write your update logic here
/// </summary>
/// <param name="deltaTime"></param>
public virtual void Update(double deltaTime)
{
}
public abstract void Update(double deltaTime);
// Not only is this needed to initialize properties on the layer brushes, it also prevents implementing anything
// but LayerBrush<T> and RgbNetLayerBrush<T> outside the core
internal abstract void Initialize(ILayerService layerService);
internal abstract void InternalRender(SKCanvas canvas, SKImageInfo canvasInfo, SKPath path, SKPaint paint);
/// <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 or layer preview, in the order configured on the layer</para>
/// Called when Artemis needs an instance of the RGB.NET brush you are implementing
/// </summary>
/// <param name="canvas">The layer canvas</param>
/// <param name="canvasInfo"></param>
/// <param name="path">The path to be filled, represents the shape</param>
/// <param name="paint">The paint to be used to fill the shape</param>
public virtual void Render(SKCanvas canvas, SKImageInfo canvasInfo, SKPath path, SKPaint paint)
{
}
/// <returns>Your RGB.NET brush</returns>
internal abstract IBrush InternalGetBrush();
}
internal virtual void InitializeProperties(ILayerService layerService, string path)
{
}
public enum LayerBrushType
{
Regular,
RgbNet
}
}
@@ -1,77 +1,52 @@
using System;
using System.Collections.Generic;
using Artemis.Core.Models.Profile;
using Artemis.Core.Models.Profile.LayerProperties;
using Artemis.Core.Plugins.Exceptions;
using Artemis.Core.Services.Interfaces;
using RGB.NET.Core;
using SkiaSharp;
namespace Artemis.Core.Plugins.LayerBrush
{
public abstract class LayerBrush<T> : BaseLayerBrush where T : LayerPropertyGroup
public abstract class LayerBrush<T> : PropertiesLayerBrush<T> where T : LayerPropertyGroup
{
private T _properties;
protected LayerBrush(Layer layer, LayerBrushDescriptor descriptor)
protected LayerBrush(Layer layer, LayerBrushDescriptor descriptor) : base(layer, descriptor)
{
Layer = layer;
Descriptor = descriptor;
}
#region Properties
/// <summary>
/// Gets the properties of this brush.
/// </summary>
public T Properties
{
get
{
// I imagine a null reference here can be confusing, so lets throw an exception explaining what to do
if (_properties == null)
throw new ArtemisPluginException("Cannot access brush properties until OnPropertiesInitialized has been called");
return _properties;
}
internal set => _properties = value;
BrushType = LayerBrushType.Regular;
}
/// <summary>
/// Gets whether all properties on this brush are initialized
/// 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 or layer preview, in the order configured on the layer</para>
/// </summary>
public bool PropertiesInitialized { get; private set; }
/// <param name="canvas">The layer canvas</param>
/// <param name="canvasInfo"></param>
/// <param name="path">The path to be filled, represents the shape</param>
/// <param name="paint">The paint to be used to fill the shape</param>
public abstract void Render(SKCanvas canvas, SKImageInfo canvasInfo, SKPath path, SKPaint paint);
internal override void InternalRender(SKCanvas canvas, SKImageInfo canvasInfo, SKPath path, SKPaint paint)
{
Render(canvas, canvasInfo, path, paint);
}
/// <summary>
/// Called when all layer properties in this brush have been initialized
/// </summary>
protected virtual void OnPropertiesInitialized()
internal override IBrush InternalGetBrush()
{
throw new NotImplementedException("Regular layer brushes do not implement InternalGetBrush");
}
internal override void Initialize(ILayerService layerService)
{
InitializeProperties(layerService);
}
protected virtual void Dispose(bool disposing)
{
}
/// <inheritdoc/>
public override LayerPropertyGroup BaseProperties => Properties;
internal override void InitializeProperties(ILayerService layerService, string path)
public sealed override void Dispose()
{
Properties = Activator.CreateInstance<T>();
Properties.InitializeProperties(layerService, Layer, path);
OnPropertiesInitialized();
PropertiesInitialized = true;
Dispose(true);
GC.SuppressFinalize(this);
}
internal virtual void ApplyToEntity()
{
Properties.ApplyToEntity();
}
internal virtual void OverrideProperties(TimeSpan overrideTime)
{
Properties.Override(overrideTime);
}
internal virtual IReadOnlyCollection<BaseLayerProperty> GetAllLayerProperties()
{
return Properties.GetAllLayerProperties();
}
#endregion
}
}
@@ -0,0 +1,57 @@
using System;
using Artemis.Core.Models.Profile;
using Artemis.Core.Plugins.Exceptions;
using Artemis.Core.Services.Interfaces;
namespace Artemis.Core.Plugins.LayerBrush
{
/// <summary>
/// For internal use only, please use <see cref="LayerBrush{T}" /> or <see cref="RgbNetLayerBrush{T}" /> or instead
/// </summary>
public abstract class PropertiesLayerBrush<T> : BaseLayerBrush where T : LayerPropertyGroup
{
private T _properties;
protected PropertiesLayerBrush(Layer layer, LayerBrushDescriptor descriptor) : base(layer, descriptor)
{
}
/// <summary>
/// Gets whether all properties on this brush are initialized
/// </summary>
public bool PropertiesInitialized { get; internal set; }
/// <inheritdoc />
public override LayerPropertyGroup BaseProperties => Properties;
/// <summary>
/// Gets the properties of this brush.
/// </summary>
public T Properties
{
get
{
// I imagine a null reference here can be confusing, so lets throw an exception explaining what to do
if (_properties == null)
throw new ArtemisPluginException("Cannot access brush properties until OnPropertiesInitialized has been called");
return _properties;
}
internal set => _properties = value;
}
/// <summary>
/// Called when all layer properties in this brush have been initialized
/// </summary>
protected virtual void OnPropertiesInitialized()
{
}
internal void InitializeProperties(ILayerService layerService)
{
Properties = Activator.CreateInstance<T>();
Properties.InitializeProperties(layerService, Layer, "LayerBrush.");
OnPropertiesInitialized();
PropertiesInitialized = true;
}
}
}
@@ -0,0 +1,80 @@
using System;
using System.Linq;
using Artemis.Core.Models.Profile;
using Artemis.Core.Services.Interfaces;
using RGB.NET.Core;
using RGB.NET.Groups;
using SkiaSharp;
namespace Artemis.Core.Plugins.LayerBrush
{
public abstract class RgbNetLayerBrush<T> : PropertiesLayerBrush<T> where T : LayerPropertyGroup
{
protected RgbNetLayerBrush(Layer layer, LayerBrushDescriptor descriptor) : base(layer, descriptor)
{
BrushType = LayerBrushType.RgbNet;
LedGroup = new ListLedGroup();
Layer = layer;
Layer.RenderPropertiesUpdated += LayerOnRenderPropertiesUpdated;
UpdateLedGroup();
}
/// <summary>
/// The LED group this layer brush is applied to
/// </summary>
public ListLedGroup LedGroup { get; internal set; }
/// <summary>
/// Called when Artemis needs an instance of the RGB.NET brush you are implementing
/// </summary>
/// <returns>Your RGB.NET brush</returns>
public abstract IBrush GetBrush();
public sealed override void Dispose()
{
Layer.RenderPropertiesUpdated -= LayerOnRenderPropertiesUpdated;
LedGroup.Detach();
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
}
internal void UpdateLedGroup()
{
// TODO: This simply renders it on top of the rest, get a ZIndex based on layer position
LedGroup.ZIndex = 1;
var missingLeds = Layer.Leds.Where(l => !LedGroup.ContainsLed(l.RgbLed)).Select(l => l.RgbLed).ToList();
var extraLeds = LedGroup.GetLeds().Where(l => Layer.Leds.All(layerLed => layerLed.RgbLed != l)).ToList();
LedGroup.AddLeds(missingLeds);
LedGroup.RemoveLeds(extraLeds);
LedGroup.Brush = GetBrush();
}
internal override void Initialize(ILayerService layerService)
{
InitializeProperties(layerService);
}
// Not used in this brush type
internal override void InternalRender(SKCanvas canvas, SKImageInfo canvasInfo, SKPath path, SKPaint paint)
{
throw new NotImplementedException("RGB.NET layer brushes do not implement InternalRender");
}
internal override IBrush InternalGetBrush()
{
return GetBrush();
}
private void LayerOnRenderPropertiesUpdated(object sender, EventArgs e)
{
UpdateLedGroup();
}
}
}
+2 -2
View File
@@ -58,8 +58,8 @@ namespace Artemis.Core.Services
new ConstructorArgument("layer", layer),
new ConstructorArgument("descriptor", descriptor)
};
layer.LayerBrush = (BaseLayerBrush)_kernel.Get(descriptor.LayerBrushType, arguments); ;
layer.LayerBrush.InitializeProperties(this, "LayerBrush.");
layer.LayerBrush = (BaseLayerBrush) _kernel.Get(descriptor.LayerBrushType, arguments);
layer.LayerBrush.Initialize(this);
layer.OnLayerBrushUpdated();
return layer.LayerBrush;
}