Conditions - Refactor layer properties WIP (needed for consistency..)

This commit is contained in:
Robert committed 2020-04-28 19:40:03 +02:00
1 parent 302ba10caa
commit d9bba8cb54
38 files changed
+809 -1099

No files matched your search

@@ -0,0 +1,39 @@
using System;
using Artemis.Core.Models.Profile;
using Artemis.Core.Services.Interfaces;
using SkiaSharp;
namespace Artemis.Core.Plugins.LayerBrush
{
public interface ILayerBrush : IDisposable
{
/// <summary>
/// Gets the layer this brush is applied to
/// </summary>
Layer Layer { get; }
/// <summary>
/// Gets the descriptor of this brush
/// </summary>
LayerBrushDescriptor Descriptor { get; }
/// <summary>
/// Called before rendering every frame, write your update logic here
/// </summary>
/// <param name="deltaTime"></param>
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 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>
void Render(SKCanvas canvas, SKImageInfo canvasInfo, SKPath path, SKPaint paint);
public void InitializeProperties(ILayerService layerService, string path);
}
}
@@ -1,16 +1,13 @@
using System;
using System.Linq;
using Artemis.Core.Models.Profile;
using Artemis.Core.Models.Profile.LayerProperties;
using Artemis.Core.Models.Profile;
using Artemis.Core.Plugins.Exceptions;
using Artemis.Core.Services.Interfaces;
using SkiaSharp;
namespace Artemis.Core.Plugins.LayerBrush
{
public abstract class LayerBrush : IDisposable
public abstract class LayerBrush<T> : ILayerBrush where T : LayerPropertyGroup
{
private ILayerService _layerService;
private T _properties;
protected LayerBrush(Layer layer, LayerBrushDescriptor descriptor)
{
@@ -18,112 +15,63 @@ namespace Artemis.Core.Plugins.LayerBrush
Descriptor = descriptor;
}
/// <inheritdoc />
public Layer Layer { get; }
/// <inheritdoc />
public LayerBrushDescriptor Descriptor { get; }
/// <inheritdoc />
public virtual void Dispose()
{
}
/// <summary>
/// Called before rendering every frame, write your update logic here
/// </summary>
/// <param name="deltaTime"></param>
/// <inheritdoc />
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 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>
/// <inheritdoc />
public virtual void Render(SKCanvas canvas, SKImageInfo canvasInfo, SKPath path, SKPaint paint)
{
}
/// <summary>
/// Provides an easy way to add your own properties to the layer, for more info see <see cref="LayerProperty{T}" />.
/// <para>Note: If found, the last value and keyframes are loaded and set when calling this method.</para>
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="parent">The parent of this property, use this to create a tree-hierarchy in the editor</param>
/// <param name="id">A and ID identifying your property, must be unique within your plugin</param>
/// <param name="name">A name for your property, this is visible in the editor</param>
/// <param name="description">A description for your property, this is visible in the editor</param>
/// <param name="defaultValue">The default value of the property, if not configured by the user</param>
/// <returns>The layer property</returns>
protected LayerProperty<T> RegisterLayerProperty<T>(BaseLayerProperty parent, string id, string name, string description, T defaultValue = default)
{
var property = new LayerProperty<T>(Layer, Descriptor.LayerBrushProvider.PluginInfo, parent, id, name, description) {Value = defaultValue};
Layer.Properties.RegisterLayerProperty(property);
// It's fine if this is null, it'll be picked up by SetLayerService later
_layerService?.InstantiateKeyframeEngine(property);
return property;
}
#region Properties
/// <summary>
/// Provides an easy way to add your own properties to the layer, for more info see <see cref="LayerProperty{T}" />.
/// <para>Note: If found, the last value and keyframes are loaded and set when calling this method.</para>
/// Gets the properties of this brush.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="id">A and ID identifying your property, must be unique within your plugin</param>
/// <param name="name">A name for your property, this is visible in the editor</param>
/// <param name="description">A description for your property, this is visible in the editor</param>
/// <param name="defaultValue">The default value of the property, if not configured by the user</param>
/// <returns>The layer property</returns>
protected LayerProperty<T> RegisterLayerProperty<T>(string id, string name, string description, T defaultValue = default)
public T Properties
{
// Check if the property already exists
var existing = Layer.Properties.FirstOrDefault(p =>
p.PluginInfo.Guid == Descriptor.LayerBrushProvider.PluginInfo.Guid &&
p.Id == id &&
p.Name == name &&
p.Description == description);
if (existing != null)
get
{
// If it exists and the types match, return the existing property
if (existing.Type == typeof(T))
return (LayerProperty<T>) existing;
// If it exists and the types are different, something is wrong
throw new ArtemisPluginException($"Cannot register the property {id} with different types twice.");
// 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;
}
var property = new LayerProperty<T>(Layer, Descriptor.LayerBrushProvider.PluginInfo, Layer.Properties.BrushReference.Parent, id, name, description)
{
Value = defaultValue
};
Layer.Properties.RegisterLayerProperty(property);
// It's fine if this is null, it'll be picked up by SetLayerService later
_layerService?.InstantiateKeyframeEngine(property);
return property;
internal set => _properties = value;
}
/// <summary>
/// Allows you to remove layer properties previously added by using <see cref="RegisterLayerProperty{T}(BaseLayerProperty,string,string,string,T)" />.
/// Gets whether all properties on this brush are initialized
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="layerProperty"></param>
protected void UnRegisterLayerProperty<T>(LayerProperty<T> layerProperty)
public bool PropertiesInitialized { get; private set; }
/// <summary>
/// Called when all layer properties in this brush have been initialized
/// </summary>
protected virtual void OnPropertiesInitialized()
{
if (layerProperty == null)
return;
if (Layer.Properties.Any(p => p == layerProperty))
Layer.Properties.RemoveLayerProperty(layerProperty);
}
internal void SetLayerService(ILayerService layerService)
public void InitializeProperties(ILayerService layerService, string path)
{
_layerService = layerService;
foreach (var baseLayerProperty in Layer.Properties)
_layerService.InstantiateKeyframeEngine(baseLayerProperty);
Properties.InitializeProperties(layerService, Descriptor.LayerBrushProvider.PluginInfo, path);
OnPropertiesInitialized();
PropertiesInitialized = true;
}
#endregion
}
}
@@ -20,7 +20,7 @@ namespace Artemis.Core.Plugins.LayerBrush
public ReadOnlyCollection<LayerBrushDescriptor> LayerBrushDescriptors => _layerBrushDescriptors.AsReadOnly();
protected void AddLayerBrushDescriptor<T>(string displayName, string description, string icon) where T : LayerBrush
protected void AddLayerBrushDescriptor<T>(string displayName, string description, string icon) where T : ILayerBrush
{
_layerBrushDescriptors.Add(new LayerBrushDescriptor(displayName, description, icon, typeof(T), this));
}