1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00

Conditions - Refactor layer properties WIP

This commit is contained in:
Robert 2020-04-29 19:44:42 +02:00
parent d9bba8cb54
commit 660324c980
33 changed files with 363 additions and 492 deletions

View File

@ -5,11 +5,11 @@ namespace Artemis.Core.Events
{
public class LayerPropertyEventArgs : EventArgs
{
public LayerPropertyEventArgs(LayerProperty layerProperty)
public LayerPropertyEventArgs(BaseLayerProperty layerProperty)
{
LayerProperty = layerProperty;
}
public LayerProperty LayerProperty { get; }
public BaseLayerProperty LayerProperty { get; }
}
}

View File

@ -0,0 +1,20 @@
using System;
namespace Artemis.Core.Events
{
public class PropertyGroupUpdatingEventArgs : EventArgs
{
public PropertyGroupUpdatingEventArgs(double deltaTime)
{
DeltaTime = deltaTime;
}
public PropertyGroupUpdatingEventArgs(TimeSpan overrideTime)
{
OverrideTime = overrideTime;
}
public double DeltaTime { get; }
public TimeSpan OverrideTime { get; }
}
}

View File

@ -1,21 +0,0 @@
using System;
using System.Collections.Generic;
using Artemis.Core.Models.Profile.LayerProperties;
namespace Artemis.Core.Models.Profile.KeyframeEngines
{
/// <inheritdoc />
public class FloatKeyframeEngine : KeyframeEngine
{
public sealed override List<Type> CompatibleTypes { get; } = new List<Type> {typeof(float)};
protected override object GetInterpolatedValue()
{
var currentKeyframe = (Keyframe<float>) CurrentKeyframe;
var nextKeyframe = (Keyframe<float>) NextKeyframe;
var diff = nextKeyframe.Value - currentKeyframe.Value;
return currentKeyframe.Value + diff * KeyframeProgressEased;
}
}
}

View File

@ -1,21 +0,0 @@
using System;
using System.Collections.Generic;
using Artemis.Core.Models.Profile.LayerProperties;
namespace Artemis.Core.Models.Profile.KeyframeEngines
{
/// <inheritdoc />
public class IntKeyframeEngine : KeyframeEngine
{
public sealed override List<Type> CompatibleTypes { get; } = new List<Type> {typeof(int)};
protected override object GetInterpolatedValue()
{
var currentKeyframe = (Keyframe<int>) CurrentKeyframe;
var nextKeyframe = (Keyframe<int>) NextKeyframe;
var diff = nextKeyframe.Value - currentKeyframe.Value;
return (int) Math.Round(currentKeyframe.Value + diff * KeyframeProgressEased, MidpointRounding.AwayFromZero);
}
}
}

View File

@ -1,135 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Artemis.Core.Exceptions;
using Artemis.Core.Models.Profile.LayerProperties;
using Artemis.Core.Utilities;
namespace Artemis.Core.Models.Profile.KeyframeEngines
{
public abstract class KeyframeEngine
{
/// <summary>
/// Indicates whether <see cref="Initialize" /> has been called.
/// </summary>
public bool Initialized { get; private set; }
/// <summary>
/// The layer property this keyframe engine applies to.
/// </summary>
public LayerProperty LayerProperty { get; private set; }
/// <summary>
/// The total progress
/// </summary>
public TimeSpan Progress { get; private set; }
/// <summary>
/// The progress from the current keyframe to the next.
/// <para>Range 0.0 to 1.0.</para>
/// </summary>
public float KeyframeProgress { get; private set; }
/// <summary>
/// The progress from the current keyframe to the next with the current keyframes easing function applied.
/// <para>Range 0.0 to 1.0 but can be higher than 1.0 depending on easing function.</para>
/// </summary>
public float KeyframeProgressEased { get; set; }
/// <summary>
/// The current keyframe
/// </summary>
public BaseKeyframe CurrentKeyframe { get; private set; }
/// <summary>
/// The next keyframe
/// </summary>
public BaseKeyframe NextKeyframe { get; private set; }
/// <summary>
/// The types this keyframe engine supports.
/// </summary>
public abstract List<Type> CompatibleTypes { get; }
/// <summary>
/// Associates the keyframe engine with the provided layer property.
/// </summary>
/// <param name="layerProperty"></param>
public void Initialize(LayerProperty layerProperty)
{
if (Initialized)
throw new ArtemisCoreException("Cannot initialize the same keyframe engine twice");
if (!CompatibleTypes.Contains(layerProperty.Type))
throw new ArtemisCoreException($"This property engine does not support the provided type {layerProperty.Type.Name}");
LayerProperty = layerProperty;
LayerProperty.KeyframeEngine = this;
Initialized = true;
}
/// <summary>
/// Updates the engine's progress
/// </summary>
/// <param name="deltaTime"></param>
public void Update(double deltaTime)
{
if (!Initialized)
return;
var keyframes = LayerProperty.UntypedKeyframes.ToList();
Progress = Progress.Add(TimeSpan.FromSeconds(deltaTime));
// The current keyframe is the last keyframe before the current time
CurrentKeyframe = keyframes.LastOrDefault(k => k.Position <= Progress);
// The next keyframe is the first keyframe that's after the current time
NextKeyframe = keyframes.FirstOrDefault(k => k.Position > Progress);
if (CurrentKeyframe == null)
{
KeyframeProgress = 0;
KeyframeProgressEased = 0;
}
else if (NextKeyframe == null)
{
KeyframeProgress = 1;
KeyframeProgressEased = 1;
}
else
{
var timeDiff = NextKeyframe.Position - CurrentKeyframe.Position;
KeyframeProgress = (float) ((Progress - CurrentKeyframe.Position).TotalMilliseconds / timeDiff.TotalMilliseconds);
KeyframeProgressEased = (float) Easings.Interpolate(KeyframeProgress, CurrentKeyframe.EasingFunction);
}
// LayerProperty determines what's next: reset, stop, continue
}
/// <summary>
/// Overrides the engine's progress to the provided value
/// </summary>
/// <param name="progress"></param>
public void OverrideProgress(TimeSpan progress)
{
Progress = TimeSpan.Zero;
Update(progress.TotalSeconds);
}
/// <summary>
/// Gets the current value, if the progress is in between two keyframes the value will be interpolated
/// </summary>
/// <returns></returns>
public object GetCurrentValue()
{
if (CurrentKeyframe == null && LayerProperty.UntypedKeyframes.Any())
return LayerProperty.UntypedKeyframes.First().BaseValue;
if (CurrentKeyframe == null)
return LayerProperty.BaseValue;
if (NextKeyframe == null)
return CurrentKeyframe.BaseValue;
return GetInterpolatedValue();
}
protected abstract object GetInterpolatedValue();
}
}

View File

@ -1,36 +0,0 @@
using System;
using System.Collections.Generic;
using Artemis.Core.Models.Profile.LayerProperties;
using SkiaSharp;
namespace Artemis.Core.Models.Profile.KeyframeEngines
{
/// <inheritdoc />
public class SKColorKeyframeEngine : KeyframeEngine
{
public sealed override List<Type> CompatibleTypes { get; } = new List<Type> {typeof(SKColor)};
protected override object GetInterpolatedValue()
{
var currentKeyframe = (Keyframe<SKColor>) CurrentKeyframe;
var nextKeyframe = (Keyframe<SKColor>) NextKeyframe;
var redDiff = nextKeyframe.Value.Red - currentKeyframe.Value.Red;
var greenDiff = nextKeyframe.Value.Green - currentKeyframe.Value.Green;
var blueDiff = nextKeyframe.Value.Blue - currentKeyframe.Value.Blue;
var alphaDiff = nextKeyframe.Value.Alpha - currentKeyframe.Value.Alpha;
return new SKColor(
ClampToByte(currentKeyframe.Value.Red + redDiff * KeyframeProgressEased),
ClampToByte(currentKeyframe.Value.Green + greenDiff * KeyframeProgressEased),
ClampToByte(currentKeyframe.Value.Blue + blueDiff * KeyframeProgressEased),
ClampToByte(currentKeyframe.Value.Alpha + alphaDiff * KeyframeProgressEased)
);
}
private byte ClampToByte(float value)
{
return (byte) Math.Max(0, Math.Min(255, value));
}
}
}

View File

@ -1,23 +0,0 @@
using System;
using System.Collections.Generic;
using Artemis.Core.Models.Profile.LayerProperties;
using SkiaSharp;
namespace Artemis.Core.Models.Profile.KeyframeEngines
{
/// <inheritdoc />
public class SKPointKeyframeEngine : KeyframeEngine
{
public sealed override List<Type> CompatibleTypes { get; } = new List<Type> {typeof(SKPoint)};
protected override object GetInterpolatedValue()
{
var currentKeyframe = (Keyframe<SKPoint>) CurrentKeyframe;
var nextKeyframe = (Keyframe<SKPoint>) NextKeyframe;
var xDiff = nextKeyframe.Value.X - currentKeyframe.Value.X;
var yDiff = nextKeyframe.Value.Y - currentKeyframe.Value.Y;
return new SKPoint(currentKeyframe.Value.X + xDiff * KeyframeProgressEased, currentKeyframe.Value.Y + yDiff * KeyframeProgressEased);
}
}
}

View File

@ -1,23 +0,0 @@
using System;
using System.Collections.Generic;
using Artemis.Core.Models.Profile.LayerProperties;
using SkiaSharp;
namespace Artemis.Core.Models.Profile.KeyframeEngines
{
/// <inheritdoc />
public class SKSizeKeyframeEngine : KeyframeEngine
{
public sealed override List<Type> CompatibleTypes { get; } = new List<Type> {typeof(SKSize)};
protected override object GetInterpolatedValue()
{
var currentKeyframe = (Keyframe<SKSize>) CurrentKeyframe;
var nextKeyframe = (Keyframe<SKSize>) NextKeyframe;
var widthDiff = nextKeyframe.Value.Width - currentKeyframe.Value.Width;
var heightDiff = nextKeyframe.Value.Height - currentKeyframe.Value.Height;
return new SKSize(currentKeyframe.Value.Width + widthDiff * KeyframeProgressEased, currentKeyframe.Value.Height + heightDiff * KeyframeProgressEased);
}
}
}

View File

@ -32,8 +32,8 @@ namespace Artemis.Core.Models.Profile
Profile = profile;
Parent = parent;
Name = name;
General = new LayerGeneralProperties();
Transform = new LayerTransformProperties();
General = new LayerGeneralProperties {IsCorePropertyGroup = true};
Transform = new LayerTransformProperties {IsCorePropertyGroup = true};
_leds = new List<ArtemisLed>();
}
@ -47,8 +47,8 @@ namespace Artemis.Core.Models.Profile
Parent = parent;
Name = layerEntity.Name;
Order = layerEntity.Order;
General = new LayerGeneralProperties();
Transform = new LayerTransformProperties();
General = new LayerGeneralProperties {IsCorePropertyGroup = true};
Transform = new LayerTransformProperties {IsCorePropertyGroup = true};
_leds = new List<ArtemisLed>();
}
@ -104,7 +104,7 @@ namespace Artemis.Core.Models.Profile
/// <summary>
/// The brush that will fill the <see cref="LayerShape" />.
/// </summary>
public ILayerBrush LayerBrush { get; internal set; }
public BaseLayerBrush LayerBrush { get; internal set; }
public override string ToString()
{
@ -121,8 +121,9 @@ namespace Artemis.Core.Models.Profile
LayerEntity.Order = Order;
LayerEntity.Name = Name;
LayerEntity.ProfileId = Profile.EntityId;
foreach (var layerProperty in Properties)
layerProperty.ApplyToEntity();
General.ApplyToEntity();
Transform.ApplyToEntity();
LayerBrush.ApplyToEntity();
// LEDs
LayerEntity.Leds.Clear();
@ -146,7 +147,7 @@ namespace Artemis.Core.Models.Profile
private void ApplyShapeType()
{
switch (Properties.ShapeType.CurrentValue)
switch (General.ShapeType.CurrentValue)
{
case LayerShapeType.Ellipse:
LayerShape = new Ellipse(this);
@ -179,20 +180,11 @@ namespace Artemis.Core.Models.Profile
/// <inheritdoc />
public override void Update(double deltaTime)
{
foreach (var property in Properties)
property.KeyframeEngine?.Update(deltaTime);
General.Update(deltaTime);
Transform.Update(deltaTime);
// For now, reset all keyframe engines after the last keyframe was hit
// This is a placeholder method of repeating the animation until repeat modes are implemented
var lastKeyframe = Properties.SelectMany(p => p.UntypedKeyframes).OrderByDescending(t => t.Position).FirstOrDefault();
if (lastKeyframe != null)
{
if (Properties.Any(p => p.KeyframeEngine?.Progress > lastKeyframe.Position))
{
foreach (var baseLayerProperty in Properties)
baseLayerProperty.KeyframeEngine?.OverrideProgress(TimeSpan.Zero);
}
}
LayerBrush?.UpdateProperties(deltaTime);
// TODO: Find the last keyframe and if required, reset the properties
LayerBrush?.Update(deltaTime);
}
@ -208,10 +200,10 @@ namespace Artemis.Core.Models.Profile
using (var paint = new SKPaint())
{
paint.BlendMode = Properties.BlendMode.CurrentValue;
paint.Color = new SKColor(0, 0, 0, (byte) (Properties.Opacity.CurrentValue * 2.55f));
paint.BlendMode = General.BlendMode.CurrentValue;
paint.Color = new SKColor(0, 0, 0, (byte) (Transform.Opacity.CurrentValue * 2.55f));
switch (Properties.FillType.CurrentValue)
switch (General.FillType.CurrentValue)
{
case LayerFillType.Stretch:
StretchRender(canvas, canvasInfo, paint);
@ -230,11 +222,11 @@ namespace Artemis.Core.Models.Profile
private void StretchRender(SKCanvas canvas, SKImageInfo canvasInfo, SKPaint paint)
{
// Apply transformations
var sizeProperty = Properties.Scale.CurrentValue;
var rotationProperty = Properties.Rotation.CurrentValue;
var sizeProperty = Transform.Scale.CurrentValue;
var rotationProperty = Transform.Rotation.CurrentValue;
var anchorPosition = GetLayerAnchorPosition();
var anchorProperty = Properties.AnchorPoint.CurrentValue;
var anchorProperty = Transform.AnchorPoint.CurrentValue;
// Translation originates from the unscaled center of the shape and is tied to the anchor
var x = anchorPosition.X - Bounds.MidX - anchorProperty.X * Bounds.Width;
@ -251,11 +243,11 @@ namespace Artemis.Core.Models.Profile
private void ClipRender(SKCanvas canvas, SKImageInfo canvasInfo, SKPaint paint)
{
// Apply transformations
var sizeProperty = Properties.Scale.CurrentValue;
var rotationProperty = Properties.Rotation.CurrentValue;
var sizeProperty = Transform.Scale.CurrentValue;
var rotationProperty = Transform.Rotation.CurrentValue;
var anchorPosition = GetLayerAnchorPosition();
var anchorProperty = Properties.AnchorPoint.CurrentValue;
var anchorProperty = Transform.AnchorPoint.CurrentValue;
// Translation originates from the unscaled center of the shape and is tied to the anchor
var x = anchorPosition.X - Bounds.MidX - anchorProperty.X * Bounds.Width;
@ -308,7 +300,7 @@ namespace Artemis.Core.Models.Profile
internal SKPoint GetLayerAnchorPosition()
{
var positionProperty = Properties.Position.CurrentValue;
var positionProperty = Transform.Position.CurrentValue;
// Start at the center of the shape
var position = new SKPoint(Bounds.MidX, Bounds.MidY);

View File

@ -0,0 +1,25 @@
using Artemis.Storage.Entities.Profile;
namespace Artemis.Core.Models.Profile.LayerProperties
{
public abstract class BaseLayerProperty
{
/// <summary>
/// Used to declare that this property doesn't belong to a plugin and should use the core plugin GUID
/// </summary>
internal bool IsCoreProperty { get; set; }
/// <summary>
/// Applies the provided property entity to the layer property by deserializing the JSON base value and keyframe values
/// </summary>
/// <param name="entity"></param>
/// <param name="layerPropertyGroup"></param>
internal abstract void ApplyToLayerProperty(PropertyEntity entity, LayerPropertyGroup layerPropertyGroup);
/// <summary>
/// Saves the property to the underlying property entity that was configured when calling
/// <see cref="ApplyToLayerProperty" />
/// </summary>
internal abstract void ApplyToEntity();
}
}

View File

@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Artemis.Core.Exceptions;
using Artemis.Core.Utilities;
using Artemis.Storage.Entities.Profile;
using Newtonsoft.Json;
@ -10,18 +12,19 @@ namespace Artemis.Core.Models.Profile.LayerProperties
/// <summary>
/// Represents a property on a layer. Properties are saved in storage and can optionally be modified from the UI.
/// <para>
/// Note: You cannot initialize layer properties yourself. If properly placed, the Artemis core will initialize
/// Note: You cannot initialize layer properties yourself. If properly placed and annotated, the Artemis core will initialize
/// these for you.
/// </para>
/// </summary>
/// <typeparam name="T">The type of property encapsulated in this layer property</typeparam>
public abstract class GenericLayerProperty<T> : LayerProperty
public abstract class LayerProperty<T> : BaseLayerProperty
{
private T _currentValue;
private List<LayerPropertyKeyframe<T>> _keyframes;
private T _baseValue;
private T _currentValue;
private bool _isInitialized;
private List<LayerPropertyKeyframe<T>> _keyframes;
protected GenericLayerProperty()
protected LayerProperty()
{
_keyframes = new List<LayerPropertyKeyframe<T>>();
}
@ -92,47 +95,8 @@ namespace Artemis.Core.Models.Profile.LayerProperties
/// </summary>
public LayerPropertyKeyframe<T> NextKeyframe { get; protected set; }
/// <summary>
/// Updates the property, moving the timeline forwards by the provided <paramref name="deltaTime" />
/// </summary>
/// <param name="deltaTime">The amount of time to move the timeline forwards</param>
public void Update(double deltaTime)
{
TimelineProgress = TimelineProgress.Add(TimeSpan.FromSeconds(deltaTime));
if (!KeyframesSupported || !KeyframesEnabled)
return;
// The current keyframe is the last keyframe before the current time
CurrentKeyframe = _keyframes.LastOrDefault(k => k.Position <= TimelineProgress);
// The next keyframe is the first keyframe that's after the current time
NextKeyframe = _keyframes.FirstOrDefault(k => k.Position > TimelineProgress);
// No need to update the current value if either of the keyframes are null
if (CurrentKeyframe == null)
CurrentValue = BaseValue;
else if (NextKeyframe == null)
CurrentValue = CurrentKeyframe.Value;
// Only determine progress and current value if both keyframes are present
else
{
var timeDiff = NextKeyframe.Position - CurrentKeyframe.Position;
var keyframeProgress = (float) ((TimelineProgress - CurrentKeyframe.Position).TotalMilliseconds / timeDiff.TotalMilliseconds);
var keyframeProgressEased = (float) Easings.Interpolate(keyframeProgress, CurrentKeyframe.EasingFunction);
UpdateCurrentValue(keyframeProgress, keyframeProgressEased);
}
OnUpdated();
}
/// <summary>
/// Overrides the timeline progress to match the provided <paramref name="progress" />
/// </summary>
/// <param name="progress">The new progress to set the layer property timeline to.</param>
public void OverrideProgress(TimeSpan progress)
{
TimelineProgress = TimeSpan.Zero;
Update(progress.TotalSeconds);
}
internal PropertyEntity PropertyEntity { get; set; }
internal LayerPropertyGroup LayerPropertyGroup { get; set; }
/// <summary>
/// Adds a keyframe to the layer property
@ -166,24 +130,106 @@ namespace Artemis.Core.Models.Profile.LayerProperties
/// <param name="keyframeProgressEased">The current keyframe progress, eased with the current easing function</param>
protected abstract void UpdateCurrentValue(float keyframeProgress, float keyframeProgressEased);
/// <summary>
/// Updates the property, moving the timeline forwards by the provided <paramref name="deltaTime" />
/// </summary>
/// <param name="deltaTime">The amount of time to move the timeline forwards</param>
internal void Update(double deltaTime)
{
TimelineProgress = TimelineProgress.Add(TimeSpan.FromSeconds(deltaTime));
if (!KeyframesSupported || !KeyframesEnabled)
return;
// The current keyframe is the last keyframe before the current time
CurrentKeyframe = _keyframes.LastOrDefault(k => k.Position <= TimelineProgress);
// The next keyframe is the first keyframe that's after the current time
NextKeyframe = _keyframes.FirstOrDefault(k => k.Position > TimelineProgress);
// No need to update the current value if either of the keyframes are null
if (CurrentKeyframe == null)
CurrentValue = BaseValue;
else if (NextKeyframe == null)
CurrentValue = CurrentKeyframe.Value;
// Only determine progress and current value if both keyframes are present
else
{
var timeDiff = NextKeyframe.Position - CurrentKeyframe.Position;
var keyframeProgress = (float) ((TimelineProgress - CurrentKeyframe.Position).TotalMilliseconds / timeDiff.TotalMilliseconds);
var keyframeProgressEased = (float) Easings.Interpolate(keyframeProgress, CurrentKeyframe.EasingFunction);
UpdateCurrentValue(keyframeProgress, keyframeProgressEased);
}
OnUpdated();
}
/// <summary>
/// Overrides the timeline progress to match the provided <paramref name="overrideTime" />
/// </summary>
/// <param name="overrideTime">The new progress to set the layer property timeline to.</param>
internal void OverrideProgress(TimeSpan overrideTime)
{
TimelineProgress = TimeSpan.Zero;
Update(overrideTime.TotalSeconds);
}
/// <summary>
/// Sorts the keyframes in ascending order by position
/// </summary>
internal void SortKeyframes()
{
_keyframes = _keyframes.OrderBy(k => k.Position).ToList();
}
internal override void LoadFromEntity(PropertyEntity entity)
internal override void ApplyToLayerProperty(PropertyEntity entity, LayerPropertyGroup layerPropertyGroup)
{
BaseValue = JsonConvert.DeserializeObject<T>(entity.Value);
CurrentValue = BaseValue;
// Doubt this will happen but let's make sure
if (_isInitialized)
throw new ArtemisCoreException("Layer property already initialized, wut");
_keyframes.Clear();
foreach (var keyframeEntity in entity.KeyframeEntities)
PropertyEntity = entity;
LayerPropertyGroup = layerPropertyGroup;
LayerPropertyGroup.PropertyGroupUpdating += (sender, args) => Update(args.DeltaTime);
LayerPropertyGroup.PropertyGroupOverriding += (sender, args) => OverrideProgress(args.OverrideTime);
try
{
var value = JsonConvert.DeserializeObject<T>(keyframeEntity.Value);
var keyframe = new LayerPropertyKeyframe<T>(value, keyframeEntity.Position, (Easings.Functions) keyframeEntity.EasingFunction);
_keyframes.Add(keyframe);
IsLoadedFromStorage = true;
BaseValue = JsonConvert.DeserializeObject<T>(entity.Value);
CurrentValue = BaseValue;
_keyframes.Clear();
_keyframes.AddRange(entity.KeyframeEntities.Select(k => new LayerPropertyKeyframe<T>(
JsonConvert.DeserializeObject<T>(k.Value),
k.Position,
(Easings.Functions) k.EasingFunction)
));
}
SortKeyframes();
catch (JsonException e)
{
// TODO: Properly log the JSON exception
Debug.WriteLine($"JSON exception while deserializing: {e}");
IsLoadedFromStorage = false;
}
finally
{
SortKeyframes();
_isInitialized = true;
}
}
internal override void ApplyToEntity()
{
if (_isInitialized)
throw new ArtemisCoreException("Layer property is not yet initialized");
PropertyEntity.Value = JsonConvert.SerializeObject(BaseValue);
PropertyEntity.KeyframeEntities.Clear();
PropertyEntity.KeyframeEntities.AddRange(Keyframes.Select(k => new KeyframeEntity
{
Value = JsonConvert.SerializeObject(k.Value),
Position = k.Position,
EasingFunction = (int) k.EasingFunction
}));
}
#region Events
@ -215,9 +261,4 @@ namespace Artemis.Core.Models.Profile.LayerProperties
#endregion
}
public abstract class LayerProperty
{
internal abstract void LoadFromEntity(PropertyEntity entity);
}
}

View File

@ -14,7 +14,7 @@ namespace Artemis.Core.Models.Profile.LayerProperties
EasingFunction = easingFunction;
}
public GenericLayerProperty<T> LayerProperty { get; internal set; }
public LayerProperty<T> LayerProperty { get; internal set; }
public T Value { get; set; }
public TimeSpan Position

View File

@ -4,7 +4,7 @@ using Artemis.Core.Models.Profile.Colors;
namespace Artemis.Core.Models.Profile.LayerProperties.Types
{
/// <inheritdoc/>
public class ColorGradientLayerProperty : GenericLayerProperty<ColorGradient>
public class ColorGradientLayerProperty : LayerProperty<ColorGradient>
{
internal ColorGradientLayerProperty()
{

View File

@ -3,7 +3,7 @@
namespace Artemis.Core.Models.Profile.LayerProperties.Types
{
/// <inheritdoc/>
public class EnumLayerProperty<T> : GenericLayerProperty<T> where T : System.Enum
public class EnumLayerProperty<T> : LayerProperty<T> where T : System.Enum
{
public EnumLayerProperty()
{

View File

@ -1,7 +1,7 @@
namespace Artemis.Core.Models.Profile.LayerProperties.Types
{
/// <inheritdoc/>
public class FloatLayerProperty : GenericLayerProperty<float>
public class FloatLayerProperty : LayerProperty<float>
{
internal FloatLayerProperty()
{

View File

@ -3,7 +3,7 @@
namespace Artemis.Core.Models.Profile.LayerProperties.Types
{
/// <inheritdoc/>
public class IntLayerProperty : GenericLayerProperty<int>
public class IntLayerProperty : LayerProperty<int>
{
internal IntLayerProperty()
{

View File

@ -5,7 +5,7 @@ namespace Artemis.Core.Models.Profile.LayerProperties.Types
/// <summary>
/// A special layer property used to configure the selected layer brush
/// </summary>
public class LayerBrushReferenceLayerProperty : GenericLayerProperty<LayerBrushReference>
public class LayerBrushReferenceLayerProperty : LayerProperty<LayerBrushReference>
{
internal LayerBrushReferenceLayerProperty()
{

View File

@ -4,7 +4,7 @@ using SkiaSharp;
namespace Artemis.Core.Models.Profile.LayerProperties.Types
{
/// <inheritdoc/>
public class SKColorLayerProperty : GenericLayerProperty<SKColor>
public class SKColorLayerProperty : LayerProperty<SKColor>
{
internal SKColorLayerProperty()
{

View File

@ -3,7 +3,7 @@
namespace Artemis.Core.Models.Profile.LayerProperties.Types
{
/// <inheritdoc/>
public class SKPointLayerProperty : GenericLayerProperty<SKPoint>
public class SKPointLayerProperty : LayerProperty<SKPoint>
{
internal SKPointLayerProperty()
{

View File

@ -3,7 +3,7 @@
namespace Artemis.Core.Models.Profile.LayerProperties.Types
{
/// <inheritdoc/>
public class SKSizeLayerProperty : GenericLayerProperty<SKSize>
public class SKSizeLayerProperty : LayerProperty<SKSize>
{
internal SKSizeLayerProperty()
{

View File

@ -1,5 +1,6 @@
using System;
using System.Linq;
using Artemis.Core.Events;
using Artemis.Core.Models.Profile.LayerProperties;
using Artemis.Core.Models.Profile.LayerProperties.Attributes;
using Artemis.Core.Plugins.Exceptions;
@ -11,6 +12,11 @@ namespace Artemis.Core.Models.Profile
{
public bool PropertiesInitialized { get; private set; }
/// <summary>
/// Used to declare that this property group doesn't belong to a plugin and should use the core plugin GUID
/// </summary>
internal bool IsCorePropertyGroup { get; set; }
/// <summary>
/// Called when all layer properties in this property group have been initialized
/// </summary>
@ -26,10 +32,10 @@ namespace Artemis.Core.Models.Profile
var propertyDescription = Attribute.GetCustomAttribute(propertyInfo, typeof(PropertyDescriptionAttribute));
if (propertyDescription != null)
{
if (!typeof(GenericLayerProperty<>).IsAssignableFrom(propertyInfo.PropertyType))
if (!typeof(LayerProperty<>).IsAssignableFrom(propertyInfo.PropertyType))
throw new ArtemisPluginException("Layer property with PropertyDescription attribute must be of type LayerProperty");
var instance = (LayerProperty) Activator.CreateInstance(propertyInfo.PropertyType);
var instance = (BaseLayerProperty) Activator.CreateInstance(propertyInfo.PropertyType);
InitializeProperty(layer, path, instance);
propertyInfo.SetValue(this, instance);
}
@ -52,11 +58,61 @@ namespace Artemis.Core.Models.Profile
PropertiesInitialized = true;
}
private void InitializeProperty(Layer layer, string path, LayerProperty instance)
internal void ApplyToEntity()
{
var entity = layer.LayerEntity.PropertyEntities.FirstOrDefault(p => p.Id == path);
if (entity != null)
instance.LoadFromEntity(entity);
// Get all properties with a PropertyDescriptionAttribute
foreach (var propertyInfo in GetType().GetProperties())
{
var propertyDescription = Attribute.GetCustomAttribute(propertyInfo, typeof(PropertyDescriptionAttribute));
if (propertyDescription != null)
{
}
else
{
var propertyGroupDescription = Attribute.GetCustomAttribute(propertyInfo, typeof(PropertyGroupDescriptionAttribute));
if (propertyGroupDescription != null)
{
}
}
}
}
internal void Update(double deltaTime)
{
// Since at this point we don't know what properties the group has without using reflection,
// let properties subscribe to the update event and update themselves
OnPropertyGroupUpdating(new PropertyGroupUpdatingEventArgs(deltaTime));
}
internal void Override(TimeSpan overrideTime)
{
// Same as above, but now the progress is overridden
OnPropertyGroupOverriding(new PropertyGroupUpdatingEventArgs(overrideTime));
}
private void InitializeProperty(Layer layer, string path, BaseLayerProperty instance)
{
var pluginGuid = IsCorePropertyGroup || instance.IsCoreProperty ? Constants.CorePluginInfo.Guid : layer.LayerBrush.PluginInfo.Guid;
var entity = layer.LayerEntity.PropertyEntities.FirstOrDefault(p => p.PluginGuid == pluginGuid && p.Path == path);
if (entity != null)
instance.ApplyToLayerProperty(entity, this);
}
#region Events
internal event EventHandler<PropertyGroupUpdatingEventArgs> PropertyGroupUpdating;
internal event EventHandler<PropertyGroupUpdatingEventArgs> PropertyGroupOverriding;
internal virtual void OnPropertyGroupUpdating(PropertyGroupUpdatingEventArgs e)
{
PropertyGroupUpdating?.Invoke(this, e);
}
protected virtual void OnPropertyGroupOverriding(PropertyGroupUpdatingEventArgs e)
{
PropertyGroupOverriding?.Invoke(this, e);
}
#endregion
}
}

View File

@ -1,7 +1,5 @@
using System;
using System.IO;
using System.IO;
using Artemis.Core.Exceptions;
using Artemis.Core.Models.Profile.KeyframeEngines;
using Artemis.Core.Plugins.Models;
using Artemis.Core.Services.Interfaces;
using Artemis.Storage.Repositories.Interfaces;
@ -53,14 +51,13 @@ namespace Artemis.Core.Ninject
catch (LiteException e)
{
// I don't like this way of error reporting, now I need to use reflection if I want a meaningful error code
if (e.ErrorCode != LiteException.INVALID_DATABASE)
if (e.ErrorCode != LiteException.INVALID_DATABASE)
throw new ArtemisCoreException($"LiteDB threw error code {e.ErrorCode}. See inner exception for more details", e);
// If the DB is invalid it's probably LiteDB v4 (TODO: we'll have to do something better later)
File.Delete($"{Constants.DataFolder}\\database.db");
return new LiteRepository(Constants.ConnectionString);
}
}).InSingletonScope();
// Bind all repositories as singletons
@ -73,15 +70,6 @@ namespace Artemis.Core.Ninject
.Configure(c => c.InSingletonScope());
});
// Bind all keyframe engines
Kernel.Bind(x =>
{
x.FromAssemblyContaining<KeyframeEngine>()
.SelectAllClasses()
.InheritedFrom<KeyframeEngine>()
.BindAllBaseClasses();
});
Kernel.Bind<PluginSettings>().ToProvider<PluginSettingsProvider>();
Kernel.Bind<ILogger>().ToProvider<LoggerProvider>();
}

View File

@ -0,0 +1,70 @@
using System;
using Artemis.Core.Models.Profile;
using Artemis.Core.Plugins.Models;
using Artemis.Core.Services.Interfaces;
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
/// </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>
/// Called when the brush is being removed from the layer
/// </summary>
public virtual void Dispose()
{
}
/// <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 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 virtual void Render(SKCanvas canvas, SKImageInfo canvasInfo, SKPath path, SKPaint paint)
{
}
internal virtual void InitializeProperties(ILayerService layerService, string path)
{
}
internal virtual void ApplyToEntity()
{
}
internal virtual void UpdateProperties(double deltaTime)
{
}
}
}

View File

@ -1,39 +0,0 @@
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);
}
}

View File

@ -1,11 +1,10 @@
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<T> : ILayerBrush where T : LayerPropertyGroup
public abstract class LayerBrush<T> : BaseLayerBrush where T : LayerPropertyGroup
{
private T _properties;
@ -15,27 +14,6 @@ namespace Artemis.Core.Plugins.LayerBrush
Descriptor = descriptor;
}
/// <inheritdoc />
public Layer Layer { get; }
/// <inheritdoc />
public LayerBrushDescriptor Descriptor { get; }
/// <inheritdoc />
public virtual void Dispose()
{
}
/// <inheritdoc />
public virtual void Update(double deltaTime)
{
}
/// <inheritdoc />
public virtual void Render(SKCanvas canvas, SKImageInfo canvasInfo, SKPath path, SKPaint paint)
{
}
#region Properties
/// <summary>
@ -52,12 +30,12 @@ namespace Artemis.Core.Plugins.LayerBrush
}
internal set => _properties = value;
}
/// <summary>
/// Gets whether all properties on this brush are initialized
/// </summary>
public bool PropertiesInitialized { get; private set; }
/// <summary>
/// Called when all layer properties in this brush have been initialized
/// </summary>
@ -65,13 +43,23 @@ namespace Artemis.Core.Plugins.LayerBrush
{
}
public void InitializeProperties(ILayerService layerService, string path)
internal override void InitializeProperties(ILayerService layerService, string path)
{
Properties.InitializeProperties(layerService, Descriptor.LayerBrushProvider.PluginInfo, path);
Properties.InitializeProperties(layerService, Layer, path);
OnPropertiesInitialized();
PropertiesInitialized = true;
}
internal override void ApplyToEntity()
{
Properties.ApplyToEntity();
}
internal override void UpdateProperties(double deltaTime)
{
Properties.Update(deltaTime);
}
#endregion
}
}

View File

@ -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 : ILayerBrush
protected void AddLayerBrushDescriptor<T>(string displayName, string description, string icon) where T : BaseLayerBrush
{
_layerBrushDescriptors.Add(new LayerBrushDescriptor(displayName, description, icon, typeof(T), this));
}

View File

@ -1,6 +1,4 @@
using Artemis.Core.Models.Profile;
using Artemis.Core.Models.Profile.KeyframeEngines;
using Artemis.Core.Models.Profile.LayerProperties;
using Artemis.Core.Plugins.LayerBrush;
namespace Artemis.Core.Services.Interfaces
@ -8,14 +6,27 @@ namespace Artemis.Core.Services.Interfaces
public interface ILayerService : IArtemisService
{
/// <summary>
/// Instantiates and adds the <see cref="LayerBrush" /> described by the provided <see cref="LayerBrushDescriptor" />
/// Creates a new layer
/// </summary>
/// <param name="profile"></param>
/// <param name="parent"></param>
/// <param name="name"></param>
/// <returns></returns>
Layer CreateLayer(Profile profile, ProfileElement parent, string name);
/// <summary>
/// Instantiates and adds the <see cref="BaseLayerBrush" /> described by the provided
/// <see cref="LayerBrushDescriptor" />
/// to the <see cref="Layer" />.
/// </summary>
/// <param name="layer">The layer to instantiate the brush for</param>
/// <returns></returns>
ILayerBrush InstantiateLayerBrush(Layer layer);
BaseLayerBrush InstantiateLayerBrush(Layer layer);
void LoadPropertyBaseValue(Layer layer, string path, object layerProperty);
void LoadPropertyKeyframes(Layer layer, string path, object layerProperty);
/// <summary>
/// Removes the layer brush from the provided layer and disposes it
/// </summary>
/// <param name="layer"></param>
void RemoveLayerBrush(Layer layer);
}
}

View File

@ -1,12 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Linq;
using Artemis.Core.Models.Profile;
using Artemis.Core.Models.Profile.KeyframeEngines;
using Artemis.Core.Models.Profile.LayerProperties;
using Artemis.Core.Plugins.LayerBrush;
using Artemis.Core.Plugins.Models;
using Artemis.Core.Services.Interfaces;
using Newtonsoft.Json;
using Ninject;
using Ninject.Parameters;
using Serilog;
@ -31,8 +26,8 @@ namespace Artemis.Core.Services
var layer = new Layer(profile, parent, name);
// Layers have two hardcoded property groups, instantiate them
layer.General.InitializeProperties(this, layer, null, null);
layer.Transform.InitializeProperties(this, layer, null, null);
layer.General.InitializeProperties(this, layer, null);
layer.Transform.InitializeProperties(this, layer, null);
// With the properties loaded, the layer brush can be instantiated
InstantiateLayerBrush(layer);
@ -40,7 +35,7 @@ namespace Artemis.Core.Services
return layer;
}
public ILayerBrush InstantiateLayerBrush(Layer layer)
public BaseLayerBrush InstantiateLayerBrush(Layer layer)
{
RemoveLayerBrush(layer);
@ -62,7 +57,7 @@ namespace Artemis.Core.Services
new ConstructorArgument("layer", layer),
new ConstructorArgument("descriptor", descriptor)
};
var layerBrush = (ILayerBrush) _kernel.Get(descriptor.LayerBrushType, arguments);
var layerBrush = (BaseLayerBrush) _kernel.Get(descriptor.LayerBrushType, arguments);
layerBrush.InitializeProperties(this, null);
layer.LayerBrush = layerBrush;
@ -76,11 +71,9 @@ namespace Artemis.Core.Services
var brush = layer.LayerBrush;
layer.LayerBrush = null;
var propertiesToRemove = layer.Properties.Where(l => l.PluginInfo == brush.Descriptor.LayerBrushProvider.PluginInfo).ToList();
foreach (var layerProperty in propertiesToRemove)
layer.Properties.RemoveLayerProperty(layerProperty);
brush.Dispose();
layer.LayerEntity.PropertyEntities.RemoveAll(p => p.PluginGuid == brush.PluginInfo.Guid);
}
}
}

View File

@ -94,7 +94,6 @@ namespace Artemis.Core.Services.Storage
if (profile != null)
{
InstantiateProfileLayerBrushes(profile);
InstantiateProfileKeyframeEngines(profile);
}
}
@ -165,14 +164,7 @@ namespace Artemis.Core.Services.Storage
foreach (var layer in profile.GetAllLayers().Where(l => l.LayerBrush == null))
_layerService.InstantiateLayerBrush(layer);
}
private void InstantiateProfileKeyframeEngines(Profile profile)
{
// Only instantiate engines for properties without an existing engine instance
foreach (var layerProperty in profile.GetAllLayers().SelectMany(l => l.Properties).Where(p => p.KeyframeEngine == null))
_layerService.InstantiateKeyframeEngine(layerProperty);
}
private void ActiveProfilesPopulateLeds(ArtemisSurface surface)
{
var profileModules = _pluginService.GetPluginsOfType<ProfileModule>();
@ -186,14 +178,7 @@ namespace Artemis.Core.Services.Storage
foreach (var profileModule in profileModules.Where(p => p.ActiveProfile != null).ToList())
InstantiateProfileLayerBrushes(profileModule.ActiveProfile);
}
private void ActiveProfilesInstantiateKeyframeEngines()
{
var profileModules = _pluginService.GetPluginsOfType<ProfileModule>();
foreach (var profileModule in profileModules.Where(p => p.ActiveProfile != null).ToList())
InstantiateProfileKeyframeEngines(profileModule.ActiveProfile);
}
#region Event handlers
private void OnActiveSurfaceConfigurationSelected(object sender, SurfaceConfigurationEventArgs e)
@ -212,7 +197,6 @@ namespace Artemis.Core.Services.Storage
if (e.PluginInfo.Instance is LayerBrushProvider)
{
ActiveProfilesInstantiateProfileLayerBrushes();
ActiveProfilesInstantiateKeyframeEngines();
}
else if (e.PluginInfo.Instance is ProfileModule profileModule)
{

View File

@ -10,8 +10,9 @@ namespace Artemis.Storage.Entities.Profile
KeyframeEntities = new List<KeyframeEntity>();
}
public string Id { get; set; }
public string ValueType { get; set; }
public Guid PluginGuid { get; set; }
public string Path { get; set; }
public string Value { get; set; }
public bool IsUsingKeyframes { get; set; }

View File

@ -50,7 +50,7 @@ namespace Artemis.UI.Ninject.Factories
public interface ILayerPropertyVmFactory : IVmFactory
{
LayerPropertyViewModel Create(LayerProperty layerProperty, LayerPropertyViewModel parent);
LayerPropertyViewModel Create(BaseLayerProperty layerProperty, LayerPropertyViewModel parent);
}
public interface IPropertyTreeVmFactory : IVmFactory

View File

@ -169,7 +169,7 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.LayerProperties
PopulateProperties(e.LayerProperty.Layer);
}
private LayerPropertyViewModel CreatePropertyViewModel(LayerProperty layerProperty)
private LayerPropertyViewModel CreatePropertyViewModel(BaseLayerProperty layerProperty)
{
LayerPropertyViewModel parent = null;
// If the property has a parent, find it's VM

View File

@ -16,7 +16,7 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.LayerProperties
private bool _keyframesEnabled;
private bool _isExpanded;
public LayerPropertyViewModel(LayerProperty layerProperty, LayerPropertyViewModel parent, IKernel kernel, IProfileEditorService profileEditorService)
public LayerPropertyViewModel(BaseLayerProperty layerProperty, LayerPropertyViewModel parent, IKernel kernel, IProfileEditorService profileEditorService)
{
_kernel = kernel;
_profileEditorService = profileEditorService;
@ -30,7 +30,7 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.LayerProperties
Parent?.Children.Add(this);
}
public LayerProperty LayerProperty { get; }
public BaseLayerProperty LayerProperty { get; }
public LayerPropertyViewModel Parent { get; }
public List<LayerPropertyViewModel> Children { get; }