From 13af74460ca82a802a29d93c6c0ccc4e1dfe8c39 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 26 Sep 2020 23:45:14 +0100 Subject: [PATCH] square Signed-off-by: Ian Jackson --- src/imports.rs | 2 ++ src/pieces.rs | 4 ++-- src/shapelib.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/imports.rs b/src/imports.rs index f48b3d38..943632e5 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -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::*; diff --git a/src/pieces.rs b/src/pieces.rs index cc0e1e34..327900d2 100644 --- a/src/pieces.rs +++ b/src/pieces.rs @@ -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, diff --git a/src/shapelib.rs b/src/shapelib.rs index f130a906..220af477 100644 --- a/src/shapelib.rs +++ b/src/shapelib.rs @@ -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 { + 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,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]}), } } } -- 2.30.2