From: Ian Jackson Date: Tue, 21 Jul 2020 21:52:04 +0000 (+0100) Subject: new load X-Git-Tag: otter-0.2.0~1277 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=38608a2a3864a6febfc5f78ded38832cec3f2152;p=otter.git new load --- diff --git a/src/pieces.rs b/src/pieces.rs index f4155ee1..ca127589 100644 --- a/src/pieces.rs +++ b/src/pieces.rs @@ -20,6 +20,7 @@ pub enum SVGProcessingError { BadNumber, WriteFail, NegativeDragraise, + ImproperSizeSpec, } display_as_debug!{SVGProcessingError} @@ -119,13 +120,6 @@ impl SimpleShape { scaled_path, desc, approx_dia, path, colours, })) } - fn new_square(edgelen: Coord, colours: Vec) -> Result,SE> { - let unit_path = - "M -1 -1 h 2 v 2 h -2 z"; - let scale = (edgelen as f64) * 0.5; - let path = svg_rescale_path(&unit_path, scale)?; - Ok(Self::new_from_path("square".to_owned(), path, edgelen, colours)?) - } } #[derive(Deserialize)] @@ -149,7 +143,7 @@ struct Disc { } #[derive(Debug,Deserialize)] -struct Rectangle { +struct Square { size : Vec, faces : Vec, } @@ -157,7 +151,7 @@ struct Rectangle { #[typetag::deserialize] impl PieceSpec for Disc { #[throws(SE)] - fn load(mut self) -> Box { + fn load(self) -> Box { let unit_path = "M 0 1 a 1 1 0 1 0 0 -2 \ a 1 1 0 1 0 0 2 z"; @@ -168,6 +162,21 @@ impl PieceSpec for Disc { } } +#[typetag::deserialize] +impl PieceSpec for Square { + #[throws(SE)] + fn load(self) -> Box { + let (x, y) = match self.size.as_slice() { + &[s,] => (s,s), + &[x, y] => (x,y), + _ => throw!(SE::ImproperSizeSpec), + }; + let path = format!("M {} {} h {} v {} h {} z", + -(x as f64)*0.5, -(y as f64)*0.5, x, y, -x); + SimpleShape::new_from_path("square".to_owned(), path, (x+y+1)/2, self.faces)? + } +} + pub fn xxx_make_pieces() -> Result)>,SE> { Ok(vec![ ([ 90, 80 ], @@ -176,9 +185,9 @@ pub fn xxx_make_pieces() -> Result)>,SE> { faces : vec![ ColourSpec("red".to_string()), ColourSpec("grey".to_string()) ], }.load()?), ([ 90, 60 ], - SimpleShape::new_square( - 20, - vec![ ColourSpec("blue".to_string()), ColourSpec("grey".to_string()) ], - )?), + Square { + size : vec![20], + faces : vec![ ColourSpec("blue".to_string()), ColourSpec("grey".to_string()) ], + }.load()?), ]) }