Data bindings - WIP commit

This commit is contained in:
SpoinkyNL committed 2020-09-06 21:24:35 +02:00
1 parent 7bcac904fc
commit 1dc58fd09a
42 files changed
+902 -356

No files matched your search

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using Artemis.Core.Services;
using Artemis.Storage.Entities.Profile;
namespace Artemis.Core
@@ -11,6 +12,7 @@ namespace Artemis.Core
public abstract class BaseLayerProperty
{
protected readonly List<DataBinding> _dataBindings = new List<DataBinding>();
protected readonly List<DataBindingRegistration> _dataBindingRegistrations = new List<DataBindingRegistration>();
private bool _isHidden;
private bool _keyframesEnabled;
@@ -18,6 +20,11 @@ namespace Artemis.Core
{
}
/// <summary>
/// Gets a list containing the active data bindings
/// </summary>
public IReadOnlyList<DataBinding> DataBindings => _dataBindings.AsReadOnly();
/// <summary>
/// Gets the profile element (such as layer or folder) this effect is applied to
/// </summary>
@@ -38,11 +45,6 @@ namespace Artemis.Core
/// </summary>
public bool DataBindingsSupported { get; protected internal set; } = true;
/// <summary>
/// Gets a read-only collection of the currently applied data bindings
/// </summary>
public IReadOnlyCollection<DataBinding> DataBindings => _dataBindings.AsReadOnly();
/// <summary>
/// Gets or sets whether keyframes are enabled on this property, has no effect if <see cref="KeyframesSupported" /> is
/// False
@@ -104,18 +106,6 @@ namespace Artemis.Core
/// </summary>
public abstract Type GetPropertyType();
/// <summary>
/// Returns a list of properties to which data bindings can be applied
/// </summary>
/// <returns></returns>
public abstract List<PropertyInfo> GetDataBindingProperties();
/// <summary>
/// Called when the provided data binding must be applied to a property
/// </summary>
/// <param name="dataBinding"></param>
protected abstract void ApplyDataBinding(DataBinding dataBinding);
/// <summary>
/// Applies the provided property entity to the layer property by deserializing the JSON base value and keyframe values
/// </summary>
@@ -132,23 +122,26 @@ namespace Artemis.Core
#region Data bindings
/// <summary>
/// Applies the current <see cref="DataBindings" /> to the layer property
/// </summary>
public void ApplyDataBindings()
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)
ApplyDataBinding(dataBinding);
dataBinding.Initialize(dataModelService, dataBindingService);
}
/// <summary>
/// Adds a new data binding targeting the given property to the <see cref="DataBindings" /> collection
/// </summary>
/// <param name="targetProperty">The property the new data binding should target</param>
/// <returns>The newly created data binding</returns>
public DataBinding AddDataBinding(PropertyInfo targetProperty)
public DataBinding EnableDataBinding(DataBindingRegistration dataBindingRegistration)
{
var dataBinding = new DataBinding(this, targetProperty);
var dataBinding = new DataBinding(this, dataBindingProperty);
_dataBindings.Add(dataBinding);
return dataBinding;
@@ -158,13 +151,11 @@ namespace Artemis.Core
/// Removes the provided data binding from the <see cref="DataBindings" /> collection
/// </summary>
/// <param name="dataBinding">The data binding to remove</param>
public void RemoveDataBinding(DataBinding dataBinding)
public void DisableDataBinding(DataBinding dataBinding)
{
_dataBindings.Remove(dataBinding);
}
#endregion
#region Events
@@ -2,7 +2,9 @@
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;
@@ -16,7 +18,7 @@ namespace Artemis.Core
/// </para>
/// </summary>
/// <typeparam name="T">The type of property encapsulated in this layer property</typeparam>
public class LayerProperty<T> : BaseLayerProperty
public abstract class LayerProperty<T> : BaseLayerProperty
{
private T _baseValue;
private bool _isInitialized;
@@ -198,41 +200,6 @@ namespace Artemis.Core
OnUpdated();
}
private void UpdateKeyframes()
{
if (!KeyframesSupported || !KeyframesEnabled)
return;
// The current keyframe is the last keyframe before the current time
CurrentKeyframe = _keyframes.LastOrDefault(k => k.Position <= ProfileElement.TimelinePosition);
// Keyframes are sorted by position so we can safely assume the next keyframe's position is after the current
var nextIndex = _keyframes.IndexOf(CurrentKeyframe) + 1;
NextKeyframe = _keyframes.Count > nextIndex ? _keyframes[nextIndex] : null;
// No need to update the current value if either of the keyframes are null
if (CurrentKeyframe == null)
CurrentValue = _keyframes.Any() ? _keyframes[0].Value : 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) ((ProfileElement.TimelinePosition - CurrentKeyframe.Position).TotalMilliseconds / timeDiff.TotalMilliseconds);
var keyframeProgressEased = (float) Easings.Interpolate(keyframeProgress, CurrentKeyframe.EasingFunction);
UpdateCurrentValue(keyframeProgress, keyframeProgressEased);
}
}
private void UpdateDataBindings(double deltaTime)
{
foreach (var dataBinding in DataBindings)
{
dataBinding.Update(deltaTime);
ApplyDataBinding(dataBinding);
}
}
/// <summary>
/// Sorts the keyframes in ascending order by position
/// </summary>
@@ -303,20 +270,88 @@ namespace Artemis.Core
Position = k.Position,
EasingFunction = (int) k.EasingFunction
}));
PropertyEntity.DataBindingEntities.Clear();
foreach (var dataBinding in DataBindings)
{
dataBinding.ApplyToEntity();
PropertyEntity.DataBindingEntities.Add(dataBinding.Entity);
}
}
private void UpdateKeyframes()
{
if (!KeyframesSupported || !KeyframesEnabled)
return;
// The current keyframe is the last keyframe before the current time
CurrentKeyframe = _keyframes.LastOrDefault(k => k.Position <= ProfileElement.TimelinePosition);
// Keyframes are sorted by position so we can safely assume the next keyframe's position is after the current
var nextIndex = _keyframes.IndexOf(CurrentKeyframe) + 1;
NextKeyframe = _keyframes.Count > nextIndex ? _keyframes[nextIndex] : null;
// No need to update the current value if either of the keyframes are null
if (CurrentKeyframe == null)
CurrentValue = _keyframes.Any() ? _keyframes[0].Value : 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) ((ProfileElement.TimelinePosition - CurrentKeyframe.Position).TotalMilliseconds / timeDiff.TotalMilliseconds);
var keyframeProgressEased = (float) Easings.Interpolate(keyframeProgress, CurrentKeyframe.EasingFunction);
UpdateCurrentValue(keyframeProgress, keyframeProgressEased);
}
}
#region Data bindings
/// <inheritdoc />
public override List<PropertyInfo> GetDataBindingProperties()
public void RegisterDataBindingProperty<TProperty>(Expression<Func<T, TProperty>> propertyLambda, IDataBindingConverter converter)
{
return new List<PropertyInfo> {GetType().GetProperty(nameof(CurrentValue))};
var propertyInfo = ReflectionUtilities.GetPropertyInfo(CurrentValue, propertyLambda);
}
/// <inheritdoc />
protected override void ApplyDataBinding(DataBinding dataBinding)
/// <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
{
CurrentValue = (T) dataBinding.GetValue(CurrentValue);
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)
{
dataBinding.Update(deltaTime);
dataBinding.ApplyToProperty();
}
}
#endregion
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using SkiaSharp;
namespace Artemis.Core
@@ -11,10 +8,15 @@ 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));
}
/// <summary>
/// Implicitly converts an <see cref="SKColorLayerProperty" /> to an <see cref="SKColor" />
/// Implicitly converts an <see cref="SKColorLayerProperty" /> to an <see cref="SKColor" />
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
@@ -28,29 +30,84 @@ namespace Artemis.Core
{
CurrentValue = CurrentKeyframe.Value.Interpolate(NextKeyframe.Value, keyframeProgressEased);
}
}
/// <inheritdoc />
public override List<PropertyInfo> GetDataBindingProperties()
internal class SKColorDataBindingConverter : IDataBindingConverter
{
private readonly Channel _channel;
public SKColorDataBindingConverter(Channel channel)
{
return typeof(SKColor).GetProperties().ToList();
_channel = channel;
}
/// <inheritdoc />
protected override void ApplyDataBinding(DataBinding dataBinding)
public BaseLayerProperty BaseLayerProperty { get; set; }
public object Sum(object a, object b)
{
if (dataBinding.TargetProperty.Name == nameof(CurrentValue.Alpha))
CurrentValue = CurrentValue.WithAlpha((byte) dataBinding.GetValue(BaseValue.Alpha));
else if (dataBinding.TargetProperty.Name == nameof(CurrentValue.Red))
CurrentValue = CurrentValue.WithRed((byte) dataBinding.GetValue(BaseValue.Red));
else if (dataBinding.TargetProperty.Name == nameof(CurrentValue.Green))
CurrentValue = CurrentValue.WithGreen((byte) dataBinding.GetValue(BaseValue.Green));
else if (dataBinding.TargetProperty.Name == nameof(CurrentValue.Blue))
CurrentValue = CurrentValue.WithBlue((byte) dataBinding.GetValue(BaseValue.Blue));
else if (dataBinding.TargetProperty.Name == nameof(CurrentValue.Hue))
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)
{
CurrentValue.ToHsv(out var h, out var s, out var v);
CurrentValue = SKColor.FromHsv((float) dataBinding.GetValue(h), s, v);
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
}
}
}
@@ -27,20 +27,5 @@ namespace Artemis.Core
var yDiff = NextKeyframe.Value.Y - CurrentKeyframe.Value.Y;
CurrentValue = new SKPoint(CurrentKeyframe.Value.X + xDiff * keyframeProgressEased, CurrentKeyframe.Value.Y + yDiff * keyframeProgressEased);
}
/// <inheritdoc />
public override List<PropertyInfo> GetDataBindingProperties()
{
return typeof(SKPoint).GetProperties().Where(p => p.CanWrite).ToList();
}
/// <inheritdoc />
protected override void ApplyDataBinding(DataBinding dataBinding)
{
if (dataBinding.TargetProperty.Name == nameof(CurrentValue.X))
CurrentValue = new SKPoint((float) dataBinding.GetValue(BaseValue), CurrentValue.Y);
else if (dataBinding.TargetProperty.Name == nameof(CurrentValue.Y))
CurrentValue = new SKPoint(CurrentValue.X, (float) dataBinding.GetValue(BaseValue));
}
}
}
@@ -27,20 +27,5 @@ namespace Artemis.Core
var heightDiff = NextKeyframe.Value.Height - CurrentKeyframe.Value.Height;
CurrentValue = new SKSize(CurrentKeyframe.Value.Width + widthDiff * keyframeProgressEased, CurrentKeyframe.Value.Height + heightDiff * keyframeProgressEased);
}
/// <inheritdoc />
public override List<PropertyInfo> GetDataBindingProperties()
{
return typeof(SKSize).GetProperties().Where(p => p.CanWrite).ToList();
}
/// <inheritdoc />
protected override void ApplyDataBinding(DataBinding dataBinding)
{
if (dataBinding.TargetProperty.Name == nameof(CurrentValue.Height))
CurrentValue = new SKSize(CurrentValue.Width, (float) dataBinding.GetValue(BaseValue));
else if (dataBinding.TargetProperty.Name == nameof(CurrentValue.Width))
CurrentValue = new SKSize((float) dataBinding.GetValue(BaseValue), CurrentValue.Width);
}
}
}