From 4d40fd6e08e3e88ac456279ad2581b2b1c05a6f7 Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Sat, 28 Nov 2020 14:51:43 +0000 Subject: [PATCH] wip --- Cargo.toml | 2 +- includes/style.less | 11 +++++++++++ index.pug | 3 +++ script.js | 34 +++++++++++++++++++++++++++++----- src/lib.rs | 45 ++++++++++++++++++++++++++++++++------------- 5 files changed, 76 insertions(+), 19 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3738210..e474d75 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pixsy" -version = "0.72.7" +version = "0.72.8" description = "convert images to Bitsy rooms" authors = ["Max Bradbury "] edition = "2018" diff --git a/includes/style.less b/includes/style.less index e811bca..ab5421d 100644 --- a/includes/style.less +++ b/includes/style.less @@ -2,6 +2,7 @@ @page-background: #968eb5; @accent: #ec6d7d; @text: #464256; +@ok: #caec6d; * { box-sizing: border-box; @@ -131,3 +132,13 @@ textarea { image-rendering: pixelated; image-rendering: crisp-edges; } + +.message { + background-color: @ok; + padding: 1em; + text-align: left; + + &#game-data-errors { + background-color: @accent; + } +} diff --git a/index.pug b/index.pug index 27f4c8d..060635f 100644 --- a/index.pug +++ b/index.pug @@ -34,6 +34,9 @@ html(lang="en-gb") autocomplete="off" ) + p#game-data-result.message(style="display: none;") + p#game-data-errors.message(style="display: none;") + button.pagination.prev previous button.pagination.next#game-data-next(disabled=true) next .page.image#page-image diff --git a/script.js b/script.js index 2904330..8c3f9f0 100644 --- a/script.js +++ b/script.js @@ -78,6 +78,8 @@ async function run() { const inputColourBackground = el("colour-background"); const inputColourForeground = el("colour-foreground"); const inputRoomName = el("room-name"); + const paragraphGameResult = el("game-data-result"); + const paragraphGameErrors = el("game-data-errors"); const selectPalette = el("palette"); const textareaGameDataInput = el("game-data"); const textareaGameDataOutput = el("output"); @@ -126,7 +128,6 @@ 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"); }); @@ -153,16 +154,39 @@ async function run() { } function checkGameData() { - if (textareaGameDataInput.value.length > 0) { + paragraphGameResult.style.display = "none"; + paragraphGameErrors.style.display = "none"; + + let game_data = textareaGameDataInput.value; + + if (game_data.length === 0) { + buttonGameDataProceed.setAttribute("disabled", "disabled"); + return; + } + + let result = load_game(game_data); + console.debug(result); + let parts = result.split(". Errors: "); + result = parts[0]; + let errors = parts[1]; + + paragraphGameResult.innerText = result; + paragraphGameResult.style.display = "block"; + + if (errors) { + paragraphGameErrors.innerText = errors; + paragraphGameErrors.style.display = "block"; + } + + if (result.startsWith("Error")) { + buttonGameDataProceed.setAttribute("disabled", "disabled"); + } else { buttonGameDataProceed.removeAttribute("disabled"); setPaletteDropdown(); - } else { - buttonGameDataProceed.setAttribute("disabled", "disabled"); } } textareaGameDataInput.addEventListener("change", checkGameData); - textareaGameDataInput.addEventListener("keyup", checkGameData); checkGameData(); el('image').addEventListener('change', function () { diff --git a/src/lib.rs b/src/lib.rs index b6178a1..49114e7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -76,16 +76,21 @@ pub fn load_game(game_data: String) -> String { let result = Game::from(game_data); match result { - Ok((game, _errs)) => { + Ok((game, errors)) => { let palette_id = game.palette_ids()[0].clone(); + let game_name = game.name.clone(); + let errors: Vec = errors.iter().map(|err| format!("{}", err)).collect(); + state.game = Some(game); state.palette = SelectedPalette::Existing { id: palette_id }; - format!("Loaded game") - }, + + format!("Loaded game: {}. Errors: {}", game_name, errors.join(", ")) + } _ => { state.game = None; state.palette = SelectedPalette::None; - format!("{}", result.err().unwrap()) + + format!("Error: {}", result.err().unwrap()) } } } @@ -167,18 +172,20 @@ pub fn get_palettes() -> String { let mut palette_objects = json::JsonValue::new_array(); - for palette in &state.game.as_ref().unwrap().palettes { - let mut object = json::JsonValue::new_object(); + if state.game.is_some() { + for palette in &state.game.as_ref().unwrap().palettes { + let mut object = json::JsonValue::new_object(); - object.insert("id", palette.id.clone()).unwrap(); + object.insert("id", palette.id.clone()).unwrap(); - object.insert( - "name", - palette.name.clone().unwrap_or( - format!("Palette {}", palette.id)) - ).unwrap(); + object.insert( + "name", + palette.name.clone().unwrap_or( + format!("Palette {}", palette.id)) + ).unwrap(); - palette_objects.push(object).unwrap(); + palette_objects.push(object).unwrap(); + } } json::stringify(palette_objects) @@ -392,4 +399,16 @@ mod test { // todo what? why are extraneous pixels appearing in the output tiles? assert_eq!(output(), include_str!("test-resources/expected.bitsy")); } + + #[test] + fn palettes() { + load_default_game(); + + assert_eq!(crate::get_palettes(), "[{\"id\":\"0\",\"name\":\"blueprint\"}]"); + } + + #[test] + fn no_palettes() { + assert_eq!(crate::get_palettes(), "[]"); + } }