Data bindings - Added converters for the default layer property types

This commit is contained in:
SpoinkyNL committed 2020-09-06 23:57:26 +02:00
1 parent 1dc58fd09a
commit aebe3005e7
17 files changed
+403 -190

No files matched your search

@@ -11,8 +11,12 @@ namespace Artemis.Core
/// </summary>
public abstract class BaseLayerProperty
{
protected readonly List<DataBinding> _dataBindings = new List<DataBinding>();
protected readonly List<DataBindingRegistration> _dataBindingRegistrations = new List<DataBindingRegistration>();
protected readonly List<DataBinding> _dataBindings = new List<DataBinding>();
private object _baseValue;
private object _currentValue;
private object _defaultValue;
private bool _isHidden;
private bool _keyframesEnabled;
@@ -20,11 +24,59 @@ namespace Artemis.Core
{
}
/// <summary>
/// Gets or sets the base value of this layer property without any keyframes applied
/// </summary>
public object BaseValue
{
get => _baseValue;
set
{
if (value.GetType() != GetPropertyType())
throw new ArtemisCoreException("Cannot update base value because of a type mismatch");
_baseValue = value;
}
}
/// <summary>
/// Gets the current value of this property as it is affected by it's keyframes, updated once every frame
/// </summary>
public object CurrentValue
{
get => _currentValue;
set
{
if (value.GetType() != GetPropertyType())
throw new ArtemisCoreException("Cannot update current value because of a type mismatch");
_currentValue = value;
}
}
/// <summary>
/// Gets or sets the default value of this layer property. If set, this value is automatically applied if the property
/// has no value in storage
/// </summary>
public object DefaultValue
{
get => _defaultValue;
set
{
if (value.GetType() != GetPropertyType())
throw new ArtemisCoreException("Cannot update default value because of a type mismatch");
_defaultValue = value;
}
}
/// <summary>
/// Gets a list containing the active data bindings
/// </summary>
public IReadOnlyList<DataBinding> DataBindings => _dataBindings.AsReadOnly();
/// <summary>
/// Gets a list containing all the data binding registrations
/// </summary>
public IReadOnlyList<DataBindingRegistration> DataBindingRegistrations => _dataBindingRegistrations.AsReadOnly();
/// <summary>
/// Gets the profile element (such as layer or folder) this effect is applied to
/// </summary>
@@ -122,13 +174,6 @@ namespace Artemis.Core
#region Data bindings
internal DataBindingRegistration RegisterDataBindingProperty(PropertyInfo property, IDataBindingConverter converter)
{
var registration = new DataBindingRegistration(this, property, converter);
_dataBindingRegistrations.Add(registration);
return registration;
}
internal void InitializeDataBindings(IDataModelService dataModelService, IDataBindingService dataBindingService)
{
foreach (var dataBinding in DataBindings)
@@ -141,7 +186,7 @@ namespace Artemis.Core
/// <returns>The newly created data binding</returns>
public DataBinding EnableDataBinding(DataBindingRegistration dataBindingRegistration)
{
var dataBinding = new DataBinding(this, dataBindingProperty);
var dataBinding = new DataBinding(dataBindingRegistration);
_dataBindings.Add(dataBinding);
return dataBinding;
@@ -3,8 +3,6 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using Artemis.Core.DataModelExpansions;
using Artemis.Storage.Entities.Profile;
using Newtonsoft.Json;
@@ -20,7 +18,6 @@ namespace Artemis.Core
/// <typeparam name="T">The type of property encapsulated in this layer property</typeparam>
public abstract class LayerProperty<T> : BaseLayerProperty
{
private T _baseValue;
private bool _isInitialized;
private List<LayerPropertyKeyframe<T>> _keyframes;
@@ -32,30 +29,37 @@ namespace Artemis.Core
/// <summary>
/// Gets or sets the base value of this layer property without any keyframes applied
/// </summary>
public T BaseValue
public new T BaseValue
{
get => _baseValue;
get => (T) base.BaseValue;
set
{
if (_baseValue != null && !_baseValue.Equals(value) || _baseValue == null && value != null)
{
_baseValue = value;
OnBaseValueChanged();
}
if (Equals(base.BaseValue, value))
return;
base.BaseValue = value;
OnBaseValueChanged();
}
}
/// <summary>
/// Gets the current value of this property as it is affected by it's keyframes, updated once every frame
/// </summary>
public T CurrentValue { get; set; }
public new T CurrentValue
{
get => (T) base.CurrentValue;
set => base.CurrentValue = value;
}
/// <summary>
/// Gets or sets the default value of this layer property. If set, this value is automatically applied if the property
/// has no
/// value in storage
/// has no value in storage
/// </summary>
public T DefaultValue { get; set; }
public new T DefaultValue
{
get => (T) base.DefaultValue;
set => base.DefaultValue = value;
}
/// <summary>
/// Gets a read-only list of all the keyframes on this layer property
@@ -309,42 +313,14 @@ namespace Artemis.Core
public void RegisterDataBindingProperty<TProperty>(Expression<Func<T, TProperty>> propertyLambda, IDataBindingConverter converter)
{
var propertyInfo = ReflectionUtilities.GetPropertyInfo(CurrentValue, propertyLambda);
if (converter.SupportedType != propertyInfo.PropertyType)
throw new ArtemisCoreException($"Cannot register data binding property for property {propertyInfo.Name} " +
"because the provided converter does not support the property's type");
_dataBindingRegistrations.Add(new DataBindingRegistration(this, propertyInfo, converter));
}
/// <summary>
/// Registers the provided property to be available for data binding using a data binding property of type
/// <typeparamref name="TD" />
/// </summary>
/// <typeparam name="TD">The type of data binding property to use</typeparam>
/// <param name="propertyName">The name of the property</param>
/// <returns></returns>
public TD RegisterDataBindingProperty<TD>(string propertyName, IDataBindingConverter converter) where TD : BaseDataBindingProperty
{
var property = typeof(T).GetProperty(propertyName);
if (property == null)
{
throw new ArtemisCoreException($"Cannot register data binding property for property {propertyName} " +
$"because it does not exist on type {typeof(T).Name}");
}
// Create an instance of the converter
var dataBindingOperator = (TD) Activator.CreateInstance(typeof(TD), property);
if (dataBindingOperator == null)
throw new ArtemisCoreException($"Cannot register data binding, failed to create an instance of {typeof(TD).Name}");
if (dataBindingOperator.PropertyType != property.PropertyType)
{
throw new ArtemisCoreException($"Cannot register data binding property for property {propertyName} using a {typeof(TD).Name} " +
$"because it does not support type {typeof(T).Name}");
}
AddDataBindingConverter(dataBindingOperator);
return dataBindingOperator;
}
private void UpdateDataBindings(double deltaTime)
{
foreach (var dataBinding in DataBindings)
@@ -11,11 +11,15 @@ namespace Artemis.Core
DataBindingsSupported = false;
}
/// <summary>
/// Implicitly converts an <see cref="ColorGradientLayerProperty" /> to a <see cref="ColorGradient"/>
/// </summary>
public static implicit operator ColorGradient(ColorGradientLayerProperty p)
{
return p.CurrentValue;
}
/// <inheritdoc />
protected override void UpdateCurrentValue(float keyframeProgress, float keyframeProgressEased)
{
throw new ArtemisCoreException("Color Gradients do not support keyframes.");
@@ -5,22 +5,29 @@ namespace Artemis.Core
/// <inheritdoc />
public class EnumLayerProperty<T> : LayerProperty<T> where T : Enum
{
public EnumLayerProperty()
internal EnumLayerProperty()
{
KeyframesSupported = false;
DataBindingsSupported = false;
}
/// <summary>
/// Implicitly converts an <see cref="EnumLayerProperty{T}" /> to a <typeparamref name="T"/>
/// </summary>
public static implicit operator T(EnumLayerProperty<T> p)
{
return p.CurrentValue;
}
/// <summary>
/// Implicitly converts an <see cref="EnumLayerProperty{T}" /> to an <see cref="int" />
/// </summary>
public static implicit operator int(EnumLayerProperty<T> p)
{
return Convert.ToInt32(p.CurrentValue);
}
/// <inheritdoc />
protected override void UpdateCurrentValue(float keyframeProgress, float keyframeProgressEased)
{
throw new ArtemisCoreException("Enum properties do not support keyframes.");
@@ -5,18 +5,26 @@
{
internal FloatLayerProperty()
{
RegisterDataBindingProperty(value => value, new FloatDataBindingConverter());
}
/// <summary>
/// Implicitly converts an <see cref="FloatLayerProperty" /> to a <see cref="float" />
/// </summary>
public static implicit operator float(FloatLayerProperty p)
{
return p.CurrentValue;
}
/// <summary>
/// Implicitly converts an <see cref="FloatLayerProperty" /> to a <see cref="double" />
/// </summary>
public static implicit operator double(FloatLayerProperty p)
{
return p.CurrentValue;
}
/// <inheritdoc />
protected override void UpdateCurrentValue(float keyframeProgress, float keyframeProgressEased)
{
var diff = NextKeyframe.Value - CurrentKeyframe.Value;
@@ -7,23 +7,34 @@ namespace Artemis.Core
{
internal IntLayerProperty()
{
RegisterDataBindingProperty(value => value, new IntDataBindingConverter());
}
/// <summary>
/// Implicitly converts an <see cref="IntLayerProperty" /> to an <see cref="int" />
/// </summary>
public static implicit operator int(IntLayerProperty p)
{
return p.CurrentValue;
}
/// <summary>
/// Implicitly converts an <see cref="IntLayerProperty" /> to a <see cref="float" />
/// </summary>
public static implicit operator float(IntLayerProperty p)
{
return p.CurrentValue;
}
/// <summary>
/// Implicitly converts an <see cref="IntLayerProperty" /> to a <see cref="double" />
/// </summary>
public static implicit operator double(IntLayerProperty p)
{
return p.CurrentValue;
}
/// <inheritdoc />
protected override void UpdateCurrentValue(float keyframeProgress, float keyframeProgressEased)
{
var diff = NextKeyframe.Value - CurrentKeyframe.Value;
@@ -11,11 +11,15 @@
DataBindingsSupported = false;
}
/// <summary>
/// Implicitly converts an <see cref="LayerBrushReferenceLayerProperty" /> to an <see cref="LayerBrushReference" />
/// </summary>
public static implicit operator LayerBrushReference(LayerBrushReferenceLayerProperty p)
{
return p.CurrentValue;
}
/// <inheritdoc />
protected override void UpdateCurrentValue(float keyframeProgress, float keyframeProgressEased)
{
throw new ArtemisCoreException("Layer brush references do not support keyframes.");
@@ -1,5 +1,4 @@
using System;
using SkiaSharp;
using SkiaSharp;
namespace Artemis.Core
{
@@ -8,11 +7,11 @@ namespace Artemis.Core
{
internal SKColorLayerProperty()
{
RegisterDataBindingProperty(color => color.Alpha, new SKColorDataBindingConverter(SKColorDataBindingConverter.Channel.Alpha));
RegisterDataBindingProperty(color => color.Red, new SKColorDataBindingConverter(SKColorDataBindingConverter.Channel.Red));
RegisterDataBindingProperty(color => color.Green, new SKColorDataBindingConverter(SKColorDataBindingConverter.Channel.Green));
RegisterDataBindingProperty(color => color.Blue, new SKColorDataBindingConverter(SKColorDataBindingConverter.Channel.Blue));
RegisterDataBindingProperty(color => color.Hue, new SKColorDataBindingConverter(SKColorDataBindingConverter.Channel.Hue));
RegisterDataBindingProperty(color => color.Alpha, new SKColorPartDataBindingConverter(SKColorPartDataBindingConverter.Channel.Alpha));
RegisterDataBindingProperty(color => color.Red, new SKColorPartDataBindingConverter(SKColorPartDataBindingConverter.Channel.Red));
RegisterDataBindingProperty(color => color.Green, new SKColorPartDataBindingConverter(SKColorPartDataBindingConverter.Channel.Green));
RegisterDataBindingProperty(color => color.Blue, new SKColorPartDataBindingConverter(SKColorPartDataBindingConverter.Channel.Blue));
RegisterDataBindingProperty(color => color.Hue, new SKColorPartDataBindingConverter(SKColorPartDataBindingConverter.Channel.Hue));
}
/// <summary>
@@ -31,83 +30,4 @@ namespace Artemis.Core
CurrentValue = CurrentKeyframe.Value.Interpolate(NextKeyframe.Value, keyframeProgressEased);
}
}
internal class SKColorDataBindingConverter : IDataBindingConverter
{
private readonly Channel _channel;
public SKColorDataBindingConverter(Channel channel)
{
_channel = channel;
}
public BaseLayerProperty BaseLayerProperty { get; set; }
public object Sum(object a, object b)
{
return (float) a + (float) b;
}
public object Interpolate(object a, object b, float progress)
{
var diff = (float) b - (float) a;
return diff * progress;
}
public void ApplyValue(object value)
{
var property = (SKColorLayerProperty) BaseLayerProperty;
switch (_channel)
{
case Channel.Alpha:
property.CurrentValue = property.CurrentValue.WithAlpha((byte) value);
break;
case Channel.Red:
property.CurrentValue = property.CurrentValue.WithRed((byte) value);
break;
case Channel.Green:
property.CurrentValue = property.CurrentValue.WithGreen((byte) value);
break;
case Channel.Blue:
property.CurrentValue = property.CurrentValue.WithBlue((byte) value);
break;
case Channel.Hue:
property.CurrentValue.ToHsv(out var h, out var s, out var v);
property.CurrentValue = SKColor.FromHsv((float) value, s, v);
break;
default:
throw new ArgumentOutOfRangeException();
}
}
public object GetValue()
{
var property = (SKColorLayerProperty) BaseLayerProperty;
switch (_channel)
{
case Channel.Alpha:
return property.CurrentValue.Alpha;
case Channel.Red:
return property.CurrentValue.Red;
case Channel.Green:
return property.CurrentValue.Green;
case Channel.Blue:
return property.CurrentValue.Blue;
case Channel.Hue:
property.CurrentValue.ToHsv(out var h, out _, out _);
return h;
default:
throw new ArgumentOutOfRangeException();
}
}
public enum Channel
{
Alpha,
Red,
Green,
Blue,
Hue
}
}
}
@@ -1,7 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using SkiaSharp;
using SkiaSharp;
namespace Artemis.Core
{
@@ -10,8 +7,10 @@ namespace Artemis.Core
{
internal SKPointLayerProperty()
{
RegisterDataBindingProperty(point => point.X, new FloatDataBindingConverter());
RegisterDataBindingProperty(point => point.Y, new FloatDataBindingConverter());
}
/// <summary>
/// Implicitly converts an <see cref="SKPointLayerProperty" /> to an <see cref="SKPoint" />
/// </summary>
@@ -1,7 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using SkiaSharp;
using SkiaSharp;
namespace Artemis.Core
{
@@ -10,6 +7,8 @@ namespace Artemis.Core
{
internal SKSizeLayerProperty()
{
RegisterDataBindingProperty(size => size.Width, new FloatDataBindingConverter());
RegisterDataBindingProperty(size => size.Height, new FloatDataBindingConverter());
}
/// <summary>