chiark / gitweb /
SimpleShape: Allow specifying edge width
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 13 Feb 2021 01:44:24 +0000 (01:44 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 13 Feb 2021 01:47:45 +0000 (01:47 +0000)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
specs/demo.game.toml
src/pieces.rs
src/spec.rs

index c3c1d3fd68e796a83b98cb43a09784f6c75e8192..c8b1028ec3cb54b633f4d19154127f24f0d8faa4 100644 (file)
@@ -38,3 +38,4 @@ size = [10]
 angle.Compass = 1
 faces = [ ]
 edges = ["yellow", "orange"]
+edge_width = 0.5
index 727141ef619c6df00daffe09d6f7849da933cfff..2cb8fd18490312a62eb288dcc40874d4e2d9f430 100644 (file)
@@ -17,12 +17,17 @@ struct SimpleShape {
   path: Html,
   colours: ColourMap,
   #[serde(default)] edges: ColourMap,
+  #[serde(default="default_edge_width")] edge_width: f64,
   itemname: String,
   outline: Box<dyn Outline>,
 }
 
 pub const SELECT_SCALE: f64 = 1.1;
 
+pub const DEFAULT_EDGE_WIDTH: f64 = 0.2;
+
+fn default_edge_width() -> f64 { DEFAULT_EDGE_WIDTH }
+
 #[derive(Copy,Clone,Debug,Error,Serialize,Deserialize)]
 pub enum SVGProcessingError {
   UnknownOperator,
@@ -117,6 +122,7 @@ impl Outline for SimpleShape {
     }
   }
 }
+//    let edge_attrs = format!(r##"stroke-width="" stroke"##
 
 #[typetag::serde]
 impl Piece for SimpleShape {
@@ -125,7 +131,7 @@ impl Piece for SimpleShape {
     let f = &mut f.0;
     let ef = |f: &mut String, cmap: &ColourMap, attrname: &str, otherwise| {
       if let Some(colour) = cmap.get(pri.face) {
-        write!(f, r##"{}="{}""##, attrname, colour.0)
+        write!(f, r##" {}="{}""##, attrname, colour.0)
       } else {
         write!(f, "{}", otherwise)
       }
@@ -136,9 +142,12 @@ impl Piece for SimpleShape {
                   stroke-width="2" stroke="transparent" d="{}"/>"##,
              &self.path.0)?;
     }
-    write!(f, r##"<path "##)?;
-    ef(f, &self.colours, "fill", r##"fill="none""##)?;
-    ef(f, &self.edges, r##"stroke-width="0.2" stroke"##, "")?;
+    write!(f, r##"<path"##)?;
+    ef(f, &self.colours, "fill", r##" fill="none""##)?;
+    if self.edges.len() != 0 {
+      write!(f, r##" stroke-width="{}""##, &self.edge_width)?;
+    }
+    ef(f, &self.edges, "stroke", "")?;
     write!(f, r##" d="{}"/>"##, &self.path.0)?;
   }
   fn describe_html(&self, face: Option<FaceId>) -> Html {
@@ -174,10 +183,15 @@ impl SimpleShape {
         .collect::<Result<_,_>>()?
     );
 
+    if common.edge_width.is_some() && common.edges.len() == 0 {
+      throw!(SpecError::SpecifiedWidthOfNoEdges);
+    }
+
     let shape = SimpleShape {
       desc, path, itemname, outline,
       colours: cmap(&common.faces)?,
       edges: cmap(&common.edges)?,
+      edge_width: common.edge_width.unwrap_or(DEFAULT_EDGE_WIDTH),
     };
 
     let count = shape.count_faces();
index 1b20ff35d623b30260ad0305941100a296dd7650..879cca400f4286ddc69538f53b59f09d2ea18e3e 100644 (file)
@@ -79,6 +79,7 @@ pub enum SpecError {
   CompassAngleInvalid,
   ZeroFaces,
   InconsistentFacesEdgecoloursCount,
+  SpecifiedWidthOfNoEdges,
 }
 display_as_debug!{SpecError}
 
@@ -201,6 +202,7 @@ pub mod piece_specs {
     pub itemname: Option<String>,
     pub faces: IndexVec<FaceId, ColourSpec>,
     #[serde(default)] pub edges: IndexVec<FaceId, ColourSpec>,
+    pub edge_width: Option<f64>,
   }
 
   #[derive(Debug,Serialize,Deserialize)]