diff --git a/.gitignore b/.gitignore index 3c2df47..541a8dc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ /target /index.html /Cargo.lock -/style.css +/includes/style.css /examples/ diff --git a/build.sh b/build.sh index 4b778c3..ac2bd52 100755 --- a/build.sh +++ b/build.sh @@ -1,5 +1,5 @@ #! /usr/bin/env bash pug index.pug -lessc style.less style.css +lessc includes/style.less includes/style.css wasm-pack build --target web diff --git a/background.png b/includes/background.png similarity index 100% rename from background.png rename to includes/background.png diff --git a/style.less b/includes/style.less similarity index 100% rename from style.less rename to includes/style.less diff --git a/index.pug b/index.pug index eafaf8e..cd850d3 100644 --- a/index.pug +++ b/index.pug @@ -3,7 +3,7 @@ html(lang="en-gb") head meta(charset="utf-8") title tilesy - link(rel="stylesheet" href="style.css") + link(rel="stylesheet" href="includes/style.css") body h1 span.background tilesy diff --git a/script.js b/script.js index 3a6080b..09486c4 100644 --- a/script.js +++ b/script.js @@ -53,9 +53,13 @@ async function run() { const buttonImportGame = el("import"); const buttonLoadGame = el("load"); const buttonNewGame = el("new"); + const buttonReset = el("reset"); + const checkboxInvertTiles = el("invert"); + const checkboxFlipTiles = el("flip"); + const checkboxMirrorTiles = el("mirror"); + const checkboxRotateTiles = el("rotate"); const inputPrefix = el("prefix"); const imagePreview = el("preview"); - const resetButton = el("reset"); const spanTileNamePreview = el("tile-name-preview") const textareaGameDataInput = el("game-data"); const textareaGameDataOutput = el("output"); @@ -135,7 +139,15 @@ async function run() { let gameData = textareaGameDataInput.value; let prefix = inputPrefix.value; - textareaGameDataOutput.value = add_tiles(gameData, image, prefix); + textareaGameDataOutput.value = add_tiles( + gameData, + image, + prefix, + checkboxInvertTiles.checked, + checkboxFlipTiles.checked, + checkboxMirrorTiles.checked, + checkboxRotateTiles.checked + ); } buttonImportGame.addEventListener("click", addTiles); @@ -162,8 +174,8 @@ async function run() { imagePreview.removeAttribute("src"); } - resetButton.addEventListener("click", reset); - resetButton.addEventListener("touchend", reset); + buttonReset.addEventListener("click", reset); + buttonReset.addEventListener("touchend", reset); } run(); diff --git a/src/lib.rs b/src/lib.rs index dd913fc..fe2fd21 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,7 +22,15 @@ fn tile_name(prefix: &str, index: &u32) -> Option { /// image is a base64-encoded string /// prefix will be ignored if empty #[wasm_bindgen] -pub fn add_tiles(game_data: String, image: String, prefix: String) -> String { +pub fn add_tiles( + game_data: String, + image: String, + prefix: String, + invert: bool, + flip: bool, + mirror: bool, + rotate: bool +) -> String { let game = Game::from(game_data); if game.is_err() { return format!("Couldn't parse game data"); @@ -43,10 +51,10 @@ pub fn add_tiles(game_data: String, image: String, prefix: String) -> String { } let image = image.unwrap(); - let width = image.width(); - let height = image.height(); - let columns = (width as f64 / 8.).floor() as u32; - let rows = (height as f64 / 8.).floor() as u32; + let width = image.width(); + let height = image.height(); + let columns = (width as f64 / SD as f64).floor() as u32; + let rows = (height as f64 / SD as f64).floor() as u32; let mut tile_index = 1; @@ -73,10 +81,75 @@ pub fn add_tiles(game_data: String, image: String, prefix: String) -> String { }; if !game.tiles.contains(&tile) { - game.add_tile(tile); + game.add_tile(tile.clone()); tile_index += 1; } + + if invert { + let mut inverted = tile.clone(); + inverted.invert(); + + if inverted.name.is_some() { + inverted.name = Some(format!("{} inverted", inverted.name.unwrap())); + } + + if !game.tiles.contains(&inverted) { + game.add_tile(inverted); + + tile_index += 1; + } + } + + if flip { + let mut flipped = tile.clone(); + flipped.flip(); + + if flipped.name.is_some() { + flipped.name = Some(format!("{} flipped", flipped.name.unwrap())); + } + + if !game.tiles.contains(&flipped) { + game.add_tile(flipped); + + tile_index += 1; + } + } + + if mirror { + let mut mirrored = tile.clone(); + mirrored.mirror(); + + if mirrored.name.is_some() { + mirrored.name = Some(format!("{} mirrored", mirrored.name.unwrap())); + } + + if !game.tiles.contains(&mirrored) { + game.add_tile(mirrored); + + tile_index += 1; + } + } + + if rotate { + for i in 1..4 { + let mut rotated = tile.clone(); + + for _ in 0..i { + rotated.rotate(); + } + + if rotated.name.is_some() { + rotated.name = Some(format!("{} {}°", rotated.name.unwrap(), i * 90)); + } + + if !game.tiles.contains(&rotated) { + game.add_tile(rotated); + + tile_index += 1; + } + } + } } } @@ -92,7 +165,17 @@ mod test { fn example() { let game_data = bitsy_parser::mock::game_default().to_string(); let image = include_str!("test-resources/test.png.base64").to_string(); - let output = add_tiles(game_data, image, "".to_string()); + + let output = add_tiles( + game_data, + image, + "".to_string(), + false, + false, + false, + false + ); + let expected = include_str!("test-resources/expected.bitsy"); assert_eq!(output, expected); }