diff --git a/client/src/context/context.ts b/client/src/context/context.ts new file mode 100644 index 00000000..f92f5168 --- /dev/null +++ b/client/src/context/context.ts @@ -0,0 +1,11 @@ +export interface ContextInterface { + +} + +export class Context { + + constructor( config:ContextInterface ) { + + } + +} \ 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 f56ef00c..ac2bc16e 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,13 +36,14 @@ export class OlympusApp { #map: Map | null = null; /* Managers */ + #contextManager!: ContextManager; #dialogManager!: Manager; #missionManager: MissionManager | null = null; #panelsManager: Manager | null = null; #pluginsManager: PluginsManager | null = null; #popupsManager: Manager | null = null; #serverManager: ServerManager | null = null; - #shortcutManager: ShortcutManager | null = null; + #shortcutManager!: ShortcutManager; #toolbarsManager: Manager | null = null; #unitsManager: UnitsManager | null = null; #weaponsManager: WeaponsManager | null = null; @@ -58,6 +61,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; } @@ -166,6 +177,10 @@ export class OlympusApp { start() { /* Initialize base functionalitites */ + + this.#contextManager = new ContextManager(); + this.#contextManager.add( "olympus", {} ); + this.#map = new Map('map-container'); this.#missionManager = new MissionManager(); @@ -251,22 +266,28 @@ export class OlympusApp { const shortcutManager = this.getShortcutManager(); shortcutManager.addKeyboardShortcut("toggleDemo", { + "altKey": false, "callback": () => { this.getServerManager().toggleDemoEnabled(); }, - "code": "KeyT" + "code": "KeyT", + "context": "olympus", + "ctrlKey": false, + "shiftKey": false }).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": () => { @@ -276,6 +297,7 @@ export class OlympusApp { } }, "code": "KeyL", + "context": "olympus", "ctrlKey": false, "shiftKey": false }).addKeyboardShortcut("toggleAcquisitionRings", { @@ -287,6 +309,7 @@ export class OlympusApp { } }, "code": "KeyE", + "context": "olympus", "ctrlKey": false, "shiftKey": false }).addKeyboardShortcut("toggleEngagementRings", { @@ -298,6 +321,7 @@ export class OlympusApp { } }, "code": "KeyQ", + "context": "olympus", "ctrlKey": false, "shiftKey": false }).addKeyboardShortcut("toggleHideShortEngagementRings", { @@ -309,6 +333,7 @@ export class OlympusApp { } }, "code": "KeyR", + "context": "olympus", "ctrlKey": false, "shiftKey": false }).addKeyboardShortcut("toggleFillEngagementRings", { @@ -320,6 +345,7 @@ export class OlympusApp { } }, "code": "KeyF", + "context": "olympus", "ctrlKey": false, "shiftKey": false }); @@ -331,6 +357,7 @@ export class OlympusApp { this.getMap().handleMapPanning(ev); }, "code": code, + "context": "olympus", "ctrlKey": false, "event": "keydown" }); @@ -339,7 +366,8 @@ export class OlympusApp { "callback": (ev: KeyboardEvent) => { this.getMap().handleMapPanning(ev); }, - "code": code + "code": code, + "context": "olympus" }); }); @@ -360,10 +388,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..2e80ea66 100644 --- a/client/src/shortcut/shortcutmanager.ts +++ b/client/src/shortcut/shortcutmanager.ts @@ -1,5 +1,6 @@ import { ShortcutKeyboardOptions, ShortcutMouseOptions } from "../interfaces"; import { Manager } from "../other/manager"; + import { ShortcutKeyboard, ShortcutMouse } from "./shortcut"; export class ShortcutManager extends Manager {