-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
434 lines (371 loc) · 14.7 KB
/
app.js
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
const fs = require('fs');
const path = require('path');
const dialog = require('electron').remote.dialog;
const util = require('util');
const writeFile = util.promisify(fs.writeFile);
const pkg = require('./package.json');
const open = require('open')
const unhandled = require('electron-unhandled');
unhandled({
showDialog: true,
reportButton: error => {
open(pkg.homepage);
}
});
var $f = function () {
document.getElementById('app-version').innerText = 'v' + pkg.version;
const resourcesDir = path.join(__dirname, 'resources', 'templates')
const inputTileCountX = 5
const tileCountX = 11
const tileCountY = 5
var inputFileName = 'demo.png';
var tileSize = 16;
var scale = 1;
var imageLoader = document.getElementById('uploader-input');
// var imageDownloader = document.getElementById('downloader-input');
var $img = document.getElementById('uploader-img');
var $imgOut = document.getElementById('img-output-generated');
var $btnSaveImage = document.getElementById('btn-save-image');
var $btnGodotExport = document.getElementById('btn-export-godot');
var $tileSizeInput = document.getElementById('tile_size');
imageLoader.addEventListener('change', handleImage, false);
// imageDownloader.addEventListener('click', downloadImage, false);
document.getElementById('uploader-pic').addEventListener('click', function (e) {
imageLoader.click();
}, false);
$btnSaveImage.addEventListener('click', saveCanvasImage, false);
$btnGodotExport.addEventListener('click', saveGodotExport, false);
$tileSizeInput.addEventListener('change', function (e) {
tileSize = this.value;
console.log("New tile size is: " + tileSize);
generateCanvasImg();
}, false);
var canvas = document.getElementById("composer-img-canvas");
var ctx = canvas.getContext("2d");
ctx.imageSmoothingEnabled = false;
ctx.scale(scale, scale);
$img.addEventListener("load", function (e) {
generateCanvasImg();
}, false);
function drawCell(sx, sy, dx, dy) {
ctx.drawImage(
$img,
sx * tileSize, sy * tileSize, tileSize, tileSize,
dx * tileSize, dy * tileSize, tileSize, tileSize
);
ctx.save();
}
function drawCellSlice(size, sx, sy, dx, dy) {
ctx.drawImage(
$img,
sx * tileSize, sy * tileSize, size, size,
dx * tileSize, dy * tileSize, size, size
);
ctx.save();
}
function handleImage(e) {
$img.setAttribute("src", "");
if (e.target.files.length > 0) {
var reader = new FileReader();
reader.onload = function (event) {
$img.setAttribute('src', event.target.result);
detectTileSize();
}
inputFileName = e.target.files[0].name;
document.title = 'Autotiler - ' + inputFileName;
reader.readAsDataURL(e.target.files[0]);
$btnSaveImage.disabled = false;
$btnGodotExport.disabled = false;
}
}
function detectTileSize() {
var tmpIm = new Image();
tmpIm.src = $img.src;
tmpIm.onload = function (e) {
// if (newTileSize === tileSize) {
// return;
// }
$tileSizeInput.value = Math.floor(tmpIm.width / inputTileCountX);
$tileSizeInput.dispatchEvent(new Event('change'))
$img.dispatchEvent(new Event('loaded'))
}
}
function downloadImage(e) {
var newIm = new Image();
newIm.src = createOutputCanvas().toDataURL();
newIm.id = 'img-output-generated-img';
// document.body.appendChild(canvas2);
$imgOut.innerHTML = '';
$imgOut.appendChild(newIm);
// document.body.app = ('<img src="' + canvas2.toDataURL() + '" />' +
// '<br><br><a href="javascript:window.location.reload();">⟸ Back</a>');
$imgOut.addEventListener("click", saveCanvasImage)
}
/**
* @returns {HTMLCanvasElement}
*/
function createOutputCanvas() {
var canvas2 = document.createElement("canvas");
var ctx2 = canvas2.getContext("2d");
ctx2.imageSmoothingEnabled = false;
// ctx2.width = ctx2.width / scale;
// ctx2.height = ctx2.height / scale;
ctx2.width = canvas.width / scale;
ctx2.height = canvas.height / scale;
canvas2.width = canvas.width / scale;
canvas2.height = canvas.height / scale;
ctx2.scale(1 / scale, 1 / scale);
ctx2.drawImage(canvas, 0, 0);
return canvas2;
}
function generateCanvasImg() {
canvas.width = (tileSize * tileCountX)
canvas.height = (tileSize * tileCountY)
ctx.width = canvas.width
ctx.height = canvas.height
ctx.clearRect(0, 0, canvas.width, canvas.height);
// ctx.width = (88 * (tileSize / 8)) * scale;
// ctx.height = (40 * (tileSize / 8)) * scale;
drawCell(0, 0, 0, 0);
drawCell(1, 0, 1, 0);
drawCell(2, 0, 2, 0);
drawCell(0, 1, 0, 1);
drawCell(1, 1, 1, 1);
drawCell(2, 1, 2, 1);
drawCell(0, 2, 0, 2);
drawCell(1, 2, 1, 2);
drawCell(2, 2, 2, 2);
drawCellSlice(tileSize / 2, 0, 0, 0, 3);
drawCellSlice(tileSize / 2, 1, 0, 0.5, 3);
drawCellSlice(tileSize / 2, 1, 0, 1, 3);
drawCellSlice(tileSize / 2, 1, 0, 1.5, 3);
drawCellSlice(tileSize / 2, 1, 0, 2, 3);
drawCellSlice(tileSize / 2, 2.5, 0, 2.5, 3);
drawCellSlice(tileSize / 2, 0, 2.5, 0, 3.5);
drawCellSlice(tileSize / 2, 1, 2.5, 0.5, 3.5);
drawCellSlice(tileSize / 2, 1, 2.5, 1, 3.5);
drawCellSlice(tileSize / 2, 1, 2.5, 1.5, 3.5);
drawCellSlice(tileSize / 2, 1, 2.5, 2, 3.5);
drawCellSlice(tileSize / 2, 2.5, 2.5, 2.5, 3.5);
drawCellSlice(tileSize / 2, 0, 0, 3, 3);
drawCellSlice(tileSize / 2, 2.5, 0, 3.5, 3);
drawCellSlice(tileSize / 2, 0, 2.5, 3, 3.5);
drawCellSlice(tileSize / 2, 2.5, 2.5, 3.5, 3.5);
drawCellSlice(tileSize / 2, 0, 0, 3, 0);
drawCellSlice(tileSize / 2, 2.5, 0, 3.5, 0);
drawCellSlice(tileSize / 2, 0, 0.5, 3, 0.5);
drawCellSlice(tileSize / 2, 2.5, 0.5, 3.5, 0.5);
drawCellSlice(tileSize / 2, 0, 0.5, 3, 1);
drawCellSlice(tileSize / 2, 2.5, 0.5, 3.5, 1);
drawCellSlice(tileSize / 2, 0, 0.5, 3, 1.5);
drawCellSlice(tileSize / 2, 2.5, 0.5, 3.5, 1.5);
drawCellSlice(tileSize / 2, 0, 0.5, 3, 2);
drawCellSlice(tileSize / 2, 2.5, 0.5, 3.5, 2);
drawCellSlice(tileSize / 2, 0, 2.5, 3, 2.5);
drawCellSlice(tileSize / 2, 2.5, 2.5, 3.5, 2.5);
///
drawCell(0, 0, 4, 0);
drawCell(1, 0, 5, 0);
drawCell(1, 0, 6, 0);
drawCell(2, 0, 7, 0);
drawCell(0, 1, 4, 1);
drawCell(1, 1, 5, 1);
drawCell(1, 1, 6, 1);
drawCell(2, 1, 7, 1);
drawCell(0, 1, 4, 2);
drawCell(1, 1, 5, 2);
drawCell(1, 1, 6, 2);
drawCell(2, 1, 7, 2);
drawCell(0, 2, 4, 3);
drawCell(1, 2, 5, 3);
drawCell(1, 2, 6, 3);
drawCell(2, 2, 7, 3);
///
drawCell(0, 1, 4, 4);
drawCell(1, 1, 5, 4);
drawCell(1, 1, 6, 4);
drawCell(2, 1, 7, 4);
drawCell(1, 1, 8, 4);
drawCell(1, 2, 8, 3);
drawCell(1, 1, 8, 2);
drawCell(1, 1, 8, 1);
drawCell(1, 0, 8, 0);
///
drawCell(1, 1, 9, 0);
drawCell(1, 1, 9, 1);
drawCell(1, 1, 9, 2);
drawCell(1, 1, 9, 3);
drawCell(1, 1, 10, 2);
drawCell(1, 1, 10, 3);
/// corners/holes
drawCellSlice(tileSize / 2, 3.5, 0.5, 4.5, 0.5);
drawCellSlice(tileSize / 2, 3.5, 0.5, 4.5, 1.5);
drawCellSlice(tileSize / 2, 3.5, 0.5, 4.5, 4.5);
drawCellSlice(tileSize / 2, 3.5, 0.5, 5.5, 0.5);
drawCellSlice(tileSize / 2, 3.5, 0.5, 5.5, 1.5);
drawCellSlice(tileSize / 2, 3.5, 0.5, 5.5, 4.5);
drawCellSlice(tileSize / 2, 3.5, 0.5, 8.5, 0.5);
drawCellSlice(tileSize / 2, 3.5, 0.5, 8.5, 1.5);
drawCellSlice(tileSize / 2, 3.5, 0.5, 8.5, 4.5);
drawCellSlice(tileSize / 2, 3.5, 0.5, 9.5, 1.5);
drawCellSlice(tileSize / 2, 3.5, 0.5, 9.5, 3.5);
drawCellSlice(tileSize / 2, 3.5, 0.5, 10.5, 2.5);
drawCellSlice(tileSize / 2, 3.5, 0.5, 10.5, 3.5);
drawCellSlice(tileSize / 2, 4, 0.5, 6, 0.5);
drawCellSlice(tileSize / 2, 4, 0.5, 6, 1.5);
drawCellSlice(tileSize / 2, 4, 0.5, 6, 4.5);
drawCellSlice(tileSize / 2, 4, 0.5, 7, 0.5);
drawCellSlice(tileSize / 2, 4, 0.5, 7, 1.5);
drawCellSlice(tileSize / 2, 4, 0.5, 7, 4.5);
drawCellSlice(tileSize / 2, 4, 0.5, 8, 4.5);
drawCellSlice(tileSize / 2, 4, 0.5, 8, 1.5);
drawCellSlice(tileSize / 2, 4, 0.5, 8, 0.5);
drawCellSlice(tileSize / 2, 4, 0.5, 9, 0.5);
drawCellSlice(tileSize / 2, 4, 0.5, 9, 2.5);
drawCellSlice(tileSize / 2, 4, 0.5, 9, 3.5);
drawCellSlice(tileSize / 2, 4, 0.5, 10, 3.5);
///
drawCellSlice(tileSize / 2, 3.5, 1, 4.5, 2);
drawCellSlice(tileSize / 2, 3.5, 1, 5.5, 2);
drawCellSlice(tileSize / 2, 3.5, 1, 4.5, 3);
drawCellSlice(tileSize / 2, 3.5, 1, 5.5, 3);
drawCellSlice(tileSize / 2, 3.5, 1, 4.5, 4);
drawCellSlice(tileSize / 2, 3.5, 1, 5.5, 4);
drawCellSlice(tileSize / 2, 3.5, 1, 8.5, 4);
drawCellSlice(tileSize / 2, 3.5, 1, 8.5, 3);
drawCellSlice(tileSize / 2, 3.5, 1, 8.5, 2);
drawCellSlice(tileSize / 2, 3.5, 1, 9.5, 0);
drawCellSlice(tileSize / 2, 3.5, 1, 9.5, 2);
drawCellSlice(tileSize / 2, 3.5, 1, 10.5, 2);
drawCellSlice(tileSize / 2, 3.5, 1, 10.5, 3);
drawCellSlice(tileSize / 2, 4, 1, 6, 2);
drawCellSlice(tileSize / 2, 4, 1, 7, 2);
drawCellSlice(tileSize / 2, 4, 1, 6, 3);
drawCellSlice(tileSize / 2, 4, 1, 7, 3);
drawCellSlice(tileSize / 2, 4, 1, 6, 4);
drawCellSlice(tileSize / 2, 4, 1, 7, 4);
drawCellSlice(tileSize / 2, 4, 1, 8, 2);
drawCellSlice(tileSize / 2, 4, 1, 8, 3);
drawCellSlice(tileSize / 2, 4, 1, 8, 4);
drawCellSlice(tileSize / 2, 4, 1, 9, 1);
drawCellSlice(tileSize / 2, 4, 1, 9, 2);
drawCellSlice(tileSize / 2, 4, 1, 9, 3);
drawCellSlice(tileSize / 2, 4, 1, 10, 2);
ctx.save();
// downloadImage();
}
function saveGodotExport() {
dialog.showSaveDialog(
{title: 'Export as Godot 3.x resource', defaultPath: inputFileName.replace(/\.([^.]+)/, '-autotile.$1')},
).then(file => {
// Stating whether dialog operation was cancelled or not.
if (!file.canceled) {
console.log(file.filePath.toString());
saveGodotResources(file.filePath.toString());
} else {
console.log("save canceled");
}
});
}
function saveCanvasImage() {
dialog.showSaveDialog(
{title: 'Save generated image', defaultPath: inputFileName.replace(/\.([^.]+)/, '-autotile.$1')},
).then(file => {
// Stating whether dialog operation was cancelled or not.
if (!file.canceled) {
console.log(file.filePath.toString());
saveImg(file.filePath.toString());
} else {
console.log("save canceled");
}
});
}
function readBlobAsUint8Array(blob) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onloadend = () => {
return new Uint8Array(reader.result);
};
reader.onerror = reject;
reader.readAsArrayBuffer(blob);
});
}
/**
*
* @param {HTMLCanvasElement} canv
* @returns {Promise<string[]>}
*/
function getCanvasAsBlob(canv) {
return new Promise((resolve) => {
canv.toBlob(resolve, "image/png");
});
}
async function saveCanvasAsPng(filename, canv) {
const blob = await getCanvasAsBlob(canv);
const data = await readBlobAsUint8Array(blob);
await writeFile(filename, data);
}
function getImageBinaryData() {
const dataUrl = createOutputCanvas().toDataURL();
return dataUrl.substring('data:image/png;base64,'.length);
}
function saveImg(filename) {
const imgBase64 = getImageBinaryData();
fs.writeFileSync(filename, imgBase64, 'base64');
}
function getFileExtension(filename) {
let ext = filename.split('.').pop()
if (filename === ext) {
return ''
}
return '.' + ext
}
function replaceTemplateVars(template, varsObj) {
let contents = template
const keys = Object.keys(varsObj)
for (const i in keys) {
const key = keys[i]
const value = varsObj[key]
contents = contents.replace(new RegExp('\\$' + key, 'g'), value)
}
return contents
}
function getGodotExportFullDir(filename) {
return path.join(path.dirname(filename), getGodotExportBasename(filename))
}
function getGodotExportBasename(filename) {
return path.join(path.basename(filename, getFileExtension(filename)))
}
function generateGodotResources(filename, tilesize) {
const baseFilename = path.basename(filename)
const baseFilenameNoExt = getGodotExportBasename(filename)
let importFileData = fs.readFileSync(path.join(resourcesDir, 'tileset.png.import'), "utf8")
let tresFileData = fs.readFileSync(path.join(resourcesDir, 'tileset.tres'), "utf8")
const tplVars = {
TS: tilesize,
IMGW: tilesize * tileCountX,
IMGH: tilesize * tileCountY,
IMGFILEPATH: (baseFilenameNoExt + '/' + baseFilename),
IMGFILE: baseFilename
}
importFileData = replaceTemplateVars(importFileData, tplVars)
tresFileData = replaceTemplateVars(tresFileData, tplVars)
return [
{file: baseFilename + '.import', content: importFileData},
{file: baseFilenameNoExt + '.tres', content: tresFileData},
]
}
function saveGodotResources(filename) {
const destDir = getGodotExportFullDir(filename)
const godotResources = generateGodotResources(filename, tileSize)
if (!fs.existsSync(destDir)) {
fs.mkdirSync(destDir)
console.log(destDir)
}
saveImg(path.join(destDir, path.basename(filename)))
godotResources.forEach(function (res) {
fs.writeFileSync(path.join(destDir, res.file), res.content);
})
}
}
document.addEventListener("DOMContentLoaded", $f);