From 8a596f14266b182e415a5fb95c1709d3d6fb6524 Mon Sep 17 00:00:00 2001 From: SpoinkyNL Date: Fri, 29 Nov 2019 08:47:05 +0100 Subject: [PATCH] Change profile editor VMs to use event aggregator --- src/Artemis.UI/Artemis.UI.csproj | 4 +- .../Events/MainWindowFocusChanged.cs | 12 +++++ .../Events/MainWindowFocusChangedEvent.cs | 14 ----- .../ProfileEditorSelectedElementChanged.cs | 15 ++++++ .../ProfileEditorSelectedProfileChanged.cs | 14 +++++ .../ProfileEditorPanelViewModel.cs | 16 +----- .../ProfileEditor/ProfileEditorViewModel.cs | 33 +++--------- .../ProfileElementsViewModel.cs | 13 ++++- .../Visualization/ProfileView.xaml | 4 +- .../Visualization/ProfileViewModel.cs | 51 ++++++++++++++----- src/Artemis.UI/Screens/RootViewModel.cs | 4 +- 11 files changed, 103 insertions(+), 77 deletions(-) create mode 100644 src/Artemis.UI/Events/MainWindowFocusChanged.cs delete mode 100644 src/Artemis.UI/Events/MainWindowFocusChangedEvent.cs create mode 100644 src/Artemis.UI/Events/ProfileEditorSelectedElementChanged.cs create mode 100644 src/Artemis.UI/Events/ProfileEditorSelectedProfileChanged.cs diff --git a/src/Artemis.UI/Artemis.UI.csproj b/src/Artemis.UI/Artemis.UI.csproj index d53c206a2..0f5d338d1 100644 --- a/src/Artemis.UI/Artemis.UI.csproj +++ b/src/Artemis.UI/Artemis.UI.csproj @@ -152,7 +152,9 @@ - + + + diff --git a/src/Artemis.UI/Events/MainWindowFocusChanged.cs b/src/Artemis.UI/Events/MainWindowFocusChanged.cs new file mode 100644 index 000000000..ed3bbc0f1 --- /dev/null +++ b/src/Artemis.UI/Events/MainWindowFocusChanged.cs @@ -0,0 +1,12 @@ +namespace Artemis.UI.Events +{ + public class MainWindowFocusChanged + { + public MainWindowFocusChanged(bool isFocused) + { + IsFocused = isFocused; + } + + public bool IsFocused { get; } + } +} \ No newline at end of file diff --git a/src/Artemis.UI/Events/MainWindowFocusChangedEvent.cs b/src/Artemis.UI/Events/MainWindowFocusChangedEvent.cs deleted file mode 100644 index 690989da7..000000000 --- a/src/Artemis.UI/Events/MainWindowFocusChangedEvent.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; - -namespace Artemis.UI.Events -{ - public class MainWindowFocusChangedEvent : EventArgs - { - public MainWindowFocusChangedEvent(bool isFocused) - { - IsFocused = isFocused; - } - - public bool IsFocused { get; } - } -} \ No newline at end of file diff --git a/src/Artemis.UI/Events/ProfileEditorSelectedElementChanged.cs b/src/Artemis.UI/Events/ProfileEditorSelectedElementChanged.cs new file mode 100644 index 000000000..65277cfb0 --- /dev/null +++ b/src/Artemis.UI/Events/ProfileEditorSelectedElementChanged.cs @@ -0,0 +1,15 @@ +using Artemis.Core.Models.Profile; +using Artemis.Core.Models.Profile.Abstract; + +namespace Artemis.UI.Events +{ + public class ProfileEditorSelectedElementChanged + { + public ProfileEditorSelectedElementChanged(ProfileElement profileElement) + { + ProfileElement = profileElement; + } + + public ProfileElement ProfileElement { get; } + } +} \ No newline at end of file diff --git a/src/Artemis.UI/Events/ProfileEditorSelectedProfileChanged.cs b/src/Artemis.UI/Events/ProfileEditorSelectedProfileChanged.cs new file mode 100644 index 000000000..01b95a90e --- /dev/null +++ b/src/Artemis.UI/Events/ProfileEditorSelectedProfileChanged.cs @@ -0,0 +1,14 @@ +using Artemis.Core.Models.Profile; + +namespace Artemis.UI.Events +{ + public class ProfileEditorSelectedProfileChanged + { + public ProfileEditorSelectedProfileChanged(Profile profile) + { + Profile = profile; + } + + public Profile Profile { get; } + } +} \ No newline at end of file diff --git a/src/Artemis.UI/Screens/Module/ProfileEditor/ProfileEditorPanelViewModel.cs b/src/Artemis.UI/Screens/Module/ProfileEditor/ProfileEditorPanelViewModel.cs index d783ef8f2..4d58d279f 100644 --- a/src/Artemis.UI/Screens/Module/ProfileEditor/ProfileEditorPanelViewModel.cs +++ b/src/Artemis.UI/Screens/Module/ProfileEditor/ProfileEditorPanelViewModel.cs @@ -1,22 +1,8 @@ -using Artemis.UI.Screens.Module.ProfileEditor.ProfileElements.ProfileElement; -using Stylet; +using Stylet; namespace Artemis.UI.Screens.Module.ProfileEditor { public class ProfileEditorPanelViewModel : Screen { - public ProfileEditorViewModel ProfileEditorViewModel { get; set; } - - public virtual void ActiveProfileChanged() - { - } - - public virtual void ActiveProfileUpdated() - { - } - - public virtual void ProfileElementSelected(ProfileElementViewModel profileElement) - { - } } } \ No newline at end of file diff --git a/src/Artemis.UI/Screens/Module/ProfileEditor/ProfileEditorViewModel.cs b/src/Artemis.UI/Screens/Module/ProfileEditor/ProfileEditorViewModel.cs index 4add86c02..898f5219f 100644 --- a/src/Artemis.UI/Screens/Module/ProfileEditor/ProfileEditorViewModel.cs +++ b/src/Artemis.UI/Screens/Module/ProfileEditor/ProfileEditorViewModel.cs @@ -8,12 +8,12 @@ using Artemis.Core.Plugins.Abstract; using Artemis.Core.Plugins.Models; using Artemis.Core.Services; using Artemis.Core.Services.Storage.Interfaces; +using Artemis.UI.Events; using Artemis.UI.Screens.Module.ProfileEditor.Dialogs; using Artemis.UI.Screens.Module.ProfileEditor.DisplayConditions; using Artemis.UI.Screens.Module.ProfileEditor.ElementProperties; using Artemis.UI.Screens.Module.ProfileEditor.LayerElements; using Artemis.UI.Screens.Module.ProfileEditor.ProfileElements; -using Artemis.UI.Screens.Module.ProfileEditor.ProfileElements.ProfileElement; using Artemis.UI.Screens.Module.ProfileEditor.Visualization; using Artemis.UI.Services.Interfaces; using Stylet; @@ -22,14 +22,16 @@ namespace Artemis.UI.Screens.Module.ProfileEditor { public class ProfileEditorViewModel : Conductor.Collection.AllActive { + private readonly IEventAggregator _eventAggregator; private readonly IProfileService _profileService; private readonly ISettingsService _settingsService; public ProfileEditorViewModel(ProfileModule module, ICollection viewModels, IProfileService profileService, - IDialogService dialogService, ISettingsService settingsService) + IDialogService dialogService, ISettingsService settingsService, IEventAggregator eventAggregator) { _profileService = profileService; _settingsService = settingsService; + _eventAggregator = eventAggregator; DisplayName = "Profile editor"; Module = module; @@ -76,12 +78,7 @@ namespace Artemis.UI.Screens.Module.ProfileEditor var oldProfile = Module.ActiveProfile; Module.ChangeActiveProfile(profile); - - foreach (var panelViewModel in Items) - { - panelViewModel.ProfileEditorViewModel = this; - panelViewModel.ActiveProfileChanged(); - } + _eventAggregator.Publish(new ProfileEditorSelectedProfileChanged(SelectedProfile)); if (oldProfile != null) _profileService.UpdateProfile(oldProfile, false); @@ -141,11 +138,7 @@ namespace Artemis.UI.Screens.Module.ProfileEditor Task.Run(() => { LoadProfiles(); - foreach (var panelViewModel in Items) - { - panelViewModel.ProfileEditorViewModel = this; - panelViewModel.ActiveProfileChanged(); - } + _eventAggregator.Publish(new ProfileEditorSelectedProfileChanged(SelectedProfile)); }); base.OnActivate(); } @@ -195,19 +188,5 @@ namespace Artemis.UI.Screens.Module.ProfileEditor if (!activeProfile.IsActivated) Module.ChangeActiveProfile(activeProfile); } - - public void OnProfileUpdated(ProfileEditorPanelViewModel source = null) - { - _profileService.UpdateProfile(SelectedProfile, true); - - foreach (var panelViewModel in Items.Where(p => p != source)) - panelViewModel.ActiveProfileUpdated(); - } - - public void OnProfileElementSelected(ProfileElementViewModel profileElement, ProfileEditorPanelViewModel source = null) - { - foreach (var panelViewModel in Items.Where(p => p != source)) - panelViewModel.ProfileElementSelected(profileElement); - } } } \ No newline at end of file diff --git a/src/Artemis.UI/Screens/Module/ProfileEditor/ProfileElements/ProfileElementsViewModel.cs b/src/Artemis.UI/Screens/Module/ProfileEditor/ProfileElements/ProfileElementsViewModel.cs index fd2c92c42..11579796f 100644 --- a/src/Artemis.UI/Screens/Module/ProfileEditor/ProfileElements/ProfileElementsViewModel.cs +++ b/src/Artemis.UI/Screens/Module/ProfileEditor/ProfileElements/ProfileElementsViewModel.cs @@ -1,17 +1,25 @@ using System.Linq; using System.Windows; using Artemis.Core.Models.Profile; +using Artemis.Core.Services.Storage.Interfaces; +using Artemis.UI.Events; using Artemis.UI.Screens.Module.ProfileEditor.ProfileElements.ProfileElement; using GongSolutions.Wpf.DragDrop; +using Stylet; namespace Artemis.UI.Screens.Module.ProfileEditor.ProfileElements { public class ProfileElementsViewModel : ProfileEditorPanelViewModel, IDropTarget { + private readonly IProfileService _profileService; + private readonly IEventAggregator _eventAggregator; private ProfileElementViewModel _selectedProfileElement; - public ProfileElementsViewModel() + public ProfileElementsViewModel(IProfileService profileService, IEventAggregator eventAggregator) { + _profileService = profileService; + _eventAggregator = eventAggregator; + CreateRootFolderViewModel(); } @@ -23,7 +31,7 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.ProfileElements set { _selectedProfileElement = value; - ProfileEditorViewModel.OnProfileElementSelected(_selectedProfileElement, this); + _eventAggregator.Publish(new ProfileEditorSelectedElementChanged(_selectedProfileElement.ProfileElement)); } } @@ -65,6 +73,7 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.ProfileElements break; } + _profileService.UpdateProfile(this.RootFolder.P); ProfileEditorViewModel.OnProfileUpdated(this); } diff --git a/src/Artemis.UI/Screens/Module/ProfileEditor/Visualization/ProfileView.xaml b/src/Artemis.UI/Screens/Module/ProfileEditor/Visualization/ProfileView.xaml index 2c79669d9..e2ac2c0a4 100644 --- a/src/Artemis.UI/Screens/Module/ProfileEditor/Visualization/ProfileView.xaml +++ b/src/Artemis.UI/Screens/Module/ProfileEditor/Visualization/ProfileView.xaml @@ -158,10 +158,10 @@ - + Highlight selected layer - + Pause visualization on focus loss diff --git a/src/Artemis.UI/Screens/Module/ProfileEditor/Visualization/ProfileViewModel.cs b/src/Artemis.UI/Screens/Module/ProfileEditor/Visualization/ProfileViewModel.cs index 9e24c1861..8f671017d 100644 --- a/src/Artemis.UI/Screens/Module/ProfileEditor/Visualization/ProfileViewModel.cs +++ b/src/Artemis.UI/Screens/Module/ProfileEditor/Visualization/ProfileViewModel.cs @@ -20,32 +20,29 @@ using Point = System.Windows.Point; namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization { - public class ProfileViewModel : ProfileEditorPanelViewModel, IHandle + public class ProfileViewModel : ProfileEditorPanelViewModel, IHandle, IHandle { private readonly ISettingsService _settingsService; - private readonly TimerUpdateTrigger _updateTrigger; + private readonly ISurfaceService _surfaceService; + private TimerUpdateTrigger _updateTrigger; public ProfileViewModel(ISurfaceService surfaceService, ISettingsService settingsService, IEventAggregator eventAggregator) { + _surfaceService = surfaceService; _settingsService = settingsService; Devices = new ObservableCollection(); + Cursor = null; + Execute.PostToUIThread(() => { SelectionRectangle = new RectangleGeometry(); PanZoomViewModel = new PanZoomViewModel(); }); - Cursor = null; ApplySurfaceConfiguration(surfaceService.ActiveSurface); + CreateUpdateTrigger(); - // Borrow RGB.NET's update trigger but limit the FPS - var targetFpsSetting = settingsService.GetSetting("Core.TargetFrameRate", 25); - var editorTargetFpsSetting = settingsService.GetSetting("ProfileEditor.TargetFrameRate", 15); - var targetFps = Math.Min(targetFpsSetting.Value, editorTargetFpsSetting.Value); - _updateTrigger = new TimerUpdateTrigger {UpdateFrequency = 1.0 / targetFps}; - _updateTrigger.Update += UpdateLeds; - - surfaceService.ActiveSurfaceConfigurationChanged += OnActiveSurfaceConfigurationChanged; + eventAggregator.Subscribe(this); } public bool IsInitializing { get; private set; } @@ -57,6 +54,18 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization public Cursor Cursor { get; set; } + private void CreateUpdateTrigger() + { + // Borrow RGB.NET's update trigger but limit the FPS + var targetFpsSetting = _settingsService.GetSetting("Core.TargetFrameRate", 25); + var editorTargetFpsSetting = _settingsService.GetSetting("ProfileEditor.TargetFrameRate", 15); + var targetFps = Math.Min(targetFpsSetting.Value, editorTargetFpsSetting.Value); + _updateTrigger = new TimerUpdateTrigger {UpdateFrequency = 1.0 / targetFps}; + _updateTrigger.Update += UpdateLeds; + + _surfaceService.ActiveSurfaceConfigurationChanged += OnActiveSurfaceConfigurationChanged; + } + private void OnActiveSurfaceConfigurationChanged(object sender, SurfaceConfigurationEventArgs e) { ApplySurfaceConfiguration(e.Surface); @@ -119,7 +128,7 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization _updateTrigger.Start(); base.OnActivate(); } - + protected override void OnDeactivate() { HighlightSelectedLayer.Save(); @@ -250,9 +259,23 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization #endregion - public void Handle(MainWindowFocusChangedEvent message) + #region EventAggregator handlers + + public void Handle(ProfileEditorSelectedElementChanged message) { - Console.WriteLine(message); + if (HighlightSelectedLayer.Value) + { + } } + + public void Handle(MainWindowFocusChanged message) + { + if (PauseRenderingOnFocusLoss.Value && !message.IsFocused) + _updateTrigger.Stop(); + else if (PauseRenderingOnFocusLoss.Value && message.IsFocused) + _updateTrigger.Start(); + } + + #endregion } } \ No newline at end of file diff --git a/src/Artemis.UI/Screens/RootViewModel.cs b/src/Artemis.UI/Screens/RootViewModel.cs index 2520feb93..7b60ac11a 100644 --- a/src/Artemis.UI/Screens/RootViewModel.cs +++ b/src/Artemis.UI/Screens/RootViewModel.cs @@ -143,7 +143,7 @@ namespace Artemis.UI.Screens return; _lostFocus = true; - _eventAggregator.Publish(new MainWindowFocusChangedEvent(false)); + _eventAggregator.Publish(new MainWindowFocusChanged(false)); } public void WindowActivated() @@ -152,7 +152,7 @@ namespace Artemis.UI.Screens return; _lostFocus = false; - _eventAggregator.Publish(new MainWindowFocusChangedEvent(true)); + _eventAggregator.Publish(new MainWindowFocusChanged(true)); } } } \ No newline at end of file