Skip to content

Commit

Permalink
Added option to take screenshots
Browse files Browse the repository at this point in the history
  • Loading branch information
josdemmers committed Mar 29, 2024
1 parent 8e27505 commit 48f485a
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 14 deletions.
8 changes: 8 additions & 0 deletions D4Companion.Entities/SettingsD4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions D4Companion.Events/ScreenCaptureEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public class ScreenCaptureReadyEventParams
public Bitmap? CurrentScreen { get; set; }
}

public class TakeScreenshotRequestedEvent : PubSubEvent
{

}

public class WindowHandleUpdatedEvent : PubSubEvent<WindowHandleUpdatedEventParams>
{

Expand Down
5 changes: 5 additions & 0 deletions D4Companion.Helpers/ScreenCapture.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Expand Down
18 changes: 18 additions & 0 deletions D4Companion.Localization/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions D4Companion.Localization/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -556,4 +556,10 @@
<data name="rsCapTooltipMaxHeight" xml:space="preserve">
<value>Tooltip max height:</value>
</data>
<data name="rsTooltipTakeScreenshot" xml:space="preserve">
<value>Take a screenshot.</value>
</data>
<data name="rsCapTakeScreenshot" xml:space="preserve">
<value>Take Screenshot</value>
</data>
</root>
15 changes: 13 additions & 2 deletions D4Companion.Services/ScreenCaptureHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -34,6 +35,7 @@ public ScreenCaptureHandler(IEventAggregator eventAggregator, ILogger<ScreenCapt
// Init IEventAggregator
_eventAggregator = eventAggregator;
_eventAggregator.GetEvent<ApplicationLoadedEvent>().Subscribe(HandleApplicationLoadedEvent);
_eventAggregator.GetEvent<TakeScreenshotRequestedEvent>().Subscribe(HandleScreencaptureSaveRequestedEvent);
_eventAggregator.GetEvent<ToggleDebugLockScreencaptureKeyBindingEvent>().Subscribe(HandleToggleDebugLockScreencaptureKeyBindingEvent);
_eventAggregator.GetEvent<ToggleOverlayEvent>().Subscribe(HandleToggleOverlayEvent);
_eventAggregator.GetEvent<ToggleOverlayFromGUIEvent>().Subscribe(HandleToggleOverlayFromGUIEvent);
Expand Down Expand Up @@ -72,6 +74,11 @@ private void HandleApplicationLoadedEvent()
_ = StartScreenTask();
}

private void HandleScreencaptureSaveRequestedEvent()
{
_isSaveScreenshotRequested = true;
}

private void HandleToggleDebugLockScreencaptureKeyBindingEvent()
{
IsScreencaptureLocked = !IsScreencaptureLocked;
Expand Down Expand Up @@ -187,15 +194,19 @@ 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<ScreenCaptureReadyEvent>().Publish(new ScreenCaptureReadyEventParams
{
CurrentScreen = _currentScreen
});
}

_delayUpdateScreen = ScreenCaptureConstants.Delay;

//ScreenCapture.WriteBitmapToFile($"Logging/Screen_{DateTime.Now.ToFileTimeUtc()}.png", _currentScreen);
}
else
{
Expand Down
11 changes: 10 additions & 1 deletion D4Companion/ViewModels/DebugViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public DebugViewModel(IEventAggregator eventAggregator, ILogger<DebugViewModel>
// Init View commands
ReloadSystemPresetImagesCommand = new DelegateCommand(ReloadSystemPresetImagesExecute);
ResetPerformceResultsCommand = new DelegateCommand(ResetPerformceResultsExecute);
TakeScreenshotCommand = new DelegateCommand(TakeScreenshotExecute);

// Init
InitGraph();
Expand All @@ -94,7 +95,9 @@ public DebugViewModel(IEventAggregator eventAggregator, ILogger<DebugViewModel>

public DelegateCommand ReloadSystemPresetImagesCommand { get; }
public DelegateCommand ResetPerformceResultsCommand { get; }

public DelegateCommand TakeScreenshotCommand { get; }


public int AffixAreaHeightOffsetTop
{
get => _settingsManager.Settings.AffixAreaHeightOffsetTop;
Expand Down Expand Up @@ -551,6 +554,12 @@ private void ResetPerformceResultsExecute()
}
}


private void TakeScreenshotExecute()
{
_eventAggregator.GetEvent<TakeScreenshotRequestedEvent>().Publish();
}

#endregion

// Start of Methods region
Expand Down
22 changes: 21 additions & 1 deletion D4Companion/ViewModels/Dialogs/HotkeysConfigViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ public HotkeysConfigViewModel(Action<HotkeysConfigViewModel> closeHandler)
CloseCommand = new DelegateCommand<HotkeysConfigViewModel>(closeHandler);
HotkeysConfigDoneCommand = new DelegateCommand(HotkeysConfigDoneExecute);
KeyBindingConfigSwitchPresetCommand = new DelegateCommand<object>(KeyBindingConfigExecute);
KeyBindingConfigTakeScreenshotCommand = new DelegateCommand<object>(KeyBindingConfigExecute);
KeyBindingConfigToggleOverlayCommand = new DelegateCommand<object>(KeyBindingConfigExecute);
KeyBindingConfigAspectCounterIncreaseCommand = new DelegateCommand<object>(KeyBindingConfigExecute);
KeyBindingConfigAspectCounterDecreaseCommand = new DelegateCommand<object>(KeyBindingConfigExecute);
KeyBindingConfigAspectCounterResetCommand = new DelegateCommand<object>(KeyBindingConfigExecute);
KeyBindingConfigToggleDebugLockScreencaptureCommand = new DelegateCommand<object>(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);
Expand All @@ -69,13 +71,15 @@ public HotkeysConfigViewModel(Action<HotkeysConfigViewModel> closeHandler)
public DelegateCommand<HotkeysConfigViewModel> CloseCommand { get; }
public DelegateCommand HotkeysConfigDoneCommand { get; }
public DelegateCommand<object> KeyBindingConfigSwitchPresetCommand { get; }
public DelegateCommand<object> KeyBindingConfigTakeScreenshotCommand { get; }
public DelegateCommand<object> KeyBindingConfigToggleOverlayCommand { get; }
public DelegateCommand<object> KeyBindingConfigAspectCounterIncreaseCommand { get; }
public DelegateCommand<object> KeyBindingConfigAspectCounterDecreaseCommand { get; }
public DelegateCommand<object> KeyBindingConfigAspectCounterResetCommand { get; }
public DelegateCommand<object> 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; }
Expand All @@ -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;
Expand Down Expand Up @@ -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));
Expand Down
18 changes: 18 additions & 0 deletions D4Companion/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,13 @@ private void SwitchPresetKeyBindingExecute(object? sender, HotkeyEventArgs hotke
_eventAggregator.GetEvent<SwitchPresetKeyBindingEvent>().Publish();
}

private void TakeScreenshotKeyBindingExecute(object? sender, HotkeyEventArgs hotkeyEventArgs)
{
hotkeyEventArgs.Handled = true;
_eventAggregator.GetEvent<TakeScreenshotRequestedEvent>().Publish();
}


private void ToggleOverlayKeyBindingExecute(object? sender, HotkeyEventArgs hotkeyEventArgs)
{
hotkeyEventArgs.Handled = true;
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions D4Companion/Views/DebugView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@
OnContent="{loc:LocExtension rsCapTopMost}"
ToolTip="{loc:LocExtension rsTooltipTopMost}"/>

<Button Margin="4 0 0 0" Height="25" Width="25"
Style="{StaticResource MahApps.Styles.Button.Circle}"
Command="{Binding TakeScreenshotCommand}"
ToolTip="{loc:LocExtension rsTooltipTakeScreenshot}">
<iconPacks:PackIconMaterial Height="15" Width="15" Kind="CameraOutline" />
</Button>

<Button Margin="4 0 0 0" Height="25" Width="25"
Style="{StaticResource MahApps.Styles.Button.Circle}"
Command="{Binding ReloadSystemPresetImagesCommand}"
Expand Down
Loading

0 comments on commit 48f485a

Please sign in to comment.