Implemented layer element saving

Added a working setting to the brush element
This commit is contained in:
Robert committed 2019-12-04 19:54:18 +01:00
1 parent be03167f7e
commit 8ba2e58c5d
24 files changed
+440 -85

No files matched your search

@@ -1,4 +1,5 @@
using Artemis.Core.Models.Profile;
using System;
using Artemis.Core.Models.Profile;
using Artemis.Core.Plugins.LayerElement;
namespace Artemis.Core.Services.Interfaces
@@ -13,6 +14,8 @@ namespace Artemis.Core.Services.Interfaces
/// <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>
/// <returns></returns>
LayerElement InstantiateLayerElement(Layer layer, LayerElementDescriptor layerElementDescriptor, string settings = null);
LayerElement InstantiateLayerElement(Layer layer, LayerElementDescriptor layerElementDescriptor, string settings = null, Guid? guid = null);
void RemoveLayerElement(Layer layer, LayerElement layerElement);
}
}
+32 -10
View File
@@ -1,4 +1,7 @@
using Artemis.Core.Models.Profile;
using System;
using System.Linq;
using System.Reflection;
using Artemis.Core.Models.Profile;
using Artemis.Core.Plugins.Exceptions;
using Artemis.Core.Plugins.LayerElement;
using Artemis.Core.Services.Interfaces;
@@ -17,28 +20,41 @@ namespace Artemis.Core.Services
_kernel = kernel;
}
public LayerElement InstantiateLayerElement(Layer layer, LayerElementDescriptor layerElementDescriptor, string settings)
public LayerElement InstantiateLayerElement(Layer layer, LayerElementDescriptor layerElementDescriptor, string settings, Guid? guid)
{
// Deserialize the settings, if provided
object deserializedSettings = null;
if (settings != null)
if (guid == null)
guid = Guid.NewGuid();
// Determine the settings type declared by the layer element
object settingsInstance = null;
var properties = layerElementDescriptor.LayerElementType.GetProperties();
var settingsType = properties.FirstOrDefault(p => p.Name == "Settings" &&
p.DeclaringType == layerElementDescriptor.LayerElementType)?.PropertyType;
// Deserialize the settings if provided, check for null in JSON as well
if (settings != null && settings != "null")
{
var settingsType = layerElementDescriptor.LayerElementType.GetProperty(nameof(LayerElement.Settings))?.PropertyType;
// Setting where provided but no settings type was found, something is wrong
if (settingsType == null)
{
throw new ArtemisPluginException(
layerElementDescriptor.LayerElementProvider.PluginInfo,
$"Layer element of type {layerElementDescriptor.LayerElementType} has no Settings property."
$"Settings where provided but layer element of type {layerElementDescriptor.LayerElementType.Name} has no Settings property."
);
}
deserializedSettings = JsonConvert.DeserializeObject(settings, settingsType);
settingsInstance = JsonConvert.DeserializeObject(settings, settingsType);
}
// If no settings found, provide a fresh instance of the settings type
else if (settingsType != null)
{
settingsInstance = Activator.CreateInstance(settingsType);
}
var arguments = new IParameter[]
{
new ConstructorArgument("layer", layer),
new ConstructorArgument("settings", deserializedSettings),
new ConstructorArgument("guid", guid.Value),
new ConstructorArgument("settings", settingsInstance),
new ConstructorArgument("descriptor", layerElementDescriptor)
};
var layerElement = (LayerElement) _kernel.Get(layerElementDescriptor.LayerElementType, arguments);
@@ -46,5 +62,11 @@ namespace Artemis.Core.Services
return layerElement;
}
public void RemoveLayerElement(Layer layer, LayerElement layerElement)
{
layer.RemoveLayerElement(layerElement);
layerElement.Dispose();
}
}
}
@@ -4,6 +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.Services.Interfaces;
using Artemis.Core.Services.Storage.Interfaces;
using Artemis.Storage.Repositories.Interfaces;
@@ -18,17 +19,21 @@ namespace Artemis.Core.Services.Storage
private readonly IPluginService _pluginService;
private readonly IProfileRepository _profileRepository;
private readonly ISurfaceService _surfaceService;
private readonly ILayerService _layerService;
internal ProfileService(IPluginService pluginService, ISurfaceService surfaceService, IProfileRepository profileRepository)
internal ProfileService(IPluginService pluginService, ISurfaceService surfaceService, ILayerService layerService, IProfileRepository profileRepository)
{
_pluginService = pluginService;
_surfaceService = surfaceService;
_layerService = layerService;
_profileRepository = profileRepository;
_surfaceService.ActiveSurfaceConfigurationChanged += SurfaceServiceOnActiveSurfaceConfigurationChanged;
_surfaceService.SurfaceConfigurationUpdated += SurfaceServiceOnSurfaceConfigurationUpdated;
_surfaceService.ActiveSurfaceConfigurationChanged += OnActiveSurfaceConfigurationChanged;
_surfaceService.SurfaceConfigurationUpdated += OnSurfaceConfigurationUpdated;
_pluginService.PluginLoaded += OnPluginLoaded;
}
public List<Profile> GetProfiles(ProfileModule module)
{
var profileEntities = _profileRepository.GetByPluginGuid(module.PluginInfo.Guid);
@@ -64,7 +69,7 @@ namespace Artemis.Core.Services.Storage
_profileRepository.Add(profile.ProfileEntity);
if (_surfaceService.ActiveSurface != null)
profile.ApplySurface(_surfaceService.ActiveSurface);
profile.PopulateLeds(_surfaceService.ActiveSurface);
return profile;
}
@@ -72,6 +77,7 @@ namespace Artemis.Core.Services.Storage
public void ActivateProfile(ProfileModule module, Profile profile)
{
module.ChangeActiveProfile(profile, _surfaceService.ActiveSurface);
InstantiateProfileLayerElements(profile);
}
public void DeleteProfile(Profile profile)
@@ -90,29 +96,68 @@ namespace Artemis.Core.Services.Storage
layer.ApplyToEntity();
if (_surfaceService.ActiveSurface != null)
profile.ApplySurface(_surfaceService.ActiveSurface);
profile.PopulateLeds(_surfaceService.ActiveSurface);
}
_profileRepository.Save(profile.ProfileEntity);
}
private void SurfaceServiceOnActiveSurfaceConfigurationChanged(object sender, SurfaceConfigurationEventArgs e)
private void InstantiateProfileLayerElements(Profile profile)
{
ApplySurfaceToProfiles(e.Surface);
var layerElementProviders = _pluginService.GetPluginsOfType<LayerElementProvider>();
var descriptors = layerElementProviders.SelectMany(l => l.LayerElementDescriptors).ToList();
foreach (var layer in profile.GetAllLayers())
{
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.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);
}
}
}
private void SurfaceServiceOnSurfaceConfigurationUpdated(object sender, SurfaceConfigurationEventArgs e)
{
if (!e.Surface.IsActive)
return;
ApplySurfaceToProfiles(e.Surface);
}
private void ApplySurfaceToProfiles(ArtemisSurface surface)
private void ActiveProfilesPopulateLeds(ArtemisSurface surface)
{
var profileModules = _pluginService.GetPluginsOfType<ProfileModule>();
foreach (var profileModule in profileModules.Where(p => p.ActiveProfile != null).ToList())
profileModule.ActiveProfile.ApplySurface(surface);
profileModule.ActiveProfile.PopulateLeds(surface);
}
private void ActiveProfilesInstantiateProfileLayerElements()
{
var profileModules = _pluginService.GetPluginsOfType<ProfileModule>();
foreach (var profileModule in profileModules.Where(p => p.ActiveProfile != null).ToList())
InstantiateProfileLayerElements(profileModule.ActiveProfile);
}
#region Event handlers
private void OnActiveSurfaceConfigurationChanged(object sender, SurfaceConfigurationEventArgs e)
{
ActiveProfilesPopulateLeds(e.Surface);
}
private void OnSurfaceConfigurationUpdated(object sender, SurfaceConfigurationEventArgs e)
{
if (e.Surface.IsActive)
ActiveProfilesPopulateLeds(e.Surface);
}
private void OnPluginLoaded(object sender, PluginEventArgs e)
{
if (e.PluginInfo.Instance is LayerElementProvider)
ActiveProfilesInstantiateProfileLayerElements();
}
#endregion
}
}