-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreviewmap.html
More file actions
483 lines (401 loc) · 17.8 KB
/
previewmap.html
File metadata and controls
483 lines (401 loc) · 17.8 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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
<!--
This webpage uses Prismarine NBT (https://github.com/PrismarineJS/prismarine-nbt)
code adapted from litemapy (https://github.com/SmylerMC/litemapy/)
and Three.js (https://threejs.org/)
Since Litematica and litemapy are GPLv3.0, here it is:
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
-->
<!DOCTYPE html>
<html lang="en">
<head>
<title>Schematic Previewer</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css" integrity="sha512-KfkfwYDsLkIlwQp6LFnl8zNdLGxu9YAA1QvwINks4PhcElQSvqcyVLLD9aMhXd13uQjoXtEKNosOWaZqXgel0g==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<script src="./pnbt.js"></script>
<script>
const nbt = require('prismarine-nbt');
</script>
<script src="https://unpkg.com/[email protected]/dist/pako.min.js"></script>
<style>
.hidden {
display: none;
}
.no-visibility {
visibility: hidden;
opacity: 0;
}
#dropzone {
position: fixed;
top: 0;
left: 0;
z-index: 999;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
transition: visibility 175ms, opacity 175ms;
}
#text-repr {
position: fixed;
top: 0;
bottom: 0;
right: 0;
width: 33%;
background: #333;
}
#text-repr textarea {
position: relative;
background: #333;
color: #eee;
overflow: auto;
padding: 1em;
resize: none;
width: 100%;
height: 100%;
border: none;
}
</style>
</head>
<body>
<div id="dropzone" class="no-visibility"></div>
<div id="main">
<header class="w3-container w3-dark-grey">
<h2>Minimap previewer (.nbt, .litematic, .schem, .txt)</h2>
</header>
<form class="w3-container">
<div class="w3-input">
<input type="checkbox" id="noobline">
<label for="noobline">Show/hide noobline (topmost extra row)</label>
<br>
<input type="checkbox" id="text">
<label for="text">Show/hide textual representation</label>
</div>
<input type="file" id="schem" class="w3-input" multiple="multiple">
<div id="schem_error" class="w3-border-red w3-pale-red hidden"></div>
</form>
<div id="results" class="w3-container"></div>
<div id="text-repr" class="no-visibility">
<canvas id="text-canvas"></canvas>
<br>
<button id="text-download" disabled="disabled">
<i class="fa fa-download" style="padding-right: 9px;"></i><span>Download as .schem</span>
</button>
<textarea id="text-contents" placeholder="X Y Z minecraft:material[prop1=val1,prop2=val2]"></textarea>
</div>
</div>
<script type="module">
import { convertToSchem, parseTxt } from './js/schematics.js';
const workerTxtOrNbt = new Worker('./jsworker/txtornbtworker.js');
const workerTxtParser = new Worker('./jsworker/txtparserworker.js', {type: 'module'});
const workerSchemParser = new Worker('./jsworker/schemparserworker.js', {type: 'module'});
const workerPreviewMap = new Worker('./jsworker/previewmap.js', {type: 'module'});
const noobline = document.getElementById('noobline');
const results = document.getElementById('results');
const dropzone = document.getElementById("dropzone");
const schem = document.getElementById("schem");
const schem_error = document.getElementById("schem_error");
function showSchematicError(errorHTML, order) {
schem_error.classList.remove("hidden");
schem_error.innerHTML = errorHTML;
if (order) {
const progress = document.getElementById('progress_' + order);
progress.classList.add('hidden');
setProgressText('', order);
}
}
let lastTarget;
document.addEventListener("dragover", event => {
event.preventDefault(); // prevent default to allow drop
});
document.addEventListener("dragenter", function (e) {
lastTarget = e.target; // cache the last target here
// unhide our dropzone overlay
dropzone.style.visibility = "";
dropzone.style.opacity = 1;
});
document.addEventListener("dragleave", function (e) {
// this is the magic part. when leaving the window,
// e.target happens to be exactly what we want: what we cached
// at the start, the dropzone we dragged into.
// so..if dragleave target matches our cache, we hide the dropzone.
// `e.target === document` is a workaround for Firefox 57
if (e.target === lastTarget || e.target === document) {
dropzone.style.visibility = "hidden";
dropzone.style.opacity = 0;
}
});
document.addEventListener("drop", function (e) {
e.preventDefault(); // prevent dragged file from being opened
dropzone.style.visibility = "hidden";
dropzone.style.opacity = 0;
schem.value = "";
if (e.dataTransfer.items) {
// Use DataTransferItemList interface to access the file(s)
const files = [];
for (let i = 0; i < e.dataTransfer.items.length; i++) {
// If dropped items aren't files, reject them
if (e.dataTransfer.items[i].kind === 'file') {
const file = e.dataTransfer.items[i].getAsFile();
files.push(file);
}
}
if (files.length > 0) {
handleSchematic(files);
}
}
else {
// Use DataTransfer interface to access the file(s)
if (e.dataTransfer.files.length > 0) {
handleSchematicFile(e.dataTransfer.files);
}
}
});
schem.addEventListener("change", function (event) {
const fileList = this.files;
if (fileList.length > 0) {
handleSchematicFile(fileList);
}
}, false);
function handleSchematicFile(files) {
for (const file of files) {
const i = file.name + Date.now();
//prepare result area
const section = document.createElement('section');
section.id = 'result_' + i;
section.classList.add('w3-cell', 'w3-border-left', 'w3-border-right', 'w3-border-bottom');
results.appendChild(section);
const span = document.createElement('span');
span.innerText = file.name;
section.appendChild(span);
const div = document.createElement('div');
div.classList.add('w3-container');
section.appendChild(div);
const progress = document.createElement('i');
progress.id = 'progress_' + i;
progress.classList.add('fa', 'fa-spinner', 'w3-spin');
progress.style.fontSize = '48px';
div.appendChild(progress);
const progress_text = document.createElement('span');
progress_text.id = 'progress_text_' + i;
progress_text.innerText = 'Parsing file...';
progress_text.style.display = 'block';
div.appendChild(progress_text);
//offload to worker
const reader = new FileReader();
reader.readAsArrayBuffer(file);
reader.onload = function (evt) {
workerTxtOrNbt.postMessage({
name: file.name,
contents: evt.target.result,
echo: i
});
};
}
}
workerTxtOrNbt.onmessage = (e) => {
const name = e.data.name;
const nbt = e.data.nbt;
const txt = e.data.txt;
const echo = e.data.echo;
const order = echo.order;
if (txt) {
setProgressText('Parsing text file...', order);
workerTxtParser.postMessage({
name: name,
txt: txt,
echo: echo
});
}
else if (nbt) {
setProgressText('Parsing schematic...', order);
workerSchemParser.postMessage({
name: name,
nbt: nbt,
echo: echo
});
}
else {
showSchematicError("Unable to load file", order);
}
};
workerTxtParser.onmessage = onSchematic;
workerSchemParser.onmessage = onSchematic;
function onSchematic(e) {
const name = e.data.name;
const schematic = e.data.schematic;
const order = e.data.echo;
if (name && schematic && order) {
const section = document.getElementById('result_' + order);
const dimensions = document.createElement('div');
dimensions.classList.add('w3-container');
dimensions.innerText = `Dimensions: ${schematic.xsize} x ${schematic.ysize} x ${schematic.zsize}`;
section.appendChild(dimensions);
const divButton = document.createElement('div');
divButton.classList.add('w3-container');
section.appendChild(divButton);
const button = document.createElement("button");
button.setAttribute('data-filename', name);
button.id = 'download_' + order;
divButton.appendChild(button);
const icon = document.createElement("i");
icon.classList.add("fa", "fa-download");
icon.style.paddingRight = '8px';
button.appendChild(icon);
const text = document.createElement("span");
text.innerText = 'Download as .schem';
button.appendChild(text);
//TODO: offload convertToSchem to a Worker. Complicated because the function needs both require() and import().
const buffer = convertToSchem(schematic);
button.addEventListener("click", function(event) {
let filename = name;
for (const ext of ['.schem', '.nbt', '.litematic', '.txt']) {
if (filename.endsWith(ext)) {
filename = filename.slice(0, -ext.length);
}
}
filename += '.preview.schem';
download(filename, buffer);
});
setProgressText('Parsing map art...', order);
workerPreviewMap.postMessage({
name: name,
schematic: schematic,
order: order,
});
}
else if (schematic) {
//Text editor
workerPreviewMap.postMessage({
schematic: schematic,
});
const buffer = convertToSchem(schematic);
const domTextDownload = document.getElementById('text-download');
domTextDownload.removeAttribute("disabled");
domTextDownload.onclick = function(event) {
download('txtedited.schem', buffer);
};
}
else {
showSchematicError("Unable to parse schematic", order);
}
};
workerPreviewMap.onmessage = (e) => {
const name = e.data.name;
const imageData = e.data.imageData;
const materialList = e.data.materialList;
const textData = e.data.textData;
const order = e.data.order;
if (name && imageData && order) {
const section = document.getElementById('result_' + order);
section.appendChild(document.createElement('br'));
//recreate canvas as it's lost between js workers
const offscreenCanvas = new OffscreenCanvas(imageData.width, imageData.height);
const offscreenCtx = offscreenCanvas.getContext('2d');
offscreenCtx.putImageData(imageData, 0, 0);
const canvas = document.createElement('canvas');
canvas.width = imageData.width;
canvas.height = imageData.height;
const canvasCtx = canvas.getContext("2d");
canvasCtx.drawImage(offscreenCanvas, 0, 0);
canvas.classList.add('noobline-yes');
if (!noobline.checked) {
canvas.classList.add('hidden');
}
section.appendChild(canvas);
const canvasSkip1 = document.createElement('canvas');
canvasSkip1.width = imageData.width;
canvasSkip1.height = imageData.height - 1;
const canvasCtxSkip1 = canvasSkip1.getContext("2d");
canvasCtxSkip1.drawImage(offscreenCanvas, 0, 1, canvasSkip1.width, canvasSkip1.height, 0, 0, canvasSkip1.width, canvasSkip1.height);
canvasSkip1.classList.add('noobline-no');
if (noobline.checked) {
canvasSkip1.classList.add('hidden');
}
section.appendChild(canvasSkip1);
section.appendChild(document.createElement('br'));
const domMaterialList = document.createElement('div');
domMaterialList.innerHTML = materialList;
section.appendChild(domMaterialList);
const domToTxtButton = document.createElement('button');
domToTxtButton.innerText = 'Load to txt editor';
section.appendChild(domToTxtButton);
domToTxtButton.addEventListener('click', function() {
domTextContents.value = textData;
document.getElementById("text").checked = true;
document.getElementById("text-repr").classList.remove("no-visibility");
loadTextEditorCanvas();
});
const progress = document.getElementById('progress_' + order);
progress.classList.add('hidden');
setProgressText('', order);
}
else if (imageData) {
//For text editor
const domTextCanvas = document.getElementById('text-canvas');
domTextCanvas.width = imageData.width;
domTextCanvas.height = imageData.height;
const canvasCtx = domTextCanvas.getContext("2d");
canvasCtx.putImageData(imageData, 0, 0);
}
else {
showSchematicError("Unable to parse schematic", order);
}
};
noobline.addEventListener('click', function() {
let elements = document.getElementsByClassName('noobline-yes');
for (const el of elements) {
if (this.checked) {
el.classList.remove('hidden');
}
else {
el.classList.add('hidden');
}
}
elements = document.getElementsByClassName('noobline-no');
for (const el of elements) {
if (this.checked) {
el.classList.add('hidden');
}
else {
el.classList.remove('hidden');
}
}
});
document.getElementById("text").addEventListener("click", function() {
document.getElementById("text-repr").classList.toggle("no-visibility");
});
const domTextContents = document.getElementById("text-contents");
domTextContents.addEventListener('change', loadTextEditorCanvas);
function loadTextEditorCanvas() {
workerTxtParser.postMessage({
txt: domTextContents.value,
});
}
function setProgressText(text, order) {
const progress_text = document.getElementById('progress_text_' + order);
if (progress_text)
progress_text.innerText = text;
}
function download(filename, uint8array) {
const element = document.createElement("a");
const blob = new Blob([uint8array], {type: "application/octet-stream"});
element.setAttribute('href', window.URL.createObjectURL(blob));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
</script>
</body>
</html>