Merge pull request #411 from Pax1601/410-update-shortcuts-to-plugin-friendly-format

Converted for plugin use
This commit is contained in:
Pax1601 2023-10-02 09:03:30 +02:00 committed by GitHub
commit 5d4fdf1e76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 79 additions and 32 deletions

View File

@ -588,11 +588,11 @@ declare module "interfaces" {
name?: string;
shiftKey?: boolean;
}
export interface KeyboardShortcutOptions extends ShortcutOptions {
export interface ShortcutKeyboardOptions extends ShortcutOptions {
code: string;
event?: "keydown" | "keyup";
}
export interface MouseShortcutOptions extends ShortcutOptions {
export interface ShortcutMouseOptions extends ShortcutOptions {
button: number;
event: "mousedown" | "mouseup";
}
@ -1493,26 +1493,28 @@ declare module "plugin/pluginmanager" {
}
}
declare module "shortcut/shortcut" {
import { KeyboardShortcutOptions, MouseShortcutOptions, ShortcutOptions } from "interfaces";
import { ShortcutKeyboardOptions, ShortcutMouseOptions, ShortcutOptions } from "interfaces";
export abstract class Shortcut {
#private;
constructor(config: ShortcutOptions);
getConfig(): ShortcutOptions;
}
export class ShortcutKeyboard extends Shortcut {
constructor(config: KeyboardShortcutOptions);
constructor(config: ShortcutKeyboardOptions);
}
export class ShortcutMouse extends Shortcut {
constructor(config: MouseShortcutOptions);
constructor(config: ShortcutMouseOptions);
}
}
declare module "shortcut/shortcutmanager" {
import { ShortcutKeyboardOptions, ShortcutMouseOptions } from "interfaces";
import { Manager } from "other/manager";
import { Shortcut } from "shortcut/shortcut";
export class ShortcutManager extends Manager {
#private;
constructor();
add(name: string, shortcut: Shortcut): this;
add(name: string, shortcut: any): this;
addKeyboardShortcut(name: string, shortcutKeyboardOptions: ShortcutKeyboardOptions): this;
addMouseShortcut(name: string, shortcutMouseOptions: ShortcutMouseOptions): this;
getKeysBeingHeld(): string[];
keyComboMatches(combo: string[]): boolean;
onKeyDown(callback: CallableFunction): void;

View File

@ -1,3 +1,5 @@
import { OlympusPlugin } from "interfaces";
const SHOW_CONTROL_TIPS = "Show control tips"
export class ControlTipsPlugin implements OlympusPlugin {
@ -41,7 +43,7 @@ export class ControlTipsPlugin implements OlympusPlugin {
this.#updateTips();
});
document.addEventListener("unitDeselection", (ev: CustomEvent) => {
document.addEventListener("unitDeselection", (ev: CustomEventInit ) => {
this.#updateTips();
});
@ -55,7 +57,7 @@ export class ControlTipsPlugin implements OlympusPlugin {
this.#updateTips();
});
document.addEventListener("unitSelection", (ev: CustomEvent) => {
document.addEventListener("unitSelection", (ev: CustomEventInit ) => {
this.#updateTips();
});

View File

@ -258,12 +258,12 @@ export interface ShortcutOptions {
shiftKey?: boolean;
}
export interface KeyboardShortcutOptions extends ShortcutOptions {
export interface ShortcutKeyboardOptions extends ShortcutOptions {
code: string;
event?: "keydown" | "keyup";
}
export interface MouseShortcutOptions extends ShortcutOptions {
export interface ShortcutMouseOptions extends ShortcutOptions {
button: number;
event: "mousedown" | "mouseup";
}

View File

@ -238,22 +238,38 @@ export class OlympusApp {
});
const shortcutManager = this.getShortcutManager();
shortcutManager.add("toggleDemo", new ShortcutKeyboard({
shortcutManager.addKeyboardShortcut("toggleDemo", {
"callback": () => {
this.getServerManager().toggleDemoEnabled();
},
"code": "KeyT"
})).add("togglePause", new ShortcutKeyboard({
}).addKeyboardShortcut("togglePause", {
"altKey": false,
"callback": () => {
this.getServerManager().setPaused(!this.getServerManager().getPaused());
},
"code": "Space",
"ctrlKey": false
}));
}).addKeyboardShortcut( "deselectAll", {
"callback": ( ev:KeyboardEvent ) => {
this.getUnitsManager().deselectAllUnits();
},
"code": "Escape"
}).addKeyboardShortcut( "toggleUnitLabels", {
"altKey": false,
"callback": () => {
const chk = document.querySelector( `label[title="Show unit labels"] input[type="checkbox"]` );
if ( chk instanceof HTMLElement ) {
chk.click();
}
},
"code": "KeyL",
"ctrlKey": false,
"shiftKey": false
});
["KeyW", "KeyA", "KeyS", "KeyD", "ArrowLeft", "ArrowRight", "ArrowUp", "ArrowDown"].forEach(code => {
shortcutManager.add(`pan${code}keydown`, new ShortcutKeyboard({
shortcutManager.addKeyboardShortcut(`pan${code}keydown`, {
"altKey": false,
"callback": (ev: KeyboardEvent) => {
this.getMap().handleMapPanning(ev);
@ -261,30 +277,42 @@ export class OlympusApp {
"code": code,
"ctrlKey": false,
"event": "keydown"
}));
});
});
["KeyW", "KeyA", "KeyS", "KeyD", "ArrowLeft", "ArrowRight", "ArrowUp", "ArrowDown"].forEach(code => {
shortcutManager.add(`pan${code}keyup`, new ShortcutKeyboard({
shortcutManager.addKeyboardShortcut(`pan${code}keyup`, {
"callback": (ev: KeyboardEvent) => {
this.getMap().handleMapPanning(ev);
},
"code": code
}));
});
});
["Digit1", "Digit2", "Digit3", "Digit4", "Digit5", "Digit6", "Digit7", "Digit8", "Digit9"].forEach(code => {
shortcutManager.add(`hotgroup${code}`, new ShortcutKeyboard({
const digits = ["Digit1", "Digit2", "Digit3", "Digit4", "Digit5", "Digit6", "Digit7", "Digit8", "Digit9"];
digits.forEach(code => {
shortcutManager.addKeyboardShortcut(`hotgroup${code}`, {
"altKey": false,
"callback": (ev: KeyboardEvent) => {
if (ev.ctrlKey && ev.shiftKey)
this.getUnitsManager().selectedUnitsAddToHotgroup(parseInt(ev.code.substring(5)));
else if (ev.ctrlKey && !ev.shiftKey)
else if (ev.ctrlKey && !ev.shiftKey)
this.getUnitsManager().selectedUnitsSetHotgroup(parseInt(ev.code.substring(5)));
else if (!ev.ctrlKey && ev.shiftKey)
this.getUnitsManager().selectUnitsByHotgroup(parseInt(ev.code.substring(5)), false);
else
this.getUnitsManager().selectUnitsByHotgroup(parseInt(ev.code.substring(5)));
},
"code": code
}));
});
});
// Stop hotgroup controls sending the browser to another tab
digits.forEach( code => {
document.addEventListener( "keydown", ( ev:KeyboardEvent ) => {
if ( ev.code === code && ev.ctrlKey === true && ev.altKey === false && ev.shiftKey === false ) {
ev.preventDefault();
}
});
});
// TODO: move from here in dedicated class

View File

@ -1,4 +1,4 @@
import { KeyboardShortcutOptions, MouseShortcutOptions, ShortcutOptions } from "../interfaces";
import { ShortcutKeyboardOptions, ShortcutMouseOptions, ShortcutOptions } from "../interfaces";
import { keyEventWasInInput } from "../other/utils";
export abstract class Shortcut {
@ -14,7 +14,7 @@ export abstract class Shortcut {
}
export class ShortcutKeyboard extends Shortcut {
constructor(config: KeyboardShortcutOptions) {
constructor(config: ShortcutKeyboardOptions) {
config.event = config.event || "keyup";
super(config);
@ -37,7 +37,7 @@ export class ShortcutKeyboard extends Shortcut {
}
export class ShortcutMouse extends Shortcut {
constructor(config: MouseShortcutOptions) {
constructor(config: ShortcutMouseOptions) {
super(config);
}
}

View File

@ -1,5 +1,6 @@
import { ShortcutKeyboardOptions, ShortcutMouseOptions } from "../interfaces";
import { Manager } from "../other/manager";
import { Shortcut } from "./shortcut";
import { ShortcutKeyboard, ShortcutMouse } from "./shortcut";
export class ShortcutManager extends Manager {
@ -25,8 +26,18 @@ export class ShortcutManager extends Manager {
}
add(name: string, shortcut: Shortcut) {
super.add(name, shortcut);
add( name: string, shortcut:any ) {
console.error( "ShortcutManager:add() cannot be used. Use addKeyboardShortcut or addMouseShortcut." );
return this;
}
addKeyboardShortcut( name:string, shortcutKeyboardOptions:ShortcutKeyboardOptions ) {
super.add( name, new ShortcutKeyboard( shortcutKeyboardOptions ) );
return this;
}
addMouseShortcut( name:string, shortcutMouseOptions:ShortcutMouseOptions ) {
super.add( name, new ShortcutMouse( shortcutMouseOptions ) );
return this;
}

View File

@ -201,8 +201,12 @@ export class UnitsManager {
*
* @param hotgroup The hotgroup number
*/
selectUnitsByHotgroup(hotgroup: number) {
this.deselectAllUnits();
selectUnitsByHotgroup(hotgroup: number, deselectAllUnits: boolean = true ) {
if ( deselectAllUnits ) {
this.deselectAllUnits();
}
this.getUnitsByHotgroup(hotgroup).forEach((unit: Unit) => unit.setSelected(true))
}