diff --git a/Cargo.toml b/Cargo.toml index 271a2c7..0f091e5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,5 +10,6 @@ edition = "2018" crate-type = ["cdylib"] [dependencies] -"bitsy-parser" = "^0.71.1" +"bitsy-parser" = "^0.72.0" +"lazy_static" = "^1.4.0" "wasm-bindgen" = "^0.2.64" diff --git a/index.js b/index.js index 8190e31..1cc751e 100644 --- a/index.js +++ b/index.js @@ -1,13 +1,27 @@ -import init, {merge} from './pkg/bitsy_merge.js'; +import init, {load_main, load_additional, merge} from './pkg/mixsy.js'; async function run() { await init(); - document.getElementById('merge').addEventListener('click', () => { - let a = document.getElementById('a').value; - let b = document.getElementById('b').value; - document.getElementById('c').value = merge(a, b); - }) + const main = document.getElementById('a'); + const additional = document.getElementById('b'); + const output = document.getElementById('c'); + const merge_button = document.getElementById('merge'); + + main.addEventListener('change', () => { + console.debug(load_main(main.value)); + }); + + additional.addEventListener('change', () => { + console.debug(load_additional(additional.value)); + }); + + function mix() { + output.value = merge(); + } + + merge_button.addEventListener('click', mix); + merge_button.addEventListener('touchend', mix); } run(); diff --git a/src/lib.rs b/src/lib.rs index b9a26ff..9ecf6c0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,22 +1,48 @@ -use wasm_bindgen::prelude::*; use bitsy_parser::game::Game; +use lazy_static::lazy_static; +use std::sync::Mutex; +use wasm_bindgen::prelude::*; + +struct State { + main: Option, + additional: Option, +} + +lazy_static! { + static ref STATE: Mutex = Mutex::new(State { main: None, additional: None}); +} + +// TODO DEDUPE #[wasm_bindgen] -pub fn merge(a: String, b: String) -> String { - let main = Game::from(a); - let additional = Game::from(b); +pub fn load_main(game_data: String) -> String { + let game = Game::from(game_data); - if main.is_err() { - return "Couldn't parse main game.".to_string(); - } + if game.is_ok() { + STATE.lock().unwrap().main = Some(game.unwrap()); + "OK!" + } else { + "Could not parse game data" + }.to_string() +} - if additional.is_err() { - return "Couldn't parse additional game.".to_string(); - } +#[wasm_bindgen] +pub fn load_additional(game_data: String) -> String { + let game = Game::from(game_data); - let mut main = main.unwrap(); - let additional = additional.unwrap(); + if game.is_ok() { + STATE.lock().unwrap().additional = Some(game.unwrap()); + "OK!" + } else { + "Could not parse game data" + }.to_string() +} +#[wasm_bindgen] +pub fn merge() -> String { + let state = STATE.lock().unwrap(); + let mut main = state.main.clone().unwrap(); + let additional = state.additional.clone().unwrap(); main.merge(additional); main.dedupe_tiles(); main.to_string()