File size: 12,958 Bytes
3af080a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>FastAPI Maps</title>
	 <link rel="icon" type="image/x-icon" href="/static/favicon.ico">
    <link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.3/dist/leaflet.css"/>
	<!--link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet-groupedlayercontrol/0.6.1/leaflet.groupedlayercontrol.css"/-->
    <script src="https://unpkg.com/leaflet@1.9.3/dist/leaflet.js"></script>
    <script src="/static/js/leaflet-control-groupedlayer.js"></script>
    <script src="/static/js/leaflet-control-coordinates.js"></script>
    <!--script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet-groupedlayercontrol/0.6.1/leaflet.groupedlayercontrol.js"></script-->
	<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@1.0.2/css/bulma.min.css"/>
	<link rel="stylesheet" href="/static/style.css"/>
    <style>

		html, body {

			margin: 0;

			padding: 0;

		}

        #map {

            width: 100%;

            height: 100vh;

        }

    </style>
</head>
<body>
    <div id="map"></div>
    <script>

        // Embed the maps data as a JavaScript constant

        const mapData = {{ maps[0] | tojson }}

		//const transform = [0.1855, 113.1, 0.1855, 167.8]

		const transform = mapData.transform

		const bounds =  mapData.bounds

		const svgBounds = mapData.svgBounds ? mapData.svgBounds : bounds

		const coordinateRotation = mapData.coordinateRotation ? mapData.coordinateRotation : 0

		const svgPath = mapData.svgPath

		const imageUrl = mapData.svgPath

		const minZoom = mapData.minZoom ? mapData.minZoom : 1

		const maxZoom = mapData.maxZoom ? mapData.maxZoom : 6

		

		const images = {

			'container_bank-cash-register': 'container_cash-register',

			'container_bank-safe': 'container_safe',

			'container_buried-barrel-cache': 'container_buried-barrel-cache',

			'container_cash-register': 'container_cash-register',

			'container_cash-register-tar2-2': 'container_cash-register',

			'container_dead-civilian': 'container_dead-scav',

			'container_dead-scav': 'container_dead-scav',

			'container_festive-airdrop-supply-crate': 'container_festive-airdrop-supply-crate',

			'container_pmc-body': 'container_dead-scav',

			'container_civilian-body': 'container_dead-scav',

			'container_drawer': 'container_drawer',

			'container_duffle-bag': 'container_duffle-bag',

			'container_grenade-box': 'container_grenade-box',

			'container_ground-cache': 'container_ground-cache',

			'container_jacket': 'container_jacket',

			'container_lab-technician-body': 'container_dead-scav',

			'container_medbag-smu06': 'container_medbag-smu06',

			'container_medcase': 'container_medcase',

			'container_medical-supply-crate': 'container_crate',

			'container_pc-block': 'container_pc-block',

			'container_plastic-suitcase': 'container_plastic-suitcase',

			'container_ration-supply-crate': 'container_crate',

			'container_safe': 'container_safe',

			'container_scav-body': 'container_dead-scav',

			'container_shturmans-stash': 'container_weapon-box',

			'container_technical-supply-crate': 'container_crate',

			'container_toolbox': 'container_toolbox',

			'container_weapon-box': 'container_weapon-box',

			'container_wooden-ammo-box': 'container_wooden-ammo-box',

			'container_wooden-crate': 'container_wooden-crate',

			'extract_pmc': 'extract_pmc',

			'extract_scav': 'extract_scav',

			'extract_shared': 'extract_shared',

			'extract_transit': 'extract_transit',

			'hazard': 'hazard',

			'hazard_mortar': 'hazard_mortar',

			'hazard_minefield': 'hazard',

			'hazard_sniper': 'hazard',

			'key': 'key',

			'lock': 'lock',

			'loose_loot': 'loose_loot',

			'quest_item': 'quest_item',

			'quest_objective': 'quest_objective',

			'spawn_sniper_scav': 'spawn_sniper_scav',

			'spawn_bloodhound': 'spawn_bloodhound',

			'spawn_boss': 'spawn_boss',

			'spawn_cultist-priest': 'spawn_cultist-priest',

			'spawn_pmc': 'spawn_pmc',

			'spawn_rogue': 'spawn_rogue',

			'spawn_scav': 'spawn_scav',

			'stationarygun': 'stationarygun',

			'switch': 'switch',

		};

		

		const categories = {

            'extract_pmc': 'PMC',

            'extract_shared': 'Shared',

            'extract_scav': 'Scav',

            'extract_transit': 'Transit',

            'spawn_sniper_scav': 'Sniper Scav',

            'spawn_pmc': 'PMC',

            'spawn_scav': 'Scav',

            'spawn_boss': 'Boss',

            'quest_item': 'Item',

            'quest_objective': 'Objective',

            'lock': 'Locks',

            'lever': 'Lever',

            'stationarygun': 'Stationary Gun',

            'switch': 'Switch',

            'place-names': 'Place Names',

        };

	

		function getCRS(transform) {

			let scaleX = 1;

			let scaleY = 1;

			let marginX = 0;

			let marginY = 0;

			if (transform) {

				scaleX = transform[0];

				scaleY = transform[2] * -1;

				marginX = transform[1];

				marginY = transform[3];

			}

			return L.extend({}, L.CRS.Simple, {

				transformation: new L.Transformation(scaleX, marginX, scaleY, marginY),

				projection: L.extend({}, L.Projection.LonLat, {

					project: latLng => {

						return L.Projection.LonLat.project(applyRotation(latLng, coordinateRotation));

					},

					unproject: point => {

						return applyRotation(L.Projection.LonLat.unproject(point), coordinateRotation * -1);

					},

				}),

			});

		}

		

		function applyRotation(latLng, rotation) {

			if (!latLng.lng && !latLng.lat) {

				return L.latLng(0, 0);

			}

			if (!rotation) {

				return latLng;

			}



			const angleInRadians = (rotation * Math.PI) / 180;

			const cosAngle = Math.cos(angleInRadians);

			const sinAngle = Math.sin(angleInRadians);



			const {lng: x, lat: y} = latLng;

			const rotatedX = x * cosAngle - y * sinAngle;

			const rotatedY = x * sinAngle + y * cosAngle;

			return L.latLng(rotatedY, rotatedX);

		}



		function pos(position) {

			return [position.z, position.x];

		}



		

		function getScaledBounds(bounds, scaleFactor) {

			// Calculate the center point of the bounds

			const centerX = (bounds[0][0] + bounds[1][0]) / 2;

			const centerY = (bounds[0][1] + bounds[1][1]) / 2;

			

			// Calculate the new width and height

			const width = bounds[1][0] - bounds[0][0];

			const height = bounds[1][1] - bounds[0][1];

			const newWidth = width * scaleFactor;

			const newHeight = height * scaleFactor;

			

			// Update the coordinates of the two points defining the bounds

			const newBounds = [

				[centerY - newHeight / 2, centerX - newWidth / 2],

				[centerY + newHeight / 2, centerX + newWidth / 2]

			];



			// console.log("Initial Rectangle:", bounds);

			// console.log("Scaled Rectangle:", newBounds);

			// console.log("Center:", L.bounds(bounds).getCenter(true));

			

			return newBounds;

		}

		

        // Erstelle die Karte

		//const map = L.map('map').setView([0, 0], 2);

	

		const map = L.map('map', {

            maxBounds: getScaledBounds(svgBounds, 1.5),

            //maxBounds: bounds,

            center: [0, 0],

            zoom: 2,

            minZoom: minZoom,

            maxZoom: maxZoom,

            zoomSnap: 0.1,

            scrollWheelZoom: true,

            wheelPxPerZoomLevel: 120,

            crs: getCRS(transform),

            attributionControl: false,

            id: "wwoodsMap",

        });

		

		const layerControl = L.control.groupedLayers(null, null, {

				position: 'topleft',

				collapsed: true,

				groupCheckboxes: true,

				groupsCollapsable: true,

				exclusiveOptionalGroups: ['Levels'],

			}).addTo(map);

			layerControl.on('overlayToggle', (e) => {

				const layerState = e.detail; 

				if (layerState.checked) {

					mapViewRef.current.layer = layerState.key;

				} else {

					mapViewRef.current.layer = undefined;

				}

			});

			layerControl.on('layerToggle', (e) => {

				const layerState = e.detail;

				if (!layerState.checked) {

					mapSettingsRef.current.hiddenLayers.push(layerState.key);

				} else {

					mapViewRef.current.layer = layerState.key;

					mapSettingsRef.current.hiddenLayers = mapSettingsRef.current.hiddenLayers.filter(key => key !== layerState.key);

				}

				updateSavedMapSettings();

			});

			layerControl.on('groupToggle', (e) => {

				const groupState = e.detail;

				for (const groupLayer of layerControl._layers) {

					if (groupLayer.group?.key !== groupState.key) {

						continue;

					}

					if (!groupState.checked) {

						mapSettingsRef.current.hiddenLayers.push(groupLayer.key);

					} else {

						mapSettingsRef.current.hiddenLayers = mapSettingsRef.current.hiddenLayers.filter(key => key !== groupLayer.key);

					}

				}

				if (!groupState.checked) {

					mapSettingsRef.current.hiddenGroups.push(groupState.key);

				} else {

					mapSettingsRef.current.hiddenGroups = mapSettingsRef.current.hiddenGroups.filter(key => key !== groupState.key);

				}

				updateSavedMapSettings();

			});

			layerControl.on('groupCollapseToggle', (e) => {

				const groupState = e.detail;

				if (groupState.collapsed) {

					mapSettingsRef.current.collapsedGroups.push(groupState.key);

					

				} else {

					mapSettingsRef.current.collapsedGroups = mapSettingsRef.current.collapsedGroups.filter(key => key !== groupState.key);

				}

				updateSavedMapSettings();

			});



			const getLayerOptions = (layerKey, groupKey, layerName) => {

				return {

					groupKey,

					layerKey,

					groupName: groupKey,

					layerName: layerName || categories[layerKey] || layerKey,

					//groupHidden: Boolean(mapSettingsRef.current.hiddenGroups?.includes(groupKey)),

					//layerHidden: Boolean(mapSettingsRef.current.hiddenLayers?.includes(layerKey)),

					image: images[layerKey] ? "/static/maps/interactive/${images[layerKey]}.png" : undefined,

					//groupCollapsed: Boolean(mapSettingsRef.current.collapsedGroups?.includes(groupKey)),

				};

			};



			const addLayer = (layer, layerKey, groupKey, layerName) => {

				layer.key = layerKey;

				const layerOptions = getLayerOptions(layerKey, groupKey, layerName);

				if (!layerOptions.layerHidden) {

					layer.addTo(map);

				}

				layerControl.addOverlay(layer, layerOptions.layerName, layerOptions);

			};

			map.layerControl = layerControl;



		// Hinzufügen des Image-Overlays

		const overlay = L.imageOverlay(imageUrl, getBounds(svgBounds));

		overlay.addTo(map);



		

		function getBounds(bounds) {

			if (!bounds) {

				return undefined;

			}

			return L.latLngBounds([bounds[0][1], bounds[0][0]], [bounds[1][1], bounds[1][0]]);

			//return [[bounds[0][1], bounds[0][0]], [bounds[1][1], bounds[1][0]]];

		}

		

		 const layerOptions = {

				maxZoom: maxZoom,

				maxNativeZoom: maxZoom,

				extents: [

					{

						height: [Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER],

						bounds: [bounds],

					}

				],

				type: 'map-layer',

			};

			let tileLayer = false;

			const baseLayers = [];

			const tileSize = 256;

		let svgLayer = false;

        if (svgPath) {

            const svgBounds2 = svgBounds ? getBounds(svgBounds) : bounds;

            svgLayer = L.imageOverlay(svgPath, svgBounds2, layerOptions);

            baseLayers.push(svgLayer);

        }



        if (mapData.labels?.length > 0) {

            const labelsGroup = L.layerGroup();

            const defaultHeight = ((layerOptions.extents[0].height[1] - layerOptions.extents[0].height[0]) / 2) + layerOptions.extents[0].height[0];

            for (const label of mapData.labels) {

                const fontSize = label.size ? label.size : 100;

                const height = label.position.length < 3 ? defaultHeight : label.position[2];

                const rotation = label.rotation ? label.rotation : 0;

                L.marker(pos({x: label.position[0], z: label.position[1]}), {

                    icon: L.divIcon({

                        html: `<div class="label" style="font-size: ${fontSize}%; transform: translate3d(-50%, -50%, 0) rotate(${rotation}deg)">${label.text}</div>`,

                        className: 'map-area-label',

                        layers: baseLayers,

                    }),

                    interactive: false,

                    zIndexOffset: -100000,

                    position: {

                        x: label.position[0],

                        y: height,

                        z: label.position[1],

                    },

                    top: typeof label.top !== 'undefined' ? label.top : 1000,

                    bottom: typeof label.bottom !== 'undefined' ? label.bottom : -1000,

                }).addTo(labelsGroup);

            }

            addLayer(labelsGroup, 'place-names', 'Landmarks');

			//labelsGroup.addTo(map);

        }

    </script>
</body>
</html>