Skip to content

Commit

Permalink
Added custom hotkeys in .ini file, and more robustness
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanpmartell committed Jul 20, 2024
1 parent 16aa4e9 commit 41bf155
Show file tree
Hide file tree
Showing 7 changed files with 393 additions and 112 deletions.
44 changes: 34 additions & 10 deletions LittleWarGameClient/AddOns.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,22 @@
}
},

addOptionsMenuHotkey: function (hotkey) {
this.addCustomHotkeyToTitle("ingameMenuButton", hotkey);
},

addFriendsMenuHotkey: function (hotkey) {
this.addCustomHotkeyToTitle("friendsButton", hotkey);
},

addChatHistoryHotkey: function (hotkey) {
this.addCustomHotkeyToTitle("ingameChatHistoryButton", hotkey);
},

addFullscreenBtnHotkey: function (hotkey) {
this.addCustomHotkeyToInnerText("optionsFullscreenButton", hotkey);
},

setSmallWindowSizes: function () {
this.setElement("optionsWindow", "height: 600px;");
this.setElement("queriesWindow", "height: 600px;");
Expand Down Expand Up @@ -76,14 +92,31 @@
if (smallIdx == -1) {
element.style.cssText += styleProperty;
}
},

addCustomHotkeyToTitle: function (elementId, hotkey) {
var element = document.getElementById(elementId);
var hotkeyIdx = element.title.indexOf('[')
if (hotkeyIdx != -1) {
element.title = element.title.substring(0, hotkeyIdx).trim();
}
element.title = `${element.title} [${hotkey}]`;
},

addCustomHotkeyToInnerText: function (elementId, hotkey) {
var element = document.getElementById(elementId);
var hotkeyIdx = element.innerText.indexOf('[')
if (hotkeyIdx != -1) {
element.innerText = element.innerText.substring(0, hotkeyIdx).trim();
}
element.innerText = `${element.innerText} [${hotkey}]`;
}
};

addons.init = {
function(mouseLock, clientVersion) {
this.addExitButton();
this.addClientVersion(clientVersion);
this.addCustomHotkeysToTitles();
this.replaceMouseLockCheckbox(mouseLock);
var fullScreenButton = document.getElementById("optionsFullscreenButton");
fullScreenButton.onclick = function () {
Expand All @@ -98,15 +131,6 @@ addons.init = {
title.innerText = `${title.innerText} [Client v${clientVersion}]`;
},

addCustomHotkeysToTitles: function () {
var menuButton = document.getElementById("ingameMenuButton");
menuButton.title = `${menuButton.title} [F10]`;
var friendsButton = document.getElementById("friendsButton");
friendsButton.title = `${friendsButton.title} [F9]`;
var chatHistoryButton = document.getElementById("ingameChatHistoryButton");
chatHistoryButton.title = `${chatHistoryButton.title} [F11]`;
},

addExitButton: function () {
var exitId = "exitButton";
if (!document.getElementById(exitId)) {
Expand Down
10 changes: 7 additions & 3 deletions LittleWarGameClient/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public Form1()
settings = new Settings();
this.Size = settings.GetWindowSize();
fullScreen = new Fullscreen(this, settings);
kbHandler = new KeyboardHandler(webView, fullScreen);
kbHandler = new KeyboardHandler(webView, fullScreen, settings);
vHandler = new VersionHandler(settings);
mouseLocked = settings.GetMouseLock();
}
Expand All @@ -42,13 +42,15 @@ private void webView_NavigationStarting(object sender, Microsoft.Web.WebView2.Co
{
webView.CoreWebView2.Settings.AreDefaultContextMenusEnabled = false;
webView.CoreWebView2.Settings.AreBrowserAcceleratorKeysEnabled = false;
webView.CoreWebView2.Settings.IsStatusBarEnabled = false;
}

private void webView_NavigationCompleted(object sender, Microsoft.Web.WebView2.Core.CoreWebView2NavigationCompletedEventArgs e)
{
var addOnJS = System.IO.File.ReadAllText("AddOns.js");
webView.CoreWebView2.ExecuteScriptAsync(addOnJS);
ElementMessage.CallJSFunc(webView, $"init.function", $"{settings.GetMouseLock().ToString().ToLower()}, \"{vHandler.CurrentVersion}\"");
ElementMessage.CallJSFunc(webView, "init.function", $"{settings.GetMouseLock().ToString().ToLower()}, \"{vHandler.CurrentVersion}\"");
kbHandler.InitHotkeyNames(settings);
gameHasLoaded = true;
ResizeGameWindows();
}
Expand All @@ -72,6 +74,7 @@ private void webView_WebMessageReceived(object sender, Microsoft.Web.WebView2.Co
else
mouseLocked = false;
settings.SetMouseLock(mouseLocked);
settings.Save();
CaptureCursor();
break;
default:
Expand Down Expand Up @@ -105,7 +108,8 @@ private void Form1_Activated(object sender, EventArgs e)
private void Form1_ResizeEnd(object sender, EventArgs e)
{
CaptureCursor();
settings.SetWindowSize(this.Width, this.Height);
settings.SetWindowSize(this.Size);
settings.Save();
}

private void Form1_Resize(object sender, EventArgs e)
Expand Down
1 change: 1 addition & 0 deletions LittleWarGameClient/Fullscreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ internal void Toggle()
Enter();
}
settings.SetFullScreen(state);
settings.Save();
}

private void Enter()
Expand Down
109 changes: 76 additions & 33 deletions LittleWarGameClient/KeyboardHandler.cs
Original file line number Diff line number Diff line change
@@ -1,45 +1,95 @@
using Microsoft.Web.WebView2.WinForms;
using System;
using System.Reflection.Metadata;
using System.Reflection;

namespace LittleWarGameClient
{
internal class KeyboardHandler
{
readonly WebView2 webView;
readonly Fullscreen fullScreen;
internal KeyboardHandler(WebView2 wv, Fullscreen fs)
private readonly WebView2 webView;
private readonly Fullscreen fullScreen;
private readonly Dictionary<Keys, MethodInfo?> hotKeys = new Dictionary<Keys, MethodInfo?>();

internal KeyboardHandler(WebView2 wv, Fullscreen fs, Settings settings)
{
InitHotkeys(settings);
fullScreen = fs;
webView = wv;
webView.KeyDown += TargetWebView_KeyDown;
webView.KeyUp += TargetWebView_KeyUp;
}

private void TargetWebView_KeyUp(object? sender, KeyEventArgs e)
private void InitHotkeys(Settings settings)
{
Type type = typeof(Settings);
foreach (var methodInfo in type.GetMethods().Where(p => Attribute.IsDefined(p, typeof(Hotkey))))
{
object[] attribute = methodInfo.GetCustomAttributes(typeof(Hotkey), true);
MethodInfo? funcToCall = null;
if (attribute.Length > 0)
{
Hotkey hotkey = (Hotkey)attribute[0];
if (hotkey.FuncToCall != null)
{
Type thisType = this.GetType();
funcToCall = thisType.GetMethod(hotkey.FuncToCall, BindingFlags.NonPublic | BindingFlags.Instance);
}
}
var key = methodInfo.Invoke(settings, null);
if (key != null)
hotKeys.Add((Keys)key, funcToCall);
}
}

internal void InitHotkeyNames(Settings settings)
{
switch (e.KeyData)
Type type = typeof(Settings);
foreach (var methodInfo in type.GetMethods().Where(p => Attribute.IsDefined(p, typeof(Hotkey))))
{
case Keys.F8:
fullScreen.Toggle();
e.Handled = true;
break;
case Keys.F9:
if (sender != null)
ElementMessage.CallJSFunc((WebView2)sender, "toggleFriends");
e.Handled = true;
break;
case Keys.F10:
if (sender != null)
ElementMessage.CallJSFunc((WebView2)sender, "toggleMenu");
e.Handled = true;
break;
case Keys.F11:
if (sender != null)
ElementMessage.CallJSFunc((WebView2)sender, "toggleChat");
e.Handled = true;
break;
object[] attribute = methodInfo.GetCustomAttributes(typeof(Hotkey), true);
string? jsFuncToCall = null;
if (attribute.Length > 0)
{
Hotkey hotkey = (Hotkey)attribute[0];
jsFuncToCall = hotkey.JSFuncToCall;
}
var key = methodInfo.Invoke(settings, null);
if (jsFuncToCall != null && key != null)
ElementMessage.CallJSFunc(webView, jsFuncToCall, $"\"{(Keys)key}\"");
}
}

private void TargetWebView_KeyUp(object? sender, KeyEventArgs e)
{
if (hotKeys.ContainsKey(e.KeyData))
{
var funcToCall = hotKeys[e.KeyData];
if (funcToCall != null && sender != null)
funcToCall.Invoke(this, new object[] { (WebView2)sender });
e.Handled = true;
}
}

private void FullscreenHotkeyFunc(WebView2 sender)
{
fullScreen.Toggle();
}

private void OptionsMenuHotkeyFunc(WebView2 sender)
{
ElementMessage.CallJSFunc(sender, "toggleMenu");
}

private void ChatHistoryHotkeyFunc(WebView2 sender)
{
ElementMessage.CallJSFunc((WebView2)sender, "toggleChat");
}

private void FriendsHotkeyFunc(WebView2 sender)
{
ElementMessage.CallJSFunc((WebView2)sender, "toggleFriends");
}

private void TargetWebView_KeyDown(object? sender, KeyEventArgs e)
{
Expand All @@ -51,15 +101,8 @@ private void TargetWebView_KeyDown(object? sender, KeyEventArgs e)
return;
}
#endif
switch (e.KeyData)
{
case Keys.F8:
case Keys.F9:
case Keys.F10:
case Keys.F11:
e.Handled = true;
break;
}
if (hotKeys.ContainsKey(e.KeyData))
e.Handled = true;
}
}
}
Loading

0 comments on commit 41bf155

Please sign in to comment.