mirror of
https://github.com/Artemis-RGB/Artemis
synced 2026-11-12 11:56:30 +00:00
Data bindings - Further implemented default converters
This commit is contained in:
1 parent
5972e148ee
commit
237f8b8eea
21 files changed
+447
-257
No files matched your search
+26
-20
@@ -3,40 +3,46 @@
|
||||
namespace Artemis.Core
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public class FloatDataBindingConverter : IDataBindingConverter
|
||||
public class FloatDataBindingConverter : DataBindingConverter
|
||||
{
|
||||
/// <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)
|
||||
public FloatDataBindingConverter()
|
||||
{
|
||||
return (float) a + (float) b;
|
||||
SupportedType = typeof(float);
|
||||
SupportsSum = true;
|
||||
SupportsInterpolate = true;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public object Interpolate(BaseLayerProperty layerProperty, object a, object b, float progress)
|
||||
public override object Sum(object a, object b)
|
||||
{
|
||||
var diff = (float) b - (float) a;
|
||||
return diff * progress;
|
||||
return Convert.ToSingle(a) + Convert.ToSingle(b);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void ApplyValue(BaseLayerProperty layerProperty, object value)
|
||||
public override object Interpolate(object a, object b, double progress)
|
||||
{
|
||||
layerProperty.CurrentValue = value;
|
||||
var floatA = Convert.ToSingle(a);
|
||||
var floatB = Convert.ToSingle(b);
|
||||
var diff = floatB - floatA;
|
||||
return floatA + diff * progress;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public object GetValue(BaseLayerProperty layerProperty)
|
||||
public override void ApplyValue(object value)
|
||||
{
|
||||
return layerProperty.CurrentValue;
|
||||
var floatValue = Convert.ToSingle(value);
|
||||
if (DataBinding.LayerProperty.PropertyDescription.MaxInputValue is float max)
|
||||
floatValue = Math.Min(floatValue, max);
|
||||
if (DataBinding.LayerProperty.PropertyDescription.MinInputValue is float min)
|
||||
floatValue = Math.Max(floatValue, min);
|
||||
|
||||
ValueSetter?.Invoke(floatValue);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override object GetValue()
|
||||
{
|
||||
return ValueGetter?.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
+13
-15
@@ -3,39 +3,37 @@
|
||||
namespace Artemis.Core
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public class GeneralDataBindingConverter : IDataBindingConverter
|
||||
public class GeneralDataBindingConverter : DataBindingConverter
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public Type SupportedType => typeof(object);
|
||||
public GeneralDataBindingConverter()
|
||||
{
|
||||
SupportedType = typeof(object);
|
||||
SupportsSum = false;
|
||||
SupportsInterpolate = false;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool SupportsSum => false;
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool SupportsInterpolate => false;
|
||||
|
||||
/// <inheritdoc />
|
||||
public object Sum(BaseLayerProperty layerProperty, object a, object b)
|
||||
public override object Sum(object a, object b)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public object Interpolate(BaseLayerProperty layerProperty, object a, object b, float progress)
|
||||
public override object Interpolate(object a, object b, double progress)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void ApplyValue(BaseLayerProperty layerProperty, object value)
|
||||
public override void ApplyValue(object value)
|
||||
{
|
||||
layerProperty.CurrentValue = value;
|
||||
ValueSetter?.Invoke(value);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public object GetValue(BaseLayerProperty layerProperty)
|
||||
public override object GetValue()
|
||||
{
|
||||
return layerProperty.CurrentValue;
|
||||
return ValueGetter?.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
+23
-17
@@ -3,40 +3,46 @@
|
||||
namespace Artemis.Core
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public class IntDataBindingConverter : IDataBindingConverter
|
||||
public class IntDataBindingConverter : DataBindingConverter
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public Type SupportedType => typeof(int);
|
||||
public IntDataBindingConverter()
|
||||
{
|
||||
SupportedType = typeof(int);
|
||||
SupportsSum = true;
|
||||
SupportsInterpolate = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the <see cref="MidpointRounding" /> mode used for rounding during interpolation. Defaults to
|
||||
/// <see cref="MidpointRounding.AwayFromZero" />
|
||||
/// </summary>
|
||||
public MidpointRounding InterpolationRoundingMode { get; set; } = MidpointRounding.AwayFromZero;
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool SupportsSum => true;
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool SupportsInterpolate => true;
|
||||
|
||||
/// <inheritdoc />
|
||||
public object Sum(BaseLayerProperty layerProperty, object a, object b)
|
||||
public override object Sum(object a, object b)
|
||||
{
|
||||
return (int) a + (int) b;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public object Interpolate(BaseLayerProperty layerProperty, object a, object b, float progress)
|
||||
public override object Interpolate(object a, object b, double progress)
|
||||
{
|
||||
var diff = (int) b - (int) a;
|
||||
return diff * progress;
|
||||
var intA = (int) a;
|
||||
var intB = (int) b;
|
||||
var diff = intB - intA;
|
||||
return (int) Math.Round(intA + diff * progress, InterpolationRoundingMode);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void ApplyValue(BaseLayerProperty layerProperty, object value)
|
||||
public override void ApplyValue(object value)
|
||||
{
|
||||
layerProperty.CurrentValue = value;
|
||||
ValueSetter?.Invoke(value);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public object GetValue(BaseLayerProperty layerProperty)
|
||||
public override object GetValue()
|
||||
{
|
||||
return layerProperty.CurrentValue;
|
||||
return ValueGetter?.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
+17
-28
@@ -4,52 +4,41 @@ using SkiaSharp;
|
||||
namespace Artemis.Core
|
||||
{
|
||||
// This is internal because it's mainly a proof-of-concept
|
||||
internal class SKColorPartDataBindingConverter : IDataBindingConverter
|
||||
internal class SKColorPartDataBindingConverter : DataBindingConverter
|
||||
{
|
||||
private readonly Channel _channel;
|
||||
|
||||
public SKColorPartDataBindingConverter(Channel channel)
|
||||
{
|
||||
_channel = channel;
|
||||
}
|
||||
|
||||
// This depends on what channel was passed
|
||||
public Type SupportedType
|
||||
{
|
||||
get
|
||||
SupportsSum = true;
|
||||
SupportsInterpolate = true;
|
||||
SupportedType = _channel switch
|
||||
{
|
||||
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();
|
||||
}
|
||||
}
|
||||
Channel.Alpha => typeof(byte),
|
||||
Channel.Red => typeof(byte),
|
||||
Channel.Green => typeof(byte),
|
||||
Channel.Blue => typeof(byte),
|
||||
Channel.Hue => typeof(float),
|
||||
_ => throw new ArgumentOutOfRangeException()
|
||||
};
|
||||
}
|
||||
|
||||
public bool SupportsSum => true;
|
||||
public bool SupportsInterpolate => true;
|
||||
|
||||
public object Sum(BaseLayerProperty layerProperty, object a, object b)
|
||||
public override object Sum(object a, object b)
|
||||
{
|
||||
return (float) a + (float) b;
|
||||
}
|
||||
|
||||
public object Interpolate(BaseLayerProperty layerProperty, object a, object b, float progress)
|
||||
public override object Interpolate(object a, object b, double progress)
|
||||
{
|
||||
var diff = (float) b - (float) a;
|
||||
return diff * progress;
|
||||
}
|
||||
|
||||
public void ApplyValue(BaseLayerProperty layerProperty, object value)
|
||||
public override void ApplyValue(object value)
|
||||
{
|
||||
var property = (SKColorLayerProperty) layerProperty;
|
||||
var property = (SKColorLayerProperty) DataBinding.LayerProperty;
|
||||
switch (_channel)
|
||||
{
|
||||
case Channel.Alpha:
|
||||
@@ -73,9 +62,9 @@ namespace Artemis.Core
|
||||
}
|
||||
}
|
||||
|
||||
public object GetValue(BaseLayerProperty layerProperty)
|
||||
public override object GetValue()
|
||||
{
|
||||
var property = (SKColorLayerProperty) layerProperty;
|
||||
var property = (SKColorLayerProperty) DataBinding.LayerProperty;
|
||||
switch (_channel)
|
||||
{
|
||||
case Channel.Alpha:
|
||||
|
||||
@@ -15,11 +15,11 @@ namespace Artemis.Core
|
||||
public class DataBinding
|
||||
{
|
||||
private readonly List<DataBindingModifier> _modifiers = new List<DataBindingModifier>();
|
||||
private bool _isInitialized;
|
||||
|
||||
private object _currentValue;
|
||||
private bool _isInitialized;
|
||||
private object _previousValue;
|
||||
private float _easingProgress;
|
||||
private TimeSpan _easingProgress;
|
||||
|
||||
internal DataBinding(DataBindingRegistration dataBindingRegistration)
|
||||
{
|
||||
@@ -38,30 +38,23 @@ 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 data binding registration this data binding is based upon
|
||||
/// 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"/>
|
||||
/// Gets the converter used to apply this data binding to the <see cref="LayerProperty" />
|
||||
/// </summary>
|
||||
public IDataBindingConverter Converter { get; private set; }
|
||||
public DataBindingConverter Converter { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the property on the <see cref="LayerProperty"/> this data binding targets
|
||||
/// Gets the property on the <see cref="LayerProperty" /> this data binding targets
|
||||
/// </summary>
|
||||
public PropertyInfo TargetProperty { get; private set; }
|
||||
|
||||
@@ -167,21 +160,32 @@ 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 || Converter.SupportsInterpolate)
|
||||
if (EasingTime == TimeSpan.Zero || !Converter.SupportsInterpolate)
|
||||
return value;
|
||||
|
||||
// If the value changed, update the current and previous values used for easing
|
||||
if (!Equals(value, _currentValue))
|
||||
{
|
||||
_previousValue = GetInterpolatedValue();
|
||||
_currentValue = value;
|
||||
_easingProgress = 0f;
|
||||
}
|
||||
ResetEasing(value);
|
||||
|
||||
// Apply interpolation between the previous and current value
|
||||
return GetInterpolatedValue();
|
||||
}
|
||||
|
||||
private void ResetEasing(object value)
|
||||
{
|
||||
_previousValue = GetInterpolatedValue();
|
||||
_currentValue = value;
|
||||
_easingProgress = TimeSpan.Zero;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the type of the target property of this data binding
|
||||
/// </summary>
|
||||
public Type GetTargetType()
|
||||
{
|
||||
return TargetProperty.PropertyType;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the type of the source property of this data binding
|
||||
/// </summary>
|
||||
@@ -191,21 +195,26 @@ namespace Artemis.Core
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the smoothing progress of the data binding
|
||||
/// Updates the smoothing progress of the data binding
|
||||
/// </summary>
|
||||
/// <param name="deltaTime"></param>
|
||||
/// <param name="deltaTime">The time in seconds that passed since the last update</param>
|
||||
public void Update(double deltaTime)
|
||||
{
|
||||
|
||||
_easingProgress = _easingProgress.Add(TimeSpan.FromSeconds(deltaTime));
|
||||
if (_easingProgress > EasingTime)
|
||||
_easingProgress = EasingTime;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the value on the <see cref="LayerProperty"/> according to the data binding
|
||||
/// Updates the value on the <see cref="LayerProperty" /> according to the data binding
|
||||
/// </summary>
|
||||
public void ApplyToProperty()
|
||||
{
|
||||
var value = GetValue(Converter.GetValue(LayerProperty));
|
||||
Converter.ApplyValue(LayerProperty, GetValue(value));
|
||||
if (Converter == null)
|
||||
return;
|
||||
|
||||
var value = GetValue(Converter.GetValue());
|
||||
Converter.ApplyValue(GetValue(value));
|
||||
}
|
||||
|
||||
internal void ApplyToEntity()
|
||||
@@ -232,8 +241,7 @@ namespace Artemis.Core
|
||||
internal void ApplyToDataBinding()
|
||||
{
|
||||
// General
|
||||
Registration = LayerProperty.DataBindingRegistrations.FirstOrDefault(p => p.Property.Name == Entity.TargetProperty);
|
||||
TargetProperty = Registration?.Property;
|
||||
ApplyRegistration(LayerProperty.DataBindingRegistrations.FirstOrDefault(p => p.Property.Name == Entity.TargetProperty));
|
||||
|
||||
Mode = (DataBindingMode) Entity.DataBindingMode;
|
||||
EasingTime = Entity.EasingTime;
|
||||
@@ -266,14 +274,33 @@ namespace Artemis.Core
|
||||
_isInitialized = true;
|
||||
}
|
||||
|
||||
private void ApplyRegistration(DataBindingRegistration dataBindingRegistration)
|
||||
{
|
||||
if (dataBindingRegistration != null)
|
||||
dataBindingRegistration.DataBinding = this;
|
||||
|
||||
Converter = dataBindingRegistration?.Converter;
|
||||
Registration = dataBindingRegistration;
|
||||
TargetProperty = dataBindingRegistration?.Property;
|
||||
|
||||
if (GetTargetType().IsValueType)
|
||||
{
|
||||
if (_currentValue == null)
|
||||
_currentValue = GetTargetType().GetDefault();
|
||||
if (_previousValue == null)
|
||||
_previousValue = _currentValue;
|
||||
}
|
||||
|
||||
Converter?.Initialize(this);
|
||||
}
|
||||
|
||||
private object GetInterpolatedValue()
|
||||
{
|
||||
if (_easingProgress == 0f)
|
||||
return _previousValue;
|
||||
if (_easingProgress == 1f || !Converter.SupportsInterpolate)
|
||||
if (_easingProgress == EasingTime || !Converter.SupportsInterpolate)
|
||||
return _currentValue;
|
||||
|
||||
return Converter.Interpolate(LayerProperty, _previousValue, _currentValue, _easingProgress);
|
||||
var easingAmount = _easingProgress.TotalSeconds / EasingTime.TotalSeconds;
|
||||
return Converter.Interpolate(_previousValue, _currentValue, Easings.Interpolate(easingAmount, EasingFunction));
|
||||
}
|
||||
|
||||
private void CreateExpression()
|
||||
|
||||
@@ -0,0 +1,121 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Artemis.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// A data binding converter that acts as the bridge between a <see cref="DataBinding" /> and a
|
||||
/// <see cref="LayerProperty{T}" />
|
||||
/// </summary>
|
||||
public abstract class DataBindingConverter
|
||||
{
|
||||
internal Func<object> ValueGetter { get; set; }
|
||||
internal Action<object> ValueSetter { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the data binding this converter is applied to
|
||||
/// </summary>
|
||||
public DataBinding DataBinding { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the type this converter supports
|
||||
/// </summary>
|
||||
public Type SupportedType { get; protected set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether or not this data binding converter supports the <see cref="Sum" /> method
|
||||
/// </summary>
|
||||
public bool SupportsSum { get; protected set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether or not this data binding converter supports the <see cref="Interpolate" /> method
|
||||
/// </summary>
|
||||
public bool SupportsInterpolate { get; protected set; }
|
||||
|
||||
/// <summary>
|
||||
/// Called when the data binding converter has been initialized and the <see cref="DataBinding" /> is available
|
||||
/// </summary>
|
||||
protected virtual void OnInitialized()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the sum of <paramref name="a" /> and <paramref name="b" />
|
||||
/// </summary>
|
||||
public abstract object Sum(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="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>
|
||||
public abstract object Interpolate(object a, object b, double progress);
|
||||
|
||||
/// <summary>
|
||||
/// Applies the <paramref name="value" /> to the layer property
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
public abstract void ApplyValue(object value);
|
||||
|
||||
/// <summary>
|
||||
/// Returns the current base value of the data binding
|
||||
/// </summary>
|
||||
public abstract object GetValue();
|
||||
|
||||
internal void Initialize(DataBinding dataBinding)
|
||||
{
|
||||
DataBinding = dataBinding;
|
||||
ValueGetter = CreateValueGetter();
|
||||
ValueSetter = CreateValueSetter();
|
||||
|
||||
OnInitialized();
|
||||
}
|
||||
|
||||
private Func<object> CreateValueGetter()
|
||||
{
|
||||
if (DataBinding.TargetProperty?.DeclaringType == null)
|
||||
return null;
|
||||
|
||||
var getterMethod = DataBinding.TargetProperty.GetGetMethod();
|
||||
if (getterMethod == null)
|
||||
return null;
|
||||
|
||||
var constant = Expression.Constant(DataBinding.LayerProperty);
|
||||
// The path is null if the registration is applied to the root (LayerProperty.CurrentValue)
|
||||
var property = DataBinding.Registration.Path == null
|
||||
? Expression.Property(constant, DataBinding.TargetProperty)
|
||||
: (MemberExpression) DataBinding.Registration.Path.Split('.').Aggregate<string, Expression>(constant, Expression.Property);
|
||||
|
||||
// The get method should cast to the object since it receives whatever type the property is
|
||||
var body = Expression.Convert(property, typeof(object));
|
||||
var lambda = Expression.Lambda<Func<object>>(body);
|
||||
|
||||
return lambda.Compile();
|
||||
}
|
||||
|
||||
private Action<object> CreateValueSetter()
|
||||
{
|
||||
if (DataBinding.TargetProperty?.DeclaringType == null)
|
||||
return null;
|
||||
|
||||
var setterMethod = DataBinding.TargetProperty.GetSetMethod();
|
||||
if (setterMethod == null)
|
||||
return null;
|
||||
|
||||
var constant = Expression.Constant(DataBinding.LayerProperty);
|
||||
var propertyValue = Expression.Parameter(typeof(object), "propertyValue");
|
||||
|
||||
// The assign method should cast to the proper type since it receives an object
|
||||
var body = Expression.Call(constant, setterMethod, Expression.Convert(propertyValue, DataBinding.TargetProperty.PropertyType));
|
||||
var lambda = Expression.Lambda<Action<object>>(body, propertyValue);
|
||||
return lambda.Compile();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -102,7 +102,7 @@ namespace Artemis.Core
|
||||
return ModifierType.Apply(currentValue, value);
|
||||
}
|
||||
|
||||
if (ParameterType == ProfileRightSideType.Static)
|
||||
if (ParameterType == ProfileRightSideType.Static && ModifierType != null)
|
||||
return ModifierType.Apply(currentValue, ParameterStaticValue);
|
||||
|
||||
return currentValue;
|
||||
|
||||
@@ -5,15 +5,18 @@ namespace Artemis.Core
|
||||
{
|
||||
public class DataBindingRegistration
|
||||
{
|
||||
internal DataBindingRegistration(BaseLayerProperty layerProperty, PropertyInfo property, IDataBindingConverter converter)
|
||||
internal DataBindingRegistration(BaseLayerProperty layerProperty, PropertyInfo property, DataBindingConverter converter, string path)
|
||||
{
|
||||
LayerProperty = layerProperty ?? throw new ArgumentNullException(nameof(layerProperty));
|
||||
Property = property ?? throw new ArgumentNullException(nameof(property));
|
||||
Converter = converter ?? throw new ArgumentNullException(nameof(converter));
|
||||
Path = path;
|
||||
}
|
||||
|
||||
public BaseLayerProperty LayerProperty { get; set; }
|
||||
public PropertyInfo Property { get; set; }
|
||||
public IDataBindingConverter Converter { get; set; }
|
||||
public DataBinding DataBinding { get; internal set; }
|
||||
public BaseLayerProperty LayerProperty { get; }
|
||||
public PropertyInfo Property { get; }
|
||||
public DataBindingConverter Converter { get; }
|
||||
public string Path { get; }
|
||||
}
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace Artemis.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// A data binding converter that acts as the bridge between a <see cref="DataBinding" /> and a
|
||||
/// <see cref="LayerProperty{T}" />
|
||||
/// </summary>
|
||||
public interface IDataBindingConverter
|
||||
{
|
||||
/// <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>
|
||||
/// <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(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(BaseLayerProperty layerProperty, object value);
|
||||
|
||||
/// <summary>
|
||||
/// Returns the current base value of the data binding
|
||||
/// </summary>
|
||||
/// <param name="layerProperty">The layer property this value must be retrieved from</param>
|
||||
object GetValue(BaseLayerProperty layerProperty);
|
||||
}
|
||||
}
|
||||
@@ -32,7 +32,7 @@ namespace Artemis.Core
|
||||
get => _baseValue;
|
||||
set
|
||||
{
|
||||
if (value.GetType() != GetPropertyType())
|
||||
if (value != null && value.GetType() != GetPropertyType())
|
||||
throw new ArtemisCoreException("Cannot update base value because of a type mismatch");
|
||||
_baseValue = value;
|
||||
}
|
||||
@@ -46,7 +46,7 @@ namespace Artemis.Core
|
||||
get => _currentValue;
|
||||
set
|
||||
{
|
||||
if (value.GetType() != GetPropertyType())
|
||||
if (value != null && value.GetType() != GetPropertyType())
|
||||
throw new ArtemisCoreException("Cannot update current value because of a type mismatch");
|
||||
_currentValue = value;
|
||||
}
|
||||
@@ -61,7 +61,7 @@ namespace Artemis.Core
|
||||
get => _defaultValue;
|
||||
set
|
||||
{
|
||||
if (value.GetType() != GetPropertyType())
|
||||
if (value != null && value.GetType() != GetPropertyType())
|
||||
throw new ArtemisCoreException("Cannot update default value because of a type mismatch");
|
||||
_defaultValue = value;
|
||||
}
|
||||
@@ -186,6 +186,9 @@ namespace Artemis.Core
|
||||
/// <returns>The newly created data binding</returns>
|
||||
public DataBinding EnableDataBinding(DataBindingRegistration dataBindingRegistration)
|
||||
{
|
||||
if (dataBindingRegistration.LayerProperty != this)
|
||||
throw new ArtemisCoreException("Cannot enable a data binding using a data binding registration of a different layer property");
|
||||
|
||||
var dataBinding = new DataBinding(dataBindingRegistration);
|
||||
_dataBindings.Add(dataBinding);
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using Artemis.Storage.Entities.Profile;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
@@ -31,7 +32,7 @@ namespace Artemis.Core
|
||||
/// </summary>
|
||||
public new T BaseValue
|
||||
{
|
||||
get => (T) base.BaseValue;
|
||||
get => base.BaseValue != null ? (T) base.BaseValue : default;
|
||||
set
|
||||
{
|
||||
if (Equals(base.BaseValue, value))
|
||||
@@ -47,7 +48,7 @@ namespace Artemis.Core
|
||||
/// </summary>
|
||||
public new T CurrentValue
|
||||
{
|
||||
get => (T) base.CurrentValue;
|
||||
get => base.CurrentValue != null ? (T) base.CurrentValue : default;
|
||||
set => base.CurrentValue = value;
|
||||
}
|
||||
|
||||
@@ -57,7 +58,7 @@ namespace Artemis.Core
|
||||
/// </summary>
|
||||
public new T DefaultValue
|
||||
{
|
||||
get => (T) base.DefaultValue;
|
||||
get => base.DefaultValue != null ? (T) base.DefaultValue : default;
|
||||
set => base.DefaultValue = value;
|
||||
}
|
||||
|
||||
@@ -311,14 +312,35 @@ namespace Artemis.Core
|
||||
|
||||
#region Data bindings
|
||||
|
||||
public void RegisterDataBindingProperty<TProperty>(Expression<Func<T, TProperty>> propertyLambda, IDataBindingConverter converter)
|
||||
public void RegisterDataBindingProperty<TProperty>(Expression<Func<T, TProperty>> propertyLambda, DataBindingConverter converter)
|
||||
{
|
||||
var propertyInfo = ReflectionUtilities.GetPropertyInfo(CurrentValue, propertyLambda);
|
||||
// If the lambda references to itself, use the property info of public new T CurrentValue
|
||||
PropertyInfo propertyInfo;
|
||||
string path = null;
|
||||
if (propertyLambda.Parameters[0] == propertyLambda.Body)
|
||||
{
|
||||
propertyInfo = GetType().GetProperties().FirstOrDefault(p => p.Name == nameof(CurrentValue) && p.PropertyType == typeof(T));
|
||||
}
|
||||
else
|
||||
{
|
||||
propertyInfo = ReflectionUtilities.GetPropertyInfo(CurrentValue, propertyLambda);
|
||||
// Deconstruct the lambda
|
||||
var current = (MemberExpression) propertyLambda.Body;
|
||||
path = current.Member.Name;
|
||||
while (current.Expression is MemberExpression memberExpression)
|
||||
{
|
||||
path = current.Member.Name + "." + path;
|
||||
current = memberExpression;
|
||||
}
|
||||
}
|
||||
|
||||
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));
|
||||
_dataBindingRegistrations.Add(new DataBindingRegistration(this, propertyInfo, converter, path));
|
||||
}
|
||||
|
||||
private void UpdateDataBindings(double deltaTime)
|
||||
|
||||
Reference in new issue
Block a user