mirror of
https://github.com/Artemis-RGB/Artemis
synced 2026-11-12 11:56:30 +00:00
Data bindings - Added binding apply logic to the layer properties
Data bindings - Expanded to support inner layer properties
This commit is contained in:
1 parent
99522f052f
commit
1a8df1647b
12 files changed
+191
-29
No files matched your search
@@ -1,4 +1,8 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using Artemis.Core.DataModelExpansions;
|
||||
|
||||
namespace Artemis.Core
|
||||
@@ -11,9 +15,14 @@ namespace Artemis.Core
|
||||
private readonly List<DataBindingModifier> _modifiers = new List<DataBindingModifier>();
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="BaseLayerProperty" /> that the data binding targets
|
||||
/// Gets the layer property this data binding targets
|
||||
/// </summary>
|
||||
public BaseLayerProperty Target { get; set; } // BIG FAT TODO: Take into account X and Y of SkPosition etc., forgot about it again :>
|
||||
public BaseLayerProperty LayerProperty { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the inner property this data binding targets
|
||||
/// </summary>
|
||||
public PropertyInfo TargetProperty { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the currently used instance of the data model that contains the source of the data binding
|
||||
@@ -30,6 +39,8 @@ namespace Artemis.Core
|
||||
/// </summary>
|
||||
public IReadOnlyList<DataBindingModifier> Modifiers => _modifiers.AsReadOnly();
|
||||
|
||||
public Func<DataModel, object> CompiledTargetAccessor { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Adds a modifier to the data binding's <see cref="Modifiers" /> collection
|
||||
/// </summary>
|
||||
@@ -55,5 +66,44 @@ namespace Artemis.Core
|
||||
_modifiers.Remove(modifier);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the current value of the data binding
|
||||
/// </summary>
|
||||
/// <param name="baseValue">The base value of the property the data binding is applied to</param>
|
||||
/// <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})");
|
||||
}
|
||||
|
||||
if (CompiledTargetAccessor == null)
|
||||
return baseValue;
|
||||
|
||||
var dataBindingValue = CompiledTargetAccessor(SourceDataModel);
|
||||
foreach (var dataBindingModifier in Modifiers)
|
||||
dataBindingValue = dataBindingModifier.Apply(dataBindingValue);
|
||||
|
||||
return dataBindingValue;
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
var listType = SourceDataModel.GetListTypeInPath(SourcePropertyPath);
|
||||
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 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);
|
||||
CompiledTargetAccessor = lambda.Compile();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user