chiark / gitweb /
Reorganise Hand
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 20 Feb 2021 10:45:34 +0000 (10:45 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 20 Feb 2021 10:45:34 +0000 (10:45 +0000)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
specs/demo.game.toml
src/bin/otter.rs
src/hand.rs
src/pieces.rs
src/prelude.rs
src/spec.rs
wdriver.rs

index c3d6449a4e77ab86170dc779d197db5db0dc9cd5..2d68d61bc39d2ae4e86ad98629cb6f882af7d53d 100644 (file)
@@ -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]
index f5a129f20d5ccd45854d83d2348ef88e7be2e91f..8a1f6bfb15ba9ab384c68ac1762875c88395bdff 100644 (file)
@@ -680,7 +680,9 @@ fn read_spec<T: DeserializeOwned + SomeSpec>
     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))?
 }
index 17b95189c89f59ebf63147534f3f2a7d9b9e9ca7..a73c1e4c15dec88b28f8689e54b3a290528f850b 100644 (file)
@@ -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<dyn Piece> {
-    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<dyn Piece>
index 3c860afde1961a67a29bb24466071e3199eda4c1..2680fe58821a7fecf53d2c175429cf2b4c64d16e 100644 (file)
@@ -12,14 +12,15 @@ type ColourMap = IndexVec<FaceId, Colour>;
 
 #[derive(Debug,Serialize,Deserialize)]
 // todo: this serialisation is rather large
-pub struct SimpleShape {
-  pub desc: Html,
+pub struct GenericSimpleShape<Desc> {
+  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<Html>;
 
 pub const SELECT_SCALE: f64 = 1.1;
 
@@ -110,7 +111,9 @@ pub fn svg_rectangle_path(PosC([x,y]): PosC<f64>) -> Html {
                -x*0.5, -y*0.5, x, y, -x))
 }
 
-impl Outline for SimpleShape {
+impl<Desc> Outline for GenericSimpleShape<Desc>
+    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<Desc> GenericSimpleShape<Desc>
+    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<Desc>
   {
     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)?,
index 259be25b62150182e6221e84521db3ae5f47fb92..473a499159b183d6802b760ea999788e15bb5c54 100644 (file)
@@ -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::*;
index 68cc506f20f824dd9c1ab8279c1693769a27565c..59933764b191259b9d511bffd7a86ee936f30c96 100644 (file)
@@ -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<dyn crate::pieces::SimplePieceSpec>,
+    pub colour: String,
+    pub edge: Option<ColourSpec>,
+    pub edge_width: Option<f64>,
+    pub shape: OutlineRepr,
   }
 }
 
index 7e39d808fe9373c87d7d0b5dd3ecd1a057e8fd23..25ee49149e1decb4a26f707ba4f0bf029bc8ebd1 100644 (file)
@@ -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)