Added layer shapes, editor is still WIP

This commit is contained in:
SpoinkyNL committed 2019-12-15 22:52:04 +01:00
1 parent 70bf0e1c43
commit ddce69e8f0
115 files changed
+1949 -1189

No files matched your search

@@ -1,21 +1,18 @@
using System;
using Artemis.Core.Models.Profile;
using Artemis.Core.Plugins.LayerElement;
using Artemis.Core.Models.Profile;
using Artemis.Core.Plugins.LayerBrush;
namespace Artemis.Core.Services.Interfaces
{
public interface ILayerService : IArtemisService
{
/// <summary>
/// Instantiates and adds the <see cref="LayerElement" /> described by the provided
/// <see cref="LayerElementDescriptor" /> to the provided <see cref="Layer" />.
/// Instantiates and adds the <see cref="LayerBrush" /> described by the provided
/// <see cref="LayerBrushDescriptor" /> to the provided <see cref="Layer" />.
/// </summary>
/// <param name="layer">The layer to add the new layer element to</param>
/// <param name="layerElementDescriptor">The descriptor of the new layer element</param>
/// <param name="settings">JSON settings to be deserialized and injected into the layer element</param>
/// <param name="brushDescriptor">The descriptor of the new layer brush</param>
/// <param name="settings">JSON settings to be deserialized and injected into the layer brush</param>
/// <returns></returns>
LayerElement InstantiateLayerElement(Layer layer, LayerElementDescriptor layerElementDescriptor, string settings = null, Guid? guid = null);
void RemoveLayerElement(Layer layer, LayerElement layerElement);
LayerBrush InstantiateLayerBrush(Layer layer, LayerBrushDescriptor brushDescriptor, string settings = null);
}
}
+15 -18
View File
@@ -3,7 +3,7 @@ using System.Linq;
using System.Reflection;
using Artemis.Core.Models.Profile;
using Artemis.Core.Plugins.Exceptions;
using Artemis.Core.Plugins.LayerElement;
using Artemis.Core.Plugins.LayerBrush;
using Artemis.Core.Services.Interfaces;
using Newtonsoft.Json;
using Ninject;
@@ -23,16 +23,13 @@ namespace Artemis.Core.Services
_logger = logger;
}
public LayerElement InstantiateLayerElement(Layer layer, LayerElementDescriptor layerElementDescriptor, string settings, Guid? guid)
public LayerBrush InstantiateLayerBrush(Layer layer, LayerBrushDescriptor brushDescriptor, string settings)
{
if (guid == null)
guid = Guid.NewGuid();
// Determine the settings type declared by the layer element
object settingsInstance = null;
var properties = layerElementDescriptor.LayerElementType.GetProperties();
var properties = brushDescriptor.LayerBrushType.GetProperties();
var settingsType = properties.FirstOrDefault(p => p.Name == "Settings" &&
p.DeclaringType == layerElementDescriptor.LayerElementType)?.PropertyType;
p.DeclaringType == brushDescriptor.LayerBrushType)?.PropertyType;
// Deserialize the settings if provided, check for null in JSON as well
if (settings != null && settings != "null")
@@ -41,8 +38,8 @@ namespace Artemis.Core.Services
if (settingsType == null)
{
throw new ArtemisPluginException(
layerElementDescriptor.LayerElementProvider.PluginInfo,
$"Settings where provided but layer element of type {layerElementDescriptor.LayerElementType.Name} has no Settings property."
brushDescriptor.LayerBrushProvider.PluginInfo,
$"Settings where provided but layer element of type {brushDescriptor.LayerBrushType.Name} has no Settings property."
);
}
@@ -53,8 +50,8 @@ namespace Artemis.Core.Services
catch (JsonSerializationException e)
{
_logger.Warning(e, "Failed to deserialize settings for layer type {type}, resetting element settings - Plugin info: {pluginInfo}",
layerElementDescriptor.LayerElementType.Name,
layerElementDescriptor.LayerElementProvider.PluginInfo);
brushDescriptor.LayerBrushType.Name,
brushDescriptor.LayerBrushProvider.PluginInfo);
settingsInstance = Activator.CreateInstance(settingsType);
}
@@ -68,20 +65,20 @@ namespace Artemis.Core.Services
var arguments = new IParameter[]
{
new ConstructorArgument("layer", layer),
new ConstructorArgument("guid", guid.Value),
new ConstructorArgument("settings", settingsInstance),
new ConstructorArgument("descriptor", layerElementDescriptor)
new ConstructorArgument("descriptor", brushDescriptor)
};
var layerElement = (LayerElement) _kernel.Get(layerElementDescriptor.LayerElementType, arguments);
layer.AddLayerElement(layerElement);
var layerElement = (LayerBrush) _kernel.Get(brushDescriptor.LayerBrushType, arguments);
layer.LayerBrush = (layerElement);
return layerElement;
}
public void RemoveLayerElement(Layer layer, LayerElement layerElement)
public void RemoveLayerBrush(Layer layer, LayerBrush layerElement)
{
layer.RemoveLayerElement(layerElement);
layerElement.Dispose();
var brush = layer.LayerBrush;
layer.LayerBrush = null;
brush.Dispose();
}
}
}
@@ -4,7 +4,7 @@ using Artemis.Core.Events;
using Artemis.Core.Models.Profile;
using Artemis.Core.Models.Surface;
using Artemis.Core.Plugins.Abstract;
using Artemis.Core.Plugins.LayerElement;
using Artemis.Core.Plugins.LayerBrush;
using Artemis.Core.Services.Interfaces;
using Artemis.Core.Services.Storage.Interfaces;
using Artemis.Storage.Repositories.Interfaces;
@@ -16,10 +16,10 @@ namespace Artemis.Core.Services.Storage
/// </summary>
public class ProfileService : IProfileService
{
private readonly ILayerService _layerService;
private readonly IPluginService _pluginService;
private readonly IProfileRepository _profileRepository;
private readonly ISurfaceService _surfaceService;
private readonly ILayerService _layerService;
internal ProfileService(IPluginService pluginService, ISurfaceService surfaceService, ILayerService layerService, IProfileRepository profileRepository)
{
@@ -78,7 +78,7 @@ namespace Artemis.Core.Services.Storage
{
module.ChangeActiveProfile(profile, _surfaceService.ActiveSurface);
if (profile != null)
InstantiateProfileLayerElements(profile);
InstantiateProfileLayerBrushes(profile);
}
public void DeleteProfile(Profile profile)
@@ -102,27 +102,22 @@ namespace Artemis.Core.Services.Storage
_profileRepository.Save(profile.ProfileEntity);
}
private void InstantiateProfileLayerElements(Profile profile)
private void InstantiateProfileLayerBrushes(Profile profile)
{
var layerElementProviders = _pluginService.GetPluginsOfType<LayerElementProvider>();
var descriptors = layerElementProviders.SelectMany(l => l.LayerElementDescriptors).ToList();
var layerBrushProviders = _pluginService.GetPluginsOfType<LayerBrushProvider>();
var descriptors = layerBrushProviders.SelectMany(l => l.LayerBrushDescriptors).ToList();
foreach (var layer in profile.GetAllLayers())
// Only instantiate brushes for layers without an existing brush instance
foreach (var layer in profile.GetAllLayers().Where(l => l.LayerBrush == null && l.LayerEntity.BrushEntity != null))
{
foreach (var elementEntity in layer.LayerEntity.Elements)
{
// Skip already instantiated layer elements
if (layer.LayerElements.Any(e => e.Guid == elementEntity.Id))
continue;
// Get a matching descriptor
var descriptor = descriptors.FirstOrDefault(d => d.LayerBrushProvider.PluginInfo.Guid == layer.LayerEntity.BrushEntity.BrushPluginGuid &&
d.LayerBrushType.Name == layer.LayerEntity.BrushEntity.BrushType);
// Get a matching descriptor
var descriptor = descriptors.FirstOrDefault(d => d.LayerElementProvider.PluginInfo.Guid == elementEntity.PluginGuid &&
d.LayerElementType.Name == elementEntity.LayerElementType);
// If a descriptor that matches if found, instantiate it with the GUID of the element entity
if (descriptor != null)
_layerService.InstantiateLayerElement(layer, descriptor, elementEntity.Configuration, elementEntity.Id);
}
// If a descriptor that matches if found, instantiate it with the GUID of the element entity
if (descriptor != null)
_layerService.InstantiateLayerBrush(layer, descriptor, layer.LayerEntity.BrushEntity.Configuration);
}
}
@@ -133,11 +128,11 @@ namespace Artemis.Core.Services.Storage
profileModule.ActiveProfile.PopulateLeds(surface);
}
private void ActiveProfilesInstantiateProfileLayerElements()
private void ActiveProfilesInstantiateProfileLayerBrushes()
{
var profileModules = _pluginService.GetPluginsOfType<ProfileModule>();
foreach (var profileModule in profileModules.Where(p => p.ActiveProfile != null).ToList())
InstantiateProfileLayerElements(profileModule.ActiveProfile);
InstantiateProfileLayerBrushes(profileModule.ActiveProfile);
}
#region Event handlers
@@ -155,8 +150,8 @@ namespace Artemis.Core.Services.Storage
private void OnPluginLoaded(object sender, PluginEventArgs e)
{
if (e.PluginInfo.Instance is LayerElementProvider)
ActiveProfilesInstantiateProfileLayerElements();
if (e.PluginInfo.Instance is LayerBrushProvider)
ActiveProfilesInstantiateProfileLayerBrushes();
}
#endregion