Data bindings - Further implemented default converters

This commit is contained in:
Robert committed 2020-09-07 23:43:43 +02:00
1 parent aebe3005e7
commit 6b309cb1f3
21 files changed
+447 -257

No files matched your search

@@ -32,7 +32,7 @@ namespace Artemis.Core
get => _baseValue;
set
{
if (value.GetType() != GetPropertyType())
if (value != null && value.GetType() != GetPropertyType())
throw new ArtemisCoreException("Cannot update base value because of a type mismatch");
_baseValue = value;
}
@@ -46,7 +46,7 @@ namespace Artemis.Core
get => _currentValue;
set
{
if (value.GetType() != GetPropertyType())
if (value != null && value.GetType() != GetPropertyType())
throw new ArtemisCoreException("Cannot update current value because of a type mismatch");
_currentValue = value;
}
@@ -61,7 +61,7 @@ namespace Artemis.Core
get => _defaultValue;
set
{
if (value.GetType() != GetPropertyType())
if (value != null && value.GetType() != GetPropertyType())
throw new ArtemisCoreException("Cannot update default value because of a type mismatch");
_defaultValue = value;
}
@@ -186,6 +186,9 @@ namespace Artemis.Core
/// <returns>The newly created data binding</returns>
public DataBinding EnableDataBinding(DataBindingRegistration dataBindingRegistration)
{
if (dataBindingRegistration.LayerProperty != this)
throw new ArtemisCoreException("Cannot enable a data binding using a data binding registration of a different layer property");
var dataBinding = new DataBinding(dataBindingRegistration);
_dataBindings.Add(dataBinding);
@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using Artemis.Storage.Entities.Profile;
using Newtonsoft.Json;
@@ -31,7 +32,7 @@ namespace Artemis.Core
/// </summary>
public new T BaseValue
{
get => (T) base.BaseValue;
get => base.BaseValue != null ? (T) base.BaseValue : default;
set
{
if (Equals(base.BaseValue, value))
@@ -47,7 +48,7 @@ namespace Artemis.Core
/// </summary>
public new T CurrentValue
{
get => (T) base.CurrentValue;
get => base.CurrentValue != null ? (T) base.CurrentValue : default;
set => base.CurrentValue = value;
}
@@ -57,7 +58,7 @@ namespace Artemis.Core
/// </summary>
public new T DefaultValue
{
get => (T) base.DefaultValue;
get => base.DefaultValue != null ? (T) base.DefaultValue : default;
set => base.DefaultValue = value;
}
@@ -311,14 +312,35 @@ namespace Artemis.Core
#region Data bindings
public void RegisterDataBindingProperty<TProperty>(Expression<Func<T, TProperty>> propertyLambda, IDataBindingConverter converter)
public void RegisterDataBindingProperty<TProperty>(Expression<Func<T, TProperty>> propertyLambda, DataBindingConverter converter)
{
var propertyInfo = ReflectionUtilities.GetPropertyInfo(CurrentValue, propertyLambda);
// If the lambda references to itself, use the property info of public new T CurrentValue
PropertyInfo propertyInfo;
string path = null;
if (propertyLambda.Parameters[0] == propertyLambda.Body)
{
propertyInfo = GetType().GetProperties().FirstOrDefault(p => p.Name == nameof(CurrentValue) && p.PropertyType == typeof(T));
}
else
{
propertyInfo = ReflectionUtilities.GetPropertyInfo(CurrentValue, propertyLambda);
// Deconstruct the lambda
var current = (MemberExpression) propertyLambda.Body;
path = current.Member.Name;
while (current.Expression is MemberExpression memberExpression)
{
path = current.Member.Name + "." + path;
current = memberExpression;
}
}
if (converter.SupportedType != propertyInfo.PropertyType)
{
throw new ArtemisCoreException($"Cannot register data binding property for property {propertyInfo.Name} " +
"because the provided converter does not support the property's type");
}
_dataBindingRegistrations.Add(new DataBindingRegistration(this, propertyInfo, converter));
_dataBindingRegistrations.Add(new DataBindingRegistration(this, propertyInfo, converter, path));
}
private void UpdateDataBindings(double deltaTime)