Allow configuring backgrounds with a colour

This commit is contained in:
Gwilym Kuiper 2022-08-11 21:16:26 +01:00
parent 9a5ecc5cda
commit c87a9ff077
2 changed files with 16 additions and 1 deletions

View file

@ -2,7 +2,7 @@ use serde::Deserialize;
use std::collections::HashMap; use std::collections::HashMap;
use std::fs; use std::fs;
use crate::{Colour, TileSize}; use crate::{Colour, Colours, TileSize};
pub(crate) fn parse(filename: &str) -> Box<dyn Config> { pub(crate) fn parse(filename: &str) -> Box<dyn Config> {
let config_toml = let config_toml =
@ -23,6 +23,7 @@ pub(crate) fn parse(filename: &str) -> Box<dyn Config> {
pub(crate) trait Config { pub(crate) trait Config {
fn crate_prefix(&self) -> String; fn crate_prefix(&self) -> String;
fn images(&self) -> HashMap<String, &dyn Image>; fn images(&self) -> HashMap<String, &dyn Image>;
fn colours(&self) -> Colours;
} }
pub(crate) trait Image { pub(crate) trait Image {
@ -35,6 +36,7 @@ pub(crate) trait Image {
pub struct ConfigV1 { pub struct ConfigV1 {
version: String, version: String,
crate_prefix: Option<String>, crate_prefix: Option<String>,
colours: Option<u32>,
image: HashMap<String, ImageV1>, image: HashMap<String, ImageV1>,
} }
@ -52,6 +54,14 @@ impl Config for ConfigV1 {
.map(|(filename, image)| (filename.clone(), image as &dyn Image)) .map(|(filename, image)| (filename.clone(), image as &dyn Image))
.collect() .collect()
} }
fn colours(&self) -> Colours {
match self.colours {
None | Some(16) => Colours::Colours16,
Some(256) => Colours::Colours256,
_ => panic!("colours must either not be set or 16 or 256"),
}
}
} }
#[derive(Deserialize)] #[derive(Deserialize)]

View file

@ -30,6 +30,11 @@ pub(crate) enum TileSize {
Tile32, Tile32,
} }
pub(crate) enum Colours {
Colours16,
Colours256,
}
impl TileSize { impl TileSize {
fn to_size(self) -> usize { fn to_size(self) -> usize {
match self { match self {