diff --git a/client/src/index.ts b/client/src/index.ts index 2d011179..347bab58 100644 --- a/client/src/index.ts +++ b/client/src/index.ts @@ -187,10 +187,12 @@ function setupEvents( indexApp:OlympusApp ) { [ "KeyW", "KeyA", "KeyS", "KeyD", "ArrowLeft", "ArrowRight", "ArrowUp", "ArrowDown" ].forEach( code => { shortcutManager.add( `pan${code}keydown`, new ShortcutKeyboard({ + "altKey": false, "callback": ( ev:KeyboardEvent ) => { getMap().handleMapPanning(ev); }, "code": code, + "ctrlKey": false, "event": "keydown" })); }); diff --git a/client/src/plugins/helloworld/pluginhelloworld.ts b/client/src/plugins/helloworld/pluginhelloworld.ts index b024cd16..f01cbb9f 100644 --- a/client/src/plugins/helloworld/pluginhelloworld.ts +++ b/client/src/plugins/helloworld/pluginhelloworld.ts @@ -1,10 +1,13 @@ -import { IndexApp } from "../../indexapp"; import { OlympusApp } from "../../olympusapp"; import { Plugin } from "../../plugin/plugin"; +import { ShortcutManager } from "../../shortcut/shortcutmanager"; export class PluginHelloWorld extends Plugin { + #element:HTMLElement; + #shortcutManager:ShortcutManager; + constructor( olympusApp:OlympusApp ) { super( olympusApp, "HelloWorld" ); @@ -13,18 +16,74 @@ export class PluginHelloWorld extends Plugin { bar: `
CTRL: Pin tool | SHIFT: box select tool
` + z-index:999;">` } document.body.insertAdjacentHTML( "beforeend", templates.bar ); + this.#element = document.getElementById( "shortcut-bar" ); + + this.#shortcutManager = this.getOlympusApp().getShortcutManager(); + + this.#shortcutManager.onKeyDown( () => { + this.#updateText() + }); + + this.#shortcutManager.onKeyUp( () => { + this.#updateText() + }); + + this.#updateText(); + } + + #matches( combo:string[], heldKeys:string[] ) { + + if ( combo.length !== heldKeys.length ) { + return false; + } + + return combo.every( key => heldKeys.indexOf( key ) > -1 ); + + } + + #updateText() { + + const heldKeys = this.#shortcutManager.getKeysBeingHeld(); + + const combos:Array = [ + { + "keys": [], + "text": `[CTRL]: Pin tool | [SHIFT]: box select tool
[Mouse1+drag]: Move map | [Mouse2]: Spawn menu ` + }, + { + "keys": [ "ControlLeft" ], + "text": "Mouse1: drop pin" + }, + { + "keys": [ "ShiftLeft" ], + "text": "Mouse1+drag: select units" + } + ]; + + const currentCombo:any = combos.find( (combo:any) => this.#matches( combo.keys, heldKeys ) ); + + if ( currentCombo ) { + this.#element.innerHTML = currentCombo.text; + this.#element.classList.remove( "hide" ); + } else { + this.#element.classList.add( "hide" ); + } + + } + } \ No newline at end of file diff --git a/client/src/shortcut/shortcutmanager.ts b/client/src/shortcut/shortcutmanager.ts index af35324a..9e20f5b4 100644 --- a/client/src/shortcut/shortcutmanager.ts +++ b/client/src/shortcut/shortcutmanager.ts @@ -4,10 +4,26 @@ import { Shortcut } from "./shortcut"; export class ShortcutManager extends Manager { + #keysBeingHeld:string[] = []; + #keyDownCallbacks:CallableFunction[] = []; + #keyUpCallbacks:CallableFunction[] = []; + constructor( olympusApp:OlympusApp ) { super( olympusApp ); + document.addEventListener( "keydown", ( ev:KeyboardEvent ) => { + if ( this.#keysBeingHeld.indexOf( ev.code ) < 0 ) { + this.#keysBeingHeld.push( ev.code ) + } + this.#keyDownCallbacks.forEach( callback => callback( ev ) ); + }); + + document.addEventListener( "keyup", ( ev:KeyboardEvent ) => { + this.#keysBeingHeld = this.#keysBeingHeld.filter( held => held !== ev.code ); + this.#keyUpCallbacks.forEach( callback => callback( ev ) ); + }); + } add( name:string, shortcut:Shortcut ) { @@ -15,4 +31,16 @@ export class ShortcutManager extends Manager { return this; } + getKeysBeingHeld() { + return this.#keysBeingHeld; + } + + onKeyDown( callback:CallableFunction ) { + this.#keyDownCallbacks.push( callback ); + } + + onKeyUp( callback:CallableFunction ) { + this.#keyUpCallbacks.push( callback ); + } + } \ No newline at end of file