-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.html
363 lines (311 loc) · 13.3 KB
/
map.html
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
<!DOCTYPE html>
<html>
<head>
<title>A* Map</title>
<meta charset="utf-8">
<style>
body {margin: 24px auto; font-family: sans-serif; font-size: 14px; font-family: 'Segoe UI', sans-serif; text-align: center; color: gainsboro; background-color: rgb(24,32,56);}
main {width: 640px; margin: auto; text-align: left;}
h4 {display: inline-block; height: 40px; line-height: 40px; margin: 0 0 12px; padding: 0 16px; font-size: 20px; font-family: 'Sego UI Semibold', sans-serif; color: white; background-color: rgba(192,128,240,.3125); border: 3px solid rgba(192,128,240,.625); border-radius: 24px;}
p {margin: 12px 24px 24px;}
label {cursor: pointer; display: inline-block; font-size: 11px; font-style: italic; color: lightgray;}
textarea {width: 100%; padding: 6px; font-size: 11px; border: 2px solid rgba(255,255,255,.25); border-radius: 4px;}
textarea:not(:focus):hover {cursor: pointer;}
label:before {content: ''; display: block; width: 640px; height: 8px; }
label:hover {color: mediumspringgreen;}
#toast {
display: inline-block;
position: absolute;
top: 0;
left: 0;
height: 32px;
line-height: 30px;
padding: 0px 12px;
font-size: 16px;
font-family: 'Segoe UI Semibold', sans-serif;
color: #333;
background-color: gold;
border-radius: 32px;
}
#toast.error {background-color: coral;}
#canvas {
cursor: pointer;
width: 160px;
height: 160px;
margin-right: 8px;
background-color: rgba(0,0,0,.5);
float: left;
}
#file {float: left;}
</style>
</head>
<body>
<main>
<span id="toast" style="display: none; opacity: 0;"></span>
<h4>Create Map from Bitmap</h4>
<p>Gets map grid, spawns and pawns based on a bitmap pixel colors.</p>
<canvas id="canvas"></canvas>
<input id="file" type="file">
<!-- Spawns -->
<label for="red">Red Docks</label>
<textarea id="red" style="height: 16px; background-color: rgba(255,80,80,.75);"></textarea>
<label for="gold">Gold Docks</label>
<textarea id="gold" style="height: 16px; background-color: rgba(255,224,96,.75);"></textarea>
<label for="blue">Blue Docks</label>
<textarea id="blue" style="height: 16px; background-color: rgba(80,176,255,.75);"></textarea>
<label for="alien">Alien Spawns</label>
<textarea id="alien" style="height: 64px; background-color: rgba(160,160,64,.75);"></textarea>
<!-- Furnitures -->
<label for="door">Doors</label>
<textarea id="door" style="height: 64px; background-color: rgba(240,176,112,.75);"></textarea>
<label for="item">Items</label>
<textarea id="item" style="height: 16px; background-color: rgba(80,176,176,.75);"></textarea>
<!-- Characters -->
<label for="gremkin">Gremkins</label>
<textarea id="gremkin" style="height: 16px; background-color: rgba(112,255,112,.75);"></textarea>
<label for="scrof">Scrofs</label>
<textarea id="scrof" style="height: 16px; background-color: rgba(64,192,64,.75)"></textarea>
<label for="limbo_lw">Limbo LW</label>
<textarea id="limbo_lw" style="height: 16px; background-color: rgba(255,128,240,.75);"></textarea>
<label for="limbo_hw">Limbo HW</label>
<textarea id="limbo_hw" style="height: 16px; background-color: rgba(224,64,176,.75);"></textarea>
<label for="limbo_cc">Limbo CC</label>
<textarea id="limbo_cc" style="height: 16px; background-color: rgba(160,0,160,.75);"></textarea>
<label for="cyborg">Cyborgs</label>
<textarea id="cyborg" style="height: 16px; background-color: rgba(224,224,224,.75);"></textarea>
<label for="juggernaut">Juggernaut</label>
<textarea id="juggernaut" style="height: 16px; background-color: rgba(192,192,192,.75);"></textarea>
<!-- Grid -->
<label for="grid">Grid</label>
<textarea id="grid" style="height: 576px; background-color: rgba(160,160,160,.75);"></textarea>
</main>
<script src="src/util.js"></script>
<script>
//////////////////////////////////////////////////////////////////////////
// # Grid Functions
//////////////////////////////////////////////////////////////////////////
function formatGrid(a) { // a =array
var s = "";
for (x = 0; x < a.length; x++) {
s += "[";
for (y = 0; y < a[x].length; y++) {
s += a[x][y];
if (y < a[x].length - 1) s += ",";
}
s += "]";
if (x < a.length - 1) s += ",\n";
}
return s;
}
function formatPointList(s) { // s =string
return "[" + (s.substr(0, s.length - 1)) + "]";
}
//////////////////////////////////////////////////////////////////////////
// # Load Images
//////////////////////////////////////////////////////////////////////////
function loadSourceImage(src, f) { // src = image source (path or blob), f = show toast flag
let b = false;
let img = new Image(); // create image
img.src = src; // set image source
img.onload = function() { // differ image manipulation after load
// * Check file
if (this.width != 40 || this.height != 40) {
console.error("File mismatch : expecting a 40x40 pixels image"); // DEBUG
} else {
getGridFromPixel(this);
b = true;
}
// * Show toast
let q = document.getElementById("file");
if (f) showToast(b ? "Image loaded!" : "Oops... Wrong size!", q.offsetLeft + q.offsetWidth, q.offsetTop, b ? "" : "error");
}
}
function loadBinaryImage(blob, type) {
loadSourceImage("data:" + type + ";base64,"+ btoa(blob), true); // convert binary image to base64 encoded image
}
//////////////////////////////////////////////////////////////////////////
// # Pixels Manipulation From Canvas
//////////////////////////////////////////////////////////////////////////
function getGridFromPixel(img) {
let w = h = 40; // fixed value
canvas = document.getElementById("canvas");
canvas.width = w;
canvas.height = h;
ctx = canvas.getContext("2d"); // set context
ctx.drawImage(img, 0, 0); // draw image on canvas
let imgData = ctx.getImageData(0, 0, w, h); // get whole canvas image data
let grid = createGrid(40, 40);
let red = "", gold = "", blue = "", alien = "";
let door = "", item = "";
let gremkin = "", scrof = "", limbo_lw = "", limbo_hw = "", limbo_cc = "", cyborg = "", juggernaut = "";
// * Get one pixel data
// function logPixelData(p) { // p = array [x,y]
// let x = p[0] * 4 // n x 4 per row
// let y = p[1] * w * 4; // w x 4 per line
// let n = x + y;
// console.log(p);
// console.log(n);
// console.log("R = " + imgData.data[n+0]);
// console.log("G = " + imgData.data[n+1]);
// console.log("B = " + imgData.data[n+2]);
// console.log("A = " + imgData.data[n+3]);
// }
// logPixelData([2,2]);
// * Parse pixel data
(function parsePixelData() { // grid = grid
let i, r, g, b, a, x, y, s = "", last_y = 0;
for (i = 0; i < imgData.data.length; i += 4) {
r = imgData.data[i+0];
g = imgData.data[i+1];
b = imgData.data[i+2];
a = imgData.data[i+3];
y = Math.floor(i / w / 4);
x = (i - (y * w * 4)) / 4;
// * Fill grid and separate arrays
if (r == 0 && g == 154 && b == 154) {
grid[x][y] = 1; // void -- type 1
} else if (r == 64 && g == 64 && b == 64) {
grid[x][y] = 2; // wall -- type 2
} else if (r == 192 && g == 0 && b == 0) {
grid[x][y] = 5; // dock (red) -- type 5
red += "[" + x + "," + y + "],"; // red dock
} else if (r == 192 && g == 192 && b == 0) {
grid[x][y] = 6; // dock (gold) -- type 6
gold += "[" + x + "," + y + "],"; // gold dock
} else if (r == 0 && g == 0 && b == 192) {
grid[x][y] = 7; // dock (blue) -- type 7
blue += "[" + x + "," + y + "],"; // blue dock
} else if (r == 80 && g == 80 && b == 0) {
grid[x][y] = 8; // spawn (alien) -- type 8
alien += "[" + x + "," + y + "],"; // alien spawn
} else if (r == 176 && g == 128 && b == 0) {
door += "[" + x + "," + y + "],"; // door -- type 3
} else if (r == 0 && g == 112 && b == 112) {
item += "[" + x + "," + y + "],"; // item -- type 4
} else if (r == 0 && g == 224 && b == 0) {
gremkin += "[" + x + "," + y + "],"; // gremkin -- type 8
} else if (r == 0 && g == 160 && b == 0) {
scrof += "[" + x + "," + y + "],"; // scrof -- type 8
} else if (r == 255 && g == 64 && b == 224) {
limbo_lw += "[" + x + "," + y + "],"; // limbo_lw -- type 8
} else if (r == 192 && g == 32 && b == 160) {
limbo_hw += "[" + x + "," + y + "],"; // limbo_hw -- type 8
} else if (r == 128 && g == 0 && b == 128) {
limbo_cc += "[" + x + "," + y + "],"; // limbo_cc -- type 8
} else if (r == 192 && g == 192 && b == 192) {
cyborg += "[" + x + "," + y + "],"; // cyborg -- type 8
} else if (r == 128 && g == 128 && b == 128) {
juggernaut += "[" + x + "," + y + "],"; // juggernaut -- type 8
}
}
}());
// * Update HTML elements
document.getElementById("grid").value = formatGrid(grid);
document.getElementById("red").value = formatPointList(red);
document.getElementById("gold").value = formatPointList(gold);
document.getElementById("blue").value = formatPointList(blue);
document.getElementById("alien").value = formatPointList(alien);
document.getElementById("door").value = formatPointList(door);
document.getElementById("item").value = formatPointList(item);
document.getElementById("gremkin").value = formatPointList(gremkin);
document.getElementById("scrof").value = formatPointList(scrof);
document.getElementById("limbo_lw").value = formatPointList(limbo_lw);
document.getElementById("limbo_hw").value = formatPointList(limbo_hw);
document.getElementById("limbo_cc").value = formatPointList(limbo_cc);
document.getElementById("cyborg").value = formatPointList(cyborg);
document.getElementById("juggernaut").value = formatPointList(juggernaut);
// * Quadruple canvas size
canvas.width = 160;
canvas.height = 160;
// * Disable image smoothing (i.e. anti-aliasing)
ctx.imageSmoothingEnabled = false;
// * Adjust canvas scale
ctx.scale(4, 4);
// * Redraw image after pixel manipulation
ctx.drawImage(img, 0, 0); // redraw image on canvas
};
//////////////////////////////////////////////////////////////////////////
// # On Load
//////////////////////////////////////////////////////////////////////////
window.addEventListener("load", function() {
// * Load sample image
loadSourceImage("res/map/m01/mini.png"); // TEMP
// * Reset file input
document.getElementById("file").value = "";
// * Reset text areas
document.querySelectorAll("textarea").forEach(function(e) {
e.value = "";
});
});
//////////////////////////////////////////////////////////////////////////
// # Change File
//////////////////////////////////////////////////////////////////////////
document.getElementById("file").addEventListener("change", function() {
// * File API
let file = this.files[0]; // only first in list ; no multiple selection allowed
let type = file.type; // remember MIME type
// * FileReader API
let reader = new FileReader();
reader.readAsBinaryString(file);
reader.addEventListener("loadend", function() {
loadBinaryImage(this.result, type); // load file content as a binary image
});
});
document.getElementById("canvas").addEventListener("click", function() {
document.getElementById("file").click();
});
//////////////////////////////////////////////////////////////////////////
// # Toast
//////////////////////////////////////////////////////////////////////////
var toast;
function showToast(s, x, y, c) { // s = string, x, y = pixel, c = class
let q = document.getElementById("toast");
if (c !== undefined && c != "") q.classList.add(c);
else q.removeAttribute("class");
q.innerHTML = s;
q.style.display = "";
q.style.transition = "";
x -= q.offsetWidth + 16;
y -= q.offsetHeight - 24;
q.style.top = y + "px";
q.style.left = x + "px";
setTimeout(function() {
q.style.transition = "opacity .1875s ease-in, top .125s ease";
q.style.opacity = "1";
q.style.top = (y - 16) + "px";
}, 15);
clearTimeout(toast);
toast = setTimeout(hideToast, 2000);
}
function hideToast() {
let q = document.getElementById("toast");
q.style.opacity = "0";
}
document.getElementById("toast").addEventListener("transitionend", function() {
if (this.style.opacity == "0") {
this.style.display = "none";
}
});
//////////////////////////////////////////////////////////////////////////
// # Copy to Clipboard
//////////////////////////////////////////////////////////////////////////
document.querySelectorAll("textarea").forEach(function(e) {
e.addEventListener("click", function() {
if (this.value.length > 0) {
this.focus();
this.setSelectionRange(0, this.value.length);
try {
let b = document.execCommand("copy");
showToast(b ? "Copied!" : "Not copied...", this.offsetLeft + this.offsetWidth, this.offsetTop, b ? "" : "error");
} catch(err) {
console.error("Copy command failed"); // DEBUG
}
}
});
e.addEventListener("blur", function() {
this.selectionStart = this.value.length;
});
});
</script>
</body>
<html>