tilesy/script.js

196 lines
6.0 KiB
JavaScript
Raw Normal View History

2020-11-04 15:01:04 +00:00
import init, {
add_tiles,
load_image,
load_game,
load_default_game,
output,
set_prefix,
set_invert,
set_flip,
set_mirror,
set_rotate
} from './pkg/tilesy.js';
2020-10-30 17:32:49 +00:00
2020-11-04 15:01:43 +00:00
if (typeof WebAssembly !== "object") {
document.getElementById("start").innerText = "Sorry - your browser does not support WebAssembly";
}
2020-07-18 20:47:04 +00:00
2020-07-19 23:18:06 +00:00
// stolen from https://ourcodeworld.com/articles/read/189/how-to-create-a-file-and-generate-a-download-with-javascript-in-the-browser-without-a-server
function download(filename, text) {
let element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
function el(id) {
return document.getElementById(id);
}
2020-07-19 21:36:36 +00:00
function pagination(e) {
const parent = e.target.parentNode;
parent.style.display = "none";
if (e.target.classList.contains("next")) {
parent.nextSibling.style.display = "block";
} else if (e.target.classList.contains("prev")) {
parent.previousSibling.style.display = "block";
} else if (e.target.classList.contains("start")) {
document.getElementById("start").style.display = "block";
}
}
function readFile(input, callback, type = "text") {
if (input.files && input.files[0]) {
let reader = new FileReader();
reader.onload = callback;
if (type === "text") {
reader.readAsText(input.files[0]);
} else {
reader.readAsDataURL(input.files[0]);
}
}
}
2020-07-18 20:47:04 +00:00
async function run() {
await init();
const buttonAddImage = el("add");
2020-07-20 17:57:24 +00:00
const buttonBackToImage = el("back-to-image");
2020-07-19 23:18:06 +00:00
const buttonDownload = el("download");
const buttonGameDataProceed = el("game-data-next");
const buttonImportGame = el("import");
const buttonLoadGame = el("load");
const buttonNewGame = el("new");
const buttonReset = el("reset");
const checkboxInvertTiles = el("invert");
const checkboxFlipTiles = el("flip");
const checkboxMirrorTiles = el("mirror");
const checkboxRotateTiles = el("rotate");
2020-07-20 15:41:45 +00:00
const inputPrefix = el("prefix");
const imagePreview = el("preview");
const textareaGameDataInput = el("game-data");
const textareaGameDataOutput = el("output");
2020-07-18 20:47:04 +00:00
// hide all pages except start page
for (let page of document.getElementsByClassName('page')) {
page.style.display = "none";
}
el("start").style.display = "block";
2020-07-18 20:47:04 +00:00
for (let pageButton of document.getElementsByClassName("pagination")) {
2020-07-19 21:36:36 +00:00
pageButton.addEventListener('click', pagination);
2020-07-18 20:47:04 +00:00
pageButton.addEventListener('touchend', pagination);
}
function new_game() {
2021-07-05 16:35:17 +00:00
load_default_game();
2020-11-04 15:01:04 +00:00
textareaGameDataInput.value = output();
2020-07-20 16:45:22 +00:00
checkGameData();
2020-07-18 20:47:04 +00:00
// we don't need to look at the default game data, so skip ahead to the image page
2020-07-19 19:51:56 +00:00
buttonGameDataProceed.click();
2020-07-18 20:47:04 +00:00
}
function clear_game() {
textareaGameDataInput.value = "";
2020-07-20 16:45:22 +00:00
checkGameData();
2020-07-18 20:47:04 +00:00
}
buttonNewGame.addEventListener("click", new_game);
buttonNewGame.addEventListener("touchend", new_game);
buttonLoadGame.addEventListener("click", clear_game);
buttonLoadGame.addEventListener("touchend", clear_game);
2020-07-18 20:47:04 +00:00
// handle game data and image
el("game").addEventListener("change", function() {
readFile(this, function (e) {
textareaGameDataInput.value = e.target.result;
2020-11-04 15:01:04 +00:00
console.log(load_game(e.target.result));
2020-07-19 19:51:56 +00:00
checkGameData();
}, "text");
});
2020-07-19 19:51:56 +00:00
function checkGameData() {
if (textareaGameDataInput.value.length > 0) {
2020-07-19 19:51:56 +00:00
buttonGameDataProceed.removeAttribute("disabled");
} else {
buttonGameDataProceed.setAttribute("disabled", "disabled");
}
}
textareaGameDataInput.addEventListener("change", checkGameData);
textareaGameDataInput.addEventListener("keyup", checkGameData);
2020-07-20 15:41:45 +00:00
checkGameData();
2020-07-20 16:35:02 +00:00
2020-07-20 15:41:45 +00:00
imagePreview.style.display = "none";
2020-07-18 20:47:04 +00:00
el('image').addEventListener('change', function () {
2020-07-18 20:47:04 +00:00
readFile(this, function (e) {
2020-07-20 15:41:45 +00:00
imagePreview.src = e.target.result;
2020-11-04 15:01:04 +00:00
imagePreview.style.display = "initial";
console.log(load_image(imagePreview.getAttribute("src")));
2020-07-18 20:47:04 +00:00
}, "image");
});
function addTiles() {
2020-11-04 15:01:04 +00:00
console.log(add_tiles());
textareaGameDataOutput.value = output();
2020-07-18 20:47:04 +00:00
}
buttonImportGame.addEventListener("click", addTiles);
buttonImportGame.addEventListener("touchend", addTiles);
2020-07-19 19:51:56 +00:00
2020-07-19 23:18:06 +00:00
function handleDownload() {
download("output.bitsy", textareaGameDataOutput.value);
}
buttonDownload.addEventListener("click", handleDownload);
buttonDownload.addEventListener("touchend", handleDownload);
function addImage() {
textareaGameDataInput.value = textareaGameDataOutput.value;
textareaGameDataOutput.value = "";
2020-07-20 17:57:24 +00:00
buttonBackToImage.click();
}
buttonAddImage.addEventListener("click", addImage);
buttonAddImage.addEventListener("touchend", addImage);
2020-11-04 15:01:04 +00:00
inputPrefix.addEventListener("change", () => {
set_prefix(inputPrefix.value);
})
checkboxInvertTiles.addEventListener("change", () => {
set_invert(checkboxInvertTiles.checked);
});
checkboxFlipTiles.addEventListener("change", () => {
set_flip(checkboxFlipTiles.checked);
});
checkboxMirrorTiles.addEventListener("change", () => {
set_mirror(checkboxMirrorTiles.checked);
});
checkboxRotateTiles.addEventListener("change", () => {
set_rotate(checkboxRotateTiles.checked);
});
2020-07-19 19:51:56 +00:00
function reset() {
2020-07-19 21:02:22 +00:00
clear_game();
2020-07-20 15:41:45 +00:00
imagePreview.removeAttribute("src");
2020-07-19 19:51:56 +00:00
}
buttonReset.addEventListener("click", reset);
buttonReset.addEventListener("touchend", reset);
2020-07-18 20:47:04 +00:00
}
run();