Skip to content
This repository has been archived by the owner on Mar 3, 2023. It is now read-only.

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
erri120 committed Aug 18, 2020
1 parent 5f5f02c commit 793438c
Show file tree
Hide file tree
Showing 11 changed files with 189 additions and 163 deletions.
84 changes: 0 additions & 84 deletions ScreenshotPlugin/Hotkey/GlobalHotkeyService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,102 +17,18 @@

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using System.Windows.Input;
using Extensions.Common;
using JetBrains.Annotations;
using Newtonsoft.Json;
using Playnite.SDK;

namespace ScreenshotPlugin
{
//partially from https://tyrrrz.me/blog/wndproc-in-wpf

public static partial class NativeFunctions
{
[DllImport("kernel32.dll", SetLastError=true, CharSet=CharSet.Auto)]
public static extern ushort GlobalAddAtom(string lpString);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern ushort GlobalDeleteAtom(ushort nAtom);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, ModifierKeys fsModifiers, int vk);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
}

public enum HotkeyStatus
{
[Description("Registered")]
Registered,
[Description("Failed")]
Failed,
[Description("Not Configured")]
NotConfigured
}

public class Hotkey
{
[JsonIgnore]
public ushort ID { get; set; }
[JsonProperty]
public Key KeyCode { get; set; } = Key.None;
[JsonProperty]
public HotkeyStatus Status { get; set; } = HotkeyStatus.NotConfigured;
[JsonProperty]
public ModifierKeys KeyModifiers { get; set; } = ModifierKeys.None;
[JsonIgnore]
public bool IsValidHotkey => KeyCode != Key.None;

public string DebugString()
{
return $"{(ID == 0 ? "" : $"ID: {ID} ")}Keys: \"{ToString()}\" Status: {Enum.GetName(typeof(HotkeyStatus), Status)}";
}

public override string ToString()
{
var str = new StringBuilder();

if (KeyModifiers.HasFlag(ModifierKeys.Control))
str.Append("Ctrl + ");
if (KeyModifiers.HasFlag(ModifierKeys.Shift))
str.Append("Shift + ");
if (KeyModifiers.HasFlag(ModifierKeys.Alt))
str.Append("Alt + ");
if (KeyModifiers.HasFlag(ModifierKeys.Windows))
str.Append("Win + ");

str.Append(KeyCode);

return str.ToString();
}
}

public class HotkeyComparer : IEqualityComparer<Hotkey>
{
public bool Equals(Hotkey x, Hotkey y)
{
if (x == null) return false;
if (y == null) return false;
return x.ID == y.ID && x.KeyCode == y.KeyCode && x.Status == y.Status;
}

public int GetHashCode(Hotkey obj)
{
unchecked
{
var hashCode = (int) obj.ID;
hashCode = (hashCode * 397) ^ (int) obj.KeyCode;
hashCode = (hashCode * 397) ^ (int) obj.Status;
return hashCode;
}
}
}

public class GlobalHotkeyService : IDisposable
{
private readonly SpongeWindow _sponge;
Expand Down
94 changes: 94 additions & 0 deletions ScreenshotPlugin/Hotkey/Hotkey.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// /*
// Copyright (C) 2020 erri120
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows.Input;
using Newtonsoft.Json;

namespace ScreenshotPlugin
{
public enum HotkeyStatus
{
[Description("Registered")]
Registered,
[Description("Failed")]
Failed,
[Description("Not Configured")]
NotConfigured
}

public class Hotkey
{
[JsonIgnore]
public ushort ID { get; set; }
[JsonProperty]
public Key KeyCode { get; set; } = Key.None;
[JsonProperty]
public HotkeyStatus Status { get; set; } = HotkeyStatus.NotConfigured;
[JsonProperty]
public ModifierKeys KeyModifiers { get; set; } = ModifierKeys.None;
[JsonIgnore]
public bool IsValidHotkey => KeyCode != Key.None;

public string DebugString()
{
return $"{(ID == 0 ? "" : $"ID: {ID} ")}Keys: \"{ToString()}\" Status: {Enum.GetName(typeof(HotkeyStatus), Status)}";
}

public override string ToString()
{
var str = new StringBuilder();

if (KeyModifiers.HasFlag(ModifierKeys.Control))
str.Append("Ctrl + ");
if (KeyModifiers.HasFlag(ModifierKeys.Shift))
str.Append("Shift + ");
if (KeyModifiers.HasFlag(ModifierKeys.Alt))
str.Append("Alt + ");
if (KeyModifiers.HasFlag(ModifierKeys.Windows))
str.Append("Win + ");

str.Append(KeyCode);

return str.ToString();
}
}

public class HotkeyComparer : IEqualityComparer<Hotkey>
{
public bool Equals(Hotkey x, Hotkey y)
{
if (x == null) return false;
if (y == null) return false;
return x.ID == y.ID && x.KeyCode == y.KeyCode && x.Status == y.Status;
}

public int GetHashCode(Hotkey obj)
{
unchecked
{
var hashCode = (int) obj.ID;
hashCode = (hashCode * 397) ^ (int) obj.KeyCode;
hashCode = (hashCode * 397) ^ (int) obj.Status;
return hashCode;
}
}
}
}
1 change: 0 additions & 1 deletion ScreenshotPlugin/HotkeySelectionControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public Hotkey Hotkey
public HotkeySelectionControl()
{
InitializeComponent();
//DataContext = this;
}

private void HotkeyTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
Expand Down
46 changes: 46 additions & 0 deletions ScreenshotPlugin/NativeFunctions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// /*
// Copyright (C) 2020 erri120
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// */

using System;
using System.Runtime.InteropServices;
using System.Windows.Input;
using PInvoke;

namespace ScreenshotPlugin
{
public static class NativeFunctions
{
[DllImport("kernel32.dll", SetLastError=true, CharSet=CharSet.Auto)]
public static extern ushort GlobalAddAtom(string lpString);

[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern ushort GlobalDeleteAtom(ushort nAtom);

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, ModifierKeys fsModifiers, int vk);

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);

[DllImport("dwmapi.dll", PreserveSig = false)]
public static extern bool DwmIsCompositionEnabled();

[DllImport("dwmapi.dll")]
public static extern int DwmGetWindowAttribute(IntPtr hwnd, DwmApi.DWMWINDOWATTRIBUTE dwAttribute, out RECT pvAttribute, int cbAttribute);
}
}
25 changes: 2 additions & 23 deletions ScreenshotPlugin/NativeUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,12 @@ namespace ScreenshotPlugin
{
public static class NativeUtils
{
public static int GetX(this RECT r)
{
return r.left;
}

public static int GetY(this RECT r)
{
return r.top;
}

public static int GetWidth(this RECT r)
private static int GetWidth(this RECT r)
{
return r.right - r.left;
}

public static int GetHeight(this RECT r)
private static int GetHeight(this RECT r)
{
return r.bottom - r.top;
}
Expand All @@ -46,16 +36,5 @@ public static Rectangle ToRectangle(this RECT r)
{
return new Rectangle(r.left, r.top, r.GetWidth(), r.GetHeight());
}

public static RECT ToRECT(this Rectangle r)
{
return new RECT
{
left = r.Left,
top = r.Top,
bottom = r.Bottom,
right = r.Right
};
}
}
}
14 changes: 8 additions & 6 deletions ScreenshotPlugin/ScreenshotExtensionPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,23 @@ private void GlobalHotkeyServiceOnHotkeyPress(Hotkey hotkey)
{
try
{
if (_currentlyRunningGame == null && _settings.OnlyGameScreenshots)
return;

Bitmap bitmap;
var screenshot = new Screenshot();
string region;

if (hotkey == _settings.CaptureFullscreenHotkey)
{
bitmap = screenshot.CaptureFullscreen();
bitmap = Screenshot.CaptureFullscreen();
region = "fullscreen";
} else if (hotkey == _settings.CaptureActiveMonitorHotkey)
{
bitmap = screenshot.CaptureActiveMonitor();
bitmap = Screenshot.CaptureActiveMonitor();
region = "active monitor";
} else if (hotkey == _settings.CaptureActiveWindowHotkey)
{
bitmap = screenshot.CaptureActiveWindow();
bitmap = Screenshot.CaptureActiveWindow();
region = "active window";
}
else
Expand Down Expand Up @@ -105,9 +107,9 @@ private void GlobalHotkeyServiceOnHotkeyPress(Hotkey hotkey)
{
Process.Start(path);
}
catch (Exception)
catch (Exception inner)
{
//ignored
_logger.Error(inner, $"Exception while opening {path}");
}
}));
}
Expand Down
2 changes: 2 additions & 0 deletions ScreenshotPlugin/ScreenshotPlugin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@
<DependentUpon>HotkeySelectionControl.xaml</DependentUpon>
</Compile>
<Compile Include="Hotkey\GlobalHotkeyService.cs" />
<Compile Include="Hotkey\Hotkey.cs" />
<Compile Include="Hotkey\SpongeWindow.cs" />
<Compile Include="NativeFunctions.cs" />
<Compile Include="NativeUtils.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ScreenshotExtensionPlugin.cs" />
Expand Down
9 changes: 2 additions & 7 deletions ScreenshotPlugin/ScreenshotPluginSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@
// */

using System.Collections.Generic;
using System.Drawing.Imaging;
using System.IO;
using Extensions.Common;
using JetBrains.Annotations;
using Newtonsoft.Json;
using Playnite.SDK;

namespace ScreenshotPlugin
Expand All @@ -31,6 +29,7 @@ public class ScreenshotPluginSettings : ISettings

//public bool SaveToGame { get; set; }
//public bool SaveToFolder { get; set; }
public bool OnlyGameScreenshots { get; set; }
public string ScreenshotsPath { get; set; }
public Hotkey CaptureActiveMonitorHotkey { get; set; }
public Hotkey CaptureActiveWindowHotkey { get; set; }
Expand All @@ -49,6 +48,7 @@ public ScreenshotPluginSettings(ScreenshotExtensionPlugin plugin)
{
//SaveToGame = savedSettings.SaveToGame;
//SaveToFolder = savedSettings.SaveToFolder;
OnlyGameScreenshots = savedSettings.OnlyGameScreenshots;
ScreenshotsPath = savedSettings.ScreenshotsPath;
CaptureActiveMonitorHotkey = savedSettings.CaptureActiveMonitorHotkey;
CaptureActiveWindowHotkey = savedSettings.CaptureActiveWindowHotkey;
Expand Down Expand Up @@ -90,11 +90,6 @@ public bool VerifySettings(out List<string> errors)
{
errors = new List<string>();

/*if (SaveToFolder && ScreenshotsPath.IsEmpty())
{
errors.Add("Screenshots Folder Path must not be empty if you want to save the screenshots to a folder!");
}*/

if (ScreenshotsPath.IsEmpty())
{
errors.Add("Screenshots Folder Path must not be empty!");
Expand Down
2 changes: 2 additions & 0 deletions ScreenshotPlugin/ScreenshotSettingsView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
d:DataContext="{d:DesignInstance local:ScreenshotPluginSettings}"
d:DesignHeight="450" d:DesignWidth="800">
<StackPanel Margin="16 16">
<CheckBox IsChecked="{Binding OnlyGameScreenshots}" ToolTip="Un-ticking this means you could create a screenshot of everything if Playnite and this Extension is running.">Only take Screenshots when in a game</CheckBox>

<Label Margin="0 8 0 2" Target="{Binding ElementName=ScreenshotsPathTextBox}">Screenshots Folder Path</Label>
<TextBox x:Name="ScreenshotsPathTextBox" Text="{Binding ScreenshotsPath}"/>

Expand Down
Loading

0 comments on commit 793438c

Please sign in to comment.