Skip to content

Commit

Permalink
Added sidebar list of trains
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhincore committed Jun 22, 2023
1 parent 5fc950c commit 172b5df
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
30 changes: 29 additions & 1 deletion src/RenderManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from "./types/APITypes";
import { Renderer } from "./types/Renderer";
import { Stream } from "./Stream";
import { Unarray } from "./utils";
import { Unarray, getMapForDimension } from "./utils";
import { TrackBlockRenderer } from "./renderers/TrackBlockRenderer";
import { TrainRenderer } from "./renderers/TrainRenderer";
import { SignalRenderer } from "./renderers/SignalRenderer";
Expand Down Expand Up @@ -101,6 +101,34 @@ export class RenderManager {
$(dynmap).on("zoomchanged", () => {
this.update();
});

// Create sidebar lists
const trainSection = SidebarUtils.createListSection(config.layers.trains.label);
trainSection.section.appendTo(dynmap.sidebarPanel);
dynmap.sidebarSections.push(trainSection);
const trainList = trainSection.content as HTMLElement;
$(trainList).addClass("playerlist");

this.#streams.trains.onMessage((data) => {
for (const train of data.trains) {
const position = train.cars[0].leading;
const map = getMapForDimension(position.dimension, dynmap, config);

let $el = $("#train-" + train.id);
if (!$el.length) {
$el = $("<li>")
.attr("id", "train-" + train.id)
.addClass("player")
.append($("<a>").attr("href", "#").text(train.name))
.appendTo(trainList);
}
$el.off();
$el.on("click", () => {
if (dynmap.world.name == map?.options.world.name) dynmap.panToLocation(position.location);
else dynmap.selectMapAndPan(map, position.location);
});
}
});
}

#createHandler<T extends APILayers>(type: T) {
Expand Down
2 changes: 2 additions & 0 deletions src/dynmap.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type DynMap = any;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
declare const SidebarUtils: any;

declare const componentconstructors: Record<string, (dynmap: DynMap, configuration: Record<string, unknown>) => void>;

Expand Down
12 changes: 4 additions & 8 deletions src/renderers/PortalRenderer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TrainPortal } from "../types/APITypes";
import { Renderer } from "../types/Renderer";
import { Vector } from "../utils";
import { Vector, getMapForDimension } from "../utils";

export class PortalRenderer extends Renderer<TrainPortal, L.ImageOverlay> {
render(portal: TrainPortal) {
Expand All @@ -14,17 +14,13 @@ export class PortalRenderer extends Renderer<TrainPortal, L.ImageOverlay> {
interactive: true,
});

const targetName = Object.entries(this.config.worlds).find(([_, v]) => v == portal.to.dimension)?.[0];
const target = targetName ? this.dynmap.worlds[targetName] : null;
const target = getMapForDimension(portal.to.dimension, this.dynmap, this.config);

img.bindTooltip(this.config.labels.portal + target?.title ?? portal.to.dimension, {});
img.bindTooltip(this.config.labels.portal + target?.options.world.title ?? portal.to.dimension, {});

if (target) {
img.on("click", () => {
this.dynmap.selectMapAndPan(
target.maps[this.dynmap.maptype.options.name] ?? target.maps[Object.keys(target.maps)[0]],
portal.to.location
);
this.dynmap.selectMapAndPan(target, portal.to.location);
});
}

Expand Down
8 changes: 8 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
import { IConfig } from "../types/IConfig";

export * from "./Vector";

export type Unarray<T> = T extends readonly (infer P)[] ? P : T;

export function getMapForDimension(dimension: string, dynmap: DynMap, config: IConfig) {
const worldName = Object.entries(config.worlds).find(([_, v]) => v == dimension)?.[0];
const world = worldName ? dynmap.worlds[worldName] : null;
return world.maps[dynmap.maptype.options.name] ?? world.maps[Object.keys(world.maps)[0]];
}

0 comments on commit 172b5df

Please sign in to comment.