From fbaa8c7af749bb0c97c680b4250412c18b7ab9be Mon Sep 17 00:00:00 2001 From: Robert Date: Thu, 24 Jun 2021 19:41:51 +0200 Subject: [PATCH] Debugger - Reworked FPS calculation code Scripting - Save profile on scripting window close --- .../ProfileEditor/ProfileEditorViewModel.cs | 178 +++++++++--------- .../Visualization/Tools/EditToolViewModel.cs | 5 +- .../Scripting/ScriptsDialogViewModel.cs | 14 +- .../Debug/Tabs/RenderDebugViewModel.cs | 55 +++--- 4 files changed, 134 insertions(+), 118 deletions(-) diff --git a/src/Artemis.UI/Screens/ProfileEditor/ProfileEditorViewModel.cs b/src/Artemis.UI/Screens/ProfileEditor/ProfileEditorViewModel.cs index d00e0a20d..04d538220 100644 --- a/src/Artemis.UI/Screens/ProfileEditor/ProfileEditorViewModel.cs +++ b/src/Artemis.UI/Screens/ProfileEditor/ProfileEditorViewModel.cs @@ -5,14 +5,11 @@ using System.Windows; using System.Windows.Input; using Artemis.Core; using Artemis.Core.Services; -using Artemis.UI.Events; using Artemis.UI.Ninject.Factories; using Artemis.UI.Screens.ProfileEditor.DisplayConditions; using Artemis.UI.Screens.ProfileEditor.LayerProperties; using Artemis.UI.Screens.ProfileEditor.ProfileTree; using Artemis.UI.Screens.ProfileEditor.Visualization; -using Artemis.UI.Screens.Scripting; -using Artemis.UI.Screens.Sidebar.Dialogs; using Artemis.UI.Services; using Artemis.UI.Shared; using Artemis.UI.Shared.Services; @@ -23,14 +20,14 @@ namespace Artemis.UI.Screens.ProfileEditor { public class ProfileEditorViewModel : MainScreenViewModel { - private readonly IMessageService _messageService; private readonly IDebugService _debugService; - private readonly IWindowManager _windowManager; - private readonly IScriptVmFactory _scriptVmFactory; - private readonly ISidebarVmFactory _sidebarVmFactory; + private readonly IMessageService _messageService; private readonly IProfileEditorService _profileEditorService; private readonly IProfileService _profileService; + private readonly IScriptVmFactory _scriptVmFactory; private readonly ISettingsService _settingsService; + private readonly ISidebarVmFactory _sidebarVmFactory; + private readonly IWindowManager _windowManager; private PluginSetting _bottomPanelsHeight; private PluginSetting _dataModelConditionsHeight; private DisplayConditionsViewModel _displayConditionsViewModel; @@ -197,89 +194,6 @@ namespace Artemis.UI.Screens.ProfileEditor _messageService.ShowMessage("Redid profile update", "UNDO", Undo); } - #region Menu - - public bool HasSelectedElement => _profileEditorService.SelectedProfileElement != null; - public bool CanPaste => _profileEditorService.GetCanPasteProfileElement(); - - public async Task ViewProperties() - { - await _sidebarVmFactory.SidebarProfileConfigurationViewModel(_profileEditorService.SelectedProfileConfiguration).ViewProperties(); - } - - public void DuplicateProfile() - { - ProfileConfigurationExportModel export = _profileService.ExportProfile(ProfileConfiguration); - _profileService.ImportProfile(ProfileConfiguration.Category, export, true, false, "copy"); - } - - public async Task DeleteProfile() - { - await _sidebarVmFactory.SidebarProfileConfigurationViewModel(_profileEditorService.SelectedProfileConfiguration).Delete(); - } - - public async Task ExportProfile() - { - await _sidebarVmFactory.SidebarProfileConfigurationViewModel(_profileEditorService.SelectedProfileConfiguration).Export(); - } - - public void Copy() - { - if (_profileEditorService.SelectedProfileElement != null) - _profileEditorService.CopyProfileElement(_profileEditorService.SelectedProfileElement); - } - - public void Duplicate() - { - if (_profileEditorService.SelectedProfileElement != null) - _profileEditorService.DuplicateProfileElement(_profileEditorService.SelectedProfileElement); - } - - public void Paste() - { - if (_profileEditorService.SelectedProfileElement != null && _profileEditorService.SelectedProfileElement.Parent is Folder parent) - _profileEditorService.PasteProfileElement(parent, _profileEditorService.SelectedProfileElement.Order - 1); - else - { - Folder rootFolder = _profileEditorService.SelectedProfile?.GetRootFolder(); - if (rootFolder != null) - _profileEditorService.PasteProfileElement(rootFolder, rootFolder.Children.Count); - } - } - - public void OpenProfileScripts() - { - _windowManager.ShowDialog(_scriptVmFactory.ScriptsDialogViewModel(ProfileConfiguration.Profile)); - } - - public void OpenLayerScripts() - { - if (_profileEditorService.SelectedProfileElement is Layer layer) - _windowManager.ShowDialog(_scriptVmFactory.ScriptsDialogViewModel(layer)); - } - - public void OpenLayerPropertyScripts() - { - - } - - public void OpenDebugger() - { - _debugService.ShowDebugger(); - } - - public void OpenUrl(string url) - { - Core.Utilities.OpenUrl(url); - } - - public void EditMenuOpened() - { - NotifyOfPropertyChange(nameof(CanPaste)); - } - - #endregion - protected override void OnInitialActivate() { @@ -324,5 +238,89 @@ namespace Artemis.UI.Screens.ProfileEditor BottomPanelsHeight.Save(); ElementPropertiesWidth.Save(); } + + #region Menu + + public bool HasSelectedElement => _profileEditorService.SelectedProfileElement != null; + public bool CanPaste => _profileEditorService.GetCanPasteProfileElement(); + + public async Task ViewProperties() + { + await _sidebarVmFactory.SidebarProfileConfigurationViewModel(_profileEditorService.SelectedProfileConfiguration).ViewProperties(); + } + + public void DuplicateProfile() + { + ProfileConfigurationExportModel export = _profileService.ExportProfile(ProfileConfiguration); + _profileService.ImportProfile(ProfileConfiguration.Category, export, true, false, "copy"); + } + + public async Task DeleteProfile() + { + await _sidebarVmFactory.SidebarProfileConfigurationViewModel(_profileEditorService.SelectedProfileConfiguration).Delete(); + } + + public async Task ExportProfile() + { + await _sidebarVmFactory.SidebarProfileConfigurationViewModel(_profileEditorService.SelectedProfileConfiguration).Export(); + } + + public void Copy() + { + if (_profileEditorService.SelectedProfileElement != null) + _profileEditorService.CopyProfileElement(_profileEditorService.SelectedProfileElement); + } + + public void Duplicate() + { + if (_profileEditorService.SelectedProfileElement != null) + _profileEditorService.DuplicateProfileElement(_profileEditorService.SelectedProfileElement); + } + + public void Paste() + { + if (_profileEditorService.SelectedProfileElement != null && _profileEditorService.SelectedProfileElement.Parent is Folder parent) + { + _profileEditorService.PasteProfileElement(parent, _profileEditorService.SelectedProfileElement.Order - 1); + } + else + { + Folder rootFolder = _profileEditorService.SelectedProfile?.GetRootFolder(); + if (rootFolder != null) + _profileEditorService.PasteProfileElement(rootFolder, rootFolder.Children.Count); + } + } + + public void OpenProfileScripts() + { + _windowManager.ShowWindow(_scriptVmFactory.ScriptsDialogViewModel(ProfileConfiguration.Profile)); + } + + public void OpenLayerScripts() + { + if (_profileEditorService.SelectedProfileElement is Layer layer) + _windowManager.ShowWindow(_scriptVmFactory.ScriptsDialogViewModel(layer)); + } + + public void OpenLayerPropertyScripts() + { + } + + public void OpenDebugger() + { + _debugService.ShowDebugger(); + } + + public void OpenUrl(string url) + { + Core.Utilities.OpenUrl(url); + } + + public void EditMenuOpened() + { + NotifyOfPropertyChange(nameof(CanPaste)); + } + + #endregion } } \ No newline at end of file diff --git a/src/Artemis.UI/Screens/ProfileEditor/Visualization/Tools/EditToolViewModel.cs b/src/Artemis.UI/Screens/ProfileEditor/Visualization/Tools/EditToolViewModel.cs index e8351bc62..8e85a3b1f 100644 --- a/src/Artemis.UI/Screens/ProfileEditor/Visualization/Tools/EditToolViewModel.cs +++ b/src/Artemis.UI/Screens/ProfileEditor/Visualization/Tools/EditToolViewModel.cs @@ -57,10 +57,11 @@ namespace Artemis.UI.Screens.ProfileEditor.Visualization.Tools ShapePath = _layerEditorService.GetLayerPath(layer, true, true, true); ShapeAnchor = _layerEditorService.GetLayerAnchorPosition(layer).ToSKPoint(); - Rect layerBounds = _layerEditorService.GetLayerBounds(layer); - TransformGroup layerTransformGroup = _layerEditorService.GetLayerTransformGroup(layer); Execute.PostToUIThread(() => { + Rect layerBounds = _layerEditorService.GetLayerBounds(layer); + TransformGroup layerTransformGroup = _layerEditorService.GetLayerTransformGroup(layer); + RectangleGeometry shapeGeometry = new(layerBounds) {Transform = layerTransformGroup}; shapeGeometry.Freeze(); ShapeGeometry = shapeGeometry; diff --git a/src/Artemis.UI/Screens/Scripting/ScriptsDialogViewModel.cs b/src/Artemis.UI/Screens/Scripting/ScriptsDialogViewModel.cs index 8a95db8de..1067b8181 100644 --- a/src/Artemis.UI/Screens/Scripting/ScriptsDialogViewModel.cs +++ b/src/Artemis.UI/Screens/Scripting/ScriptsDialogViewModel.cs @@ -15,16 +15,23 @@ namespace Artemis.UI.Screens.Scripting { private readonly IScriptingService _scriptingService; private readonly IDialogService _dialogService; + private readonly IProfileService _profileService; private readonly IProfileEditorService _profileEditorService; private readonly IScriptVmFactory _scriptVmFactory; public Profile Profile { get; } public Layer Layer { get; } public ILayerProperty LayerProperty { get; } - public ScriptsDialogViewModel(Profile profile, IScriptingService scriptingService, IDialogService dialogService, IProfileEditorService profileEditorService, IScriptVmFactory scriptVmFactory) + public ScriptsDialogViewModel(Profile profile, + IScriptingService scriptingService, + IDialogService dialogService, + IProfileService profileService, + IProfileEditorService profileEditorService, + IScriptVmFactory scriptVmFactory) { _scriptingService = scriptingService; _dialogService = dialogService; + _profileService = profileService; _profileEditorService = profileEditorService; _scriptVmFactory = scriptVmFactory; @@ -82,7 +89,10 @@ namespace Artemis.UI.Screens.Scripting /// protected override void OnClose() { - _profileEditorService.SaveSelectedProfileConfiguration(); + if (_profileEditorService.SelectedProfile == Profile) + _profileEditorService.SaveSelectedProfileConfiguration(); + else + _profileService.SaveProfile(Profile, false); base.OnClose(); } diff --git a/src/Artemis.UI/Screens/Settings/Debug/Tabs/RenderDebugViewModel.cs b/src/Artemis.UI/Screens/Settings/Debug/Tabs/RenderDebugViewModel.cs index d7532cb2c..c1de5b041 100644 --- a/src/Artemis.UI/Screens/Settings/Debug/Tabs/RenderDebugViewModel.cs +++ b/src/Artemis.UI/Screens/Settings/Debug/Tabs/RenderDebugViewModel.cs @@ -1,5 +1,7 @@ using System; +using System.Diagnostics; using System.IO; +using System.Timers; using System.Windows; using System.Windows.Media; using System.Windows.Media.Imaging; @@ -15,22 +17,24 @@ namespace Artemis.UI.Screens.Settings.Debug.Tabs public class RenderDebugViewModel : Screen { private readonly ICoreService _coreService; + private readonly Timer _fpsTimer; private double _currentFps; - private ImageSource _currentFrame; + private WriteableBitmap _currentFrame; private int _renderWidth; private int _renderHeight; private string _frameTargetPath; private string _renderer; private int _frames; - private DateTime _frameCountStart; public RenderDebugViewModel(ICoreService coreService) { DisplayName = "RENDERING"; _coreService = coreService; + _fpsTimer = new Timer(1000); + _fpsTimer.Start(); } - public ImageSource CurrentFrame + public WriteableBitmap CurrentFrame { get => _currentFrame; set => SetAndNotify(ref _currentFrame, value); @@ -77,19 +81,28 @@ namespace Artemis.UI.Screens.Settings.Debug.Tabs protected override void OnActivate() { _coreService.FrameRendered += CoreServiceOnFrameRendered; - _coreService.FrameRendering += CoreServiceOnFrameRendering; + _fpsTimer.Elapsed += FpsTimerOnElapsed; base.OnActivate(); } protected override void OnDeactivate() { _coreService.FrameRendered -= CoreServiceOnFrameRendered; - _coreService.FrameRendering -= CoreServiceOnFrameRendering; + _fpsTimer.Elapsed -= FpsTimerOnElapsed; base.OnDeactivate(); } + protected override void OnClose() + { + _fpsTimer.Dispose(); + base.OnClose(); + } + + private void CoreServiceOnFrameRendered(object sender, FrameRenderedEventArgs e) { + _frames++; + using SKImage skImage = e.Texture.Surface.Snapshot(); SKImageInfo bitmapInfo = e.Texture.ImageInfo; @@ -106,42 +119,36 @@ namespace Artemis.UI.Screens.Settings.Debug.Tabs _frameTargetPath = null; } + RenderHeight = bitmapInfo.Height; + RenderWidth = bitmapInfo.Width; + Execute.OnUIThreadSync(() => { - RenderHeight = bitmapInfo.Height; - RenderWidth = bitmapInfo.Width; - // ReSharper disable twice CompareOfFloatsByEqualityOperator - if (CurrentFrame is not WriteableBitmap writable || writable.Width != bitmapInfo.Width || writable.Height != bitmapInfo.Height) + if (CurrentFrame == null || CurrentFrame.Width != bitmapInfo.Width || CurrentFrame.Height != bitmapInfo.Height) { CurrentFrame = e.Texture.Surface.Snapshot().ToWriteableBitmap(); return; } - writable.Lock(); - using (SKPixmap pixmap = new(bitmapInfo, writable.BackBuffer, writable.BackBufferStride)) + CurrentFrame.Lock(); + using (SKPixmap pixmap = new(bitmapInfo, CurrentFrame.BackBuffer, CurrentFrame.BackBufferStride)) { // ReSharper disable once AccessToDisposedClosure - Looks fine skImage.ReadPixels(pixmap, 0, 0); } - writable.AddDirtyRect(new Int32Rect(0, 0, writable.PixelWidth, writable.PixelHeight)); - writable.Unlock(); + CurrentFrame.AddDirtyRect(new Int32Rect(0, 0, CurrentFrame.PixelWidth, CurrentFrame.PixelHeight)); + CurrentFrame.Unlock(); }); } - private void CoreServiceOnFrameRendering(object sender, FrameRenderingEventArgs e) + private void FpsTimerOnElapsed(object sender, ElapsedEventArgs e) { - if (DateTime.Now - _frameCountStart >= TimeSpan.FromSeconds(1)) - { - CurrentFps = _frames; - Renderer = Constants.ManagedGraphicsContext != null ? Constants.ManagedGraphicsContext.GetType().Name : "Software"; - - _frames = 0; - _frameCountStart = DateTime.Now; - } - - _frames++; + CurrentFps = _frames; + // Renderer = Constants.ManagedGraphicsContext != null ? Constants.ManagedGraphicsContext.GetType().Name : "Software"; + Renderer = $"HighAccuracyTimers: {Stopwatch.IsHighResolution}"; + _frames = 0; } } } \ No newline at end of file