Datamodel - Added basic infrastructure

This commit is contained in:
SpoinkyNL committed 2020-04-26 17:30:43 +02:00
1 parent dad7591af5
commit 349e0a2c41
23 files changed
+206 -43

No files matched your search

+3
View File
@@ -56,4 +56,7 @@
<HintPath>..\..\..\RGB.NET\bin\netstandard2.0\RGB.NET.Groups.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Folder Include="Attributes\" />
</ItemGroup>
</Project>
@@ -6,7 +6,7 @@ using Artemis.Core.Annotations;
using SkiaSharp;
using Stylet;
namespace Artemis.Core.Models.Profile
namespace Artemis.Core.Models.Profile.Colors
{
public class ColorGradient : INotifyPropertyChanged
{
@@ -88,28 +88,4 @@ namespace Artemis.Core.Models.Profile
#endregion
}
public class ColorGradientStop : INotifyPropertyChanged
{
public ColorGradientStop(SKColor color, float position)
{
Color = color;
Position = position;
}
public SKColor Color { get; set; }
public float Position { get; set; }
#region PropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
}
@@ -0,0 +1,31 @@
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Artemis.Core.Annotations;
using SkiaSharp;
namespace Artemis.Core.Models.Profile.Colors
{
public class ColorGradientStop : INotifyPropertyChanged
{
public ColorGradientStop(SKColor color, float position)
{
Color = color;
Position = position;
}
public SKColor Color { get; set; }
public float Position { get; set; }
#region PropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
}
@@ -0,0 +1,11 @@
using System;
using System.Linq.Expressions;
using Artemis.Core.Plugins.Abstract.DataModels;
namespace Artemis.Core.Models.Profile.Conditions
{
public class LayerCondition
{
public Expression<Func<DataModel, bool>> ExpressionTree { get; set; }
}
}
@@ -0,0 +1,43 @@
using System;
namespace Artemis.Core.Plugins.Abstract.DataModels.Attributes
{
[AttributeUsage(System.AttributeTargets.Property)]
public class DataModelPropertyAttribute : Attribute
{
/// <summary>
/// Gets or sets the user-friendly name for this property, shown in the UI.
/// </summary>
public string Name { get; set; }
/// <summary>
/// Gets or sets the user-friendly description for this property, shown in the UI.
/// </summary>
public string Description { get; set; }
/// <summary>
/// Gets or sets the an optional input prefix to show before input elements in the UI.
/// </summary>
public string InputPrefix { get; set; }
/// <summary>
/// Gets or sets an optional input affix to show behind input elements in the UI.
/// </summary>
public string InputAffix { get; set; }
/// <summary>
/// Gets or sets an optional maximum input value, only enforced in the UI.
/// </summary>
public object MaxInputValue { get; set; }
/// <summary>
/// Gets or sets the input drag step size, used in the UI.
/// </summary>
public float InputStepSize { get; set; }
/// <summary>
/// Gets or sets an optional minimum input value, only enforced in the UI.
/// </summary>
public object MinInputValue { get; set; }
}
}
@@ -0,0 +1,69 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Artemis.Core.Plugins.Abstract.DataModels.Attributes;
using Artemis.Core.Plugins.Exceptions;
using SkiaSharp;
namespace Artemis.Core.Plugins.Abstract.DataModels
{
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)
};
protected DataModel(Module module)
{
Module = module;
Validate();
}
public Module Module { get; }
/// <summary>
/// Recursively validates the current datamodel, ensuring all properties annotated with
/// <see cref="DataModelPropertyAttribute" /> are of supported types.
/// </summary>
/// <returns></returns>
public bool Validate()
{
return ValidateType(GetType());
}
private bool ValidateType(Type type)
{
foreach (var propertyInfo in type.GetProperties())
{
var dataModelPropertyAttribute = (DataModelPropertyAttribute) Attribute.GetCustomAttribute(propertyInfo, typeof(DataModelPropertyAttribute));
if (dataModelPropertyAttribute == null)
continue;
// If the a nested datamodel, ensure the properties on there are valid
if (propertyInfo.PropertyType == typeof(DataModel))
ValidateType(propertyInfo.PropertyType);
else if (!SupportedTypes.Contains(propertyInfo.PropertyType))
{
// Show a useful error for plugin devs
throw new ArtemisPluginException(Module.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}"))}");
}
}
return true;
}
}
}
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using Artemis.Core.Models.Surface;
using Artemis.Core.Plugins.Abstract.DataModels;
using Artemis.Core.Plugins.Abstract.ViewModels;
using Artemis.Core.Plugins.Models;
using SkiaSharp;
@@ -27,6 +28,11 @@ namespace Artemis.Core.Plugins.Abstract
/// </summary>
public string DisplayIcon { get; set; }
/// <summary>
/// The optional datamodel driving this module
/// </summary>
public DataModel DataModel { get; set; }
/// <summary>
/// Whether or not this module expands upon the main data model. If set to true any data in main data model can be
/// accessed by profiles in this module
@@ -1,12 +0,0 @@
namespace Artemis.Core.Plugins.Abstract
{
public abstract class ModuleDataModel
{
protected ModuleDataModel(Module module)
{
Module = module;
}
public Module Module { get; }
}
}