chiark / gitweb /
dice: Break out OccultSpec (breaking change)
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 16 Apr 2022 13:17:22 +0000 (14:17 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 17 Apr 2022 13:26:23 +0000 (14:26 +0100)
Earlier I wrote: We want to add an ilk here.

But this is no longer going to be true, because we are getting rid of
ilk mixing for dice.  Nevertheless, this change is going in a roughly
good direction in spec API terms since it will allow us to add other
things to occult.

Notably, if we allow composition of a die image from a base and faces,
or if we allow specifying an occulted face image separately from the
face images.

And `occult_label` is just a bad pattern.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/dice.rs

index 429bdc85740df73f56ddd5e606f5da37a726fc88..33dcaaa9867e2c3ec5624339a97549ad75b2616f 100644 (file)
@@ -31,7 +31,7 @@ pub struct Spec {
   // must be >1 faces on image, or >1 texts, and if both, same number
   image: Box<dyn PieceSpec>,
   #[serde(default)] labels: SpecLabels,
-  #[serde(default)] occult_label: String,
+  occult: Option<OccultSpec>,
   // 1.0 means base outline circle size on most distant corner of bounding box
   // minimum is 0.5; maximum is 1.5
   #[serde(default="default_circle_scale")] circle_scale: f64,
@@ -40,6 +40,11 @@ pub struct Spec {
   itemname: Option<String>,
 }
 
+#[derive(Debug,Default,Clone,Serialize,Deserialize)]
+pub struct OccultSpec {
+  #[serde(default)] label: String,
+}
+
 #[derive(Debug,Clone,Serialize,Deserialize)]
 #[serde(untagged)]
 pub enum SpecLabels {
@@ -166,17 +171,17 @@ impl PieceSpec for Spec {
     };
     let _state: &mut State = gpc.xdata_mut(|| initial_state)?;
 
-    let occultable = match (img_occultable, &self.occult_label) {
-      (None, l) => if l == "" {
-        None
-      } else {
+    let occultable = match (img_occultable, &self.occult) {
+      (None, None) => None,
+      (None, Some(_occ)) => {
         throw!(SpecError::UnusedOccultLabel)
       },
-      (Some((image_occ_ilk, image_occ_image)), occ_label) => {
-        let occ_label = if occ_label == "" && labels.iter().any(|l| l != "") {
-          "?"
+      (Some((image_occ_ilk, image_occ_image)), occ) => {
+        let occ = occ.clone().unwrap_or_default();
+        let occ_label = if occ.label == "" && labels.iter().any(|l| l != "") {
+          "?".into()
         } else {
-          occ_label
+          occ.label
         };
 
         let our_ilk =
@@ -201,7 +206,7 @@ impl PieceSpec for Spec {
           nfaces, cooldown_time, cooldown_radius, surround_outline,
           itemname: itemname.clone(),
           image: image_occ_image,
-          labels: index_vec![occ_label.into()],
+          labels: index_vec![occ_label],
         }) as _;
 
         Some((our_ilk, our_occ_image))