From 755bde3c898746efa253bd15b3daf887bd020296 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 20 Feb 2021 10:45:34 +0000 Subject: [PATCH] Reorganise Hand Signed-off-by: Ian Jackson --- specs/demo.game.toml | 8 ++++---- src/bin/otter.rs | 4 +++- src/hand.rs | 21 ++++++++++++--------- src/pieces.rs | 26 +++++++++++++++++--------- src/prelude.rs | 2 +- src/spec.rs | 7 +++++-- wdriver.rs | 4 +++- 7 files changed, 45 insertions(+), 27 deletions(-) diff --git a/specs/demo.game.toml b/specs/demo.game.toml index c3d6449a..2d68d61b 100644 --- a/specs/demo.game.toml +++ b/specs/demo.game.toml @@ -43,7 +43,7 @@ edge_width = 0.5 [[pieces]] pos = [40, 40] type = "Hand" -shape.type = "Square" -shape.size = [70,20] -shape.faces = ["grey"] -shape.edges = ["white"] +edge = "white" +colour = "grey" +shape.type = "Rectangle" +shape.xy = [70,20] diff --git a/src/bin/otter.rs b/src/bin/otter.rs index f5a129f2..8a1f6bfb 100644 --- a/src/bin/otter.rs +++ b/src/bin/otter.rs @@ -680,7 +680,9 @@ fn read_spec let mut f = File::open(&filename).context("open")?; let mut buf = String::new(); f.read_to_string(&mut buf).context("read")?; - let spec: T = toml_de::from_str(&buf).context("parse")?; + let tv: toml::Value = buf.parse().context("parse TOML")?; + dbg!(&tv); + let spec: T = toml_de::from_value(&tv).context("parse value")?; Ok::<_,AE>(spec) })().with_context(|| format!("read {} {:?}", T::WHAT, &filename))? } diff --git a/src/hand.rs b/src/hand.rs index 17b95189..a73c1e4c 100644 --- a/src/hand.rs +++ b/src/hand.rs @@ -16,7 +16,7 @@ struct MagicOwner { #[derive(Debug,Serialize,Deserialize)] struct Hand { - shape: SimpleShape, + shape: GenericSimpleShape<()>, } #[derive(Debug,Clone,Default,Serialize,Deserialize)] @@ -43,14 +43,17 @@ impl Outline for Hand { impl PieceSpec for piece_specs::Hand { #[throws(SpecError)] fn load(&self, _: usize) -> Box { - let (mut shape, common) = self.shape.load_raw()?; - if common.itemname.is_some() { - throw!(SpecError::ItemnameSpecifiedWhereForbidden); - } - if shape.nfaces() != 1 { - throw!(SpecError::MultifacetedMagic); - } - shape.itemname = "magic-hand".to_string(); + let common = SimpleCommon { + itemname: None, + faces: index_vec![ColourSpec(self.colour.clone())], + edges: self.edge.iter().cloned().collect(), + edge_width: self.edge_width, + }; + let shape = GenericSimpleShape::new( + (), + self.shape.clone(), + "magic-hand", + &common)?; Box::new(Hand { shape, }) as Box diff --git a/src/pieces.rs b/src/pieces.rs index 3c860afd..2680fe58 100644 --- a/src/pieces.rs +++ b/src/pieces.rs @@ -12,14 +12,15 @@ type ColourMap = IndexVec; #[derive(Debug,Serialize,Deserialize)] // todo: this serialisation is rather large -pub struct SimpleShape { - pub desc: Html, +pub struct GenericSimpleShape { + pub desc: Desc, colours: ColourMap, #[serde(default)] pub edges: ColourMap, #[serde(default="default_edge_width")] pub edge_width: f64, pub itemname: String, pub outline: OutlineRepr, } +pub type SimpleShape = GenericSimpleShape; pub const SELECT_SCALE: f64 = 1.1; @@ -110,7 +111,9 @@ pub fn svg_rectangle_path(PosC([x,y]): PosC) -> Html { -x*0.5, -y*0.5, x, y, -x)) } -impl Outline for SimpleShape { +impl Outline for GenericSimpleShape + where Desc: Debug + Send + Sync + 'static +{ delegate! { to self.outline { fn outline_path(&self, _pri: &PieceRenderInstructions, scale: f64) @@ -141,17 +144,22 @@ impl Piece for SimpleShape { } fn nfaces(&self) -> RawFaceId { self.count_faces().try_into().unwrap() } - fn itemname(&self) -> &str { &self.itemname } + fn itemname(&self) -> &str { self.itemname() } } -impl SimpleShape { - fn count_faces(&self) -> usize { max(self.colours.len(), self.edges.len()) } +impl GenericSimpleShape + where Desc: Debug + Send + Sync + 'static +{ + pub fn count_faces(&self) -> usize { + max(self.colours.len(), self.edges.len()) + } + pub fn itemname(&self) -> &str { &self.itemname } #[throws(SpecError)] - fn new(desc: Html, outline: OutlineRepr, + pub fn new(desc: Desc, outline: OutlineRepr, def_itemname: &'_ str, common: &SimpleCommon) - -> SimpleShape + -> GenericSimpleShape { let itemname = common.itemname.clone() .unwrap_or_else(|| def_itemname.to_string()); @@ -167,7 +175,7 @@ impl SimpleShape { throw!(SpecError::SpecifiedWidthOfNoEdges); } - let shape = SimpleShape { + let shape = GenericSimpleShape { desc, itemname, outline, colours: cmap(&common.faces)?, edges: cmap(&common.edges)?, diff --git a/src/prelude.rs b/src/prelude.rs index 259be25b..473a4991 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -111,7 +111,7 @@ pub use crate::pieces::*; pub use crate::shapelib; pub use crate::slotmap_slot_idx::*; pub use crate::spec::*; -pub use crate::spec::piece_specs::FaceColourSpecs; +pub use crate::spec::piece_specs::{FaceColourSpecs, SimpleCommon}; pub use crate::sse; pub use crate::toml_de; pub use crate::tz::*; diff --git a/src/spec.rs b/src/spec.rs index 68cc506f..59933764 100644 --- a/src/spec.rs +++ b/src/spec.rs @@ -53,7 +53,7 @@ define_index_type! { } #[derive(Serialize,Deserialize)] -#[derive(Debug,Default)] +#[derive(Debug,Default,Clone)] #[repr(transparent)] #[serde(transparent)] pub struct ColourSpec(pub String); @@ -239,7 +239,10 @@ pub mod piece_specs { #[derive(Debug,Serialize,Deserialize)] pub struct Hand { - pub shape: Box, + pub colour: String, + pub edge: Option, + pub edge_width: Option, + pub shape: OutlineRepr, } } diff --git a/wdriver.rs b/wdriver.rs index 7e39d808..25ee4914 100644 --- a/wdriver.rs +++ b/wdriver.rs @@ -663,7 +663,9 @@ impl DirSubst { (||{ let data = fs::read(&path).context("read")?; let data = std::str::from_utf8(&data).context("convert from UTF-8")?; - let data = toml_de::from_str(&data).context("parse")?; + let data: toml::Value = data.parse().context("parse TOM")?; + dbg!(&data); + let data = toml_de::from_value(&data).context("interperet TOML")?; Ok::<_,AE>(data) })() .context(path) -- 2.30.2