mirror of
https://github.com/Artemis-RGB/Artemis
synced 2026-01-01 10:13:30 +00:00
Data model - More WIP stuff woo
This commit is contained in:
parent
28bcfcc95a
commit
722f3d597a
@ -11,20 +11,6 @@ namespace Artemis.Core.Plugins.Abstract.DataModels
|
|||||||
{
|
{
|
||||||
public abstract class DataModel
|
public abstract class DataModel
|
||||||
{
|
{
|
||||||
private static readonly List<Type> SupportedTypes = new List<Type>
|
|
||||||
{
|
|
||||||
typeof(bool),
|
|
||||||
typeof(byte),
|
|
||||||
typeof(decimal),
|
|
||||||
typeof(double),
|
|
||||||
typeof(float),
|
|
||||||
typeof(int),
|
|
||||||
typeof(long),
|
|
||||||
typeof(string),
|
|
||||||
typeof(SKColor),
|
|
||||||
typeof(SKPoint)
|
|
||||||
};
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the plugin info this data model belongs to
|
/// Gets the plugin info this data model belongs to
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -62,31 +48,19 @@ namespace Artemis.Core.Plugins.Abstract.DataModels
|
|||||||
foreach (var propertyInfo in GetType().GetProperties())
|
foreach (var propertyInfo in GetType().GetProperties())
|
||||||
{
|
{
|
||||||
var dataModelPropertyAttribute = (DataModelPropertyAttribute) Attribute.GetCustomAttribute(propertyInfo, typeof(DataModelPropertyAttribute));
|
var dataModelPropertyAttribute = (DataModelPropertyAttribute) Attribute.GetCustomAttribute(propertyInfo, typeof(DataModelPropertyAttribute));
|
||||||
if (dataModelPropertyAttribute == null)
|
if (dataModelPropertyAttribute == null || !typeof(DataModel).IsAssignableFrom(propertyInfo.PropertyType))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// If the a nested datamodel create an instance and initialize it
|
// If the property is 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)
|
||||||
var instance = (DataModel) Activator.CreateInstance(propertyInfo.PropertyType, true);
|
throw new ArtemisCoreException($"Failed to create instance of child datamodel at {propertyInfo.Name}");
|
||||||
if (instance == null)
|
|
||||||
throw new ArtemisCoreException($"Failed to create instance of child datamodel at {propertyInfo.Name}");
|
|
||||||
|
|
||||||
instance.PluginInfo = PluginInfo;
|
instance.PluginInfo = PluginInfo;
|
||||||
instance.DataModelDescription = dataModelPropertyAttribute;
|
instance.DataModelDescription = dataModelPropertyAttribute;
|
||||||
instance.Initialize();
|
instance.Initialize();
|
||||||
|
|
||||||
propertyInfo.SetValue(this, instance);
|
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}"))}");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Initialized = true;
|
Initialized = true;
|
||||||
|
|||||||
56
src/Artemis.UI/DataModelVisualization/DataModelViewModel.cs
Normal file
56
src/Artemis.UI/DataModelVisualization/DataModelViewModel.cs
Normal file
@ -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))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user