1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00

Dynamic data models - Rewrote the visualization to use leverage new core APIs

This commit is contained in:
SpoinkyNL 2020-10-04 22:06:21 +02:00
parent 26db794467
commit 8f552d0f71
22 changed files with 463 additions and 374 deletions

View File

@ -1,7 +1,9 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using Artemis.Core.DataModelExpansions;
namespace Artemis.Core
@ -11,22 +13,29 @@ namespace Artemis.Core
/// </summary>
public class DataModelPath
{
private readonly LinkedList<DataModelPathPart> _parts;
private readonly LinkedList<DataModelPathSegment> _segments;
internal DataModelPath(DataModel dataModel, string path)
/// <summary>
/// Creates a new instance of the <see cref="DataModelPath" /> class
/// </summary>
/// <param name="dataModel">The data model at which this path starts</param>
/// <param name="path">A string representation of the <see cref="DataModelPath" /></param>
public DataModelPath(object dataModel, string path)
{
DataModel = dataModel ?? throw new ArgumentNullException(nameof(dataModel));
Target = dataModel ?? throw new ArgumentNullException(nameof(dataModel));
Path = path ?? throw new ArgumentNullException(nameof(path));
DataModelGuid = dataModel.PluginInfo.Guid;
_parts = new LinkedList<DataModelPathPart>();
if (string.IsNullOrWhiteSpace(Path))
throw new ArgumentException("Path cannot be empty");
_segments = new LinkedList<DataModelPathSegment>();
Initialize(path);
}
/// <summary>
/// Gets the data model at which this path starts
/// </summary>
public DataModel DataModel { get; }
public object Target { get; }
/// <summary>
/// Gets a string representation of the <see cref="DataModelPath" />
@ -34,49 +43,111 @@ namespace Artemis.Core
public string Path { get; }
/// <summary>
/// Gets a boolean indicating whether all <see cref="Parts" /> are valid
/// Gets a boolean indicating whether all <see cref="Segments" /> are valid
/// </summary>
public bool IsValid => Parts.All(p => p.Type != DataModelPathPartType.Invalid);
public bool IsValid => Segments.All(p => p.Type != DataModelPathSegmentType.Invalid);
/// <summary>
/// Gets a read-only list of all parts of this path
/// Gets a read-only list of all segments of this path
/// </summary>
public IReadOnlyCollection<DataModelPathPart> Parts => _parts.ToList().AsReadOnly();
public IReadOnlyCollection<DataModelPathSegment> Segments => _segments.ToList().AsReadOnly();
internal Func<DataModel, object> Accessor { get; private set; }
/// <summary>
/// Gets a boolean indicating whether this data model path can have an inner path because it points to a list
/// </summary>
public bool CanHaveInnerPath => Segments.LastOrDefault()?.GetPropertyType()?.IsAssignableFrom(typeof(IList)) ?? false;
internal Guid DataModelGuid { get; set; }
/// <summary>
/// Gets the inner path of this path, only available if this path points to a list
/// </summary>
public DataModelPath InnerPath { get; internal set; }
internal Func<object, object> Accessor { get; private set; }
public void SetInnerPath(string path)
{
if (!CanHaveInnerPath)
{
var type = Segments.LastOrDefault()?.GetPropertyType();
throw new ArtemisCoreException($"Cannot set an inner path on a data model path if it does not point to a list (value is of type: {type?.Name})");
}
InnerPath = new DataModelPath(GetValue(), path);
}
/// <summary>
/// Gets the current value of the path
/// </summary>
public object GetValue()
{
return Accessor?.Invoke(Target);
}
/// <summary>
/// Gets the property info of the property this path points to
/// </summary>
/// <returns>If static, the property info. If dynamic, <c>null</c></returns>
public PropertyInfo GetPropertyInfo()
{
if (InnerPath != null)
return InnerPath.GetPropertyInfo();
return Segments.LastOrDefault()?.GetPropertyInfo();
}
/// <summary>
/// Gets the type of the property this path points to
/// </summary>
/// <returns>If possible, the property type</returns>
public Type GetPropertyType()
{
if (InnerPath != null)
return InnerPath.GetPropertyType();
return Segments.LastOrDefault()?.GetPropertyType();
}
/// <summary>
/// Gets the property description of the property this path points to
/// </summary>
/// <returns>If found, the data model property description</returns>
public DataModelPropertyAttribute GetPropertyDescription()
{
if (InnerPath != null)
return InnerPath.GetPropertyDescription();
return Segments.LastOrDefault()?.GetPropertyDescription();
}
/// <inheritdoc />
public override string ToString()
{
if (InnerPath != null)
return $"{Path} > {InnerPath}";
return Path;
}
private void Initialize(string path)
{
var parts = path.Split(".");
for (var index = 0; index < parts.Length; index++)
var segments = path.Split(".");
for (var index = 0; index < segments.Length; index++)
{
var identifier = parts[index];
var node = _parts.AddLast(new DataModelPathPart(this, identifier, string.Join('.', parts.Take(index + 1))));
var identifier = segments[index];
var node = _segments.AddLast(new DataModelPathSegment(this, identifier, string.Join('.', segments.Take(index + 1))));
node.Value.Node = node;
}
var parameter = Expression.Parameter(typeof(DataModel), "dm");
Expression expression = Expression.Convert(parameter, DataModel.GetType());
var parameter = Expression.Parameter(typeof(object), "t");
Expression expression = Expression.Convert(parameter, Target.GetType());
Expression nullCondition = null;
foreach (var part in _parts)
foreach (var segment in _segments)
{
var notNull = Expression.NotEqual(expression, Expression.Constant(null));
var notNull = Expression.NotEqual(expression, Expression.Default(expression.Type));
nullCondition = nullCondition != null ? Expression.AndAlso(nullCondition, notNull) : notNull;
expression = part.Initialize(parameter, expression, nullCondition);
expression = segment.Initialize(parameter, expression, nullCondition);
if (expression == null)
return;
}
Accessor = Expression.Lambda<Func<DataModel, object>>(
Accessor = Expression.Lambda<Func<object, object>>(
// Wrap with a null check
Expression.Condition(
nullCondition,

View File

@ -1,151 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using Artemis.Core.DataModelExpansions;
namespace Artemis.Core
{
/// <summary>
/// Represents a part of a data model path
/// </summary>
public class DataModelPathPart
{
internal DataModelPathPart(DataModelPath dataModelPath, string identifier, string path)
{
DataModelPath = dataModelPath;
Identifier = identifier;
Path = path;
}
/// <summary>
/// Gets the data model path this is a part of
/// </summary>
public DataModelPath DataModelPath { get; }
/// <summary>
/// Gets the identifier that is associated with this part
/// </summary>
public string Identifier { get; }
/// <summary>
/// Gets the path that leads to this part
/// </summary>
public string Path { get; }
/// <summary>
/// Gets the type of data model this part of the path points to
/// </summary>
public DataModelPathPartType Type { get; private set; }
/// <summary>
/// Gets the type of dynamic data model this path points to
/// <para>Not used if the <see cref="Type" /> is <see cref="DataModelPathPartType.Static" /></para>
/// </summary>
public Type DynamicDataModelType { get; private set; }
/// <summary>
/// Gets the previous part in the path
/// </summary>
public DataModelPathPart Previous => Node.Previous?.Value;
/// <summary>
/// Gets the next part in the path
/// </summary>
public DataModelPathPart Next => Node.Next?.Value;
internal Func<DataModel, object> Accessor { get; set; }
internal LinkedListNode<DataModelPathPart> Node { get; set; }
/// <summary>
/// Returns the current value of the path up to this part
/// </summary>
/// <returns></returns>
public object GetValue()
{
return Type == DataModelPathPartType.Invalid ? null : Accessor(DataModelPath.DataModel);
}
/// <inheritdoc />
public override string ToString()
{
return $"[{Type}] {Path}";
}
internal Expression Initialize(ParameterExpression parameter, Expression expression, Expression nullCondition)
{
var previousValue = Previous != null ? Previous.GetValue() : DataModelPath.DataModel;
if (previousValue == null)
{
Type = DataModelPathPartType.Invalid;
return null;
}
// Determine this part's type by looking for a dynamic data model with the identifier
if (previousValue is DataModel dataModel)
{
var hasDynamicDataModel = dataModel.DynamicDataModels.TryGetValue(Identifier, out var dynamicDataModel);
// If a dynamic data model is found the use that
if (hasDynamicDataModel)
DetermineDynamicType(dynamicDataModel);
// Otherwise look for a static type
else
DetermineStaticType(previousValue);
}
// Only data models can have dynamic types so if it is something else, its always going to be static
else
DetermineStaticType(previousValue);
return CreateExpression(parameter, expression, nullCondition);
}
private Expression CreateExpression(ParameterExpression parameter, Expression expression, Expression nullCondition)
{
if (Type == DataModelPathPartType.Invalid)
{
Accessor = null;
return null;
}
Expression accessorExpression;
// A static part just needs to access the property or filed
if (Type == DataModelPathPartType.Static)
accessorExpression = Expression.PropertyOrField(expression, Identifier);
// A dynamic part calls the generic method DataModel.DynamicChild<T> and provides the identifier as an argument
else
{
accessorExpression = Expression.Call(
expression,
nameof(DataModel.DynamicChild),
new[] {DynamicDataModelType},
Expression.Constant(Identifier)
);
}
Accessor = Expression.Lambda<Func<DataModel, object>>(
// Wrap with a null check
Expression.Condition(
nullCondition,
Expression.Convert(accessorExpression, typeof(object)),
Expression.Convert(Expression.Default(accessorExpression.Type), typeof(object))
),
parameter
).Compile();
return accessorExpression;
}
private void DetermineDynamicType(DataModel dynamicDataModel)
{
Type = DataModelPathPartType.Dynamic;
DynamicDataModelType = dynamicDataModel.GetType();
}
private void DetermineStaticType(object previous)
{
var previousType = previous.GetType();
var property = previousType.GetProperty(Identifier, BindingFlags.Public | BindingFlags.Instance);
Type = property == null ? DataModelPathPartType.Invalid : DataModelPathPartType.Static;
}
}
}

View File

@ -0,0 +1,208 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using Artemis.Core.DataModelExpansions;
using Humanizer;
namespace Artemis.Core
{
/// <summary>
/// Represents a segment of a data model path
/// </summary>
public class DataModelPathSegment
{
internal DataModelPathSegment(DataModelPath dataModelPath, string identifier, string path)
{
DataModelPath = dataModelPath;
Identifier = identifier;
Path = path;
}
/// <summary>
/// Gets the data model path this is a segment of
/// </summary>
public DataModelPath DataModelPath { get; }
/// <summary>
/// Gets the identifier that is associated with this segment
/// </summary>
public string Identifier { get; }
/// <summary>
/// Gets the path that leads to this segment
/// </summary>
public string Path { get; }
/// <summary>
/// Gets the type of data model this segment of the path points to
/// </summary>
public DataModelPathSegmentType Type { get; private set; }
/// <summary>
/// Gets the type of dynamic data model this path points to
/// <para>Not used if the <see cref="Type" /> is <see cref="DataModelPathSegmentType.Static" /></para>
/// </summary>
public Type DynamicDataModelType { get; private set; }
/// <summary>
/// Gets the previous segment in the path
/// </summary>
public DataModelPathSegment Previous => Node.Previous?.Value;
/// <summary>
/// Gets the next segment in the path
/// </summary>
public DataModelPathSegment Next => Node.Next?.Value;
internal Func<object, object> Accessor { get; set; }
internal LinkedListNode<DataModelPathSegment> Node { get; set; }
/// <summary>
/// Returns the current value of the path up to this segment
/// </summary>
/// <returns></returns>
public object GetValue()
{
return Type == DataModelPathSegmentType.Invalid ? null : Accessor(DataModelPath.Target);
}
/// <inheritdoc />
public override string ToString()
{
return $"[{Type}] {Path}";
}
/// <summary>
/// Gets the property info of the property this segment points to
/// </summary>
/// <returns>If static, the property info. If dynamic, <c>null</c></returns>
public PropertyInfo GetPropertyInfo()
{
// Dynamic types have no property and therefore no property info
if (Type == DataModelPathSegmentType.Dynamic)
return null;
// If this is the first segment in a path, the property is located on the data model
if (Previous == null)
return DataModelPath.Target.GetType().GetProperty(Identifier);
// If this is not the first segment in a path, the property is located on the previous segment
return Previous.GetValue()?.GetType().GetProperty(Identifier);
}
/// <summary>
/// Gets the property description of the property this segment points to
/// </summary>
/// <returns>If found, the data model property description</returns>
public DataModelPropertyAttribute GetPropertyDescription()
{
// Dynamic types have a data model description
if (Type == DataModelPathSegmentType.Dynamic)
return ((DataModel) GetValue())?.DataModelDescription;
var propertyInfo = GetPropertyInfo();
if (propertyInfo == null)
return null;
// Static types may have one as an attribute
return (DataModelPropertyAttribute) Attribute.GetCustomAttribute(propertyInfo, typeof(DataModelPropertyAttribute)) ??
new DataModelPropertyAttribute {Name = propertyInfo.Name.Humanize(), ResetsDepth = false};
}
/// <summary>
/// Gets the type of the property this path points to
/// </summary>
/// <returns>If possible, the property type</returns>
public Type GetPropertyType()
{
// Prefer basing the type on the property info
var propertyInfo = GetPropertyInfo();
var type = propertyInfo?.PropertyType;
// Property info is not available on dynamic paths though, so fall back on the current value
if (propertyInfo == null)
{
var currentValue = GetValue();
if (currentValue != null)
type = currentValue.GetType();
}
return type;
}
internal Expression Initialize(ParameterExpression parameter, Expression expression, Expression nullCondition)
{
var previousValue = Previous != null ? Previous.GetValue() : DataModelPath.Target;
if (previousValue == null)
{
Type = DataModelPathSegmentType.Invalid;
return null;
}
// Determine this segment's type by looking for a dynamic data model with the identifier
if (previousValue is DataModel dataModel)
{
var hasDynamicDataModel = dataModel.DynamicDataModels.TryGetValue(Identifier, out var dynamicDataModel);
// If a dynamic data model is found the use that
if (hasDynamicDataModel)
DetermineDynamicType(dynamicDataModel);
// Otherwise look for a static type
else
DetermineStaticType(previousValue);
}
// Only data models can have dynamic types so if it is something else, its always going to be static
else
DetermineStaticType(previousValue);
return CreateExpression(parameter, expression, nullCondition);
}
private Expression CreateExpression(ParameterExpression parameter, Expression expression, Expression nullCondition)
{
if (Type == DataModelPathSegmentType.Invalid)
{
Accessor = null;
return null;
}
Expression accessorExpression;
// A static segment just needs to access the property or filed
if (Type == DataModelPathSegmentType.Static)
accessorExpression = Expression.PropertyOrField(expression, Identifier);
// A dynamic segment calls the generic method DataModel.DynamicChild<T> and provides the identifier as an argument
else
{
accessorExpression = Expression.Call(
expression,
nameof(DataModel.DynamicChild),
new[] {DynamicDataModelType},
Expression.Constant(Identifier)
);
}
Accessor = Expression.Lambda<Func<object, object>>(
// Wrap with a null check
Expression.Condition(
nullCondition,
Expression.Convert(accessorExpression, typeof(object)),
Expression.Convert(Expression.Default(accessorExpression.Type), typeof(object))
),
parameter
).Compile();
return accessorExpression;
}
private void DetermineDynamicType(DataModel dynamicDataModel)
{
Type = DataModelPathSegmentType.Dynamic;
DynamicDataModelType = dynamicDataModel.GetType();
}
private void DetermineStaticType(object previous)
{
var previousType = previous.GetType();
var property = previousType.GetProperty(Identifier, BindingFlags.Public | BindingFlags.Instance);
Type = property == null ? DataModelPathSegmentType.Invalid : DataModelPathSegmentType.Static;
}
}
}

View File

@ -3,7 +3,7 @@
/// <summary>
/// Represents a type of data model path
/// </summary>
public enum DataModelPathPartType
public enum DataModelPathSegmentType
{
/// <summary>
/// Represents an invalid data model type that points to a missing data model

View File

@ -68,7 +68,7 @@ namespace Artemis.Core.DataModelExpansions
if (_dynamicDataModels.ContainsKey(key))
{
throw new ArtemisCoreException($"Cannot add a dynamic data model with key '{key}' " +
"because the key is already in use on this data model.");
"because the key is already in use on by another dynamic property this data model.");
}
if (_dynamicDataModels.ContainsValue(dynamicDataModel))
@ -78,6 +78,12 @@ namespace Artemis.Core.DataModelExpansions
$"because the dynamic data model is already added with key '{existingKey}.");
}
if (GetType().GetProperty(key) != null)
{
throw new ArtemisCoreException($"Cannot add a dynamic data model with key '{key}' " +
"because the key is already in use by a static property on this data model.");
}
dynamicDataModel.PluginInfo = PluginInfo;
dynamicDataModel.DataModelDescription = new DataModelPropertyAttribute
{

View File

@ -30,7 +30,7 @@
<Button Background="{Binding ButtonBrush}"
BorderBrush="{Binding ButtonBrush}"
Style="{StaticResource DataModelConditionButton}"
ToolTip="{Binding SelectedPropertyViewModel.DisplayPropertyPath}"
ToolTip="{Binding SelectedPropertyViewModel.DisplayPath}"
IsEnabled="{Binding IsEnabled}"
HorizontalAlignment="Left"
Click="PropertyButton_OnClick">

View File

@ -1,6 +1,5 @@
using System;
using System.Linq;
using System.Reflection;
using Artemis.Core;
using Artemis.Core.DataModelExpansions;
using Artemis.UI.Shared.Services;
@ -12,7 +11,7 @@ namespace Artemis.UI.Shared
private int _index;
private Type _listType;
public DataModelListPropertiesViewModel(DataModel dataModel, DataModelVisualizationViewModel parent, PropertyInfo propertyInfo) : base(dataModel, parent, propertyInfo)
public DataModelListPropertiesViewModel(DataModel dataModel, DataModelVisualizationViewModel parent, DataModelPath dataModelPath) : base(dataModel, parent, dataModelPath)
{
}
@ -34,9 +33,7 @@ namespace Artemis.UI.Shared
set => SetAndNotify(ref _displayValue, value);
}
public override string PropertyPath => null;
public override string DisplayPropertyPath => null;
public override string DisplayPath => null;
public override void Update(IDataModelUIService dataModelUIService)
{
@ -54,18 +51,5 @@ namespace Artemis.UI.Shared
{
return DisplayValue;
}
private void PopulateProperties(IDataModelUIService dataModelUIService)
{
if (Children.Any())
return;
foreach (var propertyInfo in ListType.GetProperties())
{
var child = CreateChild(dataModelUIService, propertyInfo, GetChildDepth());
if (child != null)
Children.Add(child);
}
}
}
}

View File

@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Artemis.Core;
using Artemis.Core.DataModelExpansions;
using Artemis.UI.Shared.Services;
@ -12,7 +10,7 @@ namespace Artemis.UI.Shared
private int _index;
private Type _listType;
public DataModelListPropertyViewModel(DataModel dataModel, DataModelVisualizationViewModel parent, PropertyInfo propertyInfo) : base(dataModel, parent, propertyInfo)
public DataModelListPropertyViewModel(DataModel dataModel, DataModelVisualizationViewModel parent, DataModelPath dataModelPath) : base(dataModel, parent, dataModelPath)
{
}

View File

@ -1,7 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using Artemis.Core;
using Artemis.Core.DataModelExpansions;
using Artemis.UI.Shared.Services;
@ -15,7 +13,7 @@ namespace Artemis.UI.Shared
private IList _list;
private DataModelVisualizationViewModel _listTypePropertyViewModel;
internal DataModelListViewModel(DataModel dataModel, DataModelVisualizationViewModel parent, PropertyInfo propertyInfo) : base(dataModel, parent, propertyInfo)
internal DataModelListViewModel(DataModel dataModel, DataModelVisualizationViewModel parent, DataModelPath dataModelPath) : base(dataModel, parent, dataModelPath)
{
ListChildren = new BindableCollection<DataModelVisualizationViewModel>();
}
@ -36,8 +34,12 @@ namespace Artemis.UI.Shared
public DataModelPropertiesViewModel GetListTypeViewModel(IDataModelUIService dataModelUIService)
{
var type = DataModelPath.GetPropertyType();
if (type == null)
return null;
// Create a property VM describing the type of the list
var viewModel = CreateListChild(dataModelUIService, PropertyInfo.PropertyType.GenericTypeArguments[0]);
var viewModel = CreateListChild(dataModelUIService, type.GenericTypeArguments[0]);
// Put an empty value into the list type property view model
if (viewModel is DataModelListPropertiesViewModel dataModelListClassViewModel)
@ -73,7 +75,7 @@ namespace Artemis.UI.Shared
DataModelVisualizationViewModel child;
if (ListChildren.Count <= index)
{
child = CreateListChild(dataModelUIService, item.GetType());
child = CreateListChild(dataModelUIService, item);
ListChildren.Add(child);
}
else
@ -100,18 +102,21 @@ namespace Artemis.UI.Shared
Count = $"{ListChildren.Count} {(ListChildren.Count == 1 ? "item" : "items")}";
}
protected DataModelVisualizationViewModel CreateListChild(IDataModelUIService dataModelUIService, Type listType)
protected DataModelVisualizationViewModel CreateListChild(IDataModelUIService dataModelUIService, object listItem)
{
var listType = listItem.GetType();
var path = new DataModelPath(listItem, "");
// If a display VM was found, prefer to use that in any case
var typeViewModel = dataModelUIService.GetDataModelDisplayViewModel(listType);
if (typeViewModel != null)
return new DataModelListPropertyViewModel(DataModel, this, PropertyInfo) {DisplayViewModel = typeViewModel};
return new DataModelListPropertyViewModel(DataModel, this, path) {DisplayViewModel = typeViewModel};
// For primitives, create a property view model, it may be null that is fine
if (listType.IsPrimitive || listType.IsEnum || listType == typeof(string))
return new DataModelListPropertyViewModel(DataModel, this, PropertyInfo) {ListType = listType};
return new DataModelListPropertyViewModel(DataModel, this, path) {ListType = listType};
// For other value types create a child view model
if (listType.IsClass || listType.IsStruct())
return new DataModelListPropertiesViewModel(DataModel, this, PropertyInfo) {ListType = listType};
return new DataModelListPropertiesViewModel(DataModel, this, path) {ListType = listType};
return null;
}

View File

@ -1,6 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Artemis.Core;
using Artemis.Core.DataModelExpansions;
using Artemis.UI.Shared.Services;
@ -8,7 +6,7 @@ namespace Artemis.UI.Shared
{
public class DataModelPropertiesViewModel : DataModelVisualizationViewModel
{
internal DataModelPropertiesViewModel(DataModel dataModel, DataModelVisualizationViewModel parent, PropertyInfo propertyInfo) : base(dataModel, parent, propertyInfo)
internal DataModelPropertiesViewModel(DataModel dataModel, DataModelVisualizationViewModel parent, DataModelPath dataModelPath) : base(dataModel, parent, dataModelPath)
{
}
@ -30,36 +28,9 @@ namespace Artemis.UI.Shared
return Parent.IsRootViewModel ? DataModel : base.GetCurrentValue();
}
protected int GetChildDepth()
protected override int GetChildDepth()
{
return PropertyDescription != null && !PropertyDescription.ResetsDepth ? Depth + 1 : 1;
}
private void PopulateProperties(IDataModelUIService dataModelUIService)
{
if (IsRootViewModel)
return;
// Add missing children
var modelType = Parent.IsRootViewModel ? DataModel.GetType() : PropertyInfo.PropertyType;
foreach (var propertyInfo in modelType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
if (Children.Any(c => c.PropertyInfo.Equals(propertyInfo)))
continue;
var child = CreateChild(dataModelUIService, propertyInfo, GetChildDepth());
if (child != null)
Children.Add(child);
}
// Remove children that should be hidden
var childList = new List<DataModelVisualizationViewModel>(Children);
var hiddenProperties = DataModel.GetHiddenProperties();
foreach (var dataModelVisualizationViewModel in childList)
{
if (hiddenProperties.Contains(dataModelVisualizationViewModel.PropertyInfo))
Children.Remove(dataModelVisualizationViewModel);
}
}
}
}

View File

@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Artemis.Core;
using Artemis.Core.DataModelExpansions;
using Artemis.UI.Shared.Services;
@ -11,8 +9,9 @@ namespace Artemis.UI.Shared
{
private object _displayValue;
private DataModelDisplayViewModel _displayViewModel;
private Type _displayValueType;
internal DataModelPropertyViewModel(DataModel dataModel, DataModelVisualizationViewModel parent, PropertyInfo propertyInfo) : base(dataModel, parent, propertyInfo)
internal DataModelPropertyViewModel(DataModel dataModel, DataModelVisualizationViewModel parent, DataModelPath dataModelPath) : base(dataModel, parent, dataModelPath)
{
}
@ -22,6 +21,12 @@ namespace Artemis.UI.Shared
set => SetAndNotify(ref _displayValue, value);
}
public Type DisplayValueType
{
get => _displayValueType;
set => SetAndNotify(ref _displayValueType, value);
}
public DataModelDisplayViewModel DisplayViewModel
{
get => _displayViewModel;
@ -34,12 +39,13 @@ namespace Artemis.UI.Shared
return;
if (DisplayViewModel == null)
DisplayViewModel = dataModelUIService.GetDataModelDisplayViewModel(PropertyInfo.PropertyType, true);
DisplayViewModel = dataModelUIService.GetDataModelDisplayViewModel(DataModelPath.GetPropertyType(), true);
DisplayValue = GetCurrentValue();
DisplayValueType = DataModelPath.GetPropertyType();
UpdateDisplayParameters();
}
protected void UpdateDisplayParameters()
{
DisplayViewModel?.UpdateValue(DisplayValue);

View File

@ -1,12 +1,10 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Artemis.Core;
using Artemis.Core.DataModelExpansions;
using Artemis.UI.Shared.Services;
using Humanizer;
using Stylet;
namespace Artemis.UI.Shared
@ -20,23 +18,25 @@ namespace Artemis.UI.Shared
private bool _isVisualizationExpanded;
private DataModelVisualizationViewModel _parent;
private DataModelPropertyAttribute _propertyDescription;
private PropertyInfo _propertyInfo;
internal DataModelVisualizationViewModel(DataModel dataModel, DataModelVisualizationViewModel parent, PropertyInfo propertyInfo)
internal DataModelVisualizationViewModel(DataModel dataModel, DataModelVisualizationViewModel parent, DataModelPath dataModelPath)
{
DataModel = dataModel;
PropertyInfo = propertyInfo;
Parent = parent;
DataModelPath = dataModelPath;
Children = new BindableCollection<DataModelVisualizationViewModel>();
IsMatchingFilteredTypes = true;
if (dataModel == null && parent == null && propertyInfo == null)
if (dataModel == null && parent == null && dataModelPath == null)
IsRootViewModel = true;
else
GetDescription();
PropertyDescription = DataModelPath?.GetPropertyDescription() ?? DataModel.DataModelDescription;
}
public bool IsRootViewModel { get; }
public DataModelPath DataModelPath { get; }
public string Path => DataModelPath?.Path;
public int Depth { get; set; }
public DataModel DataModel
@ -45,12 +45,6 @@ namespace Artemis.UI.Shared
set => SetAndNotify(ref _dataModel, value);
}
public PropertyInfo PropertyInfo
{
get => _propertyInfo;
protected set => SetAndNotify(ref _propertyInfo, value);
}
public DataModelPropertyAttribute PropertyDescription
{
get => _propertyDescription;
@ -85,35 +79,7 @@ namespace Artemis.UI.Shared
}
}
public virtual string PropertyPath
{
get
{
if (Parent == null)
return PropertyInfo?.Name;
if (PropertyInfo == null)
return Parent.PropertyPath;
var parentPath = Parent.PropertyPath;
return parentPath != null ? $"{parentPath}.{PropertyInfo.Name}" : PropertyInfo.Name;
}
}
public virtual string DisplayPropertyPath
{
get
{
if (Parent == null)
return PropertyDescription?.Name;
if (PropertyDescription == null)
return Parent.DisplayPropertyPath;
var parentPath = Parent.DisplayPropertyPath;
return parentPath != null ? $"{parentPath} {PropertyDescription.Name}" : PropertyDescription.Name;
}
}
public virtual string DisplayPath => Path.Replace(".", " ");
/// <summary>
/// Updates the datamodel and if in an parent, any children
@ -123,18 +89,10 @@ namespace Artemis.UI.Shared
public virtual object GetCurrentValue()
{
try
{
if (PropertyInfo.GetGetMethod() == null)
return null;
return Parent?.GetCurrentValue() == null ? null : PropertyInfo.GetValue(Parent.GetCurrentValue());
}
catch (Exception)
{
// ignored, who knows what kind of shit can go wrong here...
if (IsRootViewModel)
return null;
}
return DataModelPath.GetValue();
}
public void ApplyTypeFilter(bool looseMatch, params Type[] filteredTypes)
@ -164,17 +122,18 @@ namespace Artemis.UI.Shared
return;
}
// If this VM has no property info, assume it does not match
if (PropertyInfo == null)
// If the type couldn't be retrieved either way, assume false
var type = DataModelPath.GetPropertyType();
if (type == null)
{
IsMatchingFilteredTypes = false;
return;
}
if (looseMatch)
IsMatchingFilteredTypes = filteredTypes.Any(t => t.IsCastableFrom(PropertyInfo.PropertyType) || t == typeof(Enum) && PropertyInfo.PropertyType.IsEnum);
IsMatchingFilteredTypes = filteredTypes.Any(t => t.IsCastableFrom(type) || t == typeof(Enum) && type.IsEnum);
else
IsMatchingFilteredTypes = filteredTypes.Any(t => t == PropertyInfo.PropertyType || t == typeof(Enum) && PropertyInfo.PropertyType.IsEnum);
IsMatchingFilteredTypes = filteredTypes.Any(t => t == type || t == typeof(Enum) && type.IsEnum);
}
public DataModelVisualizationViewModel GetChildForCondition(DataModelConditionPredicate predicate, DataModelConditionSide side)
@ -194,8 +153,12 @@ namespace Artemis.UI.Shared
public DataModelVisualizationViewModel GetChildByPath(Guid dataModelGuid, string propertyPath)
{
if (DataModel.PluginInfo.Guid != dataModelGuid)
return null;
if (propertyPath == null)
return null;
if (Path.StartsWith(propertyPath))
return null;
// Ensure children are populated by requesting an update
if (!IsVisualizationExpanded)
@ -205,50 +168,107 @@ namespace Artemis.UI.Shared
IsVisualizationExpanded = false;
}
var path = propertyPath.Split(".");
var currentPart = path.First();
if (IsRootViewModel)
foreach (var child in Children)
{
var child = Children.FirstOrDefault(c => c.DataModel != null &&
c.DataModel.PluginInfo.Guid == dataModelGuid);
return child?.GetChildByPath(dataModelGuid, propertyPath);
}
else
{
var child = Children.FirstOrDefault(c => c.DataModel != null &&
c.DataModel.PluginInfo.Guid == dataModelGuid && c.PropertyInfo?.Name == currentPart);
if (child == null)
return null;
// Try the child itself first
if (child.Path == propertyPath)
return child;
if (path.Length > 1)
return child.GetChildByPath(dataModelGuid, string.Join(".", path.Skip(1)));
return child;
// Try a child on the child next, this will go recursive
var match = child.GetChildByPath(dataModelGuid, propertyPath);
if (match != null)
return match;
}
return null;
}
protected DataModelVisualizationViewModel CreateChild(IDataModelUIService dataModelUIService, PropertyInfo propertyInfo, int depth)
protected virtual int GetChildDepth()
{
return 0;
}
protected void PopulateProperties(IDataModelUIService dataModelUIService)
{
if (IsRootViewModel)
return;
// Add missing static children
var modelType = Parent.IsRootViewModel ? DataModel.GetType() : DataModelPath.GetPropertyType();
foreach (var propertyInfo in modelType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
var childPath = Path != null ? $"{Path}.{propertyInfo.Name}" : propertyInfo.Name;
if (Children.Any(c => c.Path != null && c.Path.Equals(childPath)))
continue;
var child = CreateChild(dataModelUIService, childPath, GetChildDepth());
if (child != null)
Children.Add(child);
}
// Remove static children that should be hidden
var hiddenProperties = DataModel.GetHiddenProperties();
foreach (var hiddenProperty in hiddenProperties)
{
var childPath = Path != null ? $"{Path}.{hiddenProperty.Name}" : hiddenProperty.Name;
var toRemove = Children.FirstOrDefault(c => c.Path != null && c.Path == childPath);
if (toRemove != null)
Children.Remove(toRemove);
}
// Add missing dynamic children
var value = Parent.IsRootViewModel ? DataModel : DataModelPath.GetValue();
if (value is DataModel dataModel)
{
foreach (var kvp in dataModel.DynamicDataModels)
{
var childPath = Path != null ? $"{Path}.{kvp.Key}" : kvp.Key;
if (Children.Any(c => c.Path != null && c.Path.Equals(childPath)))
continue;
var child = CreateChild(dataModelUIService, childPath, GetChildDepth());
if (child != null)
Children.Add(child);
}
}
// Remove dynamic children that have been removed from the data model
var toRemoveDynamic = Children.Where(c => !c.DataModelPath.IsValid).ToList();
if (toRemoveDynamic.Any())
Children.RemoveRange(toRemoveDynamic);
}
protected DataModelVisualizationViewModel CreateChild(IDataModelUIService dataModelUIService, string path, int depth)
{
if (depth > MaxDepth)
return null;
var dataModelPath = new DataModelPath(DataModel, path);
if (!dataModelPath.IsValid)
return null;
var propertyInfo = dataModelPath.GetPropertyInfo();
var propertyType = dataModelPath.GetPropertyType();
// Skip properties decorated with DataModelIgnore
if (Attribute.IsDefined(propertyInfo, typeof(DataModelIgnoreAttribute)))
if (propertyInfo != null && Attribute.IsDefined(propertyInfo, typeof(DataModelIgnoreAttribute)))
return null;
// Skip properties that are in the ignored properties list of the respective profile module/data model expansion
if (DataModel.GetHiddenProperties().Any(p => p.Equals(propertyInfo)))
return null;
// If a display VM was found, prefer to use that in any case
var typeViewModel = dataModelUIService.GetDataModelDisplayViewModel(propertyInfo.PropertyType);
var typeViewModel = dataModelUIService.GetDataModelDisplayViewModel(propertyType);
if (typeViewModel != null)
return new DataModelPropertyViewModel(DataModel, this, propertyInfo) {DisplayViewModel = typeViewModel, Depth = depth};
return new DataModelPropertyViewModel(DataModel, this, dataModelPath) {DisplayViewModel = typeViewModel, Depth = depth};
// For primitives, create a property view model, it may be null that is fine
if (propertyInfo.PropertyType.IsPrimitive || propertyInfo.PropertyType.IsEnum || propertyInfo.PropertyType == typeof(string))
return new DataModelPropertyViewModel(DataModel, this, propertyInfo) {Depth = depth};
if (typeof(IList).IsAssignableFrom(propertyInfo.PropertyType))
return new DataModelListViewModel(DataModel, this, propertyInfo) {Depth = depth};
if (propertyType.IsPrimitive || propertyType.IsEnum || propertyType == typeof(string))
return new DataModelPropertyViewModel(DataModel, this, dataModelPath) {Depth = depth};
if (typeof(IList).IsAssignableFrom(propertyType))
return new DataModelListViewModel(DataModel, this, dataModelPath) {Depth = depth};
// For other value types create a child view model
if (propertyInfo.PropertyType.IsClass || propertyInfo.PropertyType.IsStruct())
return new DataModelPropertiesViewModel(DataModel, this, propertyInfo) {Depth = depth};
if (propertyType.IsClass || propertyType.IsStruct())
return new DataModelPropertiesViewModel(DataModel, this, dataModelPath) {Depth = depth};
return null;
}
@ -259,25 +279,6 @@ namespace Artemis.UI.Shared
OnUpdateRequested();
}
private void GetDescription()
{
// If this is the first child of a root view model, use the data model description
if (Parent.IsRootViewModel)
PropertyDescription = DataModel?.DataModelDescription;
// Rely on property info for the description
else if (PropertyInfo != null)
{
PropertyDescription = (DataModelPropertyAttribute) Attribute.GetCustomAttribute(PropertyInfo, typeof(DataModelPropertyAttribute)) ??
new DataModelPropertyAttribute {Name = PropertyInfo.Name.Humanize(), ResetsDepth = false};
}
else
throw new ArtemisSharedUIException("Failed to get property description because plugin info is null but the parent has a datamodel");
// If a property description was provided but the name is null, use the humanized property name
if (PropertyDescription != null && PropertyDescription.Name == null && PropertyInfo != null)
PropertyDescription.Name = PropertyInfo.Name.Humanize();
}
#region Events
public event EventHandler UpdateRequested;

View File

@ -1,8 +0,0 @@
using Artemis.Core.DataModelExpansions;
namespace Artemis.UI.Shared.Services
{
public class ListDataModelWrapper : DataModel
{
}
}

View File

@ -135,7 +135,7 @@ namespace Artemis.UI.Screens.ProfileEditor.Conditions
var leftSideType = _isPrimitiveList
? DataModelConditionListPredicate.DataModelConditionList.ListType
: LeftSideSelectionViewModel.SelectedPropertyViewModel?.PropertyInfo?.PropertyType;
: LeftSideSelectionViewModel.SelectedPropertyViewModel?.DataModelPath?.GetPropertyType();
// Get the supported operators
Operators.Clear();
@ -192,7 +192,7 @@ namespace Artemis.UI.Screens.ProfileEditor.Conditions
public void ApplyLeftSide()
{
DataModelConditionListPredicate.UpdateLeftSide(LeftSideSelectionViewModel.SelectedPropertyViewModel.PropertyPath);
DataModelConditionListPredicate.UpdateLeftSide(LeftSideSelectionViewModel.SelectedPropertyViewModel.Path);
_profileEditorService.UpdateSelectedProfileElement();
SelectedOperator = DataModelConditionListPredicate.Operator;
@ -205,12 +205,12 @@ namespace Artemis.UI.Screens.ProfileEditor.Conditions
{
DataModelConditionListPredicate.UpdateRightSideDynamic(
RightSideSelectionViewModel.SelectedPropertyViewModel.DataModel,
RightSideSelectionViewModel.SelectedPropertyViewModel.PropertyPath
RightSideSelectionViewModel.SelectedPropertyViewModel.Path
);
}
else if (DataModelConditionListPredicate.PredicateType == ListRightSideType.DynamicList)
{
DataModelConditionListPredicate.UpdateRightSideDynamic(RightSideSelectionViewModel.SelectedPropertyViewModel.PropertyPath);
DataModelConditionListPredicate.UpdateRightSideDynamic(RightSideSelectionViewModel.SelectedPropertyViewModel.Path);
}
_profileEditorService.UpdateSelectedProfileElement();

View File

@ -97,7 +97,7 @@ namespace Artemis.UI.Screens.ProfileEditor.Conditions
{
DataModelConditionList.UpdateList(
TargetSelectionViewModel.SelectedPropertyViewModel.DataModel,
TargetSelectionViewModel.SelectedPropertyViewModel.PropertyPath
TargetSelectionViewModel.SelectedPropertyViewModel.Path
);
_profileEditorService.UpdateSelectedProfileElement();

View File

@ -120,7 +120,7 @@ namespace Artemis.UI.Screens.ProfileEditor.Conditions
DataModelConditionPredicate.LeftDataModel,
DataModelConditionPredicate.LeftPropertyPath
);
var leftSideType = LeftSideSelectionViewModel.SelectedPropertyViewModel?.PropertyInfo?.PropertyType;
var leftSideType = LeftSideSelectionViewModel.SelectedPropertyViewModel?.DataModelPath?.GetPropertyType();
// Get the supported operators
Operators.Clear();
@ -135,7 +135,7 @@ namespace Artemis.UI.Screens.ProfileEditor.Conditions
}
// Ensure the right side has the proper VM
var targetType = LeftSideSelectionViewModel?.SelectedPropertyViewModel?.PropertyInfo?.PropertyType;
var targetType = LeftSideSelectionViewModel?.SelectedPropertyViewModel?.DataModelPath?.GetPropertyType();
if (DataModelConditionPredicate.PredicateType == ProfileRightSideType.Dynamic && SelectedOperator.SupportsRightSide)
{
DisposeRightSideStatic();
@ -172,7 +172,7 @@ namespace Artemis.UI.Screens.ProfileEditor.Conditions
{
DataModelConditionPredicate.UpdateLeftSide(
LeftSideSelectionViewModel.SelectedPropertyViewModel.DataModel,
LeftSideSelectionViewModel.SelectedPropertyViewModel.PropertyPath
LeftSideSelectionViewModel.SelectedPropertyViewModel.Path
);
_profileEditorService.UpdateSelectedProfileElement();
@ -184,7 +184,7 @@ namespace Artemis.UI.Screens.ProfileEditor.Conditions
{
DataModelConditionPredicate.UpdateRightSide(
RightSideSelectionViewModel.SelectedPropertyViewModel.DataModel,
RightSideSelectionViewModel.SelectedPropertyViewModel.PropertyPath
RightSideSelectionViewModel.SelectedPropertyViewModel.Path
);
_profileEditorService.UpdateSelectedProfileElement();

View File

@ -104,7 +104,7 @@ namespace Artemis.UI.Screens.ProfileEditor.LayerProperties.DataBindings.DirectDa
private void ParameterSelectionViewModelOnPropertySelected(object sender, DataModelInputDynamicEventArgs e)
{
Modifier.UpdateParameter(e.DataModelVisualizationViewModel.DataModel, e.DataModelVisualizationViewModel.PropertyPath);
Modifier.UpdateParameter(e.DataModelVisualizationViewModel.DataModel, e.DataModelVisualizationViewModel.Path);
_profileEditorService.UpdateSelectedProfileElement();
}

View File

@ -111,7 +111,7 @@ namespace Artemis.UI.Screens.ProfileEditor.LayerProperties.DataBindings.DirectDa
private void TargetSelectionViewModelOnPropertySelected(object sender, DataModelInputDynamicEventArgs e)
{
DirectDataBinding.UpdateSource(e.DataModelVisualizationViewModel.DataModel, e.DataModelVisualizationViewModel.PropertyPath);
DirectDataBinding.UpdateSource(e.DataModelVisualizationViewModel.DataModel, e.DataModelVisualizationViewModel.Path);
Update();
_profileEditorService.UpdateSelectedProfileElement();

View File

@ -89,7 +89,7 @@
<!-- Value description -->
<TextBlock Grid.Column="0" Margin="0 0 5 0" FontWeight="Bold">
<Run>[</Run><Run Text="{Binding PropertyInfo.PropertyType.Name, Mode=OneWay}" /><Run>]</Run>
<Run>[</Run><Run Text="{Binding DisplayValueType.Name, Mode=OneWay}" /><Run>]</Run>
</TextBlock>
<TextBlock Grid.Column="1" Text="{Binding PropertyDescription.Name}" ToolTip="{Binding PropertyDescription.Description}" />

View File

@ -97,7 +97,10 @@ namespace Artemis.UI.Screens.Settings.Debug.Tabs
private void OnUpdateTimerOnElapsed(object sender, ElapsedEventArgs args)
{
MainDataModel.Update(_dataModelUIService);
lock (MainDataModel)
{
MainDataModel.Update(_dataModelUIService);
}
}
private void GetDataModel()

View File

@ -13,8 +13,6 @@ namespace Artemis.Plugins.DataModelExpansions.TestData
{
_rand = new Random();
AddTimedUpdate(TimeSpan.FromSeconds(1), TimedUpdate);
DataModel.AddDynamicChild(new DynamicDataModel(), "Dynamic1", "Dynamic data model 1");
}
public override void DisablePlugin()
@ -25,9 +23,6 @@ namespace Artemis.Plugins.DataModelExpansions.TestData
{
// You can access your data model here and update it however you like
DataModel.TemplateDataModelString = $"The last delta time was {deltaTime} seconds";
var dynamic = DataModel.DynamicChild<DynamicDataModel>("Dynamic1")?.DynamicString;
var dynamic2 = DataModel.DynamicChild<DynamicDataModel>("Dynamic2")?.DynamicString;
}
private void TimedUpdate(double deltaTime)

Binary file not shown.

After

Width:  |  Height:  |  Size: 332 KiB