pixsy/script.js

221 lines
6.5 KiB
JavaScript
Raw Normal View History

2020-11-04 15:38:43 +00:00
import init, {
2020-11-04 16:11:44 +00:00
add_room,
2020-11-05 18:48:22 +00:00
get_palettes,
get_preview,
2020-11-04 15:38:43 +00:00
load_image,
load_game,
load_default_game,
output,
set_brightness,
2020-11-05 18:48:22 +00:00
set_dither,
2020-11-04 16:11:44 +00:00
set_room_name,
2020-11-04 15:38:43 +00:00
} from './pkg/pixsy.js';
if (typeof WebAssembly !== "object") {
2020-11-05 18:48:22 +00:00
window.location = "./old/"
2020-11-04 15:38:43 +00:00
}
2020-10-30 17:50:03 +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);
}
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]);
}
}
}
async function run() {
await init();
const buttonAddImage = el("add");
const buttonBackToImage = el("back-to-image");
const buttonDownload = el("download");
const buttonGameDataProceed = el("game-data-next");
2020-11-05 18:48:22 +00:00
const buttonImageProceed = el("image-next");
const buttonRoomProceed = el("room-next");
2020-10-30 17:50:03 +00:00
const buttonLoadGame = el("load");
const buttonNewGame = el("new");
const buttonReset = el("reset");
2020-11-05 18:48:22 +00:00
const checkboxDither = el("dither");
const inputBrightness = el("brightness");
2020-11-05 18:48:22 +00:00
const inputRoomName = el("room-name");
const selectPalette = el("palette");
2020-10-30 17:50:03 +00:00
const textareaGameDataInput = el("game-data");
const textareaGameDataOutput = el("output");
2020-11-05 09:52:14 +00:00
const cropper = new Cropper({ width: 128, height: 128 });
let cropperRendered = false;
2020-10-30 17:50:03 +00:00
// hide all pages except start page
for (let page of document.getElementsByClassName('page')) {
page.style.display = "none";
}
el("start").style.display = "block";
for (let pageButton of document.getElementsByClassName("pagination")) {
pageButton.addEventListener('click', pagination);
pageButton.addEventListener('touchend', pagination);
}
function new_game() {
2020-11-04 15:38:43 +00:00
load_default_game()
textareaGameDataInput.value = output();
2020-10-30 17:50:03 +00:00
checkGameData();
// we don't need to look at the default game data, so skip ahead to the image page
buttonGameDataProceed.click();
}
function clear_game() {
textareaGameDataInput.value = "";
checkGameData();
}
buttonNewGame.addEventListener("click", new_game);
buttonNewGame.addEventListener("touchend", new_game);
buttonLoadGame.addEventListener("click", clear_game);
buttonLoadGame.addEventListener("touchend", clear_game);
// handle game data and image
el("game").addEventListener("change", function() {
readFile(this, function (e) {
textareaGameDataInput.value = e.target.result;
2020-11-04 15:38:43 +00:00
console.log(load_game(e.target.result));
2020-10-30 17:50:03 +00:00
checkGameData();
}, "text");
});
2020-11-05 18:48:22 +00:00
function setPaletteDropdown() {
let palettes = JSON.parse(get_palettes());
selectPalette.innerHTML = "";
for (let palette of palettes) {
let option = document.createElement("option");
option.value = palette.id;
option.innerText = palette.name;
selectPalette.appendChild(option);
}
}
2020-10-30 17:50:03 +00:00
function checkGameData() {
if (textareaGameDataInput.value.length > 0) {
buttonGameDataProceed.removeAttribute("disabled");
2020-11-05 18:48:22 +00:00
setPaletteDropdown();
2020-10-30 17:50:03 +00:00
} else {
buttonGameDataProceed.setAttribute("disabled", "disabled");
}
}
textareaGameDataInput.addEventListener("change", checkGameData);
textareaGameDataInput.addEventListener("keyup", checkGameData);
checkGameData();
el('image').addEventListener('change', function () {
readFile(this, function (e) {
2020-11-05 09:52:14 +00:00
if ( ! cropperRendered) {
cropper.render("#crop");
cropperRendered = true;
2020-11-05 18:48:22 +00:00
buttonImageProceed.removeAttribute("disabled");
2020-11-05 09:52:14 +00:00
}
cropper.loadImage(e.target.result);
2020-10-30 17:50:03 +00:00
}, "image");
});
2020-11-05 18:48:22 +00:00
function loadPreview() {
el("preview").setAttribute("src", get_preview());
}
function handleImage() {
console.log(load_image(cropper.getCroppedImage()));
loadPreview();
}
buttonImageProceed.addEventListener("click", handleImage);
buttonImageProceed.addEventListener("touchend", handleImage);
checkboxDither.addEventListener("change", () => {
set_dither(checkboxDither.checked);
loadPreview();
2020-11-05 18:48:22 +00:00
});
inputRoomName.addEventListener("change", () => {
set_room_name(inputRoomName.value);
});
inputBrightness.addEventListener("input", () => {
set_brightness(inputBrightness.value);
loadPreview();
});
2020-11-05 18:48:22 +00:00
function addRoom() {
console.log(add_room());
2020-11-04 15:38:43 +00:00
textareaGameDataOutput.value = output();
2020-10-30 17:50:03 +00:00
}
2020-11-05 18:48:22 +00:00
buttonRoomProceed.addEventListener("click", addRoom);
buttonRoomProceed.addEventListener("touchend", addRoom);
2020-10-30 17:50:03 +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 = "";
buttonBackToImage.click();
}
buttonAddImage.addEventListener("click", addImage);
buttonAddImage.addEventListener("touchend", addImage);
function reset() {
clear_game();
}
buttonReset.addEventListener("click", reset);
buttonReset.addEventListener("touchend", reset);
}
run();