mirror of
https://github.com/Artemis-RGB/Artemis
synced 2026-11-12 03:48:56 +00:00
Data bindings - Added the option for different data binding modes
This commit is contained in:
1 parent
906efdac98
commit
f270d786f0
32 files changed
+940
-497
No files matched your search
@@ -16,6 +16,7 @@
|
||||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=models_005Cprofile_005Cdatabindings_005Cconverters_005Cinternal/@EntryIndexedValue">True</s:Boolean>
|
||||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=models_005Cprofile_005Cdatabindings_005Cdatabindingproperties/@EntryIndexedValue">True</s:Boolean>
|
||||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=models_005Cprofile_005Cdatabindings_005Cdatabindingproperties_005Cabstract/@EntryIndexedValue">True</s:Boolean>
|
||||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=models_005Cprofile_005Cdatabindings_005Cmodes/@EntryIndexedValue">True</s:Boolean>
|
||||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=models_005Cprofile_005Cdatabindings_005Cmodifiers/@EntryIndexedValue">True</s:Boolean>
|
||||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=models_005Cprofile_005Cdatabindings_005Cmodifiers_005Cabstract/@EntryIndexedValue">True</s:Boolean>
|
||||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=models_005Cprofile_005Clayerproperties/@EntryIndexedValue">True</s:Boolean>
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using Artemis.Core.DataModelExpansions;
|
||||
using Artemis.Storage.Entities.Profile.DataBindings;
|
||||
|
||||
namespace Artemis.Core
|
||||
@@ -24,7 +21,7 @@ namespace Artemis.Core
|
||||
|
||||
ApplyRegistration(dataBindingRegistration);
|
||||
Save();
|
||||
Initialize();
|
||||
ApplyDataBindingMode();
|
||||
}
|
||||
|
||||
internal DataBinding(LayerProperty<TLayerProperty> layerProperty, DataBindingEntity entity)
|
||||
@@ -33,8 +30,8 @@ namespace Artemis.Core
|
||||
Entity = entity;
|
||||
|
||||
// Load will add children so be initialized before that
|
||||
Initialize();
|
||||
Load();
|
||||
ApplyDataBindingMode();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -53,16 +50,9 @@ namespace Artemis.Core
|
||||
public DataBindingConverter<TLayerProperty, TProperty> Converter { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the currently used instance of the data model that contains the source of the data binding
|
||||
/// Gets the data binding mode
|
||||
/// </summary>
|
||||
public DataModel SourceDataModel { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the path of the source property in the <see cref="SourceDataModel" />
|
||||
/// </summary>
|
||||
public string SourcePropertyPath { get; private set; }
|
||||
|
||||
public DataBindingMode Mode { get; set; }
|
||||
public IDataBindingMode<TLayerProperty, TProperty> DataBindingMode { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the easing time of the data binding
|
||||
@@ -74,15 +64,6 @@ namespace Artemis.Core
|
||||
/// </summary>
|
||||
public Easings.Functions EasingFunction { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a list of modifiers applied to this data binding
|
||||
/// </summary>
|
||||
public IReadOnlyList<DataBindingModifier<TLayerProperty, TProperty>> Modifiers => _modifiers.AsReadOnly();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the compiled function that gets the current value of the data binding target
|
||||
/// </summary>
|
||||
public Func<DataModel, object> CompiledTargetAccessor { get; private set; }
|
||||
|
||||
internal DataBindingEntity Entity { get; }
|
||||
|
||||
@@ -122,72 +103,9 @@ namespace Artemis.Core
|
||||
{
|
||||
_disposed = true;
|
||||
|
||||
DataModelStore.DataModelAdded -= DataModelStoreOnDataModelAdded;
|
||||
DataModelStore.DataModelRemoved -= DataModelStoreOnDataModelRemoved;
|
||||
|
||||
foreach (var dataBindingModifier in Modifiers)
|
||||
dataBindingModifier.Dispose();
|
||||
DataBindingMode?.Dispose();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a modifier to the data binding's <see cref="Modifiers" /> collection
|
||||
/// </summary>
|
||||
public void AddModifier(DataBindingModifier<TLayerProperty, TProperty> modifier)
|
||||
{
|
||||
if (_disposed)
|
||||
throw new ObjectDisposedException("DataBinding");
|
||||
|
||||
if (!_modifiers.Contains(modifier))
|
||||
{
|
||||
modifier.DataBinding = this;
|
||||
_modifiers.Add(modifier);
|
||||
|
||||
OnModifiersUpdated();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes a modifier from the data binding's <see cref="Modifiers" /> collection
|
||||
/// </summary>
|
||||
public void RemoveModifier(DataBindingModifier<TLayerProperty, TProperty> modifier)
|
||||
{
|
||||
if (_disposed)
|
||||
throw new ObjectDisposedException("DataBinding");
|
||||
|
||||
if (_modifiers.Contains(modifier))
|
||||
{
|
||||
modifier.DataBinding = null;
|
||||
_modifiers.Remove(modifier);
|
||||
|
||||
OnModifiersUpdated();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the source of the data binding and re-compiles the expression
|
||||
/// </summary>
|
||||
/// <param name="dataModel">The data model of the source</param>
|
||||
/// <param name="path">The path pointing to the source inside the data model</param>
|
||||
public void UpdateSource(DataModel dataModel, string path)
|
||||
{
|
||||
if (_disposed)
|
||||
throw new ObjectDisposedException("DataBinding");
|
||||
|
||||
if (dataModel != null && path == null)
|
||||
throw new ArtemisCoreException("If a data model is provided, a path is also required");
|
||||
if (dataModel == null && path != null)
|
||||
throw new ArtemisCoreException("If path is provided, a data model is also required");
|
||||
|
||||
if (dataModel != null)
|
||||
{
|
||||
if (!dataModel.ContainsPath(path))
|
||||
throw new ArtemisCoreException($"Data model of type {dataModel.GetType().Name} does not contain a property at path '{path}'");
|
||||
}
|
||||
|
||||
SourceDataModel = dataModel;
|
||||
SourcePropertyPath = path;
|
||||
CreateExpression();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the current value of the data binding
|
||||
@@ -199,15 +117,11 @@ namespace Artemis.Core
|
||||
if (_disposed)
|
||||
throw new ObjectDisposedException("DataBinding");
|
||||
|
||||
if (CompiledTargetAccessor == null || Converter == null)
|
||||
if (Converter == null || DataBindingMode == null)
|
||||
return baseValue;
|
||||
|
||||
var dataBindingValue = CompiledTargetAccessor(SourceDataModel);
|
||||
foreach (var dataBindingModifier in Modifiers)
|
||||
dataBindingValue = dataBindingModifier.Apply(dataBindingValue);
|
||||
var value = DataBindingMode.GetValue(baseValue);
|
||||
|
||||
TProperty value = Converter.ConvertFromObject(dataBindingValue);
|
||||
|
||||
// If no easing is to be applied simple return whatever the current value is
|
||||
if (EasingTime == TimeSpan.Zero || !Converter.SupportsInterpolate)
|
||||
return value;
|
||||
@@ -228,28 +142,6 @@ namespace Artemis.Core
|
||||
return Registration.PropertyExpression.ReturnType;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the type of the source property of this data binding
|
||||
/// </summary>
|
||||
public Type GetSourceType()
|
||||
{
|
||||
return SourceDataModel?.GetTypeAtPath(SourcePropertyPath);
|
||||
}
|
||||
|
||||
private void Initialize()
|
||||
{
|
||||
DataModelStore.DataModelAdded += DataModelStoreOnDataModelAdded;
|
||||
DataModelStore.DataModelRemoved += DataModelStoreOnDataModelRemoved;
|
||||
|
||||
// Source
|
||||
if (Entity.SourceDataModelGuid != null && SourceDataModel == null)
|
||||
{
|
||||
var dataModel = DataModelStore.Get(Entity.SourceDataModelGuid.Value)?.DataModel;
|
||||
if (dataModel != null && dataModel.ContainsPath(Entity.SourcePropertyPath))
|
||||
UpdateSource(dataModel, Entity.SourcePropertyPath);
|
||||
}
|
||||
}
|
||||
|
||||
private void ResetEasing(TProperty value)
|
||||
{
|
||||
_previousValue = GetInterpolatedValue();
|
||||
@@ -284,25 +176,48 @@ namespace Artemis.Core
|
||||
var easingAmount = _easingProgress.TotalSeconds / EasingTime.TotalSeconds;
|
||||
return Converter.Interpolate(_previousValue, _currentValue, Easings.Interpolate(easingAmount, EasingFunction));
|
||||
}
|
||||
|
||||
#region Mode management
|
||||
|
||||
private void CreateExpression()
|
||||
/// <summary>
|
||||
/// Changes the data binding mode of the data binding to the specified <paramref name="dataBindingMode" />
|
||||
/// </summary>
|
||||
public void ChangeDataBindingMode(DataBindingModeType dataBindingMode)
|
||||
{
|
||||
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");
|
||||
switch (dataBindingMode)
|
||||
{
|
||||
case DataBindingModeType.Direct:
|
||||
Entity.DataBindingMode = new DirectDataBindingEntity();
|
||||
break;
|
||||
case DataBindingModeType.Conditional:
|
||||
Entity.DataBindingMode = new ConditionalDataBindingEntity();
|
||||
break;
|
||||
default:
|
||||
Entity.DataBindingMode = null;
|
||||
break;
|
||||
}
|
||||
|
||||
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 returnValue = Expression.Convert(accessor, typeof(object));
|
||||
|
||||
var lambda = Expression.Lambda<Func<DataModel, object>>(returnValue, parameter);
|
||||
CompiledTargetAccessor = lambda.Compile();
|
||||
ApplyDataBindingMode();
|
||||
}
|
||||
|
||||
private void ApplyDataBindingMode()
|
||||
{
|
||||
DataBindingMode?.Dispose();
|
||||
DataBindingMode = null;
|
||||
|
||||
switch (Entity.DataBindingMode)
|
||||
{
|
||||
case DirectDataBindingEntity directDataBindingEntity:
|
||||
DataBindingMode = new DirectDataBinding<TLayerProperty, TProperty>(this, directDataBindingEntity);
|
||||
break;
|
||||
case ConditionalDataBindingEntity conditionalDataBindingEntity:
|
||||
DataBindingMode = new ConditionalDataBinding<TLayerProperty, TProperty>(this, conditionalDataBindingEntity);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Storage
|
||||
|
||||
/// <inheritdoc />
|
||||
@@ -314,15 +229,10 @@ namespace Artemis.Core
|
||||
var registration = LayerProperty.GetDataBindingRegistration<TProperty>(Entity.TargetExpression);
|
||||
ApplyRegistration(registration);
|
||||
|
||||
Mode = (DataBindingMode) Entity.DataBindingMode;
|
||||
EasingTime = Entity.EasingTime;
|
||||
EasingFunction = (Easings.Functions) Entity.EasingFunction;
|
||||
|
||||
// Data model is done during Initialize
|
||||
|
||||
// Modifiers
|
||||
foreach (var dataBindingModifierEntity in Entity.Modifiers)
|
||||
_modifiers.Add(new DataBindingModifier<TLayerProperty, TProperty>(this, dataBindingModifierEntity));
|
||||
DataBindingMode?.Load();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
@@ -336,54 +246,10 @@ namespace Artemis.Core
|
||||
|
||||
// General
|
||||
Entity.TargetExpression = Registration.PropertyExpression.ToString();
|
||||
Entity.DataBindingMode = (int) Mode;
|
||||
Entity.EasingTime = EasingTime;
|
||||
Entity.EasingFunction = (int) EasingFunction;
|
||||
|
||||
// Data model
|
||||
if (SourceDataModel != null)
|
||||
{
|
||||
Entity.SourceDataModelGuid = SourceDataModel.PluginInfo.Guid;
|
||||
Entity.SourcePropertyPath = SourcePropertyPath;
|
||||
}
|
||||
|
||||
// Modifiers
|
||||
Entity.Modifiers.Clear();
|
||||
foreach (var dataBindingModifier in Modifiers)
|
||||
dataBindingModifier.Save();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Event handlers
|
||||
|
||||
private void DataModelStoreOnDataModelAdded(object sender, DataModelStoreEvent e)
|
||||
{
|
||||
var dataModel = e.Registration.DataModel;
|
||||
if (dataModel.PluginInfo.Guid == Entity.SourceDataModelGuid && dataModel.ContainsPath(Entity.SourcePropertyPath))
|
||||
UpdateSource(dataModel, Entity.SourcePropertyPath);
|
||||
}
|
||||
|
||||
private void DataModelStoreOnDataModelRemoved(object sender, DataModelStoreEvent e)
|
||||
{
|
||||
if (SourceDataModel != e.Registration.DataModel)
|
||||
return;
|
||||
SourceDataModel = null;
|
||||
CompiledTargetAccessor = null;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Events
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when a modifier is added or removed
|
||||
/// </summary>
|
||||
public event EventHandler ModifiersUpdated;
|
||||
|
||||
protected virtual void OnModifiersUpdated()
|
||||
{
|
||||
ModifiersUpdated?.Invoke(this, EventArgs.Empty);
|
||||
DataBindingMode?.Save();
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -392,16 +258,21 @@ namespace Artemis.Core
|
||||
/// <summary>
|
||||
/// A mode that determines how the data binding is applied to the layer property
|
||||
/// </summary>
|
||||
public enum DataBindingMode
|
||||
public enum DataBindingModeType
|
||||
{
|
||||
/// <summary>
|
||||
/// Disables the data binding
|
||||
/// </summary>
|
||||
None,
|
||||
|
||||
/// <summary>
|
||||
/// Replaces the layer property value with the data binding value
|
||||
/// </summary>
|
||||
Replace,
|
||||
Direct,
|
||||
|
||||
/// <summary>
|
||||
/// Adds the data binding value to the layer property value
|
||||
/// Replaces the layer property value with the data binding value
|
||||
/// </summary>
|
||||
Add
|
||||
Conditional,
|
||||
}
|
||||
}
|
||||
@@ -10,49 +10,40 @@ namespace Artemis.Core
|
||||
/// <inheritdoc />
|
||||
public class DataBindingModifier<TLayerProperty, TProperty> : IDataBindingModifier
|
||||
{
|
||||
private DataBinding<TLayerProperty, TProperty> _dataBinding;
|
||||
private bool _disposed;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of the <see cref="DataBindingModifier{TLayerProperty,TProperty}" /> class
|
||||
/// </summary>
|
||||
/// <param name="dataBinding">The data binding the modifier is to be applied to</param>
|
||||
/// <param name="directDataBinding">The direct data binding the modifier is to be applied to</param>
|
||||
/// <param name="parameterType">The type of the parameter, can either be dynamic (based on a data model value) or static</param>
|
||||
public DataBindingModifier(DataBinding<TLayerProperty, TProperty> dataBinding, ProfileRightSideType parameterType)
|
||||
public DataBindingModifier(DirectDataBinding<TLayerProperty, TProperty> directDataBinding, ProfileRightSideType parameterType)
|
||||
{
|
||||
_dataBinding = dataBinding ?? throw new ArgumentNullException(nameof(dataBinding));
|
||||
DirectDataBinding = directDataBinding ?? throw new ArgumentNullException(nameof(directDataBinding));
|
||||
ParameterType = parameterType;
|
||||
Entity = new DataBindingModifierEntity();
|
||||
Initialize();
|
||||
Save();
|
||||
}
|
||||
|
||||
internal DataBindingModifier(DataBinding<TLayerProperty, TProperty> dataBinding, DataBindingModifierEntity entity)
|
||||
internal DataBindingModifier(DirectDataBinding<TLayerProperty, TProperty> directDataBinding, DataBindingModifierEntity entity)
|
||||
{
|
||||
_dataBinding = dataBinding;
|
||||
DirectDataBinding = directDataBinding ?? throw new ArgumentNullException(nameof(directDataBinding));
|
||||
Entity = entity;
|
||||
Load();
|
||||
Initialize();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the data binding this modifier is applied to
|
||||
/// </summary>
|
||||
public DataBinding<TLayerProperty, TProperty> DataBinding
|
||||
{
|
||||
get => _dataBinding;
|
||||
internal set
|
||||
{
|
||||
_dataBinding = value;
|
||||
CreateExpression();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the type of modifier that is being applied
|
||||
/// </summary>
|
||||
public DataBindingModifierType ModifierType { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the direct data binding this modifier is applied to
|
||||
/// </summary>
|
||||
public DirectDataBinding<TLayerProperty, TProperty> DirectDataBinding { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the type of the parameter, can either be dynamic (based on a data model value) or static
|
||||
/// </summary>
|
||||
@@ -93,8 +84,8 @@ namespace Artemis.Core
|
||||
if (_disposed)
|
||||
throw new ObjectDisposedException("DataBindingModifier");
|
||||
|
||||
if (!DataBinding.Entity.Modifiers.Contains(Entity))
|
||||
DataBinding.Entity.Modifiers.Add(Entity);
|
||||
if (!DirectDataBinding.Entity.Modifiers.Contains(Entity))
|
||||
DirectDataBinding.Entity.Modifiers.Add(Entity);
|
||||
|
||||
// Modifier
|
||||
if (ModifierType != null)
|
||||
@@ -177,7 +168,7 @@ namespace Artemis.Core
|
||||
return;
|
||||
}
|
||||
|
||||
var targetType = DataBinding.GetTargetType();
|
||||
var targetType = DirectDataBinding.DataBinding.GetTargetType();
|
||||
if (!modifierType.SupportsType(targetType))
|
||||
{
|
||||
throw new ArtemisCoreException($"Cannot apply modifier type {modifierType.GetType().Name} to this modifier because " +
|
||||
@@ -229,7 +220,7 @@ namespace Artemis.Core
|
||||
ParameterDataModel = null;
|
||||
ParameterPropertyPath = null;
|
||||
|
||||
var targetType = DataBinding.GetTargetType();
|
||||
var targetType = DirectDataBinding.DataBinding.GetTargetType();
|
||||
|
||||
// If not null ensure the types match and if not, convert it
|
||||
if (staticValue != null && staticValue.GetType() == targetType)
|
||||
@@ -271,7 +262,7 @@ namespace Artemis.Core
|
||||
else if (ParameterType == ProfileRightSideType.Static && Entity.ParameterStaticValue != null && ParameterStaticValue == null)
|
||||
{
|
||||
// Use the target type so JSON.NET has a better idea what to do
|
||||
var targetType = DataBinding.GetTargetType();
|
||||
var targetType = DirectDataBinding.DataBinding.GetTargetType();
|
||||
object staticValue;
|
||||
|
||||
try
|
||||
@@ -293,7 +284,7 @@ namespace Artemis.Core
|
||||
{
|
||||
CompiledParameterAccessor = null;
|
||||
|
||||
if (ModifierType == null || DataBinding == null)
|
||||
if (ModifierType == null)
|
||||
return;
|
||||
|
||||
if (ParameterType == ProfileRightSideType.Dynamic && ModifierType.SupportsParameter)
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
using Artemis.Storage.Entities.Profile.DataBindings;
|
||||
|
||||
namespace Artemis.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a data binding mode that applies a value depending on conditions
|
||||
/// </summary>
|
||||
public class ConditionalDataBinding<TLayerProperty, TProperty> : IDataBindingMode<TLayerProperty, TProperty>
|
||||
{
|
||||
internal ConditionalDataBinding(DataBinding<TLayerProperty, TProperty> dataBinding, ConditionalDataBindingEntity entity)
|
||||
{
|
||||
DataBinding = dataBinding;
|
||||
Entity = entity;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public DataBinding<TLayerProperty, TProperty> DataBinding { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public TProperty GetValue(TProperty baseValue)
|
||||
{
|
||||
return default;
|
||||
}
|
||||
|
||||
internal ConditionalDataBindingEntity Entity { get; }
|
||||
|
||||
#region Storage
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Load()
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Save()
|
||||
{
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region IDisposable
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,255 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using Artemis.Core.DataModelExpansions;
|
||||
using Artemis.Storage.Entities.Profile.DataBindings;
|
||||
|
||||
namespace Artemis.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a data binding mode that directly applies a data model value to a data binding
|
||||
/// </summary>
|
||||
public class DirectDataBinding<TLayerProperty, TProperty> : IDataBindingMode<TLayerProperty, TProperty>
|
||||
{
|
||||
private readonly List<DataBindingModifier<TLayerProperty, TProperty>> _modifiers = new List<DataBindingModifier<TLayerProperty, TProperty>>();
|
||||
private bool _disposed;
|
||||
|
||||
internal DirectDataBinding(DataBinding<TLayerProperty, TProperty> dataBinding, DirectDataBindingEntity entity)
|
||||
{
|
||||
DataBinding = dataBinding;
|
||||
Entity = entity;
|
||||
|
||||
Initialize();
|
||||
Load();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public DataBinding<TLayerProperty, TProperty> DataBinding { get; }
|
||||
|
||||
internal DirectDataBindingEntity Entity { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the currently used instance of the data model that contains the source of the data binding
|
||||
/// </summary>
|
||||
public DataModel SourceDataModel { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the path of the source property in the <see cref="SourceDataModel" />
|
||||
/// </summary>
|
||||
public string SourcePropertyPath { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a list of modifiers applied to this data binding
|
||||
/// </summary>
|
||||
public IReadOnlyList<DataBindingModifier<TLayerProperty, TProperty>> Modifiers => _modifiers.AsReadOnly();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the compiled function that gets the current value of the data binding target
|
||||
/// </summary>
|
||||
public Func<DataModel, object> CompiledTargetAccessor { get; private set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public TProperty GetValue(TProperty baseValue)
|
||||
{
|
||||
if (_disposed)
|
||||
throw new ObjectDisposedException("DataBinding");
|
||||
|
||||
if (CompiledTargetAccessor == null)
|
||||
return baseValue;
|
||||
|
||||
var dataBindingValue = CompiledTargetAccessor(SourceDataModel);
|
||||
foreach (var dataBindingModifier in Modifiers)
|
||||
dataBindingValue = dataBindingModifier.Apply(dataBindingValue);
|
||||
|
||||
return DataBinding.Converter.ConvertFromObject(dataBindingValue);
|
||||
}
|
||||
|
||||
#region IDisposable
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Dispose()
|
||||
{
|
||||
_disposed = true;
|
||||
|
||||
foreach (var dataBindingModifier in Modifiers)
|
||||
dataBindingModifier.Dispose();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Storage
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Load()
|
||||
{
|
||||
// Data model is done during Initialize
|
||||
|
||||
// Modifiers
|
||||
foreach (var dataBindingModifierEntity in Entity.Modifiers)
|
||||
_modifiers.Add(new DataBindingModifier<TLayerProperty, TProperty>(this, dataBindingModifierEntity));
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Save()
|
||||
{
|
||||
// Data model
|
||||
if (SourceDataModel != null)
|
||||
{
|
||||
Entity.SourceDataModelGuid = SourceDataModel.PluginInfo.Guid;
|
||||
Entity.SourcePropertyPath = SourcePropertyPath;
|
||||
}
|
||||
|
||||
// Modifiers
|
||||
Entity.Modifiers.Clear();
|
||||
foreach (var dataBindingModifier in Modifiers)
|
||||
dataBindingModifier.Save();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Initialization
|
||||
|
||||
private void Initialize()
|
||||
{
|
||||
DataModelStore.DataModelAdded += DataModelStoreOnDataModelAdded;
|
||||
DataModelStore.DataModelRemoved += DataModelStoreOnDataModelRemoved;
|
||||
|
||||
// Source
|
||||
if (Entity.SourceDataModelGuid != null && SourceDataModel == null)
|
||||
{
|
||||
var dataModel = DataModelStore.Get(Entity.SourceDataModelGuid.Value)?.DataModel;
|
||||
if (dataModel != null && dataModel.ContainsPath(Entity.SourcePropertyPath))
|
||||
UpdateSource(dataModel, Entity.SourcePropertyPath);
|
||||
}
|
||||
}
|
||||
|
||||
private void CreateExpression()
|
||||
{
|
||||
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(DataModel), "targetDataModel");
|
||||
var accessor = SourcePropertyPath.Split('.').Aggregate<string, Expression>(
|
||||
Expression.Convert(parameter, SourceDataModel.GetType()), // Cast to the appropriate type
|
||||
Expression.Property
|
||||
);
|
||||
|
||||
var returnValue = Expression.Convert(accessor, typeof(object));
|
||||
|
||||
var lambda = Expression.Lambda<Func<DataModel, object>>(returnValue, parameter);
|
||||
CompiledTargetAccessor = lambda.Compile();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Source
|
||||
|
||||
/// <summary>
|
||||
/// Returns the type of the source property of this data binding
|
||||
/// </summary>
|
||||
public Type GetSourceType()
|
||||
{
|
||||
return SourceDataModel?.GetTypeAtPath(SourcePropertyPath);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Updates the source of the data binding and re-compiles the expression
|
||||
/// </summary>
|
||||
/// <param name="dataModel">The data model of the source</param>
|
||||
/// <param name="path">The path pointing to the source inside the data model</param>
|
||||
public void UpdateSource(DataModel dataModel, string path)
|
||||
{
|
||||
if (_disposed)
|
||||
throw new ObjectDisposedException("DataBinding");
|
||||
|
||||
if (dataModel != null && path == null)
|
||||
throw new ArtemisCoreException("If a data model is provided, a path is also required");
|
||||
if (dataModel == null && path != null)
|
||||
throw new ArtemisCoreException("If path is provided, a data model is also required");
|
||||
|
||||
if (dataModel != null)
|
||||
{
|
||||
if (!dataModel.ContainsPath(path))
|
||||
throw new ArtemisCoreException($"Data model of type {dataModel.GetType().Name} does not contain a property at path '{path}'");
|
||||
}
|
||||
|
||||
SourceDataModel = dataModel;
|
||||
SourcePropertyPath = path;
|
||||
CreateExpression();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Modifiers
|
||||
|
||||
/// <summary>
|
||||
/// Adds a modifier to the data binding's <see cref="Modifiers" /> collection
|
||||
/// </summary>
|
||||
public DataBindingModifier<TLayerProperty, TProperty> AddModifier(ProfileRightSideType type)
|
||||
{
|
||||
if (_disposed)
|
||||
throw new ObjectDisposedException("DataBinding");
|
||||
|
||||
var modifier = new DataBindingModifier<TLayerProperty, TProperty>(this, type);
|
||||
_modifiers.Add(modifier);
|
||||
OnModifiersUpdated();
|
||||
|
||||
return modifier;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes a modifier from the data binding's <see cref="Modifiers" /> collection and disposes it
|
||||
/// </summary>
|
||||
public void RemoveModifier(DataBindingModifier<TLayerProperty, TProperty> modifier)
|
||||
{
|
||||
if (_disposed)
|
||||
throw new ObjectDisposedException("DataBinding");
|
||||
|
||||
if (_modifiers.Contains(modifier))
|
||||
{
|
||||
_modifiers.Remove(modifier);
|
||||
modifier.Dispose();
|
||||
|
||||
OnModifiersUpdated();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Event handlers
|
||||
|
||||
private void DataModelStoreOnDataModelAdded(object sender, DataModelStoreEvent e)
|
||||
{
|
||||
var dataModel = e.Registration.DataModel;
|
||||
if (dataModel.PluginInfo.Guid == Entity.SourceDataModelGuid && dataModel.ContainsPath(Entity.SourcePropertyPath))
|
||||
UpdateSource(dataModel, Entity.SourcePropertyPath);
|
||||
}
|
||||
|
||||
private void DataModelStoreOnDataModelRemoved(object sender, DataModelStoreEvent e)
|
||||
{
|
||||
if (SourceDataModel != e.Registration.DataModel)
|
||||
return;
|
||||
SourceDataModel = null;
|
||||
CompiledTargetAccessor = null;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Events
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when a modifier is added or removed
|
||||
/// </summary>
|
||||
public event EventHandler ModifiersUpdated;
|
||||
|
||||
protected virtual void OnModifiersUpdated()
|
||||
{
|
||||
ModifiersUpdated?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using System;
|
||||
|
||||
namespace Artemis.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a data binding mode
|
||||
/// </summary>
|
||||
public interface IDataBindingMode<TLayerProperty, TProperty> : IStorageModel, IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the data binding this mode is applied to
|
||||
/// </summary>
|
||||
DataBinding<TLayerProperty, TProperty> DataBinding { get; }
|
||||
|
||||
/// <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>
|
||||
TProperty GetValue(TProperty baseValue);
|
||||
}
|
||||
}
|
||||
@@ -738,7 +738,7 @@ namespace Artemis.Core
|
||||
|
||||
private void LayerBrushStoreOnLayerBrushAdded(object sender, LayerBrushStoreEvent e)
|
||||
{
|
||||
if (LayerBrush != null)
|
||||
if (LayerBrush != null || General.BrushReference?.CurrentValue == null)
|
||||
return;
|
||||
|
||||
var current = General.BrushReference.CurrentValue;
|
||||
|
||||
@@ -7,8 +7,8 @@ namespace Artemis.Core
|
||||
{
|
||||
internal SKSizeLayerProperty()
|
||||
{
|
||||
RegisterDataBindingProperty(size => size.Width, new FloatDataBindingConverter<SKSize>());
|
||||
RegisterDataBindingProperty(size => size.Height, new FloatDataBindingConverter<SKSize>());
|
||||
RegisterDataBindingProperty(size => size.Width, new FloatDataBindingConverter<SKSize>());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in new issue
Block a user