move state to wasm

This commit is contained in:
Max Bradbury 2020-11-04 15:01:04 +00:00
parent 35af495f51
commit 455c03b4b3
4 changed files with 187 additions and 100 deletions

1
.gitignore vendored
View File

@ -5,3 +5,4 @@
/examples/
/tilesy.zip
/.idea/
/dist/

View File

@ -13,7 +13,7 @@ crate-type = ["cdylib"]
[dependencies]
"base64" = "^0.12.3"
"bitsy-parser" = "^0.72.0"
"bitsy-parser" = "^0.72.3"
"image" = "^0.23.7"
"lazy_static" = "^1.4.0"
"wasm-bindgen" = "^0.2.64"
"wasm-bindgen" = "=0.2.64" # newer versions are bugged...

View File

@ -1,4 +1,15 @@
import init, {load_default_game_data, add_tiles} from './pkg/tilesy.js';
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';
// if (typeof WebAssembly !== "object") {
// document.getElementById("start").innerText = "Sorry - your browser does not support WebAssembly";
@ -79,7 +90,8 @@ async function run() {
}
function new_game() {
textareaGameDataInput.value = load_default_game_data();
load_default_game()
textareaGameDataInput.value = output();
checkGameData();
// we don't need to look at the default game data, so skip ahead to the image page
buttonGameDataProceed.click();
@ -100,6 +112,7 @@ async function run() {
el("game").addEventListener("change", function() {
readFile(this, function (e) {
textareaGameDataInput.value = e.target.result;
console.log(load_game(e.target.result));
checkGameData();
}, "text");
});
@ -121,24 +134,14 @@ async function run() {
el('image').addEventListener('change', function () {
readFile(this, function (e) {
imagePreview.src = e.target.result;
imagePreview.style.display = "initial"
imagePreview.style.display = "initial";
console.log(load_image(imagePreview.getAttribute("src")));
}, "image");
});
function addTiles() {
let image = imagePreview.getAttribute("src");
let gameData = textareaGameDataInput.value;
let prefix = inputPrefix.value;
textareaGameDataOutput.value = add_tiles(
gameData,
image,
prefix,
checkboxInvertTiles.checked,
checkboxFlipTiles.checked,
checkboxMirrorTiles.checked,
checkboxRotateTiles.checked
);
console.log(add_tiles());
textareaGameDataOutput.value = output();
}
buttonImportGame.addEventListener("click", addTiles);
@ -160,6 +163,26 @@ async function run() {
buttonAddImage.addEventListener("click", addImage);
buttonAddImage.addEventListener("touchend", addImage);
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);
});
function reset() {
clear_game();
imagePreview.removeAttribute("src");

View File

@ -1,40 +1,35 @@
use bitsy_parser::game::Game;
use bitsy_parser::image::Image;
use bitsy_parser::tile::Tile;
use image::{GenericImageView, Pixel};
// use lazy_static::lazy_static;
// use std::sync::Mutex;
use image::{GenericImageView, Pixel, DynamicImage};
use lazy_static::lazy_static;
use std::sync::Mutex;
use wasm_bindgen::prelude::*;
const SD: u32 = 8;
//
// struct State {
// game: Option<Game>,
// image: Option<Image>,
// prefix: String,
// invert: bool,
// flip: bool,
// mirror: bool,
// rotate: bool,
// }
// lazy_static! {
// static ref STATE: Mutex<State> = Mutex::new(
// State {
// game: None,
// image: None,
// prefix: "".to_string(),
// invert: false,
// flip: false,
// mirror: false,
// rotate: false,
// }
// );
// }
struct State {
game: Option<Game>,
image: Option<DynamicImage>,
prefix: String,
invert: bool,
flip: bool,
mirror: bool,
rotate: bool,
}
#[wasm_bindgen]
pub fn load_default_game_data() -> String {
bitsy_parser::mock::game_default().to_string()
lazy_static! {
static ref STATE: Mutex<State> = Mutex::new(
State {
game: None,
image: None,
prefix: "".to_string(),
invert: false,
flip: false,
mirror: false,
rotate: false,
}
);
}
fn tile_name(prefix: &str, index: &u32) -> Option<String> {
@ -45,40 +40,108 @@ fn tile_name(prefix: &str, index: &u32) -> Option<String> {
}
}
/// image is a base64-encoded string
#[wasm_bindgen]
pub fn load_default_game() {
let mut state = STATE.lock().unwrap();
state.game = Some(bitsy_parser::mock::game_default());
}
#[wasm_bindgen]
pub fn load_game(game_data: String) -> String {
let result = Game::from(game_data);
let mut state = STATE.lock().unwrap();
match result {
Ok(game) => {
state.game = Some(game);
"".to_string()
},
_ => {
state.game = None;
format!("{}", result.err().unwrap())
}
}
}
#[wasm_bindgen]
pub fn load_image(image_base64: String) -> String {
let mut state = STATE.lock().expect("Couldn't lock application state");
let image_base64: Vec<&str> = image_base64.split("base64,").collect();
let image_base64 = image_base64[1];
match base64::decode(image_base64) {
Ok(image) => {
match image::load_from_memory(image.as_ref()) {
Ok(image) => {
state.image = Some(image);
"OK"
},
_ => {
state.image = None;
"Couldn't load image"
}
}
},
_ => {
state.image = None;
"Couldn't decode image"