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

@@ -1,30 +1,42 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Artemis.Core.Models.Profile.DataBindings.Converters
namespace Artemis.Core
{
public class FloatDataBindingConverter : IDataBindingConverter
/// <inheritdoc />
public class FloatDataBindingConverter : IDataBindingConverter
{
public BaseLayerProperty BaseLayerProperty { get; set; }
public object Sum(object a, object b)
/// <inheritdoc />
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;
}
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;
}
}
}
}
@@ -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;
}
}
}
@@ -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;
}
}
}
@@ -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
}
}
}
@@ -24,16 +24,12 @@ namespace Artemis.Core
internal DataBinding(DataBindingRegistration dataBindingRegistration)
{
LayerProperty = dataBindingRegistration.LayerProperty;
TargetProperty = dataBindingRegistration.Property;
Registration = dataBindingRegistration;
Entity = new DataBindingEntity();
ApplyRegistration(dataBindingRegistration);
ApplyToEntity();
}
public DataBindingRegistration Registration { get; private set; }
internal DataBinding(BaseLayerProperty layerProperty, DataBindingEntity entity)
{
LayerProperty = layerProperty;
@@ -42,13 +38,30 @@ namespace Artemis.Core
ApplyToDataBinding();
}
private void ApplyRegistration(DataBindingRegistration dataBindingRegistration)
{
Converter = dataBindingRegistration.Converter;
Registration = dataBindingRegistration;
TargetProperty = dataBindingRegistration.Property;
}
/// <summary>
/// Gets the layer property this data binding targets
/// </summary>
public BaseLayerProperty LayerProperty { get; }
/// <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>
public PropertyInfo TargetProperty { get; private set; }
@@ -144,7 +157,7 @@ namespace Artemis.Core
/// <returns></returns>
public object GetValue(object baseValue)
{
if (CompiledTargetAccessor == null)
if (CompiledTargetAccessor == null || Converter == null)
return baseValue;
var dataBindingValue = CompiledTargetAccessor(SourceDataModel);
@@ -154,7 +167,7 @@ namespace Artemis.Core
var value = Convert.ChangeType(dataBindingValue, TargetProperty.PropertyType);
// 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;
// If the value changed, update the current and previous values used for easing
@@ -183,22 +196,16 @@ namespace Artemis.Core
/// <param name="deltaTime"></param>
public void Update(double deltaTime)
{
}
/// <summary>
/// Applies the data binding to the <see cref="Property"/>
/// Updates the value on the <see cref="LayerProperty"/> according to the data binding
/// </summary>
public void ApplyToProperty()
{
}
private object GetInterpolatedValue()
{
if (_easingProgress >= 1.0f)
return _currentValue;
return Registration.Converter.Interpolate(_previousValue, _currentValue, _easingProgress);
var value = GetValue(Converter.GetValue(LayerProperty));
Converter.ApplyValue(LayerProperty, GetValue(value));
}
internal void ApplyToEntity()
@@ -225,7 +232,9 @@ namespace Artemis.Core
internal void ApplyToDataBinding()
{
// 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;
EasingTime = Entity.EasingTime;
EasingFunction = (Easings.Functions) Entity.EasingFunction;
@@ -257,6 +266,16 @@ namespace Artemis.Core
_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()
{
var listType = SourceDataModel.GetListTypeInPath(SourcePropertyPath);
@@ -1,4 +1,6 @@
namespace Artemis.Core
using System;
namespace Artemis.Core
{
/// <summary>
/// A data binding converter that acts as the bridge between a <see cref="DataBinding" /> and a
@@ -6,33 +8,50 @@
/// </summary>
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>
/// Returns the sum of <paramref name="a" /> and <paramref name="b" />
/// </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>
/// 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" />
/// <para>Note: The progress may go be negative or go beyond <c>1.0</c> depending on the easing method used</para>
/// </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="b">The value to interpolate towards</param>
/// <param name="progress">The progress of the interpolation between 0.0 and 1.0</param>
/// <returns></returns>
object Interpolate(object a, object b, float progress);
object Interpolate(BaseLayerProperty layerProperty, object a, object b, float progress);
/// <summary>
/// Applies the <paramref name="value" /> to the layer property
/// </summary>
/// <param name="layerProperty">The layer property this value is to be applied to</param>
/// <param name="value"></param>
void ApplyValue(object value);
void ApplyValue(BaseLayerProperty layerProperty, object value);
/// <summary>
/// Returns the current base value of the data binding
/// </summary>
object GetValue();
/// <param name="layerProperty">The layer property this value must be retrieved from</param>
object GetValue(BaseLayerProperty layerProperty);
}
}