change the way palettes are handled; support custom palette

This commit is contained in:
2020-11-07 16:30:50 +00:00
parent aea19fcd6c
commit 6a58e8c003
3 changed files with 74 additions and 13 deletions

View File

@@ -16,11 +16,22 @@ use colour_map::ColourMap;
const SD: u32 = 8;
enum SelectedPalette {
None,
Existing {
id: String,
},
New {
background: bitsy_parser::Colour,
foreground: bitsy_parser::Colour,
}
}
struct State {
game: Option<Game>,
image: Option<DynamicImage>,
room_name: Option<String>,
palette: Option<String>,
palette: SelectedPalette,
dither: bool,
brightness: i32,
}
@@ -31,7 +42,7 @@ lazy_static! {
game: None,
image: None,
room_name: None,
palette: None,
palette: SelectedPalette::None,
dither: true,
brightness: 0,
}
@@ -49,8 +60,13 @@ fn tile_name(prefix: &Option<String>, index: &u32) -> Option<String> {
#[wasm_bindgen]
pub fn load_default_game() {
let mut state = STATE.lock().unwrap();
state.game = Some(bitsy_parser::mock::game_default());
state.palette = Some(bitsy_parser::mock::game_default().palette_ids()[0].clone())
// yes, this will probably always just be "0", but to be safe…
state.palette = SelectedPalette::Existing {
id: bitsy_parser::mock::game_default().palette_ids()[0].clone()
}
}
#[wasm_bindgen]
@@ -63,12 +79,12 @@ pub fn load_game(game_data: String) -> String {
Ok((game, _errs)) => {
let palette_id = game.palette_ids()[0].clone();
state.game = Some(game);
state.palette = Some(palette_id);
state.palette = SelectedPalette::Existing { id: palette_id };
format!("Loaded game")
},
_ => {
state.game = None;
state.palette = None;
state.palette = SelectedPalette::None;
format!("{}", result.err().unwrap())
}
}
@@ -110,12 +126,16 @@ pub fn set_dither(dither: bool) {
}
#[wasm_bindgen]
pub fn set_palette(palette_id: String) {
pub fn set_palette(palette_id: &str, background: String, foreground: String) {
let mut state = STATE.lock().unwrap();
match palette_id.is_empty() {
true => { state.palette = None },
false => { state.palette = Some(palette_id) },
state.palette = match palette_id {
"NEW_PALETTE" => SelectedPalette::New {
background: bitsy_parser::Colour::from_hex(&background).unwrap(),
foreground: bitsy_parser::Colour::from_hex(&foreground).unwrap(),
},
"" => SelectedPalette::None,
_ => SelectedPalette::Existing { id: palette_id.to_string() },
}
}
@@ -166,10 +186,22 @@ fn image_to_base64(image: &DynamicImage) -> String {
fn render_preview(state: &State) -> DynamicImage {
let mut buffer = state.image.as_ref().unwrap().clone().into_rgba();
let palette_id = state.palette.as_ref().unwrap();
let palette = *&state.game.as_ref().unwrap().get_palette(palette_id).unwrap();
let colour_map = crate::ColourMap::from(palette);
let palette = match &state.palette {
SelectedPalette::None => bitsy_parser::mock::game_default().palettes[0].clone(),
SelectedPalette::Existing { id } => state.game.as_ref().unwrap().get_palette(id).unwrap().clone(),
SelectedPalette::New { background, foreground } => Palette {
id: "0".to_string(),
name: None,
colours: vec![
background.clone(), foreground.clone(), Colour { red: 0, green: 0, blue: 0 }
],
},
};
let colour_map = crate::ColourMap::from(&palette);
// adjust brightness
let mut buffer = image::imageops::brighten(&mut buffer, state.brightness);
if state.dither {