diff --git a/src/Artemis.UI/Screens/Module/ProfileEditor/LayerProperties/LayerPropertiesViewModel.cs b/src/Artemis.UI/Screens/Module/ProfileEditor/LayerProperties/LayerPropertiesViewModel.cs index 712dda3f1..f21cb3859 100644 --- a/src/Artemis.UI/Screens/Module/ProfileEditor/LayerProperties/LayerPropertiesViewModel.cs +++ b/src/Artemis.UI/Screens/Module/ProfileEditor/LayerProperties/LayerPropertiesViewModel.cs @@ -27,6 +27,7 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.LayerProperties private readonly IPropertyTimelineVmFactory _propertyTimelineVmFactory; private readonly IProfileEditorService _profileEditorService; private readonly ISettingsService _settingsService; + private Layer _lastSelectedLayer; public LayerPropertiesViewModel(IProfileEditorService profileEditorService, ICoreService coreService, @@ -74,7 +75,7 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.LayerProperties PropertyTree = _propertyTreeVmFactory.Create(this); PropertyTimeline = _propertyTimelineVmFactory.Create(this); - PopulateProperties(_profileEditorService.SelectedProfileElement, null); + PopulateProperties(_profileEditorService.SelectedProfileElement); _profileEditorService.ProfileElementSelected += ProfileEditorServiceOnProfileElementSelected; _profileEditorService.CurrentTimeChanged += ProfileEditorServiceOnCurrentTimeChanged; @@ -86,6 +87,12 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.LayerProperties { _profileEditorService.ProfileElementSelected -= ProfileEditorServiceOnProfileElementSelected; _profileEditorService.CurrentTimeChanged -= ProfileEditorServiceOnCurrentTimeChanged; + + if (_lastSelectedLayer != null) + { + _lastSelectedLayer.LayerPropertyRegistered -= LayerOnPropertyRegistered; + _lastSelectedLayer.LayerPropertyRemoved -= LayerOnPropertyRemoved; + } PropertyTree?.Dispose(); PropertyTimeline?.Dispose(); @@ -102,7 +109,7 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.LayerProperties private void ProfileEditorServiceOnProfileElementSelected(object sender, ProfileElementEventArgs e) { - PopulateProperties(e.ProfileElement, e.PreviousProfileElement); + PopulateProperties(e.ProfileElement); } private void ProfileEditorServiceOnCurrentTimeChanged(object sender, EventArgs e) @@ -113,12 +120,12 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.LayerProperties #region View model managament - private void PopulateProperties(ProfileElement profileElement, ProfileElement previousProfileElement) + private void PopulateProperties(ProfileElement profileElement) { - if (previousProfileElement is Layer previousLayer) + if (_lastSelectedLayer != null) { - previousLayer.LayerPropertyRegistered -= LayerOnPropertyRegistered; - previousLayer.LayerPropertyRemoved -= LayerOnPropertyRemoved; + _lastSelectedLayer.LayerPropertyRegistered -= LayerOnPropertyRegistered; + _lastSelectedLayer.LayerPropertyRemoved -= LayerOnPropertyRemoved; } if (profileElement is Layer layer) @@ -137,6 +144,7 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.LayerProperties RemovePropertyViewModel(layerPropertyViewModel); } + _lastSelectedLayer = layer; layer.LayerPropertyRegistered += LayerOnPropertyRegistered; layer.LayerPropertyRemoved += LayerOnPropertyRemoved; } @@ -144,19 +152,21 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.LayerProperties { foreach (var layerPropertyViewModel in _layerPropertyViewModels.ToList()) RemovePropertyViewModel(layerPropertyViewModel); + + _lastSelectedLayer = null; } } private void LayerOnPropertyRegistered(object sender, LayerPropertyEventArgs e) { Console.WriteLine("LayerOnPropertyRegistered"); - PopulateProperties(e.LayerProperty.Layer, e.LayerProperty.Layer); + PopulateProperties(e.LayerProperty.Layer); } private void LayerOnPropertyRemoved(object sender, LayerPropertyEventArgs e) { Console.WriteLine("LayerOnPropertyRemoved"); - PopulateProperties(e.LayerProperty.Layer, e.LayerProperty.Layer); + PopulateProperties(e.LayerProperty.Layer); } private LayerPropertyViewModel CreatePropertyViewModel(BaseLayerProperty layerProperty) diff --git a/src/Artemis.UI/Screens/Module/ProfileEditor/LayerProperties/PropertyTree/PropertyInput/BrushPropertyInputViewModel.cs b/src/Artemis.UI/Screens/Module/ProfileEditor/LayerProperties/PropertyTree/PropertyInput/BrushPropertyInputViewModel.cs index 9f4a36c8c..2b235bc62 100644 --- a/src/Artemis.UI/Screens/Module/ProfileEditor/LayerProperties/PropertyTree/PropertyInput/BrushPropertyInputViewModel.cs +++ b/src/Artemis.UI/Screens/Module/ProfileEditor/LayerProperties/PropertyTree/PropertyInput/BrushPropertyInputViewModel.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using Artemis.Core.Events; using Artemis.Core.Models.Profile; using Artemis.Core.Plugins.LayerBrush; using Artemis.Core.Services.Interfaces; @@ -21,7 +22,7 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.LayerProperties.PropertyTree.P _pluginService = pluginService; EnumValues = new BindableCollection(); - _pluginService.PluginLoaded += (sender, args) => UpdateEnumValues(); + _pluginService.PluginLoaded += PluginServiceOnPluginLoaded; } public BindableCollection EnumValues { get; } @@ -59,10 +60,21 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.LayerProperties.PropertyTree.P NotifyOfPropertyChange(() => BrushInputValue); } + public override void Dispose() + { + _pluginService.PluginLoaded -= PluginServiceOnPluginLoaded; + base.Dispose(); + } + protected override void OnInitialized() { UpdateEnumValues(); base.OnInitialized(); } + + private void PluginServiceOnPluginLoaded(object sender, PluginEventArgs e) + { + UpdateEnumValues(); + } } } \ No newline at end of file diff --git a/src/Artemis.UI/Screens/Module/ProfileEditor/LayerProperties/PropertyTree/PropertyInput/PropertyInputViewModel.cs b/src/Artemis.UI/Screens/Module/ProfileEditor/LayerProperties/PropertyTree/PropertyInput/PropertyInputViewModel.cs index 7aef6f5f4..3ec4127b4 100644 --- a/src/Artemis.UI/Screens/Module/ProfileEditor/LayerProperties/PropertyTree/PropertyInput/PropertyInputViewModel.cs +++ b/src/Artemis.UI/Screens/Module/ProfileEditor/LayerProperties/PropertyTree/PropertyInput/PropertyInputViewModel.cs @@ -6,7 +6,7 @@ using Stylet; namespace Artemis.UI.Screens.Module.ProfileEditor.LayerProperties.PropertyTree.PropertyInput { - public abstract class PropertyInputViewModel : PropertyChangedBase + public abstract class PropertyInputViewModel : PropertyChangedBase, IDisposable { protected PropertyInputViewModel(IProfileEditorService profileEditorService) { @@ -37,7 +37,7 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.LayerProperties.PropertyTree.P throw new ArtemisUIException($"This input VM does not support the provided type {type.Name}"); LayerPropertyViewModel = layerPropertyViewModel; - layerPropertyViewModel.LayerProperty.ValueChanged += (sender, args) => Update(); + LayerPropertyViewModel.LayerProperty.ValueChanged += LayerPropertyOnValueChanged; Update(); Initialized = true; @@ -50,6 +50,11 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.LayerProperties.PropertyTree.P protected virtual void OnInitialized() { } + + private void LayerPropertyOnValueChanged(object? sender, EventArgs e) + { + Update(); + } private void UpdateInputValue(object value) { @@ -77,5 +82,11 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.LayerProperties.PropertyTree.P } #endregion + + public virtual void Dispose() + { + if (LayerPropertyViewModel != null) + LayerPropertyViewModel.LayerProperty.ValueChanged -= LayerPropertyOnValueChanged; + } } } \ No newline at end of file diff --git a/src/Artemis.UI/Screens/Module/ProfileEditor/LayerProperties/PropertyTree/PropertyTreeChildViewModel.cs b/src/Artemis.UI/Screens/Module/ProfileEditor/LayerProperties/PropertyTree/PropertyTreeChildViewModel.cs index 9ba57552c..3d32a21f2 100644 --- a/src/Artemis.UI/Screens/Module/ProfileEditor/LayerProperties/PropertyTree/PropertyTreeChildViewModel.cs +++ b/src/Artemis.UI/Screens/Module/ProfileEditor/LayerProperties/PropertyTree/PropertyTreeChildViewModel.cs @@ -30,5 +30,13 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.LayerProperties.PropertyTree public override void AddLayerProperty(LayerPropertyViewModel layerPropertyViewModel) { } + + public override void Dispose() + { + PropertyInputViewModel?.Dispose(); + PropertyInputViewModel = null; + + base.Dispose(); + } } } \ No newline at end of file diff --git a/src/Artemis.UI/Screens/Module/ProfileEditor/LayerProperties/PropertyTree/PropertyTreeItemViewModel.cs b/src/Artemis.UI/Screens/Module/ProfileEditor/LayerProperties/PropertyTree/PropertyTreeItemViewModel.cs index 4a0536f7c..bae2fe5dd 100644 --- a/src/Artemis.UI/Screens/Module/ProfileEditor/LayerProperties/PropertyTree/PropertyTreeItemViewModel.cs +++ b/src/Artemis.UI/Screens/Module/ProfileEditor/LayerProperties/PropertyTree/PropertyTreeItemViewModel.cs @@ -1,8 +1,9 @@ -using Stylet; +using System; +using Stylet; namespace Artemis.UI.Screens.Module.ProfileEditor.LayerProperties.PropertyTree { - public abstract class PropertyTreeItemViewModel : PropertyChangedBase + public abstract class PropertyTreeItemViewModel : PropertyChangedBase, IDisposable { protected PropertyTreeItemViewModel(LayerPropertyViewModel layerPropertyViewModel) { @@ -28,5 +29,9 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.LayerProperties.PropertyTree /// /// public abstract void AddLayerProperty(LayerPropertyViewModel layerPropertyViewModel); + + public virtual void Dispose() + { + } } } \ No newline at end of file diff --git a/src/Artemis.UI/Screens/Module/ProfileEditor/LayerProperties/PropertyTree/PropertyTreeParentViewModel.cs b/src/Artemis.UI/Screens/Module/ProfileEditor/LayerProperties/PropertyTree/PropertyTreeParentViewModel.cs index a81cb4435..21c339130 100644 --- a/src/Artemis.UI/Screens/Module/ProfileEditor/LayerProperties/PropertyTree/PropertyTreeParentViewModel.cs +++ b/src/Artemis.UI/Screens/Module/ProfileEditor/LayerProperties/PropertyTree/PropertyTreeParentViewModel.cs @@ -45,10 +45,19 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.LayerProperties.PropertyTree foreach (var child in Children.ToList()) { if (child.LayerPropertyViewModel == layerPropertyViewModel) + { Children.Remove(child); + child.Dispose(); + } else child.RemoveLayerProperty(layerPropertyViewModel); } } + + public override void Dispose() + { + foreach (var child in Children.ToList()) + child.Dispose(); + } } } \ No newline at end of file diff --git a/src/Artemis.UI/Screens/Module/ProfileEditor/LayerProperties/PropertyTree/PropertyTreeViewModel.cs b/src/Artemis.UI/Screens/Module/ProfileEditor/LayerProperties/PropertyTree/PropertyTreeViewModel.cs index e97360b26..ede6462e9 100644 --- a/src/Artemis.UI/Screens/Module/ProfileEditor/LayerProperties/PropertyTree/PropertyTreeViewModel.cs +++ b/src/Artemis.UI/Screens/Module/ProfileEditor/LayerProperties/PropertyTree/PropertyTreeViewModel.cs @@ -31,6 +31,9 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.LayerProperties.PropertyTree { _profileEditorService.CurrentTimeChanged -= OnCurrentTimeChanged; _profileEditorService.SelectedProfileElementUpdated -= OnSelectedProfileElementUpdated; + + foreach (var propertyTreeItemViewModel in PropertyTreeItemViewModels) + propertyTreeItemViewModel.Dispose(); } public void AddLayerProperty(LayerPropertyViewModel layerPropertyViewModel)