diff --git a/src/Artemis.Core/Plugins/Abstract/DataModels/DataModel.cs b/src/Artemis.Core/Plugins/Abstract/DataModels/DataModel.cs index 107f423f9..f96b551da 100644 --- a/src/Artemis.Core/Plugins/Abstract/DataModels/DataModel.cs +++ b/src/Artemis.Core/Plugins/Abstract/DataModels/DataModel.cs @@ -11,20 +11,6 @@ namespace Artemis.Core.Plugins.Abstract.DataModels { public abstract class DataModel { - private static readonly List SupportedTypes = new List - { - typeof(bool), - typeof(byte), - typeof(decimal), - typeof(double), - typeof(float), - typeof(int), - typeof(long), - typeof(string), - typeof(SKColor), - typeof(SKPoint) - }; - /// /// Gets the plugin info this data model belongs to /// @@ -62,31 +48,19 @@ namespace Artemis.Core.Plugins.Abstract.DataModels foreach (var propertyInfo in GetType().GetProperties()) { var dataModelPropertyAttribute = (DataModelPropertyAttribute) Attribute.GetCustomAttribute(propertyInfo, typeof(DataModelPropertyAttribute)); - if (dataModelPropertyAttribute == null) + if (dataModelPropertyAttribute == null || !typeof(DataModel).IsAssignableFrom(propertyInfo.PropertyType)) continue; - // If the a nested datamodel create an instance and initialize it - if (typeof(DataModel).IsAssignableFrom(propertyInfo.PropertyType)) - { - var instance = (DataModel) Activator.CreateInstance(propertyInfo.PropertyType, true); - if (instance == null) - throw new ArtemisCoreException($"Failed to create instance of child datamodel at {propertyInfo.Name}"); + // If the property is a nested datamodel create an instance and initialize it + var instance = (DataModel) Activator.CreateInstance(propertyInfo.PropertyType, true); + if (instance == null) + throw new ArtemisCoreException($"Failed to create instance of child datamodel at {propertyInfo.Name}"); - instance.PluginInfo = PluginInfo; - instance.DataModelDescription = dataModelPropertyAttribute; - instance.Initialize(); + instance.PluginInfo = PluginInfo; + instance.DataModelDescription = dataModelPropertyAttribute; + instance.Initialize(); - propertyInfo.SetValue(this, instance); - } - else if (!SupportedTypes.Contains(propertyInfo.PropertyType)) - { - // Show a useful error for plugin devs - throw new ArtemisPluginException(PluginInfo, - $"Plugin datamodel contains property of unsupported type {propertyInfo.PropertyType.Name}. \r\n\r\n" + - $"Property name: {propertyInfo.Name}\r\n" + - $"Property declared on: {propertyInfo.DeclaringType?.Name ?? "-"} \r\n\r\n" + - $"Supported properties:\r\n{string.Join("\r\n", SupportedTypes.Select(t => $" - {t.Name}"))}"); - } + propertyInfo.SetValue(this, instance); } Initialized = true; diff --git a/src/Artemis.UI/DataModelVisualization/DataModelViewModel.cs b/src/Artemis.UI/DataModelVisualization/DataModelViewModel.cs new file mode 100644 index 000000000..cc514063a --- /dev/null +++ b/src/Artemis.UI/DataModelVisualization/DataModelViewModel.cs @@ -0,0 +1,56 @@ +using System; +using System.Linq; +using Artemis.Core.Attributes; +using Artemis.Core.Plugins.Abstract.DataModels; +using Artemis.UI.Exceptions; +using Stylet; + +namespace Artemis.UI.DataModelVisualization +{ + public class DataModelViewModel : PropertyChangedBase + { + public DataModelViewModel(DataModel dataModel) + { + if (!DataModel.Initialized) + throw new ArtemisUIException("Cannot create view model for data model that is not yet initialized"); + + DataModel = dataModel; + } + + public DataModelViewModel(DataModel parent, DataModel dataModel) + { + if (!DataModel.Initialized) + throw new ArtemisUIException("Cannot create view model for data model that is not yet initialized"); + + Parent = parent; + DataModel = dataModel; + } + + public DataModel DataModel { get; } + public DataModel Parent { get; set; } + + + private void PopulateProperties() + { + foreach (var propertyInfo in DataModel.GetType().GetProperties()) + { + var dataModelPropertyAttribute = (DataModelPropertyAttribute) Attribute.GetCustomAttribute(propertyInfo, typeof(DataModelPropertyAttribute)); + if (dataModelPropertyAttribute == null) + continue; + + // For child data models create another data model view model + if (typeof(DataModel).IsAssignableFrom(propertyInfo.PropertyType)) + { + } + // For primitives, create a property view model + else if (propertyInfo.PropertyType.IsPrimitive) + { + } + // For anything else check if it has any child primitives and if so create a property container view model + else if (propertyInfo.PropertyType.GetProperties().Any(p => p.PropertyType.IsPrimitive)) + { + } + } + } + } +} \ No newline at end of file