From f85b0145a03642d66fd41d985dd9175cb601e565 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sun, 27 Sep 2020 03:57:35 +0100 Subject: [PATCH] make SimpleShape use dyn Outline Signed-off-by: Ian Jackson --- src/pieces.rs | 35 ++++++++++++++++++----------------- src/shapelib.rs | 4 ++-- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/pieces.rs b/src/pieces.rs index 327900d2..9a28cb4e 100644 --- a/src/pieces.rs +++ b/src/pieces.rs @@ -13,10 +13,9 @@ type ColourMap = IndexVec; struct SimpleShape { desc : Html, path : Html, - scaled_path : Html, - approx_dia : Coord, colours : ColourMap, itemname: String, + outline: Box, } pub const SELECT_SCALE : f64 = 1.1; @@ -108,16 +107,16 @@ pub fn svg_rectangle_path([x, y] : [f64;2]) -> Html { #[typetag::serde] impl Outline for SimpleShape { - #[throws(IE)] - fn surround_path(&self, _pri : &PieceRenderInstructions) -> Html { - self.scaled_path.clone() - } - #[throws(IE)] - fn thresh_dragraise(&self, _pri : &PieceRenderInstructions) - -> Option { - Some(self.approx_dia / 2) + delegate! { + to self.outline { + fn surround_path(&self, _pri : &PieceRenderInstructions) + -> Result; + fn thresh_dragraise(&self, _pri : &PieceRenderInstructions) + -> Result,IE>; + } } } + #[typetag::serde] impl Piece for SimpleShape { #[throws(IE)] @@ -147,17 +146,17 @@ impl Piece for SimpleShape { } impl SimpleShape { - fn new_from_path(desc: Html, path: Html, approx_dia: Coord, + fn new_from_path(desc: Html, path: Html, faces: &IndexVec, + outline: Box, itemname: String) -> Result,SpecError> { - let scaled_path = svg_rescale_path(&path, SELECT_SCALE)?; let colours = faces .iter() .map(|s| s.try_into()) .collect::>()?; Ok(Box::new(SimpleShape { - scaled_path, desc, approx_dia, path, colours, itemname + desc, path, colours, itemname, outline, })) } } @@ -166,11 +165,12 @@ impl SimpleShape { impl PieceSpec for piece_specs::Disc { #[throws(SpecError)] fn load(&self) -> Box { + let outline = Box::new(shapelib::Circle { diam: self.diam as f64 }); let path = svg_circle_path(self.diam as f64)?; let itemname = self.itemname.clone() .unwrap_or_else(||"simple-disc".to_string()); - SimpleShape::new_from_path(Html::lit("circle"), path, self.diam, - &self.faces, itemname)? + SimpleShape::new_from_path(Html::lit("circle"), path, + &self.faces, outline, itemname)? } } @@ -183,10 +183,11 @@ impl PieceSpec for piece_specs::Square { [x, y] => (x,y), _ => throw!(SpecError::ImproperSizeSpec), }; + let outline = Box::new(shapelib::Square { xy: [x as f64, y as f64] }); let path = svg_rectangle_path([x as f64, y as f64])?; let itemname = self.itemname.clone() .unwrap_or_else(||"simple-square".to_string()); - SimpleShape::new_from_path(Html::lit("square"), path, (x+y+1)/2, - &self.faces, itemname)? + SimpleShape::new_from_path(Html::lit("square"), path, + &self.faces, outline, itemname)? } } diff --git a/src/shapelib.rs b/src/shapelib.rs index 248fb9f0..0e0aa88d 100644 --- a/src/shapelib.rs +++ b/src/shapelib.rs @@ -394,7 +394,7 @@ pub fn load() { } #[derive(Serialize,Deserialize,Debug)] -struct Circle { diam: f64 } +pub struct Circle { pub diam: f64 } #[typetag::serde(name="Circle")] impl Outline for Circle { @@ -433,7 +433,7 @@ impl CircleDefn { } #[derive(Serialize,Deserialize,Debug)] -struct Square { xy: [f64;2] } +pub struct Square { pub xy: [f64;2] } #[typetag::serde(name="Square")] impl Outline for Square { -- 2.30.2