This commit is contained in:
Max Bradbury 2020-11-04 16:11:44 +00:00
parent 65257b3886
commit 4c958373ba
3 changed files with 22 additions and 115 deletions

View File

@ -7,9 +7,9 @@ html(lang="en-gb")
body body
h1 h1
| pixsy | pixsy
//img(alt="tilesy" src="includes/tilesy.png") //img(alt="pixsy" src="includes/pixsy.png")
p p convert images to Bitsy rooms
span.background convert images to Bitsy rooms p looking for the #[a(href="./old/") old version]?
.pages .pages
.page#start .page#start
button.normal.pagination.next#new create a new bitsy game button.normal.pagination.next#new create a new bitsy game

View File

@ -1,14 +1,10 @@
import init, { import init, {
add_tiles, add_room,
load_image, load_image,
load_game, load_game,
load_default_game, load_default_game,
output, output,
set_prefix, set_room_name,
set_invert,
set_flip,
set_mirror,
set_rotate
} from './pkg/pixsy.js'; } from './pkg/pixsy.js';
if (typeof WebAssembly !== "object") { if (typeof WebAssembly !== "object") {

View File

@ -11,11 +11,8 @@ const SD: u32 = 8;
struct State { struct State {
game: Option<Game>, game: Option<Game>,
image: Option<DynamicImage>, image: Option<DynamicImage>,
prefix: String, room_name: Option<String>,
invert: bool, palette: Option<String>,
flip: bool,
mirror: bool,
rotate: bool,
} }
lazy_static! { lazy_static! {
@ -23,17 +20,14 @@ lazy_static! {
State { State {
game: None, game: None,
image: None, image: None,
prefix: "".to_string(), room_name: None,
invert: false, palette: None,
flip: false,
mirror: false,
rotate: false,
} }
); );
} }
fn tile_name(prefix: &str, index: &u32) -> Option<String> { fn tile_name(prefix: &Option<String>, index: &u32) -> Option<String> {
if prefix.len() > 0 { if let Some(prefix) = prefix {
Some(format!("{} {}", prefix, index)) Some(format!("{} {}", prefix, index))
} else { } else {
None None
@ -91,43 +85,17 @@ pub fn load_image(image_base64: String) -> String {
} }
#[wasm_bindgen] #[wasm_bindgen]
pub fn set_prefix(prefix: String) { pub fn set_room_name(room_name: String) {
let mut state = STATE.lock().unwrap(); let mut state = STATE.lock().unwrap();
state.prefix = prefix; match room_name.is_empty() {
true => { state.room_name = None },
false => { state.room_name = Some(room_name) },
}
} }
#[wasm_bindgen] #[wasm_bindgen]
pub fn set_invert(invert: bool) { pub fn add_room() -> String {
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"); let mut state = STATE.lock().expect("Couldn't lock application state");
if state.game.is_none() { if state.game.is_none() {
@ -163,7 +131,7 @@ pub fn add_tiles() -> String {
let tile = Tile { let tile = Tile {
// "0" will get overwritten to a new, safe tile ID // "0" will get overwritten to a new, safe tile ID
id: "0".to_string(), id: "0".to_string(),
name: tile_name(&state.prefix, &tile_index), name: tile_name(&state.room_name, &tile_index),
wall: None, wall: None,
animation_frames: vec![Image { pixels }], animation_frames: vec![Image { pixels }],
colour_id: None colour_id: None
@ -174,63 +142,6 @@ pub fn add_tiles() -> String {
tile_index += 1; tile_index += 1;
} }
if state.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 state.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 state.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 state.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);
}
}
}
} }
} }