Skip to content

Commit

Permalink
Fix default team icon background
Browse files Browse the repository at this point in the history
Add Hotkey framework
  • Loading branch information
floh22 committed Jul 9, 2021
1 parent a68a871 commit 4895d00
Show file tree
Hide file tree
Showing 11 changed files with 341 additions and 12 deletions.
4 changes: 2 additions & 2 deletions LCUSharp/LCUSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AssemblyVersion>1.3.15.0</AssemblyVersion>
<FileVersion>1.3.15.21188</FileVersion>
<AssemblyVersion>1.4.0.0</AssemblyVersion>
<FileVersion>1.4.0.21190</FileVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions LeagueBroadcast.Common/LeagueBroadcast.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AssemblyVersion>1.3.29.0</AssemblyVersion>
<FileVersion>1.3.29.21188</FileVersion>
<AssemblyVersion>1.4.0.0</AssemblyVersion>
<FileVersion>1.4.0.21190</FileVersion>
<LangVersion>latest</LangVersion>
</PropertyGroup>

Expand Down
4 changes: 2 additions & 2 deletions LeagueBroadcast.Farsight/LeagueBroadcast.Farsight.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<LangVersion>latest</LangVersion>
<AssemblyVersion>1.3.45.0</AssemblyVersion>
<FileVersion>1.3.45.21188</FileVersion>
<AssemblyVersion>1.4.0.0</AssemblyVersion>
<FileVersion>1.4.0.21190</FileVersion>
<OutputType>Library</OutputType>
</PropertyGroup>

Expand Down
4 changes: 2 additions & 2 deletions LeagueBroadcast.Trinket/LeagueBroadcast.Trinket.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AssemblyVersion>1.3.17.0</AssemblyVersion>
<FileVersion>1.3.17.21188</FileVersion>
<AssemblyVersion>1.4.0.0</AssemblyVersion>
<FileVersion>1.4.0.21190</FileVersion>
<OutputType>Library</OutputType>
</PropertyGroup>

Expand Down
4 changes: 2 additions & 2 deletions LeagueBroadcast.Update/LeagueBroadcast.Update.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AssemblyVersion>1.3.29.0</AssemblyVersion>
<FileVersion>1.3.29.21188</FileVersion>
<AssemblyVersion>1.4.0.0</AssemblyVersion>
<FileVersion>1.4.0.21190</FileVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions LeagueBroadcast/LeagueBroadcast.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
<RepositoryUrl>https://github.com/floh22/LeagueBroadcast</RepositoryUrl>
<RepositoryType>Git</RepositoryType>
<PackageIcon>BE_icon.png</PackageIcon>
<AssemblyVersion>1.3.234.0</AssemblyVersion>
<FileVersion>1.3.234.21189</FileVersion>
<AssemblyVersion>1.4.0.0</AssemblyVersion>
<FileVersion>1.4.0.21189</FileVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
104 changes: 104 additions & 0 deletions LeagueBroadcast/MVVM/Controls/HotKeyTextBox.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
using LeagueBroadcast.MVVM.Models;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace LeagueBroadcast.MVVM.Controls
{
//Modified from https://github.com/Tyrrrz/LightBulb/blob/master/LightBulb/Views/Controls/HotKeyTextBox.cs
public class HotKeyTextBox : TextBox
{
public static readonly DependencyProperty HotKeyProperty =
DependencyProperty.Register(nameof(HotKey), typeof(HotKey), typeof(HotKeyTextBox),
new FrameworkPropertyMetadata(default(HotKey), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, HotKeyChanged));

private static void HotKeyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
if (sender is HotKeyTextBox control)
{
control.Text = control.HotKey.ToString();
}
}

public HotKey HotKey
{
get => (HotKey)GetValue(HotKeyProperty);
set => SetValue(HotKeyProperty, value);
}

public HotKeyTextBox()
{
IsReadOnly = true;
IsReadOnlyCaretVisible = false;
IsUndoEnabled = false;

if (ContextMenu is not null)
ContextMenu.Visibility = Visibility.Collapsed;

Text = HotKey.ToString();
}

private static bool HasKeyChar(Key key) =>
// A - Z
key >= Key.A && key <= Key.Z ||
// 0 - 9
key >= Key.D0 && key <= Key.D9 ||
// Numpad 0 - 9
key >= Key.NumPad0 && key <= Key.NumPad9 ||
// The rest
key == Key.OemQuestion || key == Key.OemQuotes || key == Key.OemPlus || key == Key.OemOpenBrackets || key == Key.OemCloseBrackets ||
key == Key.OemMinus || key == Key.DeadCharProcessed || key == Key.Oem1 || key == Key.Oem5 || key == Key.Oem7 || key == Key.OemPeriod || key == Key.OemComma || key == Key.Add ||
key == Key.Divide || key == Key.Multiply || key == Key.Subtract || key == Key.Oem102 || key == Key.Decimal;

protected override void OnPreviewKeyDown(KeyEventArgs e)
{
e.Handled = true;

// Get modifiers and key data
var modifiers = Keyboard.Modifiers;
var key = e.Key;

// If nothing was pressed - return
if (key == Key.None)
return;

// If Alt is used as modifier - the key needs to be extracted from SystemKey
if (key == Key.System)
key = e.SystemKey;

// If Delete/Backspace/Escape is pressed without modifiers - clear current value and return
if ((key == Key.Delete || key == Key.Back || key == Key.Escape) && modifiers == ModifierKeys.None)
{
HotKey = HotKey.None;
return;
}

// If no actual key was pressed - return
if (key == Key.LeftCtrl ||
key == Key.RightCtrl ||
key == Key.LeftAlt ||
key == Key.RightAlt ||
key == Key.LeftShift ||
key == Key.RightShift ||
key == Key.LWin ||
key == Key.RWin ||
key == Key.Clear ||
key == Key.OemClear ||
key == Key.Apps)
{
return;
}

// If Enter/Space/Tab is pressed without modifiers - return
if ((key == Key.Enter || key == Key.Space || key == Key.Tab) && modifiers == ModifierKeys.None)
return;

// If key has a character and pressed without modifiers or only with Shift - return
if (HasKeyChar(key) && (modifiers == ModifierKeys.None || modifiers == ModifierKeys.Shift))
return;

// Set value
HotKey = new HotKey(key, modifiers);
}
}
}
59 changes: 59 additions & 0 deletions LeagueBroadcast/MVVM/Models/HotKey.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.Text;
using System.Windows.Input;

namespace LeagueBroadcast.MVVM.Models
{
//https://github.com/Tyrrrz/LightBulb/blob/master/LightBulb/Models/HotKey.cs
public readonly partial struct HotKey
{
public Key Key { get; }

public ModifierKeys Modifiers { get; }

public HotKey(Key key, ModifierKeys modifiers = ModifierKeys.None)
{
Key = key;
Modifiers = modifiers;
}

public override string ToString()
{
if (Key == Key.None && Modifiers == ModifierKeys.None)
return "< None >";

var buffer = new StringBuilder();

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

buffer.Append(Key);

return buffer.ToString();
}
}

public partial struct HotKey
{
public static HotKey None { get; } = new();
}

public partial struct HotKey : IEquatable<HotKey>
{
public bool Equals(HotKey other) => Key == other.Key && Modifiers == other.Modifiers;

public override bool Equals(object? obj) => obj is HotKey other && Equals(other);

public override int GetHashCode() => HashCode.Combine(Key, Modifiers);

public static bool operator ==(HotKey a, HotKey b) => a.Equals(b);

public static bool operator !=(HotKey a, HotKey b) => !(a == b);
}
}
166 changes: 166 additions & 0 deletions LeagueBroadcast/OperatingSystem/GlobalHotKey.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Input;

namespace LeagueBroadcast.OperatingSystem
{
//Modified from https://stackoverflow.com/a/65412682
public class GlobalHotKey : IDisposable
{
/// <summary>
/// Registers a global hotkey
/// </summary>
/// <param name="aKeyGesture">e.g. Alt + Shift + Control + Win + S</param>
/// <param name="aAction">Action to be called when hotkey is pressed</param>
/// <returns>true, if registration succeeded, otherwise false</returns>
public static int RegisterHotKey(string aKeyGestureString, Action aAction)
{
var c = new KeyGestureConverter();
KeyGesture aKeyGesture = (KeyGesture)c.ConvertFrom(aKeyGestureString);
return RegisterHotKey(aKeyGesture.Modifiers, aKeyGesture.Key, aAction);
}

public static int RegisterHotKey(ModifierKeys aModifier, Key aKey, Action aAction)
{
if (aModifier == ModifierKeys.None)
{
throw new ArgumentException("Modifier must not be ModifierKeys.None");
}
if (aAction is null)
{
throw new ArgumentNullException(nameof(aAction));
}

System.Windows.Forms.Keys aVirtualKeyCode = (System.Windows.Forms.Keys)KeyInterop.VirtualKeyFromKey(aKey);
currentID = currentID + 1;
bool aRegistered = RegisterHotKey(window.Handle, currentID,
(uint)aModifier | MOD_NOREPEAT,
(uint)aVirtualKeyCode);

if (aRegistered)
{
registeredHotKeys.Add(new HotKeyWithAction(aModifier, aKey, aAction));
return currentID;
}

return -1;
}

public static void UnregisterHotKey(int ID)
{
UnregisterHotKey(window.Handle, ID);
}

public void Dispose()
{
// unregister all the registered hot keys.
for (int i = currentID; i > 0; i--)
{
UnregisterHotKey(window.Handle, i);
}

// dispose the inner native window.
window.Dispose();
}

static GlobalHotKey()
{
window.KeyPressed += (s, e) =>
{
registeredHotKeys.ForEach(x =>
{
if (e.Modifier == x.Modifier && e.Key == x.Key)
{
x.Action();
}
});
};
}

private static readonly InvisibleWindowForMessages window = new InvisibleWindowForMessages();
private static int currentID;
private static uint MOD_NOREPEAT = 0x4000;
private static List<HotKeyWithAction> registeredHotKeys = new List<HotKeyWithAction>();

private class HotKeyWithAction
{

public HotKeyWithAction(ModifierKeys modifier, Key key, Action action)
{
Modifier = modifier;
Key = key;
Action = action;
}

public ModifierKeys Modifier { get; }
public Key Key { get; }
public Action Action { get; }
}

// Registers a hot key with Windows.
[DllImport("user32.dll")]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
// Unregisters the hot key with Windows.
[DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);

private class InvisibleWindowForMessages : System.Windows.Forms.NativeWindow, IDisposable
{
public InvisibleWindowForMessages()
{
CreateHandle(new System.Windows.Forms.CreateParams());
}

private static int WM_HOTKEY = 0x0312;
protected override void WndProc(ref System.Windows.Forms.Message m)
{
base.WndProc(ref m);

if (m.Msg == WM_HOTKEY)
{
var aWPFKey = KeyInterop.KeyFromVirtualKey((int)m.LParam >> 16 & 0xFFFF);
ModifierKeys modifier = (ModifierKeys)((int)m.LParam & 0xFFFF);
if (KeyPressed != null)
{
KeyPressed(this, new HotKeyPressedEventArgs(modifier, aWPFKey));
}
}
}

public class HotKeyPressedEventArgs : EventArgs
{
private ModifierKeys _modifier;
private Key _key;

internal HotKeyPressedEventArgs(ModifierKeys modifier, Key key)
{
_modifier = modifier;
_key = key;
}

public ModifierKeys Modifier
{
get { return _modifier; }
}

public Key Key
{
get { return _key; }
}
}


public event EventHandler<HotKeyPressedEventArgs> KeyPressed;

#region IDisposable Members

public void Dispose()
{
DestroyHandle();
}

#endregion
}
}
}
Binary file modified Overlays/ingame/public/backgrounds/ScoreTeamIconBGLeft.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Overlays/ingame/public/backgrounds/ScoreTeamIconBGRight.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 4895d00

Please sign in to comment.