diff --git a/D4Companion.Entities/SettingsD4.cs b/D4Companion.Entities/SettingsD4.cs index 36eb1b45..e4ea6376 100644 --- a/D4Companion.Entities/SettingsD4.cs +++ b/D4Companion.Entities/SettingsD4.cs @@ -47,6 +47,14 @@ public class SettingsD4 KeyGestureModifier = ModifierKeys.Control }; + public KeyBindingConfig KeyBindingConfigTakeScreenshot { get; set; } = new KeyBindingConfig + { + IsEnabled = false, + Name = "Take Screenshot", + KeyGestureKey = Key.F10, + KeyGestureModifier = ModifierKeys.Control + }; + public KeyBindingConfig KeyBindingConfigToggleOverlay { get; set; } = new KeyBindingConfig { IsEnabled = false, diff --git a/D4Companion.Events/ScreenCaptureEvents.cs b/D4Companion.Events/ScreenCaptureEvents.cs index b989f6b1..ce6c36ce 100644 --- a/D4Companion.Events/ScreenCaptureEvents.cs +++ b/D4Companion.Events/ScreenCaptureEvents.cs @@ -23,6 +23,11 @@ public class ScreenCaptureReadyEventParams public Bitmap? CurrentScreen { get; set; } } + public class TakeScreenshotRequestedEvent : PubSubEvent + { + + } + public class WindowHandleUpdatedEvent : PubSubEvent { diff --git a/D4Companion.Helpers/ScreenCapture.cs b/D4Companion.Helpers/ScreenCapture.cs index 75bf841b..6c2a1e89 100644 --- a/D4Companion.Helpers/ScreenCapture.cs +++ b/D4Companion.Helpers/ScreenCapture.cs @@ -1,6 +1,7 @@ using System; using System.Drawing; using System.Drawing.Imaging; +using System.IO; using System.Runtime.InteropServices; using System.Windows; using System.Windows.Interop; @@ -108,6 +109,10 @@ public Bitmap GetScreenCapture(IntPtr windowHandle) public static void WriteBitmapToFile(string filename, Bitmap? bitmap) { + // Check folder + string folder = new FileInfo(filename)?.Directory?.FullName ?? string.Empty; + if (!Directory.Exists(folder) && string.IsNullOrWhiteSpace(folder)) Directory.CreateDirectory(folder); + // Use the OpenCv save function instead // https://stackoverflow.com/questions/52100703/bug-in-windows-nets-system-drawing-savestream-imageformat-corrupt-png-pro bitmap?.Save(filename, ImageFormat.Png); diff --git a/D4Companion.Localization/Resources.Designer.cs b/D4Companion.Localization/Resources.Designer.cs index 14353cbf..640a9007 100644 --- a/D4Companion.Localization/Resources.Designer.cs +++ b/D4Companion.Localization/Resources.Designer.cs @@ -924,6 +924,15 @@ internal static string rsCapSystempresets { } } + /// + /// Looks up a localized string similar to Take Screenshot. + /// + internal static string rsCapTakeScreenshot { + get { + return ResourceManager.GetString("rsCapTakeScreenshot", resourceCulture); + } + } + /// /// Looks up a localized string similar to Aspect Counter (-). /// @@ -1374,6 +1383,15 @@ internal static string rsTooltipSwitchPreset { } } + /// + /// Looks up a localized string similar to Take a screenshot.. + /// + internal static string rsTooltipTakeScreenshot { + get { + return ResourceManager.GetString("rsTooltipTakeScreenshot", resourceCulture); + } + } + /// /// Looks up a localized string similar to Decrease the current aspect by one.. /// diff --git a/D4Companion.Localization/Resources.resx b/D4Companion.Localization/Resources.resx index 0837c8eb..8282fde6 100644 --- a/D4Companion.Localization/Resources.resx +++ b/D4Companion.Localization/Resources.resx @@ -556,4 +556,10 @@ Tooltip max height: + + Take a screenshot. + + + Take Screenshot + \ No newline at end of file diff --git a/D4Companion.Services/ScreenCaptureHandler.cs b/D4Companion.Services/ScreenCaptureHandler.cs index 9b506b4b..0088a2b6 100644 --- a/D4Companion.Services/ScreenCaptureHandler.cs +++ b/D4Companion.Services/ScreenCaptureHandler.cs @@ -21,6 +21,7 @@ public class ScreenCaptureHandler : IScreenCaptureHandler private double _delayUpdateMouse = ScreenCaptureConstants.DelayMouse; private double _delayUpdateScreen = ScreenCaptureConstants.Delay; private bool _isEnabled = false; + private bool _isSaveScreenshotRequested = false; private ScreenCapture _screenCapture = new ScreenCapture(); private int _offsetTop = 0; private int _offsetLeft = 0; @@ -34,6 +35,7 @@ public ScreenCaptureHandler(IEventAggregator eventAggregator, ILogger().Subscribe(HandleApplicationLoadedEvent); + _eventAggregator.GetEvent().Subscribe(HandleScreencaptureSaveRequestedEvent); _eventAggregator.GetEvent().Subscribe(HandleToggleDebugLockScreencaptureKeyBindingEvent); _eventAggregator.GetEvent().Subscribe(HandleToggleOverlayEvent); _eventAggregator.GetEvent().Subscribe(HandleToggleOverlayFromGUIEvent); @@ -72,6 +74,11 @@ private void HandleApplicationLoadedEvent() _ = StartScreenTask(); } + private void HandleScreencaptureSaveRequestedEvent() + { + _isSaveScreenshotRequested = true; + } + private void HandleToggleDebugLockScreencaptureKeyBindingEvent() { IsScreencaptureLocked = !IsScreencaptureLocked; @@ -187,6 +194,12 @@ private void UpdateScreen() //_currentScreen = new Bitmap("debug-path-to-image"); } + if (_isSaveScreenshotRequested) + { + _isSaveScreenshotRequested = false; + ScreenCapture.WriteBitmapToFile($"Screenshots/{_settingsManager.Settings.SelectedSystemPreset}_{DateTime.Now.ToFileTimeUtc()}.png", _currentScreen); + } + _eventAggregator.GetEvent().Publish(new ScreenCaptureReadyEventParams { CurrentScreen = _currentScreen @@ -194,8 +207,6 @@ private void UpdateScreen() } _delayUpdateScreen = ScreenCaptureConstants.Delay; - - //ScreenCapture.WriteBitmapToFile($"Logging/Screen_{DateTime.Now.ToFileTimeUtc()}.png", _currentScreen); } else { diff --git a/D4Companion/ViewModels/DebugViewModel.cs b/D4Companion/ViewModels/DebugViewModel.cs index ff19a37e..79a44f7e 100644 --- a/D4Companion/ViewModels/DebugViewModel.cs +++ b/D4Companion/ViewModels/DebugViewModel.cs @@ -72,6 +72,7 @@ public DebugViewModel(IEventAggregator eventAggregator, ILogger // Init View commands ReloadSystemPresetImagesCommand = new DelegateCommand(ReloadSystemPresetImagesExecute); ResetPerformceResultsCommand = new DelegateCommand(ResetPerformceResultsExecute); + TakeScreenshotCommand = new DelegateCommand(TakeScreenshotExecute); // Init InitGraph(); @@ -94,7 +95,9 @@ public DebugViewModel(IEventAggregator eventAggregator, ILogger public DelegateCommand ReloadSystemPresetImagesCommand { get; } public DelegateCommand ResetPerformceResultsCommand { get; } - + public DelegateCommand TakeScreenshotCommand { get; } + + public int AffixAreaHeightOffsetTop { get => _settingsManager.Settings.AffixAreaHeightOffsetTop; @@ -551,6 +554,12 @@ private void ResetPerformceResultsExecute() } } + + private void TakeScreenshotExecute() + { + _eventAggregator.GetEvent().Publish(); + } + #endregion // Start of Methods region diff --git a/D4Companion/ViewModels/Dialogs/HotkeysConfigViewModel.cs b/D4Companion/ViewModels/Dialogs/HotkeysConfigViewModel.cs index eac6dbcf..34302c1c 100644 --- a/D4Companion/ViewModels/Dialogs/HotkeysConfigViewModel.cs +++ b/D4Companion/ViewModels/Dialogs/HotkeysConfigViewModel.cs @@ -40,6 +40,7 @@ public HotkeysConfigViewModel(Action closeHandler) CloseCommand = new DelegateCommand(closeHandler); HotkeysConfigDoneCommand = new DelegateCommand(HotkeysConfigDoneExecute); KeyBindingConfigSwitchPresetCommand = new DelegateCommand(KeyBindingConfigExecute); + KeyBindingConfigTakeScreenshotCommand = new DelegateCommand(KeyBindingConfigExecute); KeyBindingConfigToggleOverlayCommand = new DelegateCommand(KeyBindingConfigExecute); KeyBindingConfigAspectCounterIncreaseCommand = new DelegateCommand(KeyBindingConfigExecute); KeyBindingConfigAspectCounterDecreaseCommand = new DelegateCommand(KeyBindingConfigExecute); @@ -47,6 +48,7 @@ public HotkeysConfigViewModel(Action closeHandler) KeyBindingConfigToggleDebugLockScreencaptureCommand = new DelegateCommand(KeyBindingConfigExecute); ToggleKeybindingOverlayCommand = new DelegateCommand(ToggleKeybindingExecute); ToggleKeybindingPresetsCommand = new DelegateCommand(ToggleKeybindingExecute); + ToggleKeybindingTakeScreenshotCommand = new DelegateCommand(ToggleKeybindingExecute); ToggleKeybindingAspectCounterIncreaseCommand = new DelegateCommand(ToggleKeybindingExecute); ToggleKeybindingAspectCounterDecreaseCommand = new DelegateCommand(ToggleKeybindingExecute); ToggleKeybindingAspectCounterResetCommand = new DelegateCommand(ToggleKeybindingExecute); @@ -69,13 +71,15 @@ public HotkeysConfigViewModel(Action closeHandler) public DelegateCommand CloseCommand { get; } public DelegateCommand HotkeysConfigDoneCommand { get; } public DelegateCommand KeyBindingConfigSwitchPresetCommand { get; } + public DelegateCommand KeyBindingConfigTakeScreenshotCommand { get; } public DelegateCommand KeyBindingConfigToggleOverlayCommand { get; } public DelegateCommand KeyBindingConfigAspectCounterIncreaseCommand { get; } public DelegateCommand KeyBindingConfigAspectCounterDecreaseCommand { get; } public DelegateCommand KeyBindingConfigAspectCounterResetCommand { get; } public DelegateCommand KeyBindingConfigToggleDebugLockScreencaptureCommand { get; } - public DelegateCommand ToggleKeybindingPresetsCommand { get; set; } public DelegateCommand ToggleKeybindingOverlayCommand { get; set; } + public DelegateCommand ToggleKeybindingPresetsCommand { get; set; } + public DelegateCommand ToggleKeybindingTakeScreenshotCommand { get; set; } public DelegateCommand ToggleKeybindingAspectCounterIncreaseCommand { get; set; } public DelegateCommand ToggleKeybindingAspectCounterDecreaseCommand { get; set; } public DelegateCommand ToggleKeybindingAspectCounterResetCommand { get; set; } @@ -96,6 +100,21 @@ public KeyBindingConfig KeyBindingConfigSwitchPreset } } + public KeyBindingConfig KeyBindingConfigTakeScreenshot + { + get => _settingsManager.Settings.KeyBindingConfigTakeScreenshot; + set + { + if (value != null) + { + _settingsManager.Settings.KeyBindingConfigTakeScreenshot = value; + RaisePropertyChanged(nameof(KeyBindingConfigTakeScreenshot)); + + _settingsManager.SaveSettings(); + } + } + } + public KeyBindingConfig KeyBindingConfigToggleOverlay { get => _settingsManager.Settings.KeyBindingConfigToggleOverlay; @@ -195,6 +214,7 @@ private async void KeyBindingConfigExecute(object obj) _settingsManager.SaveSettings(); RaisePropertyChanged(nameof(KeyBindingConfigSwitchPreset)); + RaisePropertyChanged(nameof(KeyBindingConfigTakeScreenshot)); RaisePropertyChanged(nameof(KeyBindingConfigToggleOverlay)); RaisePropertyChanged(nameof(KeyBindingConfigAspectCounterIncrease)); RaisePropertyChanged(nameof(KeyBindingConfigAspectCounterDecrease)); diff --git a/D4Companion/ViewModels/MainWindowViewModel.cs b/D4Companion/ViewModels/MainWindowViewModel.cs index 445cfba7..77e58cf5 100644 --- a/D4Companion/ViewModels/MainWindowViewModel.cs +++ b/D4Companion/ViewModels/MainWindowViewModel.cs @@ -229,6 +229,13 @@ private void SwitchPresetKeyBindingExecute(object? sender, HotkeyEventArgs hotke _eventAggregator.GetEvent().Publish(); } + private void TakeScreenshotKeyBindingExecute(object? sender, HotkeyEventArgs hotkeyEventArgs) + { + hotkeyEventArgs.Handled = true; + _eventAggregator.GetEvent().Publish(); + } + + private void ToggleOverlayKeyBindingExecute(object? sender, HotkeyEventArgs hotkeyEventArgs) { hotkeyEventArgs.Handled = true; @@ -286,6 +293,7 @@ private void InitKeyBindings() try { KeyBindingConfig switchPresetKeyBindingConfig = _settingsManager.Settings.KeyBindingConfigSwitchPreset; + KeyBindingConfig takeScreenshotBindingConfig = _settingsManager.Settings.KeyBindingConfigTakeScreenshot; KeyBindingConfig toggleOverlayKeyBindingConfig = _settingsManager.Settings.KeyBindingConfigToggleOverlay; KeyBindingConfig aspectCounterIncreaseKeyBindingConfig = _settingsManager.Settings.KeyBindingConfigAspectCounterIncrease; KeyBindingConfig aspectCounterDecreaseKeyBindingConfig = _settingsManager.Settings.KeyBindingConfigAspectCounterDecrease; @@ -295,6 +303,7 @@ private void InitKeyBindings() HotkeyManager.HotkeyAlreadyRegistered += HotkeyManager_HotkeyAlreadyRegistered; KeyGesture switchPresetKeyGesture = new KeyGesture(switchPresetKeyBindingConfig.KeyGestureKey, switchPresetKeyBindingConfig.KeyGestureModifier); + KeyGesture takeScreenshotKeyGesture = new KeyGesture(takeScreenshotBindingConfig.KeyGestureKey, takeScreenshotBindingConfig.KeyGestureModifier); KeyGesture toggleOverlayKeyGesture = new KeyGesture(toggleOverlayKeyBindingConfig.KeyGestureKey, toggleOverlayKeyBindingConfig.KeyGestureModifier); KeyGesture aspectCounterIncreaseKeyGesture = new KeyGesture(aspectCounterIncreaseKeyBindingConfig.KeyGestureKey, aspectCounterIncreaseKeyBindingConfig.KeyGestureModifier); KeyGesture aspectCounterDecreaseKeyGesture = new KeyGesture(aspectCounterDecreaseKeyBindingConfig.KeyGestureKey, aspectCounterDecreaseKeyBindingConfig.KeyGestureModifier); @@ -310,6 +319,15 @@ private void InitKeyBindings() HotkeyManager.Current.Remove(switchPresetKeyBindingConfig.Name); } + if (takeScreenshotBindingConfig.IsEnabled) + { + HotkeyManager.Current.AddOrReplace(takeScreenshotBindingConfig.Name, takeScreenshotKeyGesture, TakeScreenshotKeyBindingExecute); + } + else + { + HotkeyManager.Current.Remove(takeScreenshotBindingConfig.Name); + } + if (toggleOverlayKeyBindingConfig.IsEnabled) { HotkeyManager.Current.AddOrReplace(toggleOverlayKeyBindingConfig.Name, toggleOverlayKeyGesture, ToggleOverlayKeyBindingExecute); diff --git a/D4Companion/Views/DebugView.xaml b/D4Companion/Views/DebugView.xaml index 48c39b37..b85fb99b 100644 --- a/D4Companion/Views/DebugView.xaml +++ b/D4Companion/Views/DebugView.xaml @@ -42,6 +42,13 @@ OnContent="{loc:LocExtension rsCapTopMost}" ToolTip="{loc:LocExtension rsTooltipTopMost}"/> + +