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

Data bindings - Added converters for the default layer property types

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

View File

@ -10,6 +10,7 @@
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=models_005Cprofile_005Cconditions_005Coperators/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=models_005Cprofile_005Cconditions_005Coperators/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=models_005Cprofile_005Cdatabindings/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=models_005Cprofile_005Cdatabindings/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=models_005Cprofile_005Cdatabindings_005Cconverters/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=models_005Cprofile_005Cdatabindings_005Cconverters/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=models_005Cprofile_005Cdatabindings_005Cconverters_005Cinternal/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=models_005Cprofile_005Cdatabindings_005Cdatabindingproperties/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=models_005Cprofile_005Cdatabindings_005Cdatabindingproperties/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=models_005Cprofile_005Cdatabindings_005Cdatabindingproperties_005Cabstract/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=models_005Cprofile_005Cdatabindings_005Cdatabindingproperties_005Cabstract/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=models_005Cprofile_005Cdatabindings_005Cmodifiers/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=models_005Cprofile_005Cdatabindings_005Cmodifiers/@EntryIndexedValue">True</s:Boolean>

View File

@ -1,30 +1,42 @@
using System; using System;
using System.Collections.Generic;
using System.Text;
namespace Artemis.Core.Models.Profile.DataBindings.Converters namespace Artemis.Core
{ {
/// <inheritdoc />
public class FloatDataBindingConverter : IDataBindingConverter public class FloatDataBindingConverter : IDataBindingConverter
{ {
public BaseLayerProperty BaseLayerProperty { get; set; } /// <inheritdoc />
public object Sum(object a, object b) public Type SupportedType => typeof(float);
/// <inheritdoc />
public bool SupportsSum => true;
/// <inheritdoc />
public bool SupportsInterpolate => true;
/// <inheritdoc />
public object Sum(BaseLayerProperty layerProperty, object a, object b)
{ {
return (float) a + (float) b; return (float) a + (float) b;
} }
public object Interpolate(object a, object b, float progress) /// <inheritdoc />
public object Interpolate(BaseLayerProperty layerProperty, object a, object b, float progress)
{ {
throw new NotImplementedException(); var diff = (float) b - (float) a;
return diff * progress;
} }
public void ApplyValue(object value) /// <inheritdoc />
public void ApplyValue(BaseLayerProperty layerProperty, object value)
{ {
throw new NotImplementedException(); layerProperty.CurrentValue = value;
} }
public object GetValue() /// <inheritdoc />
public object GetValue(BaseLayerProperty layerProperty)
{ {
throw new NotImplementedException(); return layerProperty.CurrentValue;
} }
} }
} }

View File

@ -0,0 +1,41 @@
using System;
namespace Artemis.Core
{
/// <inheritdoc />
public class GeneralDataBindingConverter : IDataBindingConverter
{
/// <inheritdoc />
public Type SupportedType => typeof(object);
/// <inheritdoc />
public bool SupportsSum => false;
/// <inheritdoc />
public bool SupportsInterpolate => false;
/// <inheritdoc />
public object Sum(BaseLayerProperty layerProperty, object a, object b)
{
throw new NotSupportedException();
}
/// <inheritdoc />
public object Interpolate(BaseLayerProperty layerProperty, object a, object b, float progress)
{
throw new NotSupportedException();
}
/// <inheritdoc />
public void ApplyValue(BaseLayerProperty layerProperty, object value)
{
layerProperty.CurrentValue = value;
}
/// <inheritdoc />
public object GetValue(BaseLayerProperty layerProperty)
{
return layerProperty.CurrentValue;
}
}
}

View File

@ -0,0 +1,42 @@
using System;
namespace Artemis.Core
{
/// <inheritdoc />
public class IntDataBindingConverter : IDataBindingConverter
{
/// <inheritdoc />
public Type SupportedType => typeof(int);
/// <inheritdoc />
public bool SupportsSum => true;
/// <inheritdoc />
public bool SupportsInterpolate => true;
/// <inheritdoc />
public object Sum(BaseLayerProperty layerProperty, object a, object b)
{
return (int) a + (int) b;
}
/// <inheritdoc />
public object Interpolate(BaseLayerProperty layerProperty, object a, object b, float progress)
{
var diff = (int) b - (int) a;
return diff * progress;
}
/// <inheritdoc />
public void ApplyValue(BaseLayerProperty layerProperty, object value)
{
layerProperty.CurrentValue = value;
}
/// <inheritdoc />
public object GetValue(BaseLayerProperty layerProperty)
{
return layerProperty.CurrentValue;
}
}
}

View File

@ -0,0 +1,106 @@
using System;
using SkiaSharp;
namespace Artemis.Core
{
// This is internal because it's mainly a proof-of-concept
internal class SKColorPartDataBindingConverter : IDataBindingConverter
{
private readonly Channel _channel;
public SKColorPartDataBindingConverter(Channel channel)
{
_channel = channel;
}
// This depends on what channel was passed
public Type SupportedType
{
get
{
switch (_channel)
{
case Channel.Alpha:
case Channel.Red:
case Channel.Green:
case Channel.Blue:
return typeof(byte);
case Channel.Hue:
return typeof(float);
default:
throw new ArgumentOutOfRangeException();
}
}
}
public bool SupportsSum => true;
public bool SupportsInterpolate => true;
public object Sum(BaseLayerProperty layerProperty, object a, object b)
{
return (float) a + (float) b;
}
public object Interpolate(BaseLayerProperty layerProperty, object a, object b, float progress)
{
var diff = (float) b - (float) a;
return diff * progress;
}
public void ApplyValue(BaseLayerProperty layerProperty, object value)
{
var property = (SKColorLayerProperty) layerProperty;
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(BaseLayerProperty layerProperty)
{
var property = (SKColorLayerProperty) layerProperty;
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
}
}
}

View File

@ -24,16 +24,12 @@ namespace Artemis.Core
internal DataBinding(DataBindingRegistration dataBindingRegistration) internal DataBinding(DataBindingRegistration dataBindingRegistration)
{ {
LayerProperty = dataBindingRegistration.LayerProperty; LayerProperty = dataBindingRegistration.LayerProperty;
TargetProperty = dataBindingRegistration.Property;
Registration = dataBindingRegistration;
Entity = new DataBindingEntity(); Entity = new DataBindingEntity();
ApplyRegistration(dataBindingRegistration);
ApplyToEntity(); ApplyToEntity();
} }
public DataBindingRegistration Registration { get; private set; }
internal DataBinding(BaseLayerProperty layerProperty, DataBindingEntity entity) internal DataBinding(BaseLayerProperty layerProperty, DataBindingEntity entity)
{ {
LayerProperty = layerProperty; LayerProperty = layerProperty;
@ -42,13 +38,30 @@ namespace Artemis.Core
ApplyToDataBinding(); ApplyToDataBinding();
} }
private void ApplyRegistration(DataBindingRegistration dataBindingRegistration)
{
Converter = dataBindingRegistration.Converter;
Registration = dataBindingRegistration;
TargetProperty = dataBindingRegistration.Property;
}
/// <summary> /// <summary>
/// Gets the layer property this data binding targets /// Gets the layer property this data binding targets
/// </summary> /// </summary>
public BaseLayerProperty LayerProperty { get; } public BaseLayerProperty LayerProperty { get; }
/// <summary> /// <summary>
/// Gets the inner property this data binding targets /// Gets the data binding registration this data binding is based upon
/// </summary>
public DataBindingRegistration Registration { get; private set; }
/// <summary>
/// Gets the converter used to apply this data binding to the <see cref="LayerProperty"/>
/// </summary>
public IDataBindingConverter Converter { get; private set; }
/// <summary>
/// Gets the property on the <see cref="LayerProperty"/> this data binding targets
/// </summary> /// </summary>
public PropertyInfo TargetProperty { get; private set; } public PropertyInfo TargetProperty { get; private set; }
@ -144,7 +157,7 @@ namespace Artemis.Core
/// <returns></returns> /// <returns></returns>
public object GetValue(object baseValue) public object GetValue(object baseValue)
{ {
if (CompiledTargetAccessor == null) if (CompiledTargetAccessor == null || Converter == null)
return baseValue; return baseValue;
var dataBindingValue = CompiledTargetAccessor(SourceDataModel); var dataBindingValue = CompiledTargetAccessor(SourceDataModel);
@ -154,7 +167,7 @@ namespace Artemis.Core
var value = Convert.ChangeType(dataBindingValue, TargetProperty.PropertyType); var value = Convert.ChangeType(dataBindingValue, TargetProperty.PropertyType);
// If no easing is to be applied simple return whatever the current value is // If no easing is to be applied simple return whatever the current value is
if (EasingTime == TimeSpan.Zero) if (EasingTime == TimeSpan.Zero || Converter.SupportsInterpolate)
return value; return value;
// If the value changed, update the current and previous values used for easing // If the value changed, update the current and previous values used for easing
@ -183,22 +196,16 @@ namespace Artemis.Core
/// <param name="deltaTime"></param> /// <param name="deltaTime"></param>
public void Update(double deltaTime) public void Update(double deltaTime)
{ {
} }
/// <summary> /// <summary>
/// Applies the data binding to the <see cref="Property"/> /// Updates the value on the <see cref="LayerProperty"/> according to the data binding
/// </summary> /// </summary>
public void ApplyToProperty() public void ApplyToProperty()
{ {
var value = GetValue(Converter.GetValue(LayerProperty));
} Converter.ApplyValue(LayerProperty, GetValue(value));
private object GetInterpolatedValue()
{
if (_easingProgress >= 1.0f)
return _currentValue;
return Registration.Converter.Interpolate(_previousValue, _currentValue, _easingProgress);
} }
internal void ApplyToEntity() internal void ApplyToEntity()
@ -225,7 +232,9 @@ namespace Artemis.Core
internal void ApplyToDataBinding() internal void ApplyToDataBinding()
{ {
// General // General
TargetProperty = LayerProperty.GetDataBindingProperties()?.FirstOrDefault(p => p.Name == Entity.TargetProperty); Registration = LayerProperty.DataBindingRegistrations.FirstOrDefault(p => p.Property.Name == Entity.TargetProperty);
TargetProperty = Registration?.Property;
Mode = (DataBindingMode) Entity.DataBindingMode; Mode = (DataBindingMode) Entity.DataBindingMode;
EasingTime = Entity.EasingTime; EasingTime = Entity.EasingTime;
EasingFunction = (Easings.Functions) Entity.EasingFunction; EasingFunction = (Easings.Functions) Entity.EasingFunction;
@ -257,6 +266,16 @@ namespace Artemis.Core
_isInitialized = true; _isInitialized = true;
} }
private object GetInterpolatedValue()
{
if (_easingProgress == 0f)
return _previousValue;
if (_easingProgress == 1f || !Converter.SupportsInterpolate)
return _currentValue;
return Converter.Interpolate(LayerProperty, _previousValue, _currentValue, _easingProgress);
}
private void CreateExpression() private void CreateExpression()
{ {
var listType = SourceDataModel.GetListTypeInPath(SourcePropertyPath); var listType = SourceDataModel.GetListTypeInPath(SourcePropertyPath);

View File

@ -1,4 +1,6 @@
namespace Artemis.Core using System;
namespace Artemis.Core
{ {
/// <summary> /// <summary>
/// A data binding converter that acts as the bridge between a <see cref="DataBinding" /> and a /// A data binding converter that acts as the bridge between a <see cref="DataBinding" /> and a
@ -6,33 +8,50 @@
/// </summary> /// </summary>
public interface IDataBindingConverter public interface IDataBindingConverter
{ {
public BaseLayerProperty BaseLayerProperty { get; set; } /// <summary>
/// Gets the type this converter supports
/// </summary>
Type SupportedType { get; }
/// <summary>
/// Gets whether or not this data binding converter supports the <see cref="Sum" /> method
/// </summary>
bool SupportsSum { get; }
/// <summary>
/// Gets whether or not this data binding converter supports the <see cref="Interpolate" /> method
/// </summary>
bool SupportsInterpolate { get; }
/// <summary> /// <summary>
/// Returns the sum of <paramref name="a" /> and <paramref name="b" /> /// Returns the sum of <paramref name="a" /> and <paramref name="b" />
/// </summary> /// </summary>
object Sum(object a, object b); /// <param name="layerProperty">The layer property this sum is done for</param>
object Sum(BaseLayerProperty layerProperty, object a, object b);
/// <summary> /// <summary>
/// Returns the the interpolated value between <paramref name="a" /> and <paramref name="b" /> on a scale (generally) /// Returns the the interpolated value between <paramref name="a" /> and <paramref name="b" /> on a scale (generally)
/// between <c>0.0</c> and <c>1.0</c> defined by the <paramref name="progress" /> /// between <c>0.0</c> and <c>1.0</c> defined by the <paramref name="progress" />
/// <para>Note: The progress may go be negative or go beyond <c>1.0</c> depending on the easing method used</para> /// <para>Note: The progress may go be negative or go beyond <c>1.0</c> depending on the easing method used</para>
/// </summary> /// </summary>
/// <param name="layerProperty">The layer property this interpolation is done for</param>
/// <param name="a">The value to interpolate away from</param> /// <param name="a">The value to interpolate away from</param>
/// <param name="b">The value to interpolate towards</param> /// <param name="b">The value to interpolate towards</param>
/// <param name="progress">The progress of the interpolation between 0.0 and 1.0</param> /// <param name="progress">The progress of the interpolation between 0.0 and 1.0</param>
/// <returns></returns> /// <returns></returns>
object Interpolate(object a, object b, float progress); object Interpolate(BaseLayerProperty layerProperty, object a, object b, float progress);
/// <summary> /// <summary>
/// Applies the <paramref name="value" /> to the layer property /// Applies the <paramref name="value" /> to the layer property
/// </summary> /// </summary>
/// <param name="layerProperty">The layer property this value is to be applied to</param>
/// <param name="value"></param> /// <param name="value"></param>
void ApplyValue(object value); void ApplyValue(BaseLayerProperty layerProperty, object value);
/// <summary> /// <summary>
/// Returns the current base value of the data binding /// Returns the current base value of the data binding
/// </summary> /// </summary>
object GetValue(); /// <param name="layerProperty">The layer property this value must be retrieved from</param>
object GetValue(BaseLayerProperty layerProperty);
} }
} }

View File

@ -11,8 +11,12 @@ namespace Artemis.Core
/// </summary> /// </summary>
public abstract class BaseLayerProperty public abstract class BaseLayerProperty
{ {
protected readonly List<DataBinding> _dataBindings = new List<DataBinding>();
protected readonly List<DataBindingRegistration> _dataBindingRegistrations = new List<DataBindingRegistration>(); 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 _isHidden;
private bool _keyframesEnabled; 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> /// <summary>
/// Gets a list containing the active data bindings /// Gets a list containing the active data bindings
/// </summary> /// </summary>
public IReadOnlyList<DataBinding> DataBindings => _dataBindings.AsReadOnly(); public IReadOnlyList<DataBinding> DataBindings => _dataBindings.AsReadOnly();
/// <summary>
/// Gets a list containing all the data binding registrations
/// </summary>
public IReadOnlyList<DataBindingRegistration> DataBindingRegistrations => _dataBindingRegistrations.AsReadOnly();
/// <summary> /// <summary>
/// Gets the profile element (such as layer or folder) this effect is applied to /// Gets the profile element (such as layer or folder) this effect is applied to
/// </summary> /// </summary>
@ -122,13 +174,6 @@ namespace Artemis.Core
#region Data bindings #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) internal void InitializeDataBindings(IDataModelService dataModelService, IDataBindingService dataBindingService)
{ {
foreach (var dataBinding in DataBindings) foreach (var dataBinding in DataBindings)
@ -141,7 +186,7 @@ namespace Artemis.Core
/// <returns>The newly created data binding</returns> /// <returns>The newly created data binding</returns>
public DataBinding EnableDataBinding(DataBindingRegistration dataBindingRegistration) public DataBinding EnableDataBinding(DataBindingRegistration dataBindingRegistration)
{ {
var dataBinding = new DataBinding(this, dataBindingProperty); var dataBinding = new DataBinding(dataBindingRegistration);
_dataBindings.Add(dataBinding); _dataBindings.Add(dataBinding);
return dataBinding; return dataBinding;

View File

@ -3,8 +3,6 @@ using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Reflection;
using Artemis.Core.DataModelExpansions;
using Artemis.Storage.Entities.Profile; using Artemis.Storage.Entities.Profile;
using Newtonsoft.Json; using Newtonsoft.Json;
@ -20,7 +18,6 @@ namespace Artemis.Core
/// <typeparam name="T">The type of property encapsulated in this layer property</typeparam> /// <typeparam name="T">The type of property encapsulated in this layer property</typeparam>
public abstract class LayerProperty<T> : BaseLayerProperty public abstract class LayerProperty<T> : BaseLayerProperty
{ {
private T _baseValue;
private bool _isInitialized; private bool _isInitialized;
private List<LayerPropertyKeyframe<T>> _keyframes; private List<LayerPropertyKeyframe<T>> _keyframes;
@ -32,30 +29,37 @@ namespace Artemis.Core
/// <summary> /// <summary>
/// Gets or sets the base value of this layer property without any keyframes applied /// Gets or sets the base value of this layer property without any keyframes applied
/// </summary> /// </summary>
public T BaseValue public new T BaseValue
{ {
get => _baseValue; get => (T) base.BaseValue;
set set
{ {
if (_baseValue != null && !_baseValue.Equals(value) || _baseValue == null && value != null) if (Equals(base.BaseValue, value))
{ return;
_baseValue = value;
base.BaseValue = value;
OnBaseValueChanged(); OnBaseValueChanged();
} }
} }
}
/// <summary> /// <summary>
/// Gets the current value of this property as it is affected by it's keyframes, updated once every frame /// Gets the current value of this property as it is affected by it's keyframes, updated once every frame
/// </summary> /// </summary>
public T CurrentValue { get; set; } public new T CurrentValue
{
get => (T) base.CurrentValue;
set => base.CurrentValue = value;
}
/// <summary> /// <summary>
/// Gets or sets the default value of this layer property. If set, this value is automatically applied if the property /// Gets or sets the default value of this layer property. If set, this value is automatically applied if the property
/// has no /// has no value in storage
/// value in storage
/// </summary> /// </summary>
public T DefaultValue { get; set; } public new T DefaultValue
{
get => (T) base.DefaultValue;
set => base.DefaultValue = value;
}
/// <summary> /// <summary>
/// Gets a read-only list of all the keyframes on this layer property /// Gets a read-only list of all the keyframes on this layer property
@ -309,40 +313,12 @@ namespace Artemis.Core
public void RegisterDataBindingProperty<TProperty>(Expression<Func<T, TProperty>> propertyLambda, IDataBindingConverter converter) public void RegisterDataBindingProperty<TProperty>(Expression<Func<T, TProperty>> propertyLambda, IDataBindingConverter converter)
{ {
var propertyInfo = ReflectionUtilities.GetPropertyInfo(CurrentValue, propertyLambda); 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) private void UpdateDataBindings(double deltaTime)

View File

@ -11,11 +11,15 @@ namespace Artemis.Core
DataBindingsSupported = false; DataBindingsSupported = false;
} }
/// <summary>
/// Implicitly converts an <see cref="ColorGradientLayerProperty" /> to a <see cref="ColorGradient"/>
/// </summary>
public static implicit operator ColorGradient(ColorGradientLayerProperty p) public static implicit operator ColorGradient(ColorGradientLayerProperty p)
{ {
return p.CurrentValue; return p.CurrentValue;
} }
/// <inheritdoc />
protected override void UpdateCurrentValue(float keyframeProgress, float keyframeProgressEased) protected override void UpdateCurrentValue(float keyframeProgress, float keyframeProgressEased)
{ {
throw new ArtemisCoreException("Color Gradients do not support keyframes."); throw new ArtemisCoreException("Color Gradients do not support keyframes.");

View File

@ -5,22 +5,29 @@ namespace Artemis.Core
/// <inheritdoc /> /// <inheritdoc />
public class EnumLayerProperty<T> : LayerProperty<T> where T : Enum public class EnumLayerProperty<T> : LayerProperty<T> where T : Enum
{ {
public EnumLayerProperty() internal EnumLayerProperty()
{ {
KeyframesSupported = false; KeyframesSupported = false;
DataBindingsSupported = 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) public static implicit operator T(EnumLayerProperty<T> p)
{ {
return p.CurrentValue; 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) public static implicit operator int(EnumLayerProperty<T> p)
{ {
return Convert.ToInt32(p.CurrentValue); return Convert.ToInt32(p.CurrentValue);
} }
/// <inheritdoc />
protected override void UpdateCurrentValue(float keyframeProgress, float keyframeProgressEased) protected override void UpdateCurrentValue(float keyframeProgress, float keyframeProgressEased)
{ {
throw new ArtemisCoreException("Enum properties do not support keyframes."); throw new ArtemisCoreException("Enum properties do not support keyframes.");

View File

@ -5,18 +5,26 @@
{ {
internal FloatLayerProperty() 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) public static implicit operator float(FloatLayerProperty p)
{ {
return p.CurrentValue; return p.CurrentValue;
} }
/// <summary>
/// Implicitly converts an <see cref="FloatLayerProperty" /> to a <see cref="double" />
/// </summary>
public static implicit operator double(FloatLayerProperty p) public static implicit operator double(FloatLayerProperty p)
{ {
return p.CurrentValue; return p.CurrentValue;
} }
/// <inheritdoc />
protected override void UpdateCurrentValue(float keyframeProgress, float keyframeProgressEased) protected override void UpdateCurrentValue(float keyframeProgress, float keyframeProgressEased)
{ {
var diff = NextKeyframe.Value - CurrentKeyframe.Value; var diff = NextKeyframe.Value - CurrentKeyframe.Value;

View File

@ -7,23 +7,34 @@ namespace Artemis.Core
{ {
internal IntLayerProperty() 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) public static implicit operator int(IntLayerProperty p)
{ {
return p.CurrentValue; return p.CurrentValue;
} }
/// <summary>
/// Implicitly converts an <see cref="IntLayerProperty" /> to a <see cref="float" />
/// </summary>
public static implicit operator float(IntLayerProperty p) public static implicit operator float(IntLayerProperty p)
{ {
return p.CurrentValue; return p.CurrentValue;
} }
/// <summary>
/// Implicitly converts an <see cref="IntLayerProperty" /> to a <see cref="double" />
/// </summary>
public static implicit operator double(IntLayerProperty p) public static implicit operator double(IntLayerProperty p)
{ {
return p.CurrentValue; return p.CurrentValue;
} }
/// <inheritdoc />
protected override void UpdateCurrentValue(float keyframeProgress, float keyframeProgressEased) protected override void UpdateCurrentValue(float keyframeProgress, float keyframeProgressEased)
{ {
var diff = NextKeyframe.Value - CurrentKeyframe.Value; var diff = NextKeyframe.Value - CurrentKeyframe.Value;

View File

@ -11,11 +11,15 @@
DataBindingsSupported = false; DataBindingsSupported = false;
} }
/// <summary>
/// Implicitly converts an <see cref="LayerBrushReferenceLayerProperty" /> to an <see cref="LayerBrushReference" />
/// </summary>
public static implicit operator LayerBrushReference(LayerBrushReferenceLayerProperty p) public static implicit operator LayerBrushReference(LayerBrushReferenceLayerProperty p)
{ {
return p.CurrentValue; return p.CurrentValue;
} }
/// <inheritdoc />
protected override void UpdateCurrentValue(float keyframeProgress, float keyframeProgressEased) protected override void UpdateCurrentValue(float keyframeProgress, float keyframeProgressEased)
{ {
throw new ArtemisCoreException("Layer brush references do not support keyframes."); throw new ArtemisCoreException("Layer brush references do not support keyframes.");

View File

@ -1,5 +1,4 @@
using System; using SkiaSharp;
using SkiaSharp;
namespace Artemis.Core namespace Artemis.Core
{ {
@ -8,11 +7,11 @@ namespace Artemis.Core
{ {
internal SKColorLayerProperty() internal SKColorLayerProperty()
{ {
RegisterDataBindingProperty(color => color.Alpha, new SKColorDataBindingConverter(SKColorDataBindingConverter.Channel.Alpha)); RegisterDataBindingProperty(color => color.Alpha, new SKColorPartDataBindingConverter(SKColorPartDataBindingConverter.Channel.Alpha));
RegisterDataBindingProperty(color => color.Red, new SKColorDataBindingConverter(SKColorDataBindingConverter.Channel.Red)); RegisterDataBindingProperty(color => color.Red, new SKColorPartDataBindingConverter(SKColorPartDataBindingConverter.Channel.Red));
RegisterDataBindingProperty(color => color.Green, new SKColorDataBindingConverter(SKColorDataBindingConverter.Channel.Green)); RegisterDataBindingProperty(color => color.Green, new SKColorPartDataBindingConverter(SKColorPartDataBindingConverter.Channel.Green));
RegisterDataBindingProperty(color => color.Blue, new SKColorDataBindingConverter(SKColorDataBindingConverter.Channel.Blue)); RegisterDataBindingProperty(color => color.Blue, new SKColorPartDataBindingConverter(SKColorPartDataBindingConverter.Channel.Blue));
RegisterDataBindingProperty(color => color.Hue, new SKColorDataBindingConverter(SKColorDataBindingConverter.Channel.Hue)); RegisterDataBindingProperty(color => color.Hue, new SKColorPartDataBindingConverter(SKColorPartDataBindingConverter.Channel.Hue));
} }
/// <summary> /// <summary>
@ -31,83 +30,4 @@ namespace Artemis.Core
CurrentValue = CurrentKeyframe.Value.Interpolate(NextKeyframe.Value, keyframeProgressEased); 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
}
}
} }

View File

@ -1,7 +1,4 @@
using System.Collections.Generic; using SkiaSharp;
using System.Linq;
using System.Reflection;
using SkiaSharp;
namespace Artemis.Core namespace Artemis.Core
{ {
@ -10,6 +7,8 @@ namespace Artemis.Core
{ {
internal SKPointLayerProperty() internal SKPointLayerProperty()
{ {
RegisterDataBindingProperty(point => point.X, new FloatDataBindingConverter());
RegisterDataBindingProperty(point => point.Y, new FloatDataBindingConverter());
} }
/// <summary> /// <summary>

View File

@ -1,7 +1,4 @@
using System.Collections.Generic; using SkiaSharp;
using System.Linq;
using System.Reflection;
using SkiaSharp;
namespace Artemis.Core namespace Artemis.Core
{ {
@ -10,6 +7,8 @@ namespace Artemis.Core
{ {
internal SKSizeLayerProperty() internal SKSizeLayerProperty()
{ {
RegisterDataBindingProperty(size => size.Width, new FloatDataBindingConverter());
RegisterDataBindingProperty(size => size.Height, new FloatDataBindingConverter());
} }
/// <summary> /// <summary>