From 2a4903236d1abc8078cdda3e60190407c59acfb0 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Fri, 12 Feb 2021 23:09:13 +0000 Subject: [PATCH] simple pieces: support edges Signed-off-by: Ian Jackson --- specs/demo.game.toml | 1 + src/pieces.rs | 50 +++++++++++++++++++++++++++++++++++--------- src/spec.rs | 3 +++ 3 files changed, 44 insertions(+), 10 deletions(-) diff --git a/specs/demo.game.toml b/specs/demo.game.toml index c1d91819..cb5c3a36 100644 --- a/specs/demo.game.toml +++ b/specs/demo.game.toml @@ -9,6 +9,7 @@ pos = [90,40] type = "Disc" diam = 10 faces = ["red","grey"] +edges = ["black","white"] [[pieces]] pos = [90, 20] diff --git a/src/pieces.rs b/src/pieces.rs index 2bc59e80..9d7e531d 100644 --- a/src/pieces.rs +++ b/src/pieces.rs @@ -16,6 +16,7 @@ struct SimpleShape { desc: Html, path: Html, colours: ColourMap, + #[serde(default)] edges: ColourMap, itemname: String, outline: Box, } @@ -121,8 +122,16 @@ impl Outline for SimpleShape { impl Piece for SimpleShape { #[throws(IE)] fn svg_piece(&self, f: &mut Html, pri: &PieceRenderInstructions) { - write!(&mut f.0, r##""##, - self.colours[pri.face].0, + let ef = |cmap: &ColourMap, attrname: &str| { + if let Some(colour) = cmap.get(pri.face) { + format!(r##"{}="{}""##, attrname, colour.0) + } else { + "".to_owned() + } + }; + write!(&mut f.0, r##""##, + ef(&self.colours, "fill"), + ef(&self.edges, r##"stroke-width="0.2" stroke"##), &self.path.0)?; } fn describe_html(&self, face: Option) -> Html { @@ -132,12 +141,14 @@ impl Piece for SimpleShape { format!("a {}", self.desc.0) }) } - fn nfaces(&self) -> RawFaceId { self.colours.len().try_into().unwrap() } + fn nfaces(&self) -> RawFaceId { self.count_faces().try_into().unwrap() } fn itemname(&self) -> &str { &self.itemname } } impl SimpleShape { + fn count_faces(&self) -> usize { max(self.colours.len(), self.edges.len()) } + #[throws(SpecError)] fn new(desc: Html, path: Html, outline: Box, @@ -147,13 +158,32 @@ impl SimpleShape { { let itemname = common.itemname.clone() .unwrap_or_else(|| def_itemname.to_string()); - let colours = common.faces - .iter() - .map(|s| s.try_into()) - .collect::>()?; - SimpleShape { - desc, path, colours, itemname, outline, - } + + let cmap = |spec: &FaceColourSpecs| Ok::<_,SpecError>( + spec + .iter() + .map(|s| s.try_into()) + .collect::>()? + ); + + let shape = SimpleShape { + desc, path, itemname, outline, + colours: cmap(&common.faces)?, + edges: cmap(&common.edges)?, + }; + + let count = shape.count_faces(); + if count == 0 { throw!(SpecError::ZeroFaces) } + + let check = |colours: &ColourMap| { + let x = colours.len(); + if x == 0 || x == count { Ok(()) } + else { Err(SpecError::InconsistentFacesEdgecoloursCount) } + }; + check(&shape.colours)?; + check(&shape.edges)?; + + shape } } diff --git a/src/spec.rs b/src/spec.rs index 03bcccb2..1b20ff35 100644 --- a/src/spec.rs +++ b/src/spec.rs @@ -77,6 +77,8 @@ pub enum SpecError { BadUrlSyntax, UrlTooLong, CompassAngleInvalid, + ZeroFaces, + InconsistentFacesEdgecoloursCount, } display_as_debug!{SpecError} @@ -198,6 +200,7 @@ pub mod piece_specs { pub struct SimpleCommon { pub itemname: Option, pub faces: IndexVec, + #[serde(default)] pub edges: IndexVec, } #[derive(Debug,Serialize,Deserialize)] -- 2.30.2