alternate tile functionality; move some things around
This commit is contained in:
parent
225316cfb0
commit
33f2552003
|
@ -1,5 +1,5 @@
|
||||||
/target
|
/target
|
||||||
/index.html
|
/index.html
|
||||||
/Cargo.lock
|
/Cargo.lock
|
||||||
/style.css
|
/includes/style.css
|
||||||
/examples/
|
/examples/
|
||||||
|
|
2
build.sh
2
build.sh
|
@ -1,5 +1,5 @@
|
||||||
#! /usr/bin/env bash
|
#! /usr/bin/env bash
|
||||||
|
|
||||||
pug index.pug
|
pug index.pug
|
||||||
lessc style.less style.css
|
lessc includes/style.less includes/style.css
|
||||||
wasm-pack build --target web
|
wasm-pack build --target web
|
||||||
|
|
Before Width: | Height: | Size: 7.8 KiB After Width: | Height: | Size: 7.8 KiB |
|
@ -3,7 +3,7 @@ html(lang="en-gb")
|
||||||
head
|
head
|
||||||
meta(charset="utf-8")
|
meta(charset="utf-8")
|
||||||
title tilesy
|
title tilesy
|
||||||
link(rel="stylesheet" href="style.css")
|
link(rel="stylesheet" href="includes/style.css")
|
||||||
body
|
body
|
||||||
h1
|
h1
|
||||||
span.background tilesy
|
span.background tilesy
|
||||||
|
|
20
script.js
20
script.js
|
@ -53,9 +53,13 @@ async function run() {
|
||||||
const buttonImportGame = el("import");
|
const buttonImportGame = el("import");
|
||||||
const buttonLoadGame = el("load");
|
const buttonLoadGame = el("load");
|
||||||
const buttonNewGame = el("new");
|
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 inputPrefix = el("prefix");
|
||||||
const imagePreview = el("preview");
|
const imagePreview = el("preview");
|
||||||
const resetButton = el("reset");
|
|
||||||
const spanTileNamePreview = el("tile-name-preview")
|
const spanTileNamePreview = el("tile-name-preview")
|
||||||
const textareaGameDataInput = el("game-data");
|
const textareaGameDataInput = el("game-data");
|
||||||
const textareaGameDataOutput = el("output");
|
const textareaGameDataOutput = el("output");
|
||||||
|
@ -135,7 +139,15 @@ async function run() {
|
||||||
let gameData = textareaGameDataInput.value;
|
let gameData = textareaGameDataInput.value;
|
||||||
let prefix = inputPrefix.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);
|
buttonImportGame.addEventListener("click", addTiles);
|
||||||
|
@ -162,8 +174,8 @@ async function run() {
|
||||||
imagePreview.removeAttribute("src");
|
imagePreview.removeAttribute("src");
|
||||||
}
|
}
|
||||||
|
|
||||||
resetButton.addEventListener("click", reset);
|
buttonReset.addEventListener("click", reset);
|
||||||
resetButton.addEventListener("touchend", reset);
|
buttonReset.addEventListener("touchend", reset);
|
||||||
}
|
}
|
||||||
|
|
||||||
run();
|
run();
|
||||||
|
|
93
src/lib.rs
93
src/lib.rs
|
@ -22,7 +22,15 @@ fn tile_name(prefix: &str, index: &u32) -> Option<String> {
|
||||||
/// image is a base64-encoded string
|
/// image is a base64-encoded string
|
||||||
/// prefix will be ignored if empty
|
/// prefix will be ignored if empty
|
||||||
#[wasm_bindgen]
|
#[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);
|
let game = Game::from(game_data);
|
||||||
if game.is_err() {
|
if game.is_err() {
|
||||||
return format!("Couldn't parse game data");
|
return format!("Couldn't parse game data");
|
||||||
|
@ -45,8 +53,8 @@ pub fn add_tiles(game_data: String, image: String, prefix: String) -> String {
|
||||||
|
|
||||||
let width = image.width();
|
let width = image.width();
|
||||||
let height = image.height();
|
let height = image.height();
|
||||||
let columns = (width as f64 / 8.).floor() as u32;
|
let columns = (width as f64 / SD as f64).floor() as u32;
|
||||||
let rows = (height as f64 / 8.).floor() as u32;
|
let rows = (height as f64 / SD as f64).floor() as u32;
|
||||||
|
|
||||||
let mut tile_index = 1;
|
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) {
|
if !game.tiles.contains(&tile) {
|
||||||
game.add_tile(tile);
|
game.add_tile(tile.clone());
|
||||||
|
|
||||||
tile_index += 1;
|
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() {
|
fn example() {
|
||||||
let game_data = bitsy_parser::mock::game_default().to_string();
|
let game_data = bitsy_parser::mock::game_default().to_string();
|
||||||
let image = include_str!("test-resources/test.png.base64").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");
|
let expected = include_str!("test-resources/expected.bitsy");
|
||||||
assert_eq!(output, expected);
|
assert_eq!(output, expected);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue