From 956707b23dc2c0468d2123bbe0b620d63dead707 Mon Sep 17 00:00:00 2001 From: Robert Date: Tue, 30 Jun 2020 21:39:29 +0200 Subject: [PATCH] Profiles - Improved handling of enabling/disabling plugins --- src/Artemis.Core/Models/Profile/Layer.cs | 2 +- src/Artemis.Core/Services/CoreService.cs | 9 +++--- .../Services/Interfaces/ILayerService.cs | 6 ++++ src/Artemis.Core/Services/LayerService.cs | 15 +++++++--- .../Services/Storage/ProfileService.cs | 29 +++++++++++++++---- .../LayerPropertyGroupViewModel.cs | 6 +++- 6 files changed, 50 insertions(+), 17 deletions(-) diff --git a/src/Artemis.Core/Models/Profile/Layer.cs b/src/Artemis.Core/Models/Profile/Layer.cs index 917562ae5..e353f3852 100644 --- a/src/Artemis.Core/Models/Profile/Layer.cs +++ b/src/Artemis.Core/Models/Profile/Layer.cs @@ -472,7 +472,7 @@ namespace Artemis.Core.Models.Profile DeactivateLayerEffect(baseLayerEffect); } - private void DeactivateLayerBrush() + internal void DeactivateLayerBrush() { if (LayerBrush == null) return; diff --git a/src/Artemis.Core/Services/CoreService.cs b/src/Artemis.Core/Services/CoreService.cs index bc99e057a..0b72778af 100644 --- a/src/Artemis.Core/Services/CoreService.cs +++ b/src/Artemis.Core/Services/CoreService.cs @@ -46,6 +46,10 @@ namespace Artemis.Core.Services _surfaceService = surfaceService; _profileService = profileService; _loggingLevel = settingsService.GetSetting("Core.LoggingLevel", LogEventLevel.Debug); + _frameStopWatch = new Stopwatch(); + + UpdatePluginCache(); + ConfigureJsonConvert(); _rgbService.Surface.Updating += SurfaceOnUpdating; _rgbService.Surface.Updated += SurfaceOnUpdated; @@ -53,11 +57,6 @@ namespace Artemis.Core.Services _pluginService.PluginEnabled += (sender, args) => UpdatePluginCache(); _pluginService.PluginDisabled += (sender, args) => UpdatePluginCache(); - UpdatePluginCache(); - - _frameStopWatch = new Stopwatch(); - - ConfigureJsonConvert(); } public TimeSpan FrameTime { get; private set; } diff --git a/src/Artemis.Core/Services/Interfaces/ILayerService.cs b/src/Artemis.Core/Services/Interfaces/ILayerService.cs index 85c6b7e93..69d281977 100644 --- a/src/Artemis.Core/Services/Interfaces/ILayerService.cs +++ b/src/Artemis.Core/Services/Interfaces/ILayerService.cs @@ -23,6 +23,12 @@ namespace Artemis.Core.Services.Interfaces /// The layer to remove the active brush from void RemoveLayerBrush(Layer layer); + /// + /// Deactivates the currently active layer brush from the but keeps all settings + /// + /// The layer to deactivate the active brush on + void DeactivateLayerBrush(Layer layer); + /// /// Instantiates and adds the described by the provided /// diff --git a/src/Artemis.Core/Services/LayerService.cs b/src/Artemis.Core/Services/LayerService.cs index 2606d5372..704250729 100644 --- a/src/Artemis.Core/Services/LayerService.cs +++ b/src/Artemis.Core/Services/LayerService.cs @@ -49,6 +49,12 @@ namespace Artemis.Core.Services layer.OnLayerBrushUpdated(); } + public void DeactivateLayerBrush(Layer layer) + { + layer.DeactivateLayerBrush(); + layer.OnLayerBrushUpdated(); + } + public BaseLayerBrush InstantiateLayerBrush(Layer layer) { if (layer.LayerBrush != null) @@ -105,15 +111,16 @@ namespace Artemis.Core.Services public void InstantiateLayerEffects(EffectProfileElement effectElement) { - if (effectElement.LayerEffects.Any()) - throw new ArtemisCoreException("Effect element (layer/folder) already has instantiated layer effects"); - var layerEffectProviders = _pluginService.GetPluginsOfType(); var descriptors = layerEffectProviders.SelectMany(l => l.LayerEffectDescriptors).ToList(); var entities = effectElement.EffectsEntity.LayerEffects.OrderByDescending(e => e.Order).ToList(); - + foreach (var layerEffectEntity in entities) { + // Skip effects already on the element + if (effectElement.LayerEffects.Any(e => e.EntityId == layerEffectEntity.Id)) + continue; + // Get a matching descriptor var descriptor = descriptors.FirstOrDefault(d => d.LayerEffectProvider.PluginInfo.Guid == layerEffectEntity.PluginGuid && d.LayerEffectType.Name == layerEffectEntity.EffectType); diff --git a/src/Artemis.Core/Services/Storage/ProfileService.cs b/src/Artemis.Core/Services/Storage/ProfileService.cs index 1d40e87ee..ce5801eb1 100644 --- a/src/Artemis.Core/Services/Storage/ProfileService.cs +++ b/src/Artemis.Core/Services/Storage/ProfileService.cs @@ -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.LayerEffect.Abstract; using Artemis.Core.Services.Interfaces; using Artemis.Core.Services.Storage.Interfaces; using Artemis.Storage.Entities.Profile; @@ -34,7 +35,8 @@ namespace Artemis.Core.Services.Storage _surfaceService.ActiveSurfaceConfigurationSelected += OnActiveSurfaceConfigurationSelected; _surfaceService.SurfaceConfigurationUpdated += OnSurfaceConfigurationUpdated; - _pluginService.PluginLoaded += OnPluginLoaded; + _pluginService.PluginEnabled += OnPluginToggled; + _pluginService.PluginDisabled += OnPluginToggled; } public void ActivateDefaultProfiles() @@ -89,9 +91,6 @@ namespace Artemis.Core.Services.Storage public void ActivateProfile(ProfileModule module, Profile profile) { - if (module.ActiveProfile == profile) - return; - module.ChangeActiveProfile(profile, _surfaceService.ActiveSurface); if (profile != null) { @@ -181,7 +180,12 @@ namespace Artemis.Core.Services.Storage { foreach (var folder in profile.GetAllFolders()) { + // Instantiate effects _layerService.InstantiateLayerEffects(folder); + // Remove effects of plugins that are disabled + var disabledEffects = new List(folder.LayerEffects.Where(layerLayerEffect => !layerLayerEffect.PluginInfo.Enabled)); + foreach (var layerLayerEffect in disabledEffects) + _layerService.RemoveLayerEffect(layerLayerEffect); } } @@ -189,8 +193,19 @@ namespace Artemis.Core.Services.Storage { foreach (var layer in profile.GetAllLayers()) { - _layerService.InstantiateLayerBrush(layer); + // Instantiate brush + if (layer.LayerBrush == null) + _layerService.InstantiateLayerBrush(layer); + // Remove brush if plugin is disabled + else if (!layer.LayerBrush.PluginInfo.Enabled) + _layerService.DeactivateLayerBrush(layer); + + // Instantiate effects _layerService.InstantiateLayerEffects(layer); + // Remove effects of plugins that are disabled + var disabledEffects = new List(layer.LayerEffects.Where(layerLayerEffect => !layerLayerEffect.PluginInfo.Enabled)); + foreach (var layerLayerEffect in disabledEffects) + _layerService.RemoveLayerEffect(layerLayerEffect); } } @@ -224,10 +239,12 @@ namespace Artemis.Core.Services.Storage ActiveProfilesPopulateLeds(e.Surface); } - private void OnPluginLoaded(object sender, PluginEventArgs e) + private void OnPluginToggled(object sender, PluginEventArgs e) { if (e.PluginInfo.Instance is LayerBrushProvider) ActiveProfilesInstantiatePlugins(); + if (e.PluginInfo.Instance is LayerEffectProvider) + ActiveProfilesInstantiatePlugins(); else if (e.PluginInfo.Instance is ProfileModule profileModule) { var activeProfile = GetActiveProfile(profileModule); diff --git a/src/Artemis.UI/Screens/Module/ProfileEditor/LayerProperties/LayerPropertyGroupViewModel.cs b/src/Artemis.UI/Screens/Module/ProfileEditor/LayerProperties/LayerPropertyGroupViewModel.cs index 34fbd6286..c3f85b376 100644 --- a/src/Artemis.UI/Screens/Module/ProfileEditor/LayerProperties/LayerPropertyGroupViewModel.cs +++ b/src/Artemis.UI/Screens/Module/ProfileEditor/LayerProperties/LayerPropertyGroupViewModel.cs @@ -55,7 +55,11 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.LayerProperties public override bool IsExpanded { get => LayerPropertyGroup.ProfileElement.IsPropertyGroupExpanded(LayerPropertyGroup); - set => LayerPropertyGroup.ProfileElement.SetPropertyGroupExpanded(LayerPropertyGroup, value); + set + { + LayerPropertyGroup.ProfileElement.SetPropertyGroupExpanded(LayerPropertyGroup, value); + NotifyOfPropertyChange(nameof(IsExpanded)); + } } public ViewModelType GroupType