Plugins - Removed plugin info from constructor

This commit is contained in:
SpoinkyNL committed 2020-06-07 21:51:56 +02:00
1 parent 33bb3ffa6c
commit 1d35a02d5b
49 files changed
+305 -390

No files matched your search

@@ -0,0 +1,79 @@
using System;
using Artemis.Core.Models.Profile;
using Artemis.Core.Plugins.Models;
using Artemis.Core.Services.Interfaces;
using SkiaSharp;
namespace Artemis.Core.Plugins.LayerBrush.Abstract
{
/// <summary>
/// For internal use only, please use <see cref="LayerBrush{T}" /> or <see cref="RgbNetLayerBrush{T}" /> or instead
/// </summary>
public abstract class BaseLayerBrush : IDisposable
{
/// <summary>
/// Gets the layer this brush is applied to
/// </summary>
public Layer Layer { get; internal set; }
/// <summary>
/// Gets the descriptor of this brush
/// </summary>
public LayerBrushDescriptor Descriptor { get; internal set; }
/// <summary>
/// Gets the plugin info that defined this brush
/// </summary>
public PluginInfo PluginInfo => Descriptor.LayerBrushProvider.PluginInfo;
/// <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;
public void Dispose()
{
DisableLayerBrush();
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Called when the layer brush is activated
/// </summary>
public abstract void EnableLayerBrush();
/// <summary>
/// Called when the layer brush is deactivated
/// </summary>
public abstract void DisableLayerBrush();
/// <summary>
/// Called before rendering every frame, write your update logic here
/// </summary>
/// <param name="deltaTime"></param>
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);
internal virtual void Dispose(bool disposing)
{
}
}
public enum LayerBrushType
{
Regular,
RgbNet
}
}
@@ -0,0 +1,40 @@
using Artemis.Core.Models.Profile;
using Artemis.Core.Services.Interfaces;
using SkiaSharp;
namespace Artemis.Core.Plugins.LayerBrush.Abstract
{
public abstract class LayerBrush<T> : PropertiesLayerBrush<T> where T : LayerPropertyGroup
{
protected LayerBrush()
{
BrushType = LayerBrushType.Regular;
}
/// <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>
/// </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 abstract void Render(SKCanvas canvas, SKImageInfo canvasInfo, SKPath path, SKPaint paint);
internal override void InternalRender(SKCanvas canvas, SKImageInfo canvasInfo, SKPath path, SKPaint paint)
{
// Move the canvas to the top-left of the render path
canvas.Translate(path.Bounds.Left, path.Bounds.Top);
// Pass the render path to the layer brush positioned at 0,0
path.Transform(SKMatrix.MakeTranslation(path.Bounds.Left * -1, path.Bounds.Top * -1));
Render(canvas, canvasInfo, path, paint);
}
internal override void Initialize(ILayerService layerService)
{
InitializeProperties(layerService);
}
}
}
@@ -0,0 +1,47 @@
using System;
using Artemis.Core.Models.Profile;
using Artemis.Core.Plugins.Exceptions;
using Artemis.Core.Services.Interfaces;
namespace Artemis.Core.Plugins.LayerBrush.Abstract
{
/// <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;
/// <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;
}
internal void InitializeProperties(ILayerService layerService)
{
Properties = Activator.CreateInstance<T>();
Properties.InitializeProperties(layerService, Layer, "LayerBrush.");
PropertiesInitialized = true;
EnableLayerBrush();
}
}
}
@@ -0,0 +1,72 @@
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.Abstract
{
public abstract class RgbNetLayerBrush<T> : PropertiesLayerBrush<T> where T : LayerPropertyGroup
{
protected RgbNetLayerBrush()
{
BrushType = LayerBrushType.RgbNet;
}
/// <summary>
/// The LED group this layer effect is applied to
/// </summary>
public ListLedGroup LedGroup { get; internal set; }
/// <summary>
/// Called when Artemis needs an instance of the RGB.NET effect you are implementing
/// </summary>
/// <returns>Your RGB.NET effect</returns>
public abstract IBrush GetBrush();
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)
{
LedGroup = new ListLedGroup();
Layer.RenderPropertiesUpdated += LayerOnRenderPropertiesUpdated;
InitializeProperties(layerService);
UpdateLedGroup();
}
internal override void Dispose(bool disposing)
{
if (disposing)
{
Layer.RenderPropertiesUpdated -= LayerOnRenderPropertiesUpdated;
LedGroup.Detach();
}
base.Dispose(disposing);
}
// Not used in this effect type
internal override void InternalRender(SKCanvas canvas, SKImageInfo canvasInfo, SKPath path, SKPaint paint)
{
throw new NotImplementedException("RGB.NET layer effectes do not implement InternalRender");
}
private void LayerOnRenderPropertiesUpdated(object sender, EventArgs e)
{
UpdateLedGroup();
}
}
}