alternate tile functionality; move some things around

This commit is contained in:
Max Bradbury 2020-07-20 22:34:07 +01:00
parent 225316cfb0
commit 33f2552003
7 changed files with 109 additions and 14 deletions

2
.gitignore vendored
View File

@ -1,5 +1,5 @@
/target
/index.html
/Cargo.lock
/style.css
/includes/style.css
/examples/

View File

@ -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

View File

Before

Width:  |  Height:  |  Size: 7.8 KiB

After

Width:  |  Height:  |  Size: 7.8 KiB

View File

@ -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

View File

@ -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();

View File

@ -22,7 +22,15 @@ fn tile_name(prefix: &str, index: &u32) -> Option<String> {
/// 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);
}