-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
266 lines (237 loc) · 9.86 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/// <reference path="./interfaces/index.ts" />
/// <reference path="./rest/index.ts" />
/// <reference path="./hero-map.ts" />
/// <reference path="./listener.ts" />
/// <reference path="./toolbox/index.ts" />
/// <reference path="./state/index.ts" />
/// <reference path="./game-data.ts" />
function setUpHeroMap(element: HTMLDivElement): HeroMap {
const heroMap = new HeroMap(element);
heroMap.viewport.addEventListener("ps2map_basehover", event => {
const evt = (event as CustomEvent<BaseHoverEvent>).detail;
const base = GameData.getInstance().getBase(evt.baseId);
if (base)
StateManager.dispatch(State.user.baseHovered, base as never);
});
StateManager.subscribe(State.map.baseCaptured, state => {
heroMap.updateBaseOwnership(state.map.baseOwnership);
});
StateManager.subscribe(State.user.continentChanged, state => {
const cont = state.user.continent;
if (!cont)
return;
GameData.getInstance().setActiveContinent(cont)
.then(() => {
heroMap.switchContinent(cont).then(() => {
heroMap.updateBaseOwnership(state.map.baseOwnership);
heroMap.jumpTo({ x: 0, y: 0 });
});
});
});
// Set up layer visibility hooks
const container = document.getElementById(
"layer-toggle-container") as HTMLDivElement;
StateManager.subscribe(State.user.layerVisibilityChanged, (state, data) => {
const vis = state.user.layerVisibility;
const layer = heroMap.getLayer((data as any).id);
if (!layer)
return;
const isVisible = vis.get(layer.id) || false;
layer.setVisibility(isVisible);
const toggles = container.getElementsByTagName("div");
for (let i = 0; i < toggles.length; i++) {
const toggle = toggles[i];
if (toggle?.getAttribute("data-layer-id") !== layer.id)
continue;
if (isVisible)
toggle.setAttribute("data-active", "");
else
toggle.removeAttribute("data-active");
}
if (layer.id == "canvas") {
if (isVisible)
StateManager.dispatch(State.toolbox.canvasEnabled, {} as never);
else
StateManager.dispatch(State.toolbox.canvasDisabled, {} as never);
}
});
const layerNameFromId = (id: string) => {
switch (id) {
case "names":
return "Facility Names";
case "hexes":
return "Facility Outlines";
case "lattice":
return "Lattice Links";
case "terrain":
return "Terrain";
case "canvas":
return "Drawing Canvas";
default:
return "Unknown";
}
};
["canvas", "names", "lattice", "hexes", "terrain"].forEach(id => {
const name = layerNameFromId(id);
const toggle = document.createElement("div");
toggle.setAttribute("data-active", "");
toggle.setAttribute("data-layer-id", id);
toggle.innerText = name;
toggle.addEventListener("click", () => {
StateManager.dispatch(State.user.layerVisibilityChanged, {
id, visible: !toggle.hasAttribute("data-active"),
} as never);
}, { passive: true });
container.insertAdjacentElement("afterbegin", toggle);
});
return heroMap;
}
function setUpMapPickers(): [HTMLSelectElement, HTMLSelectElement] {
// Server picker
const serverPicker = document.getElementById(
"server-dropdown") as HTMLSelectElement;
serverPicker.addEventListener("change", () => {
const server = GameData.getInstance().servers()
.find(s => s.id === parseInt(serverPicker.value, 10));
if (!server)
throw new Error(`Server ${serverPicker.value} not found`);
StateManager.dispatch(State.user.serverChanged, server as never);
});
// Continent picker
const continentPicker = document.getElementById(
"continent-dropdown") as HTMLSelectElement;
continentPicker.addEventListener("change", () => {
const continent = GameData.getInstance().continents()
.find(c => c.id === parseInt(continentPicker.value, 10));
if (!continent)
throw new Error(`Continent ${continentPicker.value} not found`);
StateManager.dispatch(State.user.continentChanged, continent as never);
});
return [serverPicker, continentPicker];
}
function setUpSidebarResizing(): void {
const grabber = document.getElementById("sidebar-grabber") as HTMLDivElement;
grabber.addEventListener("mousedown", (event: MouseEvent) => {
const sidebar = document.getElementById("sidebar") as HTMLDivElement;
const initialWidth = sidebar.clientWidth;
const minWidth = 360;
const maxWidth = 540;
const startX = event.clientX;
const onMove = (evt: MouseEvent) => {
const delta = evt.clientX - startX;
let newWidth = initialWidth + delta;
if (newWidth < minWidth)
newWidth = minWidth;
else if (newWidth > maxWidth)
newWidth = maxWidth;
document.body.style.setProperty("--sidebar-width", `${newWidth}px`);
};
const onUp = () => {
document.removeEventListener("mousemove", onMove);
document.removeEventListener("mouseup", onUp);
};
document.addEventListener("mousemove", onMove);
document.addEventListener("mouseup", onUp);
});
}
function setUpToolbox(heroMap: HeroMap): void {
StateManager.dispatch(State.toolbox.setup, { map: heroMap } as never);
StateManager.dispatch(State.toolbox.setTool, { id: Tool.id } as never);
}
/** Initialisation hook for components that need to be run on DOM load. */
document.addEventListener("DOMContentLoaded", () => {
// Create main components
const heroMap = setUpHeroMap(
document.getElementById("map") as HTMLDivElement);
// Hook up sidebar resize grabber
setUpSidebarResizing();
// Create map state listener
const listener = new MapListener();
listener.subscribe((name, data) => {
StateManager.dispatch(`map/${name}`, data as never);
});
StateManager.subscribe(State.user.serverChanged, state => {
if (state.user.server)
listener.switchServer(state.user.server);
});
// Set up toolbox
setUpToolbox(heroMap);
// Hook up map pickers
const [serverPicker, continentPicker] = setUpMapPickers();
// Load game data
GameData.load().then(gameData => {
const servers = [...gameData.servers()];
const continents = [...gameData.continents()];
// Populate server picker
servers.sort((a, b) => a.name.localeCompare(b.name));
servers.forEach(server => {
const option = document.createElement("option");
option.value = server.id.toString();
option.text = server.name;
serverPicker.appendChild(option);
});
// Populate continent picker
continents.sort((a, b) => a.name.localeCompare(b.name));
continents.forEach(cont => {
const option = document.createElement("option");
option.value = cont.id.toString();
option.text = cont.name;
continentPicker.appendChild(option);
});
// Set default server and continent
StateManager.dispatch(State.user.serverChanged,
servers[0] as never);
StateManager.dispatch(State.user.continentChanged,
continents[0] as never);
});
// const baseFinderBtn = document.getElementById("base-finder-btn") as HTMLInputElement;
// baseFinderBtn.addEventListener("click", () => {
// const finder = document.getElementById("base-finder") as HTMLSelectElement;
// if (finder.value)
// heroMap.jumpToBase(parseInt(finder.value, 10));
// });
// StateManager.subscribe(State.user.continentChanged, async state => {
// const bases = await GameData.getInstance().getBasesForContinent(
// state.user.continent);
// bases.sort((a, b) => a.name.localeCompare(b.name));
// const options: HTMLOptionElement[] = [];
// bases.forEach(base => {
// const option = document.createElement("option");
// option.value = base.id.toString();
// option.text = base.name;
// options.push(option);
// });
// const finder = document.getElementById("base-finder") as HTMLSelectElement;
// finder.innerHTML = "";
// options.forEach(option => finder.appendChild(option));
// });
StateManager.subscribe(State.user.baseHovered, state => {
const names = heroMap.getLayer<BaseNamesLayer>("names");
if (names)
names.setHoveredBase(state.user.hoveredBase);
});
const tags = ["tr", "nc", "vs", "ns"];
const html = document.querySelector("html") as HTMLHtmlElement;
tags.forEach(tag => {
const input = document.getElementById(`color-${tag}`) as HTMLInputElement;
input.addEventListener("input", () => {
const color = input.value;
html.style.setProperty(`--ps2map__faction-${tag}-colour`, color);
});
});
document.getElementById("color-reset")?.addEventListener("click", () => {
const defaults = new Map([
["ns", "#3f3f3f"],
["vs", "#b74dd5"],
["nc", "#3c7fff"],
["tr", "#e21919"]]);
tags.forEach(tag => {
const input = document.getElementById(`color-${tag}`) as HTMLInputElement;
const color = defaults.get(tag);
if (!color)
return;
input.value = color;
html.style.setProperty(`--ps2map__faction-${tag}-colour`, color);
});
});
});