Profile editor - Finished most of the refactor except databindings

This commit is contained in:
SpoinkyNL committed 2020-09-14 01:24:07 +02:00
1 parent 7fff1a593f
commit fea454ad12
56 files changed
+866 -459

No files matched your search

@@ -1,4 +1,5 @@
using System;
using SkiaSharp;
namespace Artemis.Core
{
@@ -36,12 +37,23 @@ namespace Artemis.Core
/// <inheritdoc />
public override void ApplyValue(float value)
{
if (SetExpression == null)
return;
if (DataBinding.LayerProperty.PropertyDescription.MaxInputValue is float max)
value = Math.Min(value, max);
if (DataBinding.LayerProperty.PropertyDescription.MinInputValue is float min)
value = Math.Max(value, min);
SetExpression?.Invoke(value);
var test = new SKSize(5,5);
test.Width = 10;
SetExpression(DataBinding.LayerProperty.CurrentValue, value);
}
private void Mehtest(ref SKSize test)
{
test.Width = 20;
}
/// <inheritdoc />
@@ -26,7 +26,7 @@ namespace Artemis.Core
/// <inheritdoc />
public override void ApplyValue(object value)
{
SetExpression?.Invoke(value);
SetExpression?.Invoke(DataBinding.LayerProperty.CurrentValue, value);
}
/// <inheritdoc />
@@ -41,7 +41,7 @@ namespace Artemis.Core
/// <inheritdoc />
public override void ApplyValue(int value)
{
SetExpression?.Invoke(value);
SetExpression?.Invoke(DataBinding.LayerProperty.CurrentValue, value);
}
/// <inheritdoc />
@@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using Artemis.Core.DataModelExpansions;
using Artemis.Storage.Entities.Profile.DataBindings;
@@ -14,9 +13,9 @@ namespace Artemis.Core
private readonly List<DataBindingModifier<TLayerProperty, TProperty>> _modifiers = new List<DataBindingModifier<TLayerProperty, TProperty>>();
private TProperty _currentValue;
private bool _disposed;
private TimeSpan _easingProgress;
private TProperty _previousValue;
private bool _disposed;
internal DataBinding(DataBindingRegistration<TLayerProperty, TProperty> dataBindingRegistration)
{
@@ -53,11 +52,6 @@ namespace Artemis.Core
/// </summary>
public DataBindingConverter<TLayerProperty, TProperty> Converter { get; private set; }
/// <summary>
/// Gets the property on the <see cref="LayerProperty" /> 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
/// </summary>
@@ -118,8 +112,21 @@ namespace Artemis.Core
if (Converter == null)
return;
var value = GetValue(Converter.GetValue());
Converter.ApplyValue(GetValue(value));
var converterValue = Converter.GetValue();
var value = GetValue(converterValue);
Converter.ApplyValue(value);
}
/// <inheritdoc />
public void Dispose()
{
_disposed = true;
DataModelStore.DataModelAdded -= DataModelStoreOnDataModelAdded;
DataModelStore.DataModelRemoved -= DataModelStoreOnDataModelRemoved;
foreach (var dataBindingModifier in Modifiers)
dataBindingModifier.Dispose();
}
/// <summary>
@@ -218,7 +225,7 @@ namespace Artemis.Core
/// </summary>
public Type GetTargetType()
{
return TargetProperty.PropertyType;
return Registration.PropertyExpression.ReturnType;
}
/// <summary>
@@ -257,7 +264,6 @@ namespace Artemis.Core
Converter = dataBindingRegistration?.Converter;
Registration = dataBindingRegistration;
TargetProperty = dataBindingRegistration?.Property;
if (GetTargetType().IsValueType)
{
@@ -305,7 +311,7 @@ namespace Artemis.Core
if (_disposed)
throw new ObjectDisposedException("DataBinding");
// General
var registration = LayerProperty.GetDataBindingRegistration<TProperty>(Entity.TargetProperty);
var registration = LayerProperty.GetDataBindingRegistration<TProperty>(Entity.TargetExpression);
ApplyRegistration(registration);
Mode = (DataBindingMode) Entity.DataBindingMode;
@@ -329,7 +335,7 @@ namespace Artemis.Core
LayerProperty.Entity.DataBindingEntities.Add(Entity);
// General
Entity.TargetProperty = TargetProperty?.Name;
Entity.TargetExpression = Registration.PropertyExpression.ToString();
Entity.DataBindingMode = (int) Mode;
Entity.EasingTime = EasingTime;
Entity.EasingFunction = (int) EasingFunction;
@@ -340,7 +346,7 @@ namespace Artemis.Core
Entity.SourceDataModelGuid = SourceDataModel.PluginInfo.Guid;
Entity.SourcePropertyPath = SourcePropertyPath;
}
// Modifiers
Entity.Modifiers.Clear();
foreach (var dataBindingModifier in Modifiers)
@@ -381,18 +387,6 @@ namespace Artemis.Core
}
#endregion
/// <inheritdoc />
public void Dispose()
{
_disposed = true;
DataModelStore.DataModelAdded -= DataModelStoreOnDataModelAdded;
DataModelStore.DataModelRemoved -= DataModelStoreOnDataModelRemoved;
foreach (var dataBindingModifier in Modifiers)
dataBindingModifier.Dispose();
}
}
/// <summary>
@@ -1,6 +1,7 @@
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
namespace Artemis.Core
{
@@ -18,7 +19,7 @@ namespace Artemis.Core
/// <summary>
/// A dynamically compiled setter pointing to the data bound property
/// </summary>
public Action<TProperty> SetExpression { get; private set; }
public Action<TLayerProperty, TProperty> SetExpression { get; private set; }
/// <summary>
/// Gets the data binding this converter is applied to
@@ -81,19 +82,34 @@ namespace Artemis.Core
OnInitialized();
}
private Action<TProperty> CreateValueSetter()
private Action<TLayerProperty, TProperty> CreateValueSetter()
{
if (DataBinding.TargetProperty?.DeclaringType == null)
return null;
MethodInfo setterMethod = null;
if (DataBinding.Registration.Member != null)
{
if (DataBinding.Registration.Member is PropertyInfo propertyInfo)
setterMethod = propertyInfo.GetSetMethod();
}
var setterMethod = DataBinding.TargetProperty.GetSetMethod();
if (setterMethod == null)
return null;
var parameter = Expression.Parameter(typeof(TLayerProperty), "currentValue");
var propertyValue = Expression.Parameter(typeof(TProperty), "propertyValue");
var body = DataBinding.Registration.PropertyExpression;
var lambda = Expression.Lambda<Action<TProperty>>(body, propertyValue);
var memberAccess = Expression.MakeMemberAccess(parameter, DataBinding.Registration.Member);
var assignment = Expression.Assign(memberAccess, propertyValue);
var lambda = Expression.Lambda<Action<TLayerProperty, TProperty>>(assignment, parameter, propertyValue);
if (typeof(TLayerProperty).IsValueType)
{
// var layerProperty = Expression.Constant(DataBinding.LayerProperty);
// var layerPropertyMemberAccess = Expression.MakeMemberAccess(layerProperty, DataBinding.LayerProperty.GetType().GetMember(nameof(DataBinding.LayerProperty.CurrentValue))[0]);
// var assingment = Expression.Assign()
}
return lambda.Compile();
}
}
@@ -175,7 +175,7 @@ namespace Artemis.Core
return;
}
var targetType = DataBinding.TargetProperty.PropertyType;
var targetType = DataBinding.GetTargetType();
if (!modifierType.SupportsType(targetType))
{
throw new ArtemisCoreException($"Cannot apply modifier type {modifierType.GetType().Name} to this modifier because " +
@@ -227,7 +227,7 @@ namespace Artemis.Core
ParameterDataModel = null;
ParameterPropertyPath = null;
var targetType = DataBinding.TargetProperty.PropertyType;
var targetType = DataBinding.GetTargetType();
// If not null ensure the types match and if not, convert it
if (staticValue != null && staticValue.GetType() == targetType)
@@ -269,7 +269,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.TargetProperty.PropertyType;
var targetType = DataBinding.GetTargetType();
object staticValue;
try
@@ -8,13 +8,16 @@ namespace Artemis.Core
/// <inheritdoc />
public class DataBindingRegistration<TLayerProperty, TProperty> : IDataBindingRegistration
{
internal DataBindingRegistration(LayerProperty<TLayerProperty> layerProperty,
DataBindingConverter<TLayerProperty, TProperty> converter,
internal DataBindingRegistration(LayerProperty<TLayerProperty> layerProperty,
DataBindingConverter<TLayerProperty, TProperty> converter,
Expression<Func<TLayerProperty, TProperty>> propertyExpression)
{
LayerProperty = layerProperty ?? throw new ArgumentNullException(nameof(layerProperty));
Converter = converter ?? throw new ArgumentNullException(nameof(converter));
PropertyExpression = propertyExpression ?? throw new ArgumentNullException(nameof(propertyExpression));
if (propertyExpression.Body is MemberExpression memberExpression)
Member = memberExpression.Member;
}
/// <summary>
@@ -28,15 +31,16 @@ namespace Artemis.Core
public DataBindingConverter<TLayerProperty, TProperty> Converter { get; }
/// <summary>
/// Gets the expression that that accesses the property
/// Gets the expression that that accesses the property
/// </summary>
public Expression<Func<TLayerProperty, TProperty>> PropertyExpression { get; }
/// <summary>
/// Gets the registered property
/// Gets the member the <see cref="PropertyExpression" /> targets
/// <para><c>null</c> if the <see cref="PropertyExpression" /> is not a member expression</para>
/// </summary>
public PropertyInfo Property { get; }
public MemberInfo Member { get; }
/// <summary>
/// Gets the data binding created using this registration
/// </summary>
@@ -48,7 +52,7 @@ namespace Artemis.Core
if (DataBinding != null)
return DataBinding;
var dataBinding = LayerProperty.Entity.DataBindingEntities.FirstOrDefault(e => e.TargetProperty == PropertyExpression.ToString());
var dataBinding = LayerProperty.Entity.DataBindingEntities.FirstOrDefault(e => e.TargetExpression == PropertyExpression.ToString());
if (dataBinding == null)
return null;
+2 -10
View File
@@ -193,7 +193,7 @@ namespace Artemis.Core
canvas.Restore();
}
/// <inheritdoc />
public override void AddChild(ProfileElement child, int? order = null)
{
@@ -242,17 +242,13 @@ namespace Artemis.Core
protected override void Dispose(bool disposing)
{
if (!disposing)
return;
_disposed = true;
foreach (var baseLayerEffect in LayerEffects)
baseLayerEffect.Dispose();
foreach (var profileElement in Children)
profileElement.Dispose();
_folderBitmap?.Dispose();
base.Dispose(disposing);
}
internal override void Load()
@@ -290,10 +286,6 @@ namespace Artemis.Core
FolderEntity.ExpandedPropertyGroups.Clear();
FolderEntity.ExpandedPropertyGroups.AddRange(_expandedPropertyGroups);
// Conditions
RenderElementEntity.RootDisplayCondition = DisplayConditionGroup?.Entity;
DisplayConditionGroup?.Save();
SaveRenderElement();
}
+5 -19
View File
@@ -124,20 +124,16 @@ namespace Artemis.Core
/// <inheritdoc />
protected override void Dispose(bool disposing)
{
if (!disposing)
return;
_disposed = true;
// Brush first in case it depends on any of the other disposables during it's own disposal
_layerBrush?.Dispose();
foreach (var baseLayerEffect in LayerEffects)
baseLayerEffect.Dispose();
_general?.Dispose();
_layerBitmap?.Dispose();
_transform?.Dispose();
base.Dispose(disposing);
}
#endregion
@@ -210,10 +206,6 @@ namespace Artemis.Core
LayerEntity.Leds.Add(ledEntity);
}
// Conditions
RenderElementEntity.RootDisplayCondition = DisplayConditionGroup?.Entity;
DisplayConditionGroup?.Save();
SaveRenderElement();
}
@@ -691,14 +683,8 @@ namespace Artemis.Core
// Ensure the brush reference matches the brush
var current = General.BrushReference.CurrentValue;
if (current.BrushPluginGuid != descriptor.LayerBrushProvider.PluginInfo.Guid || current.BrushType != descriptor.LayerBrushType.Name)
{
General.BrushReference.CurrentValue = new LayerBrushReference
{
BrushPluginGuid = descriptor.LayerBrushProvider.PluginInfo.Guid,
BrushType = descriptor.LayerBrushType.Name
};
}
if (!descriptor.MatchesLayerBrushReference(current))
General.BrushReference.CurrentValue = new LayerBrushReference(descriptor);
ActivateLayerBrush();
}
@@ -746,7 +732,7 @@ namespace Artemis.Core
private void LayerBrushStoreOnLayerBrushRemoved(object sender, LayerBrushStoreEvent e)
{
if (LayerBrush.Descriptor == e.Registration.LayerBrushDescriptor)
if (LayerBrush?.Descriptor == e.Registration.LayerBrushDescriptor)
DeactivateLayerBrush();
}
@@ -17,5 +17,15 @@ namespace Artemis.Core
/// The full type name of the brush descriptor
/// </summary>
public string BrushType { get; set; }
public LayerBrushReference()
{
}
public LayerBrushReference(LayerBrushDescriptor descriptor)
{
BrushPluginGuid = descriptor.LayerBrushProvider.PluginInfo.Guid;
BrushType = descriptor.LayerBrushType.Name;
}
}
}
@@ -26,10 +26,5 @@ namespace Artemis.Core
/// Returns a list off all data binding registrations
/// </summary>
List<IDataBindingRegistration> GetAllDataBindingRegistrations();
/// <summary>
/// Gets or sets whether the property is hidden in the UI
/// </summary>
bool IsHidden { get; set; }
}
}
@@ -61,7 +61,9 @@ namespace Artemis.Core
private bool _isHidden;
/// <inheritdoc />
/// <summary>
/// Gets or sets whether the property is hidden in the UI
/// </summary>
public bool IsHidden
{
get => _isHidden;
@@ -318,13 +320,13 @@ namespace Artemis.Core
/// </summary>
public bool DataBindingsSupported { get; protected internal set; } = true;
public DataBindingRegistration<T, TProperty> GetDataBindingRegistration<TProperty>(string propertyName)
public DataBindingRegistration<T, TProperty> GetDataBindingRegistration<TProperty>(string expression)
{
if (_disposed)
throw new ObjectDisposedException("LayerProperty");
var match = _dataBindingRegistrations.FirstOrDefault(r => r is DataBindingRegistration<T, TProperty> registration &&
registration.Property.Name == propertyName);
registration.PropertyExpression.ToString() == expression);
return (DataBindingRegistration<T, TProperty>) match;
}
@@ -237,6 +237,10 @@ namespace Artemis.Core
if (instance == null)
throw new ArtemisPluginException($"Failed to create instance of layer property group at {path + propertyInfo.Name}");
// Ensure the description has a name, if not this is a good point to set it based on the property info
if (string.IsNullOrWhiteSpace(propertyGroupDescription.Name))
propertyGroupDescription.Name = propertyInfo.Name.Humanize();
instance.Parent = this;
instance.GroupDescription = propertyGroupDescription;
instance.LayerBrush = LayerBrush;
@@ -32,6 +32,10 @@ namespace Artemis.Core
DisplayContinuously = RenderElementEntity.DisplayContinuously;
AlwaysFinishTimeline = RenderElementEntity.AlwaysFinishTimeline;
DisplayConditionGroup = RenderElementEntity.RootDisplayCondition != null
? new DisplayConditionGroup(null, RenderElementEntity.RootDisplayCondition)
: new DisplayConditionGroup(null);
ActivateEffects();
}
@@ -49,7 +53,7 @@ namespace Artemis.Core
var layerEffectEntity = new LayerEffectEntity
{
Id = layerEffect.EntityId,
PluginGuid = layerEffect.PluginInfo.Guid,
PluginGuid = layerEffect.Descriptor.PlaceholderFor ?? layerEffect.PluginInfo.Guid,
EffectType = layerEffect.GetEffectTypeName(),
Name = layerEffect.Name,
Enabled = layerEffect.Enabled,
@@ -59,6 +63,10 @@ namespace Artemis.Core
RenderElementEntity.LayerEffects.Add(layerEffectEntity);
layerEffect.BaseProperties.ApplyToEntity();
}
// Conditions
RenderElementEntity.RootDisplayCondition = DisplayConditionGroup?.Entity;
DisplayConditionGroup?.Save();
}
#region Properties
@@ -204,7 +212,7 @@ namespace Artemis.Core
return (TimelinePosition - oldPosition).TotalSeconds;
}
/// <summary>
/// Overrides the progress of the element
/// </summary>
@@ -238,6 +246,8 @@ namespace Artemis.Core
Order = LayerEffects.Count + 1
};
descriptor.CreateInstance(this, entity);
OrderEffects();
OnLayerEffectsUpdated();
}
@@ -254,6 +264,12 @@ namespace Artemis.Core
effect.Dispose();
// Update the order on the remaining effects
OrderEffects();
OnLayerEffectsUpdated();
}
private void OrderEffects()
{
var index = 0;
foreach (var baseLayerEffect in LayerEffects.OrderBy(e => e.Order))
{
@@ -261,7 +277,7 @@ namespace Artemis.Core
index++;
}
OnLayerEffectsUpdated();
_layerEffects.Sort((a, b) => a.Order.CompareTo(b.Order));
}
internal void ActivateEffects()
@@ -270,7 +286,7 @@ namespace Artemis.Core
{
// If there is a non-placeholder existing effect, skip this entity
var existing = _layerEffects.FirstOrDefault(e => e.EntityId == layerEffectEntity.Id);
if (existing != null && !existing.Descriptor.IsPlaceHolder)
if (existing != null && existing.Descriptor.PlaceholderFor == null)
continue;
var descriptor = LayerEffectStore.Get(layerEffectEntity.PluginGuid, layerEffectEntity.EffectType)?.LayerEffectDescriptor;
@@ -282,37 +298,42 @@ namespace Artemis.Core
_layerEffects.Remove(existing);
existing.Dispose();
}
// Create an instance with the descriptor
descriptor.CreateInstance(this, layerEffectEntity);
}
else if (existing == null)
{
// If no descriptor was found and there was no existing placeholder, create a placeholder
descriptor = PlaceholderLayerEffectDescriptor.Create();
descriptor = PlaceholderLayerEffectDescriptor.Create(layerEffectEntity.PluginGuid);
descriptor.CreateInstance(this, layerEffectEntity);
}
}
OrderEffects();
}
internal void ActivateLayerEffect(BaseLayerEffect layerEffect)
{
_layerEffects.Add(layerEffect);
// Update the order on the effects
var index = 0;
foreach (var baseLayerEffect in LayerEffects.OrderBy(e => e.Order))
{
baseLayerEffect.Order = Order = index + 1;
index++;
}
OnLayerEffectsUpdated();
}
private void LayerEffectStoreOnLayerEffectRemoved(object? sender, LayerEffectStoreEvent e)
private void LayerEffectStoreOnLayerEffectRemoved(object sender, LayerEffectStoreEvent e)
{
throw new NotImplementedException();
// If effects provided by the plugin are on the element, replace them with placeholders
var pluginEffects = _layerEffects.Where(ef => ef.Descriptor.LayerEffectProvider != null &&
ef.PluginInfo.Guid == e.Registration.Plugin.PluginInfo.Guid).ToList();
foreach (var pluginEffect in pluginEffects)
{
var entity = RenderElementEntity.LayerEffects.First(en => en.Id == pluginEffect.EntityId);
_layerEffects.Remove(pluginEffect);
pluginEffect.Dispose();
var descriptor = PlaceholderLayerEffectDescriptor.Create(pluginEffect.PluginInfo.Guid);
descriptor.CreateInstance(this, entity);
}
}
private void LayerEffectStoreOnLayerEffectAdded(object sender, LayerEffectStoreEvent e)
@@ -359,6 +380,21 @@ namespace Artemis.Core
#endregion
#region IDisposable
protected override void Dispose(bool disposing)
{
LayerEffectStore.LayerEffectAdded -= LayerEffectStoreOnLayerEffectAdded;
LayerEffectStore.LayerEffectRemoved -= LayerEffectStoreOnLayerEffectRemoved;
foreach (var baseLayerEffect in LayerEffects)
baseLayerEffect.Dispose();
base.Dispose(disposing);
}
#endregion
#region Events
public event EventHandler LayerEffectsUpdated;