diff --git a/client/src/context/context.ts b/client/src/context/context.ts new file mode 100644 index 00000000..6073799b --- /dev/null +++ b/client/src/context/context.ts @@ -0,0 +1,22 @@ +import { ShortcutManager } from "../shortcut/shortcutmanager"; + + +export interface ContextInterface { + +} + +export class Context { + + #shortcutManager: ShortcutManager; + + constructor( config:ContextInterface ) { + + this.#shortcutManager = new ShortcutManager(); + + } + + getShortcutManager() { + return this.#shortcutManager; + } + +} \ No newline at end of file diff --git a/client/src/context/contextmanager.ts b/client/src/context/contextmanager.ts new file mode 100644 index 00000000..46bcb7dd --- /dev/null +++ b/client/src/context/contextmanager.ts @@ -0,0 +1,43 @@ +import { Manager } from "../other/manager"; +import { Context, ContextInterface } from "./context"; + +export class ContextManager extends Manager { + + #currentContext!:string; + + constructor() { + super(); + } + + add( name:string, contextConfig:ContextInterface ) { + super.add( name, new Context( contextConfig ) ); + + if ( Object.values( this.getAll() ).length === 1 ) { + this.#currentContext = name; + } + + return this; + } + + currentContextIs( contextName:string ) { + return contextName === this.#currentContext; + } + + getCurrentContext() { + const contexts = this.getAll(); + + return ( contexts.hasOwnProperty( this.#currentContext ) ) ? contexts[this.#currentContext] : false; + } + + setContext( contextName:string ) { + + if ( !this.get( contextName ) ) { + console.error( `setContext(): context name "${contextName}" does not exist.` ); + return false; + } + this.#currentContext = contextName; + + console.log( `Setting context to "${this.#currentContext}".` ); + } + +} \ No newline at end of file diff --git a/client/src/context/contextmenumanager.ts b/client/src/context/contextmenumanager.ts new file mode 100644 index 00000000..296fc7ec --- /dev/null +++ b/client/src/context/contextmenumanager.ts @@ -0,0 +1,15 @@ +import { ContextMenu } from "../contextmenus/contextmenu"; +import { Manager } from "../other/manager"; + +export class ContextMenuManager extends Manager { + + constructor() { + super(); + } + + add( name:string, contextMenu:ContextMenu ) { + super.add( name, contextMenu ); + return this; + } + +} \ No newline at end of file diff --git a/client/src/interfaces.ts b/client/src/interfaces.ts index c5d45f36..dc4931a0 100644 --- a/client/src/interfaces.ts +++ b/client/src/interfaces.ts @@ -263,6 +263,7 @@ export interface Listener { export interface ShortcutOptions { altKey?: boolean; callback: CallableFunction; + context?: string; ctrlKey?: boolean; name?: string; shiftKey?: boolean; diff --git a/client/src/olympusapp.ts b/client/src/olympusapp.ts index a8d9483d..83a4370c 100644 --- a/client/src/olympusapp.ts +++ b/client/src/olympusapp.ts @@ -25,6 +25,8 @@ import { groundUnitDatabase } from "./unit/databases/groundunitdatabase"; import { navyUnitDatabase } from "./unit/databases/navyunitdatabase"; import { ConfigurationOptions } from "./interfaces"; import { UnitListPanel } from "./panels/unitlistpanel"; +import { ContextManager } from "./context/contextmanager"; +import { Context } from "./context/context"; export class OlympusApp { /* Global data */ @@ -34,15 +36,16 @@ export class OlympusApp { #map: Map | null = null; /* Managers */ + #contextManager!: ContextManager; + #missionManager: MissionManager | null = null; + #panelsManager: Manager | null = null; + #pluginsManager: PluginsManager | null = null; + #popupsManager: Manager | null = null; #serverManager: ServerManager | null = null; + #shortcutManager!: ShortcutManager; + #toolbarsManager: Manager | null = null; #unitsManager: UnitsManager | null = null; #weaponsManager: WeaponsManager | null = null; - #missionManager: MissionManager | null = null; - #pluginsManager: PluginsManager | null = null; - #panelsManager: Manager | null = null; - #popupsManager: Manager | null = null; - #toolbarsManager: Manager | null = null; - #shortcutManager: ShortcutManager | null = null; constructor() { @@ -53,6 +56,14 @@ export class OlympusApp { return this.#map as Map; } + getCurrentContext() { + return this.getContextManager().getCurrentContext() as Context; + } + + getContextManager() { + return this.#contextManager as ContextManager; + } + getServerManager() { return this.#serverManager as ServerManager; } @@ -161,6 +172,11 @@ export class OlympusApp { start() { /* Initialize base functionalitites */ + + this.#contextManager = new ContextManager(); + this.#contextManager.add( "olympus", {} ); + this.#contextManager.add( "secondary", {} ); + this.#map = new Map('map-container'); this.#serverManager = new ServerManager(); @@ -168,10 +184,9 @@ export class OlympusApp { this.#weaponsManager = new WeaponsManager(); this.#missionManager = new MissionManager(); - this.#shortcutManager = new ShortcutManager(); - this.#panelsManager = new Manager(); this.#popupsManager = new Manager(); + this.#shortcutManager = new ShortcutManager(); this.#toolbarsManager = new Manager(); // Panels @@ -240,22 +255,28 @@ export class OlympusApp { const shortcutManager = this.getShortcutManager(); shortcutManager.addKeyboardShortcut("toggleDemo", { + "altKey": true, "callback": () => { this.getServerManager().toggleDemoEnabled(); }, - "code": "KeyT" + "code": "KeyT", + "context": "olympus", + "ctrlKey": false, + "shiftKey": true }).addKeyboardShortcut("togglePause", { "altKey": false, "callback": () => { this.getServerManager().setPaused(!this.getServerManager().getPaused()); }, "code": "Space", + "context": "olympus", "ctrlKey": false }).addKeyboardShortcut("deselectAll", { "callback": (ev: KeyboardEvent) => { this.getUnitsManager().deselectAllUnits(); }, - "code": "Escape" + "code": "Escape", + "context": "olympus" }).addKeyboardShortcut("toggleUnitLabels", { "altKey": false, "callback": () => { @@ -265,6 +286,7 @@ export class OlympusApp { } }, "code": "KeyL", + "context": "olympus", "ctrlKey": false, "shiftKey": false }).addKeyboardShortcut("toggleAcquisitionRings", { @@ -276,6 +298,7 @@ export class OlympusApp { } }, "code": "KeyE", + "context": "olympus", "ctrlKey": false, "shiftKey": false }).addKeyboardShortcut("toggleEngagementRings", { @@ -287,6 +310,7 @@ export class OlympusApp { } }, "code": "KeyQ", + "context": "olympus", "ctrlKey": false, "shiftKey": false }).addKeyboardShortcut("toggleHideShortEngagementRings", { @@ -298,6 +322,7 @@ export class OlympusApp { } }, "code": "KeyR", + "context": "olympus", "ctrlKey": false, "shiftKey": false }).addKeyboardShortcut("toggleFillEngagementRings", { @@ -309,6 +334,7 @@ export class OlympusApp { } }, "code": "KeyF", + "context": "olympus", "ctrlKey": false, "shiftKey": false }); @@ -320,6 +346,7 @@ export class OlympusApp { this.getMap().handleMapPanning(ev); }, "code": code, + "context": "olympus", "ctrlKey": false, "event": "keydown" }); @@ -328,7 +355,8 @@ export class OlympusApp { "callback": (ev: KeyboardEvent) => { this.getMap().handleMapPanning(ev); }, - "code": code + "code": code, + "context": "olympus" }); }); @@ -349,10 +377,8 @@ export class OlympusApp { }, "code": code }); - }); - // Stop hotgroup controls sending the browser to another tab - digits.forEach(code => { + // Stop hotgroup controls sending the browser to another tab document.addEventListener("keydown", (ev: KeyboardEvent) => { if (ev.code === code && ev.ctrlKey === true && ev.altKey === false && ev.shiftKey === false) { ev.preventDefault(); diff --git a/client/src/other/manager.ts b/client/src/other/manager.ts index 6ac10ea8..c889e713 100644 --- a/client/src/other/manager.ts +++ b/client/src/other/manager.ts @@ -1,7 +1,11 @@ +import { Context } from "../context/context"; + export class Manager { + #items: { [key: string]: any } = {}; constructor() { + } add(name: string, item: any) { diff --git a/client/src/shortcut/shortcut.ts b/client/src/shortcut/shortcut.ts index 09fbe79a..39c6d362 100644 --- a/client/src/shortcut/shortcut.ts +++ b/client/src/shortcut/shortcut.ts @@ -1,3 +1,4 @@ +import { getApp } from ".."; import { ShortcutKeyboardOptions, ShortcutMouseOptions, ShortcutOptions } from "../interfaces"; import { keyEventWasInInput } from "../other/utils"; @@ -19,6 +20,10 @@ export class ShortcutKeyboard extends Shortcut { super(config); document.addEventListener(config.event, (ev: any) => { + if ( typeof config.context === "string" && !getApp().getContextManager().currentContextIs( config.context ) ) { + return; + } + if (ev instanceof KeyboardEvent === false || keyEventWasInInput(ev)) { return; } diff --git a/client/src/shortcut/shortcutmanager.ts b/client/src/shortcut/shortcutmanager.ts index be8b70fe..7f9fb65e 100644 --- a/client/src/shortcut/shortcutmanager.ts +++ b/client/src/shortcut/shortcutmanager.ts @@ -1,5 +1,7 @@ +import { Context } from "../context/context"; import { ShortcutKeyboardOptions, ShortcutMouseOptions } from "../interfaces"; import { Manager } from "../other/manager"; + import { ShortcutKeyboard, ShortcutMouse } from "./shortcut"; export class ShortcutManager extends Manager {