175 lines
4.7 KiB
Rust
175 lines
4.7 KiB
Rust
|
use bitsy_parser::game::Game;
|
||
|
use bitsy_parser::image::Image;
|
||
|
use bitsy_parser::tile::Tile;
|
||
|
use image::{GenericImageView, Pixel};
|
||
|
use wasm_bindgen::prelude::*;
|
||
|
|
||
|
const SD: u32 = 8;
|
||
|
|
||
|
#[wasm_bindgen]
|
||
|
pub fn load_default_game_data() -> String {
|
||
|
bitsy_parser::mock::game_default().to_string()
|
||
|
}
|
||
|
|
||
|
fn tile_name(prefix: &str, index: &u32) -> Option<String> {
|
||
|
if prefix.len() > 0 {
|
||
|
Some(format!("{} {}", prefix, index))
|
||
|
} else {
|
||
|
None
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// 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,
|
||
|
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");
|
||
|
}
|
||
|
let mut game = game.unwrap();
|
||
|
|
||
|
let image: Vec<&str> = image.split("base64,").collect();
|
||
|
let image = image[1];
|
||
|
|
||
|
let image = base64::decode(image);
|
||
|
if image.is_err() {
|
||
|
return format!("Couldn't decode image");
|
||
|
}
|
||
|
let image = image.unwrap();
|
||
|
let image = image::load_from_memory(image.as_ref());
|
||
|
if image.is_err() {
|
||
|
return format!("Couldn't load image");
|
||
|
}
|
||
|
let image = image.unwrap();
|
||
|
|
||
|
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;
|
||
|
|
||
|
for column in 0..columns {
|
||
|
for row in 0..rows {
|
||
|
let mut pixels = Vec::with_capacity(64);
|
||
|
|
||
|
for y in (row * SD)..((row + 1) * SD) {
|
||
|
for x in (column * SD)..((column + 1) * SD) {
|
||
|
let pixel = image.get_pixel(x, y).to_rgb();
|
||
|
let total = pixel[0] as u32 + pixel[1] as u32 + pixel[2] as u32;
|
||
|
// is each channel brighter than 128/255 on average?
|
||
|
pixels.push(if total >= 384 {1} else {0});
|
||
|
}
|
||
|
}
|
||
|
|
||
|
let tile = Tile {
|
||
|
/// "0" will get overwritten to a new, safe tile ID
|
||
|
id: "0".to_string(),
|
||
|
name: tile_name(&prefix, &tile_index),
|
||
|
wall: None,
|
||
|
animation_frames: vec![Image { pixels }],
|
||
|
colour_id: None
|
||
|
};
|
||
|
|
||
|
if !game.tiles.contains(&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);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
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);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
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);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
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);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
game.dedupe_tiles();
|
||
|
game.to_string()
|
||
|
}
|
||
|
|
||
|
#[cfg(test)]
|
||
|
mod test {
|
||
|
use crate::add_tiles;
|
||
|
|
||
|
#[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(),
|
||
|
false,
|
||
|
false,
|
||
|
false,
|
||
|
false
|
||
|
);
|
||
|
|
||
|
let expected = include_str!("test-resources/expected.bitsy");
|
||
|
assert_eq!(output, expected);
|
||
|
}
|
||
|
}
|