import { EventManager } from "./events.js"; import { FloorManager } from "./floors.js"; import { GameObjManager } from "./gameobj.js"; import { LangManager } from "./lang.js"; import { TagManager } from "./tags.js"; import { UIManager } from "./ui.js"; export class EventMapperManager { #currentFocus = [null, null, null]; map; langmanager; eventmanager; floormanager; uimanager; gameobjmanager; constructor( map, mapUi, opts = { gameobj: {}, } ) { this.map = map; this.langmanager = new LangManager("en-US"); this.langmanager.mainmanager = this; this.eventmanager = new EventManager(); this.eventmanager.mainmanager = this; this.floormanager = new FloorManager(); this.floormanager.mainmanager = this; this.tagmanager = new TagManager(); this.tagmanager.mainmanager = this; this.uimanager = new UIManager(mapUi); this.uimanager.mainmanager = this; this.gameobjmanager = new GameObjManager(this.map, opts.gameobj); this.gameobjmanager.mainmanager = this; } // Init async load() { this.uimanager.setLoading(true); await this.floormanager.getFloors(); this.uimanager.mainmanagerInit(); this.uimanager.__updateFloorsHard(); this.uimanager.setLoading(false); this.uimanager.setLoading(true); await this.tagmanager.getTags(); this.uimanager.__updateSearchUI(); this.uimanager.setLoading(false); this.uimanager.setLoading(true); window.addEventListener("hashchange", () => this.hashchange()); await this.hashchange(); this.uimanager.setLoading(false); } async hashchange() { const prevID = this.#currentFocus; let hash = location.hash; if (hash.includes("#")) hash = hash.replace("#", ""); else return; const floorFocus = this.convertIdFocus(hash, "floor"); const roomFocus = this.convertIdFocus(hash, "room"); const eventFocus = this.convertIdFocus(hash, "event"); this.uimanager.setLoading(true); if (floorFocus != this.convertFocus(prevID, "floor")) await this.selectFloor(floorFocus); this.uimanager.setLoading(true); this.uimanager.setLoading(false); if (roomFocus != this.convertFocus(prevID, "room")) await this.selectRoom(roomFocus); this.uimanager.setLoading(true); this.uimanager.setLoading(false); if (eventFocus != this.convertFocus(prevID, "event")) await this.selectEvent(eventFocus); this.uimanager.setLoading(false); } async selectFloor(id) { this.floormanager.setCurrentFloor(id); this.uimanager.__updateFloorsSoft(); this.uimanager.__updateEventsSoft(); this.gameobjmanager.generateFloor(); this.gameobjmanager.zoomToFloor(); this.uimanager.setLoading(true); await this.floormanager.getRooms(); this.gameobjmanager.generateRooms(); this.uimanager.setLoading(false); this.uimanager.setLoading(true); await this.eventmanager.getEvents(); this.uimanager.updateEvents(); if (!this.uimanager.getEventsEmpty()) this.uimanager.setEventsMinimized(false); // this.uimanager.setSearchDialogOpen(false); await this.uimanager.__updateSearch(); this.uimanager.setLoading(false); } async selectRoom(id) { this.floormanager.setCurrentRoom(id); if (id == null) this.gameobjmanager.zoomToFloor(); else this.gameobjmanager.zoomToRoom(id); this.uimanager.updateEvents(); if (!this.uimanager.getEventsEmpty()) this.uimanager.setEventsMinimized(false); if (id != null) { this.uimanager.setSearchDialogOpen(false); } this.uimanager.setEventsInspector(false); } async selectEvent(id) { this.eventmanager.setCurrentEvent(id); if (id != null) { this.gameobjmanager.zoomToRoom(this.convertIdFocus(id, "room")); this.uimanager.updateInspector(); this.uimanager.setSearchDialogOpen(false); } this.uimanager.setEventsInspector(id != null); } // Focus convertFocus(id, focus) { let res; switch (focus) { case "floor": return id.slice(0, 1).toString(); case "room": res = id.slice(0, 2); if (res.length < 2) return null; return res.filter(Boolean).join("_"); case "event": res = id.slice(0, 3); if (res.length < 3) return null; return res.join("_"); } } convertIdFocus(id, focus) { const focusFromId = id.split("_"); let res; switch (focus) { case "floor": return focusFromId.slice(0, 1).toString(); case "room": res = focusFromId.slice(0, 2); if (res.length < 2) return null; return res.filter(Boolean).join("_"); case "event": res = focusFromId.slice(0, 3); if (res.length < 3) return null; return res.join("_"); } } splitIdFocus(id, focus) { const focusFromId = id.split("_"); switch (focus) { case "floor": return focusFromId.slice(0, 1); case "room": return focusFromId.slice(0, 2); case "event": return focusFromId; } } setCurrentFocus(focus, id) { if (id == null) { switch (focus) { case "floor": this.#currentFocus[0] = id; break; case "room": this.#currentFocus[1] = id; break; case "event": this.#currentFocus[2] = id; break; } } else this.#currentFocus = this.splitIdFocus(id, focus); } getCurrentFocus(focus) { switch (focus) { case "floor": return this.#currentFocus.slice(0, 1).toString(); case "room": return this.#currentFocus.slice(0, 2).filter(Boolean).join("_"); case "event": return this.currentFocusId; } } getIsFocused(focus) { switch (focus) { case "floor": return this.#currentFocus[0] != null; case "room": return this.#currentFocus[1] != null; case "event": return this.#currentFocus[2] != null; } } set currentFocusId(id) { this.#currentFocus = id.split("_"); } get currentFocusId() { return this.#currentFocus.filter(Boolean).join("_"); } // IMC shorthand (focus) getAllFocusObject(focus) { switch (focus) { case "floor": return this.floormanager.allFloors; case "room": return this.floormanager.currentFloorRooms; case "event": return this.eventmanager.currentRoomEvents; } } getCurrentFocusObject(focus) { switch (focus) { case "floor": return this.floormanager.currentFloor; case "room": return this.floormanager.currentRoom; case "event": return this.eventmanager.currentEvent; } } getCurrentLocalization() { return this.langmanager.currentLocalization; } getCurrentLangCode() { return this.langmanager.langCode; } // Inter-manager comms // I don't know if this is Horrible or Fine addManagerEventListener(manager, event, callback) { switch (manager) { case "map": return this.map.events.addEventListener(event, callback); } } removeManagerEventListener(manager, event, callback) { switch (manager) { case "map": return this.map.events.removeEventListener(event, callback); } } callManagerFunction(manager, func, ...args) { switch (manager) { case "map": return this.map[func](...args); case "lang": return this.langmanager[func](...args); case "event": return this.eventmanager[func](...args); case "floor": return this.floormanager[func](...args); case "tag": return this.tagmanager[func](...args); case "ui": return this.uimanager[func](...args); case "gameobj": return this.gameobjmanager[func](...args); } } getManagerVariable(manager, variable) { switch (manager) { case "map": return this.map[variable]; case "lang": return this.langmanager[variable]; case "event": return this.eventmanager[variable]; case "floor": return this.floormanager[variable]; case "tag": return this.tagmanager[variable]; case "ui": return this.uimanager[variable]; case "gameobj": return this.gameobjmanager[variable]; } } }