mirror of
https://github.com/Artemis-RGB/Artemis
synced 2026-11-11 19:36:28 +00:00
Conditions - Refactor layer properties WIP
This commit is contained in:
1 parent
d9bba8cb54
commit
660324c980
33 files changed
+363
-492
No files matched your search
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
+101
-60
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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()
|
||||
{
|
||||
|
||||
@@ -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()
|
||||
{
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
namespace Artemis.Core.Models.Profile.LayerProperties.Types
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public class FloatLayerProperty : GenericLayerProperty<float>
|
||||
public class FloatLayerProperty : LayerProperty<float>
|
||||
{
|
||||
internal FloatLayerProperty()
|
||||
{
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
namespace Artemis.Core.Models.Profile.LayerProperties.Types
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public class IntLayerProperty : GenericLayerProperty<int>
|
||||
public class IntLayerProperty : LayerProperty<int>
|
||||
{
|
||||
internal IntLayerProperty()
|
||||
{
|
||||
|
||||
+1
-1
@@ -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()
|
||||
{
|
||||
|
||||
@@ -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()
|
||||
{
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
namespace Artemis.Core.Models.Profile.LayerProperties.Types
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public class SKPointLayerProperty : GenericLayerProperty<SKPoint>
|
||||
public class SKPointLayerProperty : LayerProperty<SKPoint>
|
||||
{
|
||||
internal SKPointLayerProperty()
|
||||
{
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
namespace Artemis.Core.Models.Profile.LayerProperties.Types
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public class SKSizeLayerProperty : GenericLayerProperty<SKSize>
|
||||
public class SKSizeLayerProperty : LayerProperty<SKSize>
|
||||
{
|
||||
internal SKSizeLayerProperty()
|
||||
{
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user