pixsy/src/lib.rs

267 lines
6.7 KiB
Rust
Raw Normal View History

2020-10-30 17:50:03 +00:00
use bitsy_parser::game::Game;
use bitsy_parser::image::Image;
use bitsy_parser::tile::Tile;
2020-11-04 15:38:43 +00:00
use image::{GenericImageView, Pixel, DynamicImage};
use lazy_static::lazy_static;
use std::sync::Mutex;
2020-10-30 17:50:03 +00:00
use wasm_bindgen::prelude::*;
const SD: u32 = 8;
2020-11-04 15:38:43 +00:00
struct State {
game: Option<Game>,
image: Option<DynamicImage>,
prefix: String,
invert: bool,
flip: bool,
mirror: bool,
rotate: bool,
}
lazy_static! {
static ref STATE: Mutex<State> = Mutex::new(
State {
game: None,
image: None,
prefix: "".to_string(),
invert: false,
flip: false,
mirror: false,
rotate: false,
}
);
2020-10-30 17:50:03 +00:00
}
fn tile_name(prefix: &str, index: &u32) -> Option<String> {
if prefix.len() > 0 {
Some(format!("{} {}", prefix, index))
} else {
None
}
}
#[wasm_bindgen]
2020-11-04 15:38:43 +00:00
pub fn load_default_game() {
let mut state = STATE.lock().unwrap();
state.game = Some(bitsy_parser::mock::game_default());
}
#[wasm_bindgen]
pub fn load_game(game_data: String) -> String {
let result = Game::from(game_data);
let mut state = STATE.lock().unwrap();
match result {
Ok(game) => {
state.game = Some(game);
"".to_string()
},
_ => {
state.game = None;
format!("{}", result.err().unwrap())
}
2020-10-30 17:50:03 +00:00
}
2020-11-04 15:38:43 +00:00
}
#[wasm_bindgen]
pub fn load_image(image_base64: String) -> String {
let mut state = STATE.lock().expect("Couldn't lock application state");
let image_base64: Vec<&str> = image_base64.split("base64,").collect();
let image_base64 = image_base64[1];
match base64::decode(image_base64) {
Ok(image) => {
match image::load_from_memory(image.as_ref()) {
Ok(image) => {
state.image = Some(image);
"OK"
},
_ => {
state.image = None;
"Couldn't load image"
}
}
},
_ => {
state.image = None;
"Couldn't decode image"
}
}.to_string()
}
2020-10-30 17:50:03 +00:00
2020-11-04 15:38:43 +00:00
#[wasm_bindgen]
pub fn set_prefix(prefix: String) {
let mut state = STATE.lock().unwrap();
2020-10-30 17:50:03 +00:00
2020-11-04 15:38:43 +00:00
state.prefix = prefix;
}
#[wasm_bindgen]
pub fn set_invert(invert: bool) {
let mut state = STATE.lock().unwrap();
state.invert = invert;
}
#[wasm_bindgen]
pub fn set_flip(flip: bool) {
let mut state = STATE.lock().unwrap();
state.flip = flip;
}
#[wasm_bindgen]
pub fn set_mirror(mirror: bool) {
let mut state = STATE.lock().unwrap();
state.mirror = mirror;
}
#[wasm_bindgen]
pub fn set_rotate(rotate: bool) {
let mut state = STATE.lock().unwrap();
state.rotate = rotate;
}
/// prefix will be ignored if empty
#[wasm_bindgen]
pub fn add_tiles() -> String {
let mut state = STATE.lock().expect("Couldn't lock application state");
if state.game.is_none() {
return "No game data loaded".to_string();
2020-10-30 17:50:03 +00:00
}
2020-11-04 15:38:43 +00:00
let mut game = state.game.clone().unwrap();
if state.image.is_none() {
return "No image loaded".to_string();
2020-10-30 17:50:03 +00:00
}
2020-11-04 15:38:43 +00:00
let width = state.image.as_ref().unwrap().width();
let height = state.image.as_ref().unwrap().height();
2020-10-30 17:50:03 +00:00
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) {
2020-11-04 15:38:43 +00:00
let pixel = state.image.as_ref().unwrap().get_pixel(x, y).to_rgb();
2020-10-30 17:50:03 +00:00
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 {
2020-11-04 15:38:43 +00:00
// "0" will get overwritten to a new, safe tile ID
2020-10-30 17:50:03 +00:00
id: "0".to_string(),
2020-11-04 15:38:43 +00:00
name: tile_name(&state.prefix, &tile_index),
2020-10-30 17:50:03 +00:00
wall: None,
animation_frames: vec![Image { pixels }],
colour_id: None
};
if !game.tiles.contains(&tile) {
game.add_tile(tile.clone());
tile_index += 1;
}
2020-11-04 15:38:43 +00:00
if state.invert {
2020-10-30 17:50:03 +00:00
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);
}
}
2020-11-04 15:38:43 +00:00
if state.flip {
2020-10-30 17:50:03 +00:00
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);
}
}
2020-11-04 15:38:43 +00:00
if state.mirror {
2020-10-30 17:50:03 +00:00
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);
}
}
2020-11-04 15:38:43 +00:00
if state.rotate {
2020-10-30 17:50:03 +00:00
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();
2020-11-04 15:38:43 +00:00
state.game = Some(game.to_owned());
"OK".to_string()
}
#[wasm_bindgen]
pub fn output() -> String {
let state = STATE.lock().unwrap();
match &state.game {
Some(game) => game.to_string(),
None => "No game loaded".to_string(),
}
2020-10-30 17:50:03 +00:00
}
#[cfg(test)]
mod test {
2020-11-04 15:38:43 +00:00
use crate::{add_tiles, load_image, load_default_game, output};
2020-10-30 17:50:03 +00:00
#[test]
fn example() {
2020-11-04 15:38:43 +00:00
load_default_game();
load_image(include_str!("test-resources/test.png.base64").to_string());
add_tiles();
assert_eq!(output(), include_str!("test-resources/expected.bitsy"));
2020-10-30 17:50:03 +00:00
}
}