chiark / gitweb /
make SimpleShape use dyn Outline
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 27 Sep 2020 02:57:35 +0000 (03:57 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 27 Sep 2020 02:57:35 +0000 (03:57 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/pieces.rs
src/shapelib.rs

index 327900d2f91cd0717c744180efe618c97db2c59c..9a28cb4ed8053e0d2f87b42d0ff61150f24a174b 100644 (file)
@@ -13,10 +13,9 @@ type ColourMap = IndexVec<FaceId,Colour>;
 struct SimpleShape {
   desc : Html,
   path : Html,
-  scaled_path : Html,
-  approx_dia : Coord,
   colours : ColourMap,
   itemname: String,
+  outline: Box<dyn Outline>,
 }
 
 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<Coord> {
-    Some(self.approx_dia / 2)
+  delegate! {
+    to self.outline {
+      fn surround_path(&self, _pri : &PieceRenderInstructions)
+                       -> Result<Html,IE>;
+      fn thresh_dragraise(&self, _pri : &PieceRenderInstructions)
+                          -> Result<Option<Coord>,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<FaceId,ColourSpec>,
+                   outline: Box<dyn Outline>,
                    itemname: String)
                    -> Result<Box<dyn Piece>,SpecError> {
-    let scaled_path = svg_rescale_path(&path, SELECT_SCALE)?;
     let colours = faces
       .iter()
       .map(|s| s.try_into())
       .collect::<Result<_,SpecError>>()?;
     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<dyn Piece> {
+    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)?
   }
 } 
index 248fb9f091eb62d556cad071f9e8edd2d4c074c1..0e0aa88de1f910a52e9f62697a6357d393b11f6c 100644 (file)
@@ -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 {