chiark / gitweb /
square
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 26 Sep 2020 22:45:14 +0000 (23:45 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 26 Sep 2020 22:45:14 +0000 (23:45 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/imports.rs
src/pieces.rs
src/shapelib.rs

index f48b3d38f49517586d12de259c5b69da04f1c245..943632e53829ab468041c70ed17f7a7fcb7b6f0d 100644 (file)
@@ -90,6 +90,8 @@ pub use delegate::delegate;
 
 pub use itertools::Itertools;
 
+pub use ordered_float::OrderedFloat;
+
 pub use crate::global::*;
 pub use crate::gamestate::*;
 pub use crate::pieces::*;
index cc0e1e346c974a5a295b7f79477ef04490ee76eb..327900d2f91cd0717c744180efe618c97db2c59c 100644 (file)
@@ -101,7 +101,7 @@ pub fn svg_circle_path(diam: f64) -> Html {
 }
 
 #[throws(SE)]
-pub fn svg_rectangle_path(x: f64, y: f64) -> Html {
+pub fn svg_rectangle_path([x, y] : [f64;2]) -> Html {
   Html(format!("M {} {} h {} v {} h {} z",
                -x*0.5, -y*0.5, x, y, -x))
 }
@@ -183,7 +183,7 @@ impl PieceSpec for piece_specs::Square {
       [x, y] => (x,y),
       _ => throw!(SpecError::ImproperSizeSpec),
     };
-    let path = svg_rectangle_path(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,
index f130a9067141f488bf32fa3124db5c2a571eea93..220af477e3de66e0486fffba576486f2b524b615 100644 (file)
@@ -90,7 +90,7 @@ pub enum LibraryLoadError{
   #[error("{:?}",&self)]
   ExpectedString(String),
   #[error("{:?}",&self)]
-  WrongNumberOfSizeDimensions { got: usize, expected: usize },
+  WrongNumberOfSizeDimensions { got: usize, expected: [usize;2] },
   #[error("{:?}",&self)]
   InheritMissingParent(String,String),
   #[error("{:?}",&self)]
@@ -418,7 +418,52 @@ impl CircleDefn {
     match group.d.size.as_slice() {
       &[c] => c,
       size => throw!(LLE::WrongNumberOfSizeDimensions
-                     { got: size.len(), expected : 1 }),
+                     { got: size.len(), expected : [1,1] }),
+    }
+  }
+}
+
+#[derive(Serialize,Deserialize,Debug)]
+struct Square { xy: [f64;2] }
+
+#[typetag::serde(name="Square")]
+impl Outline for Square {
+  #[throws(IE)]
+  fn surround_path(&self, _pri : &PieceRenderInstructions) -> Html {
+    let size : ArrayVec<_> =
+      self.xy.iter().map(|s| s * SELECT_SCALE)
+      .collect();
+    svg_rectangle_path(size.into_inner().unwrap())?
+  }
+  #[throws(IE)]
+  fn thresh_dragraise(&self, _pri : &PieceRenderInstructions)
+                      -> Option<Coord> {
+    let smallest : f64 = self.xy.iter().cloned()
+      .map(OrderedFloat::from).min().unwrap().into();
+    Some((smallest * 0.5) as Coord)
+  }
+}
+
+#[derive(Deserialize,Debug)]
+struct SquareDefn { }
+#[typetag::deserialize(name="Square")]
+impl OutlineDefn for SquareDefn {
+  #[throws(LibraryLoadError)]
+  fn check(&self, lgd: &GroupData) { Self::get(lgd)?; }
+  fn load(&self, lgd: &GroupData) -> Result<Box<dyn Outline>,IE> {
+    Ok(Box::new(
+      Self::get(lgd).map_err(|e| e.ought())?
+    ))
+  }
+}
+impl SquareDefn {
+  #[throws(LibraryLoadError)]
+  fn get(group: &GroupData) -> Square {
+    match group.d.size.as_slice() {
+      &[s] => Square { xy: [s,s] },
+      s if s.len() == 2 => Square { xy: s.try_into().unwrap() },
+      size => throw!(LLE::WrongNumberOfSizeDimensions
+                     { got: size.len(), expected : [1,2]}),
     }
   }
 }