-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathindex.html
More file actions
435 lines (387 loc) · 13.7 KB
/
Copy pathindex.html
File metadata and controls
435 lines (387 loc) · 13.7 KB
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
<!DOCTYPE html>
<html>
<head>
<title>PolyZone Creator</title>
<meta charset="utf-8" />
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/leaflet.css" integrity="sha512-xodZBNTC5n17Xt2atTPuE1HxjVMSvLVW9ocqUKLsCC5CXdbqCmblAshOMAS6/keqq/sMZMZ19scR4PsZChSR7A==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet.draw/1.0.4/leaflet.draw.css" integrity="sha512-gc3xjCmIy673V6MyOAZhIW93xhM9ei1I+gLbmFjUHIjocENRsLX/QUE1htk5q1XV2D/iie/VQ8DXI6Vu8bexvQ==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<style>
html,
body,
#map1 {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
z-index: 1;
background-color: #143c6a;
}
.btn {
position: absolute;
z-index: 10;
border-radius: 3px;
padding: 5px 15px;
border-color: #202020;
background-color: #303030;
color: white;
}
.btn-clear {
top: 2px;
right: 2px;
}
.btn-debug {
top: 2px;
right: 140px;
}
.btn-overlay {
top: 2px;
right: 250px;
}
.btn:hover {
cursor: pointer;
}
.btn:active {
background-color: #404040;
}
.leaflet-grid-label .lng {
margin-left: 8px;
-webkit-transform: rotate(90deg);
transform: rotate(90deg);
}
</style>
</head>
<body>
<div id="map1"></div>
<button class="btn btn-clear" onclick="clearLayer();">
Clear All Shapes
</button>
<button
class="btn btn-debug"
onclick="debug = !debug;this.style.backgroundColor = debug ? '#40aa40' : '#303030';"
>
Debug Grid
</button>
<button
class="btn btn-overlay"
onclick="overlay = !overlay;this.style.backgroundColor = overlay ? '#40aa40' : '#303030';setOverlay();"
>
Overlay Grid
</button>
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/leaflet.js" integrity="sha512-XQoYMqMTK8LvdxXYG3nZ448hOEQiglfqkJs1NOQV44cWnUrBc8PkAOcXy20w0vlaXaVUearIOBhiXZ5V3ynxwA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet.draw/1.0.4/leaflet.draw.js" integrity="sha512-ozq8xQKq6urvuU6jNgkfqAmT7jKN2XumbrX1JiB3TnF7tI48DPI4Gy1GXKD/V3EExgAs1V+pRO7vwtS1LHg0Gw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script>
//Map Options
var mapMinZoom = 0;
var mapMaxZoom = 7;
var mapMaxResolution = 0.25;
var mapMinResolution = Math.pow(2, mapMaxZoom) * mapMaxResolution;
var mapCenterLat = -5525;
var mapCenterLng = 3755;
var gtaOffset = 0.66;
var debug = false;
var overlay = false;
var bottomLeft = [-8192, 0];
var topRight = [0, 8192];
var bounds = [bottomLeft, topRight];
L.Draw.Circle = L.Draw.SimpleShape.extend({
statics: {
TYPE: "circle",
},
options: {
shapeOptions: {
stroke: true,
color: "#3388ff",
weight: 4,
opacity: 0.5,
fill: true,
fillColor: null, //same as color by default
fillOpacity: 0.2,
clickable: true,
},
showRadius: true,
metric: true, // Whether to use the metric measurement system or imperial
feet: true, // When not metric, use feet instead of yards for display
nautic: false, // When not metric, not feet use nautic mile for display
},
// @method initialize(): void
initialize: function (map, options) {
// Save the type so super can fire, need to do this as cannot do this.TYPE :(
this.type = L.Draw.Circle.TYPE;
this._initialLabelText =
L.drawLocal.draw.handlers.circle.tooltip.start;
L.Draw.SimpleShape.prototype.initialize.call(this, map, options);
},
_drawShape: function (latlng) {
// Calculate the distance based on the version
let outerPos = latlngToGTA(latlng);
let innerPos = latlngToGTA(this._startLatLng);
var distance = Math.hypot(
innerPos[0] - outerPos[0],
innerPos[1] - outerPos[1]
);
if (!this._shape) {
this._shape = new L.Circle(
this._startLatLng,
distance,
this.options.shapeOptions
);
this._map.addLayer(this._shape);
} else {
this._shape.setRadius(distance);
}
},
_fireCreatedEvent: function () {
var circle = new L.Circle(
this._startLatLng,
this._shape.getRadius(),
this.options.shapeOptions
);
L.Draw.SimpleShape.prototype._fireCreatedEvent.call(this, circle);
},
_onMouseMove: function (e) {
var latlng = e.latlng,
showRadius = this.options.showRadius,
useMetric = this.options.metric,
radius;
this._tooltip.updatePosition(latlng);
if (this._isDrawing) {
this._drawShape(latlng);
// Get the new radius (rounded to 1 dp)
radius = this._shape.getRadius().toFixed(1);
var subtext = "";
if (showRadius) {
subtext =
L.drawLocal.draw.handlers.circle.radius +
": " +
L.GeometryUtil.readableDistance(
radius,
useMetric,
this.options.feet,
this.options.nautic
);
}
this._tooltip.updateContent({
text: this._endLabelText,
subtext: subtext,
});
}
},
});
L.Edit.CircleMarker = L.Edit.Circle.extend({
_resize: function (latlng) {
var moveLatLng = this._moveMarker.getLatLng();
// Calculate the distance based on the version
let outerPos = latlngToGTA(latlng);
let innerPos = latlngToGTA(this._startLatLng);
var distance = Math.hypot(
innerPos[0] - outerPos[0],
innerPos[1] - outerPos[1]
);
if (this._map.editTooltip) {
this._map._editTooltip.updateContent({
text:
L.drawLocal.edit.handlers.edit.tooltip.subtext +
"<br />" +
L.drawLocal.edit.handlers.edit.tooltip.text,
subtext:
L.drawLocal.draw.handlers.circle.radius +
": " +
L.GeometryUtil.readableDistance(
distance,
true,
this.options.feet,
this.options.nautic
),
});
}
this._shape.setRadius(distance);
this._map.fire(L.Draw.Event.EDITRESIZE, { layer: this._shape });
},
});
//Create a new (square) coordinate system
var crs = L.CRS.Simple;
crs.scale = function (zoom) {
return Math.pow(2, zoom) / mapMinResolution;
};
/*
crs.zoom = function (scale) {
return Math.log(scale * mapMinResolution) / Math.LN2;
};*/
//Create GTA map tiles
var layer = L.tileLayer("tiles/{z}/{x}/{y}.png", {
minZoom: mapMinZoom,
maxZoom: mapMaxZoom,
attribution:
'Rendered with <a href="https://www.maptiler.com/desktop/">MapTiler Desktop</a> by <a href="https://github.com/RussianRonin">RussianRonin</a> | Created by <a href="https://github.com/skyrossm">Skyrossm</a>',
noWrap: true,
tms: true,
});
//Create the map object
var map = new L.Map("map1", {
maxZoom: mapMaxZoom,
minZoom: mapMinZoom,
layers: [layer],
crs: crs,
center: [mapCenterLat, mapCenterLng],
zoom: 3,
});
//Shows GTA grid overlay on map
let bgimage = L.imageOverlay("https://i.imgur.com/MQr04nZ.jpg", bounds, {
opacity: 0,
}).addTo(map);
function setOverlay() {
let imagelee = document.querySelector(".leaflet-image-layer");
imagelee.style.opacity = overlay ? 0.5 : 0;
}
//Create Leaflet.Draw toolbar/layer
//Fix for broken Draw
L.Draw.Polyline.prototype._onTouch = L.Util.falseFn;
var editableLayers = L.featureGroup();
map.addLayer(editableLayers);
var drawControl = new L.Control.Draw({
draw: {
polyline: false,
circlemarker: false,
},
edit: {
featureGroup: editableLayers,
},
});
map.addControl(drawControl);
//Convert latlng on the map to GTA coords
function latlngToGTA(latlng) {
var x = (latlng.lng - mapCenterLng) / gtaOffset;
var y = (latlng.lat - mapCenterLat) / gtaOffset;
return [x.toFixed(2), y.toFixed(2)];
}
//Convert GTA coords to latlng on the map
function gtaToLatLng(x, y) {
var lng = x * gtaOffset + mapCenterLng;
var lat = y * gtaOffset + mapCenterLat;
return L.latLng(lat, lng);
}
//Remove all shapes from Draw
function clearLayer() {
editableLayers.getLayers().forEach((layer) => {
editableLayers.removeLayer(layer);
});
}
//https://github.com/mkafrin/PolyZone/blob/master/server.lua
function parseShape(name, points) {
var printout = "--Name: " + name + " | " + new Date().toDateString();
printout += "\nPolyZone:Create({\n";
points.forEach((point, index) => {
var last = index < points.length - 1 ? "," : "";
printout +=
" vector2(" + point[0] + ", " + point[1] + ")" + last + "\n";
});
var debugText = debug ? "\n debugGrid=true," : "";
printout +=
'}, {\n name="' +
name +
'",' +
debugText +
"\n --minZ=0,\n --maxZ=800\n})\n\n";
return printout;
}
//https://github.com/mkafrin/PolyZone/blob/master/server.lua
function parseCircle(name, center, radius) {
var printout = "--Name: " + name + " | " + new Date().toDateString();
printout +=
"\nCircleZone:Create(vector2(" +
center[0] +
", " +
center[1] +
"), " +
radius +
", ";
var debugText = debug ? "\n debugPoly=true," : "";
printout += '{\n name="' + name + '",' + debugText + "\n})\n\n";
return printout;
}
//On shape created
map.on(L.Draw.Event.CREATED, (e) => {
var type = e.layerType,
layer = e.layer;
//If it's a marker just show the GTA position
if (type === "marker") {
var position = latlngToGTA(layer.getLatLng());
layer.bindPopup(
"GTA Position: " + position + ", latlng: " + layer.getLatLng()
);
} else if (type === "circle") {
var position = latlngToGTA(layer.getLatLng());
let content = parseCircle(
prompt("Enter circlezone name: "),
latlngToGTA(layer.getLatLng()),
(layer.getRadius() * 1.5).toFixed(2)
);
layer.bindPopup(
"<code style='white-space:pre-wrap;'>" + content + "</code>",
{ minWidth: 500 }
);
} else {
//Get the polyzone name
var name = prompt("Enter polyzone name:");
//Show polyzone code on popup
var gtapoints = [];
layer._latlngs[0].forEach((latlng) => {
gtapoints.push(latlngToGTA(latlng));
});
var content = parseShape(name, gtapoints);
//Show in console as well just in case
console.log(content);
//Bind the popup
layer.bindPopup(
"<code style='white-space:pre-wrap;'>" + content + "</code>",
{ minWidth: 500 }
);
}
//Add shape to map
editableLayers.addLayer(layer);
});
map.on(L.Draw.Event.EDITED, (e) => {
var layers = e.layers;
layers.eachLayer(function (layer) {
if (layer instanceof L.Marker) {
var position = latlngToGTA(layer.getLatLng());
layer.bindPopup(
"GTA Position: " + position + ", latlng: " + layer.getLatLng()
);
} else if (layer instanceof L.Circle) {
var name = layer.getPopup().getContent().split(" |")[0].slice(44);
var position = latlngToGTA(layer.getLatLng());
let content = parseCircle(
name,
latlngToGTA(layer.getLatLng()),
(layer.getRadius() * 1.5).toFixed(2)
);
layer.bindPopup(
"<code style='white-space:pre-wrap;'>" + content + "</code>",
{ minWidth: 500 }
);
} else {
//Get the polyzone name
//extracts from the previous popup, could use some love
var name = layer.getPopup().getContent().split(" |")[0].slice(44);
//Show polyzone code on popup
var gtapoints = [];
layer._latlngs[0].forEach((latlng) => {
gtapoints.push(latlngToGTA(latlng));
});
var content = parseShape(name, gtapoints);
//Bind the popup
layer.bindPopup(
"<code style='white-space:pre-wrap;'>" + content + "</code>",
{ minWidth: 500 }
);
}
});
});
//Center the map
//map.setView(xy(0, 0), 3);
</script>
</body>
</html>