2020-07-18 20:47:04 +00:00
|
|
|
import init, {load_default_game_data, add_tiles} from './pkg/bitsy_tiles.js';
|
|
|
|
|
2020-07-19 22:12:32 +00:00
|
|
|
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();
|
|
|
|
|
2020-07-19 22:12:32 +00:00
|
|
|
const buttonAddImage = el("add");
|
|
|
|
const buttonGameDataProceed = el("game-data-next");
|
|
|
|
const textareaGameDataInput = el("game-data");
|
|
|
|
const buttonImportGame = el("import");
|
|
|
|
const buttonLoadGame = el("load");
|
|
|
|
const buttonNewGame = el("new");
|
|
|
|
const textareaGameDataOutput = el("output");
|
|
|
|
const preview = el("preview");
|
|
|
|
const resetButton = el("reset");
|
2020-07-19 21:01:51 +00:00
|
|
|
|
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";
|
|
|
|
}
|
2020-07-19 22:12:32 +00:00
|
|
|
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() {
|
2020-07-19 22:12:32 +00:00
|
|
|
textareaGameDataInput.value = load_default_game_data();
|
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() {
|
2020-07-19 22:12:32 +00:00
|
|
|
textareaGameDataInput.value = "";
|
2020-07-18 20:47:04 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 22:12:32 +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
|
|
|
|
|
2020-07-19 22:12:32 +00:00
|
|
|
el("game").addEventListener("change", function() {
|
2020-07-19 12:17:50 +00:00
|
|
|
readFile(this, function (e) {
|
2020-07-19 22:12:32 +00:00
|
|
|
textareaGameDataInput.value = e.target.result;
|
2020-07-19 19:51:56 +00:00
|
|
|
checkGameData();
|
2020-07-19 12:17:50 +00:00
|
|
|
}, "text");
|
|
|
|
});
|
|
|
|
|
2020-07-19 19:51:56 +00:00
|
|
|
function checkGameData() {
|
2020-07-19 22:12:32 +00:00
|
|
|
if (textareaGameDataInput.value.length > 0) {
|
2020-07-19 19:51:56 +00:00
|
|
|
buttonGameDataProceed.removeAttribute("disabled");
|
|
|
|
} else {
|
|
|
|
buttonGameDataProceed.setAttribute("disabled", "disabled");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-19 22:12:32 +00:00
|
|
|
textareaGameDataInput.addEventListener("change", checkGameData);
|
|
|
|
textareaGameDataInput.addEventListener("keyup", checkGameData);
|
2020-07-19 19:51:56 +00:00
|
|
|
|
2020-07-18 20:47:04 +00:00
|
|
|
preview.style.display = "none";
|
|
|
|
|
2020-07-19 22:12:32 +00:00
|
|
|
el('image').addEventListener('change', function () {
|
2020-07-18 20:47:04 +00:00
|
|
|
readFile(this, function (e) {
|
|
|
|
preview.src = e.target.result;
|
|
|
|
preview.style.display = "initial"
|
|
|
|
}, "image");
|
|
|
|
});
|
|
|
|
|
|
|
|
function addTiles() {
|
2020-07-19 22:12:32 +00:00
|
|
|
let image = el("preview").getAttribute("src");
|
|
|
|
let gameData = el("game-data").value;
|
|
|
|
let prefix = el("prefix").value;
|
2020-07-19 21:02:22 +00:00
|
|
|
|
2020-07-19 22:12:32 +00:00
|
|
|
textareaGameDataOutput.value = add_tiles(gameData, image, prefix);
|
2020-07-18 20:47:04 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 22:12:32 +00:00
|
|
|
buttonImportGame.addEventListener("click", addTiles);
|
|
|
|
buttonImportGame.addEventListener("touchend", addTiles);
|
2020-07-19 19:51:56 +00:00
|
|
|
|
2020-07-19 21:01:51 +00:00
|
|
|
function addImage() {
|
2020-07-19 22:12:32 +00:00
|
|
|
textareaGameDataInput.value = textareaGameDataOutput.value;
|
|
|
|
textareaGameDataOutput.value = "";
|
2020-07-19 21:01:51 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 22:12:32 +00:00
|
|
|
buttonAddImage.addEventListener("click", addImage);
|
|
|
|
buttonAddImage.addEventListener("touchend", addImage);
|
2020-07-19 21:01:51 +00:00
|
|
|
|
2020-07-19 19:51:56 +00:00
|
|
|
function reset() {
|
2020-07-19 21:02:22 +00:00
|
|
|
clear_game();
|
2020-07-19 19:51:56 +00:00
|
|
|
preview.removeAttribute("src");
|
|
|
|
}
|
|
|
|
|
2020-07-19 21:36:43 +00:00
|
|
|
resetButton.addEventListener("click", reset);
|
|
|
|
resetButton.addEventListener("touchend", reset);
|
2020-07-18 20:47:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
run();
|