Data bindings - Simplified getter/setter code

This commit is contained in:
Robert committed 2021-03-02 21:41:56 +01:00
1 parent 267e8e6a1e
commit f4ad41cbd4
38 files changed
+86 -89

No files matched your search

@@ -6,7 +6,7 @@
internal BoolLayerProperty()
{
KeyframesSupported = false;
RegisterDataBindingProperty(value => value, (_, newValue) => CurrentValue = newValue, new GeneralDataBindingConverter<bool>(), "Value");
RegisterDataBindingProperty(() => CurrentValue, value => CurrentValue = value, new GeneralDataBindingConverter<bool>(), "Value");
}
/// <summary>
@@ -23,14 +23,12 @@ namespace Artemis.Core
for (int index = 0; index < CurrentValue.Stops.Count; index++)
{
int stopIndex = index;
DataBindingRegistration<ColorGradient, SKColor> registerDataBindingProperty = RegisterDataBindingProperty(
gradient => gradient.Stops[stopIndex].Color,
(gradient, value) => gradient.Stops[stopIndex].Color = value,
RegisterDataBindingProperty(
() => CurrentValue.Stops[stopIndex].Color,
value => CurrentValue.Stops[stopIndex].Color = value,
new ColorStopDataBindingConverter(),
$"Color #{stopIndex + 1}"
);
registerDataBindingProperty.DisplayName = $"Color #{stopIndex + 1}";
}
}
@@ -5,7 +5,7 @@
{
internal FloatLayerProperty()
{
RegisterDataBindingProperty(value => value, (_, newValue) => CurrentValue = newValue, new FloatDataBindingConverter(), "Value");
RegisterDataBindingProperty(() => CurrentValue, value => CurrentValue = value, new FloatDataBindingConverter(), "Value");
}
/// <summary>
@@ -5,9 +5,9 @@
{
internal FloatRangeLayerProperty()
{
RegisterDataBindingProperty(value => value.Start, (value, newValue) => value.Start = newValue, new FloatDataBindingConverter<FloatRange>(), "Start");
RegisterDataBindingProperty(value => value.End, (value, newValue) => value.End = newValue, new FloatDataBindingConverter<FloatRange>(), "End");
RegisterDataBindingProperty(() => CurrentValue.Start, value => CurrentValue.Start = value, new FloatDataBindingConverter<FloatRange>(), "Start");
RegisterDataBindingProperty(() => CurrentValue.End, value => CurrentValue.End = value, new FloatDataBindingConverter<FloatRange>(), "End");
CurrentValueSet += OnCurrentValueSet;
}
@@ -7,7 +7,7 @@ namespace Artemis.Core
{
internal IntLayerProperty()
{
RegisterDataBindingProperty(value => value, (_, newValue) => CurrentValue = newValue, new IntDataBindingConverter(), "Value");
RegisterDataBindingProperty(() => CurrentValue, value => CurrentValue = value, new IntDataBindingConverter(), "Value");
}
/// <summary>
@@ -5,8 +5,8 @@
{
internal IntRangeLayerProperty()
{
RegisterDataBindingProperty(value => value.Start, (value, newValue) => value.Start = newValue, new IntDataBindingConverter<IntRange>(), "Start");
RegisterDataBindingProperty(value => value.End, (value, newValue) => value.End = newValue, new IntDataBindingConverter<IntRange>(), "End");
RegisterDataBindingProperty(() => CurrentValue.Start, value => CurrentValue.Start = value, new IntDataBindingConverter<IntRange>(), "Start");
RegisterDataBindingProperty(() => CurrentValue.End, value => CurrentValue.End = value, new IntDataBindingConverter<IntRange>(), "End");
CurrentValueSet += OnCurrentValueSet;
}
@@ -7,7 +7,7 @@ namespace Artemis.Core
{
internal SKColorLayerProperty()
{
RegisterDataBindingProperty(value => value, (_, newValue) => CurrentValue = newValue, new SKColorDataBindingConverter(), "Value");
RegisterDataBindingProperty(() => CurrentValue, value => CurrentValue = value, new SKColorDataBindingConverter(), "Value");
}
/// <summary>
@@ -7,9 +7,8 @@ namespace Artemis.Core
{
internal SKPointLayerProperty()
{
RegisterDataBindingProperty(value => value.X, (value, newValue) => value.X = newValue, new FloatDataBindingConverter<SKPoint>(), "X");
RegisterDataBindingProperty(value => value.Y, (value, newValue) => value.Y = newValue, new FloatDataBindingConverter<SKPoint>(), "Y");
RegisterDataBindingProperty(() => CurrentValue.X, value => CurrentValue = new SKPoint(value, CurrentValue.Y), new FloatDataBindingConverter<SKPoint>(), "X");
RegisterDataBindingProperty(() => CurrentValue.Y, value => CurrentValue = new SKPoint(CurrentValue.X, value), new FloatDataBindingConverter<SKPoint>(), "Y");
}
/// <summary>
@@ -7,8 +7,8 @@ namespace Artemis.Core
{
internal SKSizeLayerProperty()
{
RegisterDataBindingProperty(value => value.Height, (value, newValue) => value.Height = newValue, new FloatDataBindingConverter<SKSize>(), "Height");
RegisterDataBindingProperty(value => value.Width, (value, newValue) => value.Width = newValue, new FloatDataBindingConverter<SKSize>(), "Width");
RegisterDataBindingProperty(() => CurrentValue.Width, (value) => CurrentValue = new SKSize(value, CurrentValue.Height), new FloatDataBindingConverter<SKSize>(), "Width");
RegisterDataBindingProperty(() => CurrentValue.Height, (value) => CurrentValue = new SKSize(CurrentValue.Width, value), new FloatDataBindingConverter<SKSize>(), "Height");
}
/// <summary>
@@ -272,7 +272,7 @@ namespace Artemis.Core
throw new ObjectDisposedException("DataBinding");
// General
DataBindingRegistration<TLayerProperty, TProperty>? registration = LayerProperty.GetDataBindingRegistration<TProperty>(Entity.TargetExpression);
DataBindingRegistration<TLayerProperty, TProperty>? registration = LayerProperty.GetDataBindingRegistration<TProperty>(Entity.Identifier);
if (registration != null)
ApplyRegistration(registration);
@@ -293,7 +293,7 @@ namespace Artemis.Core
// Don't save an invalid state
if (Registration != null)
Entity.TargetExpression = Registration.Getter.ToString();
Entity.Identifier = Registration.DisplayName;
Entity.EasingTime = EasingTime;
Entity.EasingFunction = (int) EasingFunction;
@@ -47,7 +47,7 @@ namespace Artemis.Core
{
if (DataBinding?.Registration == null)
throw new ArtemisCoreException("Data binding converter is not yet initialized");
DataBinding.Registration.Setter(DataBinding.LayerProperty.CurrentValue, value);
DataBinding.Registration.Setter(value);
}
/// <summary>
@@ -57,7 +57,7 @@ namespace Artemis.Core
{
if (DataBinding?.Registration == null)
throw new ArtemisCoreException("Data binding converter is not yet initialized");
return DataBinding.Registration.Getter(DataBinding.LayerProperty.CurrentValue);
return DataBinding.Registration.Getter();
}
/// <summary>
@@ -8,9 +8,7 @@ namespace Artemis.Core
public class DataBindingRegistration<TLayerProperty, TProperty> : IDataBindingRegistration
{
internal DataBindingRegistration(LayerProperty<TLayerProperty> layerProperty, DataBindingConverter<TLayerProperty, TProperty> converter,
Func<TLayerProperty, TProperty> getter,
Action<TLayerProperty, TProperty> setter,
string displayName)
Func<TProperty> getter, Action<TProperty> setter, string displayName)
{
LayerProperty = layerProperty ?? throw new ArgumentNullException(nameof(layerProperty));
Converter = converter ?? throw new ArgumentNullException(nameof(converter));
@@ -32,17 +30,15 @@ namespace Artemis.Core
/// <summary>
/// Gets the function to call to get the value of the property
/// </summary>
public Func<TLayerProperty, TProperty> Getter { get; }
public Func<TProperty> Getter { get; }
/// <summary>
/// Gets the action to call to set the value of the property
/// </summary>
public Action<TLayerProperty, TProperty> Setter { get; }
public Action<TProperty> Setter { get; }
/// <summary>
/// Gets or sets the display name of the data binding registration
/// </summary>
public string DisplayName { get; set; }
/// <inheritdoc />
public string DisplayName { get; }
/// <summary>
/// Gets the data binding created using this registration
@@ -61,7 +57,7 @@ namespace Artemis.Core
if (DataBinding != null)
return DataBinding;
DataBindingEntity? dataBinding = LayerProperty.Entity.DataBindingEntities.FirstOrDefault(e => e.TargetExpression == Getter.ToString());
DataBindingEntity? dataBinding = LayerProperty.Entity.DataBindingEntities.FirstOrDefault(e => e.Identifier == DisplayName);
if (dataBinding == null)
return null;
@@ -5,6 +5,11 @@
/// </summary>
public interface IDataBindingRegistration
{
/// <summary>
/// Gets or sets the display name of the data binding registration
/// </summary>
string DisplayName { get; }
/// <summary>
/// Returns the data binding applied using this registration
/// </summary>
@@ -379,21 +379,16 @@ namespace Artemis.Core
public bool HasDataBinding => GetAllDataBindingRegistrations().Any(r => r.GetDataBinding() != null);
/// <summary>
/// Gets a data binding registration by the expression used to register it
/// Gets a data binding registration by the display name used to register it
/// <para>Note: The expression must exactly match the one used to register the data binding</para>
/// </summary>
public DataBindingRegistration<T, TProperty>? GetDataBindingRegistration<TProperty>(Expression<Func<T, TProperty>> propertyExpression)
{
return GetDataBindingRegistration<TProperty>(propertyExpression.ToString());
}
internal DataBindingRegistration<T, TProperty>? GetDataBindingRegistration<TProperty>(string expression)
public DataBindingRegistration<T, TProperty>? GetDataBindingRegistration<TProperty>(string identifier)
{
if (_disposed)
throw new ObjectDisposedException("LayerProperty");
IDataBindingRegistration? match = _dataBindingRegistrations.FirstOrDefault(r => r is DataBindingRegistration<T, TProperty> registration &&
registration.Getter.ToString() == expression);
registration.DisplayName == identifier);
return (DataBindingRegistration<T, TProperty>?) match;
}
@@ -414,13 +409,15 @@ namespace Artemis.Core
/// <param name="setter">The action to call to set the value of the property</param>
/// <param name="converter">The converter to use while applying the data binding</param>
/// <param name="displayName">The display name of the data binding property</param>
public DataBindingRegistration<T, TProperty> RegisterDataBindingProperty<TProperty>(Func<T, TProperty> getter, Action<T, TProperty> setter, DataBindingConverter<T, TProperty> converter,
public DataBindingRegistration<T, TProperty> RegisterDataBindingProperty<TProperty>(Func<TProperty> getter, Action<TProperty> setter, DataBindingConverter<T, TProperty> converter,
string displayName)
{
if (_disposed)
throw new ObjectDisposedException("LayerProperty");
if (_dataBindingRegistrations.Any(d => d.DisplayName == displayName))
throw new ArtemisCoreException($"A databinding property named '{displayName}' is already registered.");
DataBindingRegistration<T, TProperty> registration = new(this, converter, getter, setter, displayName);
DataBindingRegistration<T, TProperty> registration = new(this, converter, getter, setter, displayName);
_dataBindingRegistrations.Add(registration);
return registration;
}