Data bindings - Fixed some conversion issues

Data bindings - Started hooking up the UI
This commit is contained in:
Robert committed 2020-09-04 12:03:28 +02:00
1 parent 3434c4f9c2
commit 9e71cae7c7
16 files changed
+357 -90

No files matched your search

@@ -132,11 +132,13 @@ namespace Artemis.Core
/// <returns></returns>
public object GetValue(object baseValue)
{
if (baseValue.GetType() != TargetProperty.PropertyType)
{
throw new ArtemisCoreException($"The provided current value type ({baseValue.GetType().Name}) not match the " +
$"target property type ({TargetProperty.PropertyType.Name})");
}
// Validating this is kinda expensive, it'll fail on ChangeType later anyway ^^
// var targetType = TargetProperty.PropertyType;
// if (!targetType.IsCastableFrom(baseValue.GetType()))
// {
// throw new ArtemisCoreException($"The provided current value type ({baseValue.GetType().Name}) not match the " +
// $"target property type ({targetType.Name})");
// }
if (CompiledTargetAccessor == null)
return baseValue;
@@ -145,7 +147,11 @@ namespace Artemis.Core
foreach (var dataBindingModifier in Modifiers)
dataBindingValue = dataBindingModifier.Apply(dataBindingValue);
return dataBindingValue;
return Convert.ChangeType(dataBindingValue, TargetProperty.PropertyType);
}
internal void Update(double deltaTime)
{
}
internal void ApplyToEntity()
@@ -210,20 +216,32 @@ namespace Artemis.Core
if (listType != null)
throw new ArtemisCoreException($"Cannot create a regular accessor at path {SourcePropertyPath} because the path contains a list");
var parameter = Expression.Parameter(typeof(object), "targetDataModel");
var parameter = Expression.Parameter(typeof(DataModel), "targetDataModel");
var accessor = SourcePropertyPath.Split('.').Aggregate<string, Expression>(
Expression.Convert(parameter, SourceDataModel.GetType()), // Cast to the appropriate type
Expression.Property
);
var lambda = Expression.Lambda<Func<DataModel, object>>(accessor);
var returnValue = Expression.Convert(accessor, typeof(object));
var lambda = Expression.Lambda<Func<DataModel, object>>(returnValue, parameter);
CompiledTargetAccessor = lambda.Compile();
}
}
/// <summary>
/// A mode that determines how the data binding is applied to the layer property
/// </summary>
public enum DataBindingMode
{
Override,
/// <summary>
/// Replaces the layer property value with the data binding value
/// </summary>
Replace,
/// <summary>
/// Adds the data binding value to the layer property value
/// </summary>
Add
}
}
@@ -100,13 +100,6 @@ namespace Artemis.Core
/// <returns>The modified value</returns>
public object Apply(object currentValue)
{
var targetType = DataBinding.TargetProperty.GetType();
if (currentValue.GetType() != targetType)
{
throw new ArtemisCoreException("The current value of the data binding does not match the type of the target property." +
$" {targetType.Name} expected, received {currentValue.GetType().Name}.");
}
if (CompiledDynamicPredicate != null)
return CompiledDynamicPredicate(currentValue, ParameterDataModel);
if (CompiledStaticPredicate != null)
@@ -20,11 +20,6 @@ namespace Artemis.Core
/// </summary>
public PluginInfo PluginInfo { get; internal set; }
/// <summary>
/// Gets the data binding modifier this modifier type is applied to
/// </summary>
public DataBindingModifier Modifier { get; internal set; }
/// <summary>
/// Gets the types this modifier supports
/// </summary>
+2 -2
View File
@@ -79,7 +79,7 @@ namespace Artemis.Core
foreach (var baseLayerEffect in LayerEffects.Where(e => e.Enabled))
{
baseLayerEffect.BaseProperties?.Update();
baseLayerEffect.BaseProperties?.Update(deltaTime);
baseLayerEffect.Update(deltaTime);
}
@@ -125,7 +125,7 @@ namespace Artemis.Core
foreach (var baseLayerEffect in LayerEffects.Where(e => e.Enabled))
{
baseLayerEffect.BaseProperties?.Update();
baseLayerEffect.BaseProperties?.Update(delta);
baseLayerEffect.Update(delta);
}
}
+8 -8
View File
@@ -261,14 +261,14 @@ namespace Artemis.Core
if (TimelinePosition > TimelineLength)
return;
General.Update();
Transform.Update();
LayerBrush.BaseProperties?.Update();
General.Update(deltaTime);
Transform.Update(deltaTime);
LayerBrush.BaseProperties?.Update(deltaTime);
LayerBrush.Update(deltaTime);
foreach (var baseLayerEffect in LayerEffects.Where(e => e.Enabled))
{
baseLayerEffect.BaseProperties?.Update();
baseLayerEffect.BaseProperties?.Update(deltaTime);
baseLayerEffect.Update(deltaTime);
}
}
@@ -301,14 +301,14 @@ namespace Artemis.Core
var delta = (TimelinePosition - beginTime).TotalSeconds;
General.Update();
Transform.Update();
LayerBrush.BaseProperties?.Update();
General.Update(delta);
Transform.Update(delta);
LayerBrush.BaseProperties?.Update(delta);
LayerBrush.Update(delta);
foreach (var baseLayerEffect in LayerEffects.Where(e => e.Enabled))
{
baseLayerEffect.BaseProperties?.Update();
baseLayerEffect.BaseProperties?.Update(delta);
baseLayerEffect.Update(delta);
}
}
@@ -19,7 +19,6 @@ namespace Artemis.Core
public class LayerProperty<T> : BaseLayerProperty
{
private T _baseValue;
private T _currentValue;
private bool _isInitialized;
private List<LayerPropertyKeyframe<T>> _keyframes;
@@ -47,11 +46,7 @@ namespace Artemis.Core
/// <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 => !KeyframesEnabled || !KeyframesSupported ? BaseValue : _currentValue;
internal set => _currentValue = value;
}
public T CurrentValue { get; set; }
/// <summary>
/// Gets or sets the default value of this layer property. If set, this value is automatically applied if the property
@@ -100,7 +95,7 @@ namespace Artemis.Core
currentKeyframe.Value = value;
// Update the property so that the new keyframe is reflected on the current value
Update();
Update(0);
}
}
@@ -190,9 +185,19 @@ namespace Artemis.Core
}
/// <summary>
/// Updates the property, applying keyframes to the current value
/// Updates the property, applying keyframes and data bindings to the current value
/// </summary>
internal void Update()
internal void Update(double deltaTime)
{
CurrentValue = BaseValue;
UpdateKeyframes();
UpdateDataBindings(deltaTime);
OnUpdated();
}
private void UpdateKeyframes()
{
if (!KeyframesSupported || !KeyframesEnabled)
return;
@@ -216,8 +221,15 @@ namespace Artemis.Core
var keyframeProgressEased = (float) Easings.Interpolate(keyframeProgress, CurrentKeyframe.EasingFunction);
UpdateCurrentValue(keyframeProgress, keyframeProgressEased);
}
}
OnUpdated();
private void UpdateDataBindings(double deltaTime)
{
foreach (var dataBinding in DataBindings)
{
dataBinding.Update(deltaTime);
ApplyDataBinding(dataBinding);
}
}
/// <summary>
@@ -237,7 +249,7 @@ namespace Artemis.Core
PropertyEntity = entity;
LayerPropertyGroup = layerPropertyGroup;
LayerPropertyGroup.PropertyGroupUpdating += (sender, args) => Update();
LayerPropertyGroup.PropertyGroupUpdating += (sender, args) => Update(args.DeltaTime);
try
{
@@ -303,9 +315,7 @@ namespace Artemis.Core
/// <inheritdoc />
protected override void ApplyDataBinding(DataBinding dataBinding)
{
// The default implementation only supports simple types
if (dataBinding.TargetProperty.DeclaringType == GetType())
CurrentValue = (T) dataBinding.GetValue(CurrentValue);
CurrentValue = (T) dataBinding.GetValue(CurrentValue);
}
#endregion
@@ -227,11 +227,11 @@ namespace Artemis.Core
}
}
internal void Update()
internal void Update(double deltaTime)
{
// Since at this point we don't know what properties the group has without using reflection,
// let properties subscribe to the update event and update themselves
OnPropertyGroupUpdating();
OnPropertyGroupUpdating(new LayerPropertyGroupUpdatingEventArgs(deltaTime));
}
private void InitializeProperty(RenderProfileElement profileElement, string path, BaseLayerProperty instance)
@@ -264,7 +264,7 @@ namespace Artemis.Core
#region Events
internal event EventHandler PropertyGroupUpdating;
internal event EventHandler<LayerPropertyGroupUpdatingEventArgs> PropertyGroupUpdating;
/// <summary>
/// Occurs when the property group has initialized all its children
@@ -282,9 +282,9 @@ namespace Artemis.Core
/// </summary>
public event EventHandler VisibilityChanged;
protected virtual void OnPropertyGroupUpdating()
protected virtual void OnPropertyGroupUpdating(LayerPropertyGroupUpdatingEventArgs e)
{
PropertyGroupUpdating?.Invoke(this, EventArgs.Empty);
PropertyGroupUpdating?.Invoke(this, e);
}
protected virtual void OnVisibilityChanged()