chiark / gitweb /
hidden: Introduce ShowUnocculted as an svg_piece parameter
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 15 Mar 2021 16:14:02 +0000 (16:14 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 15 Mar 2021 16:14:02 +0000 (16:14 +0000)
The need to provide a vpid served (and still servers) the same
function, but this plumbing work will make prepare the way for doing
the same to other methods.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/bin/otterlib.rs
src/gamestate.rs
src/hand.rs
src/hidden.rs
src/pcrender.rs
src/pieces.rs
src/shapelib.rs

index e3a5ffd66c2f552f4b0c202bfd359b99bbc2dfe0..55de6bcaf0ee5b50d26adeb15a4bd4599d633ca0 100644 (file)
@@ -172,7 +172,8 @@ fn preview(items: Vec<ItemForOutput>) {
         }
         let mut html = Html("".into());
         let gpc = GPiece { face: face.into(), ..GPiece::dummy() };
-        p.svg_piece(&mut html, &gpc, default())?;
+        p.svg_piece(&mut html, &gpc, default(),
+                    ShowUnocculted::new_visible())?;
         println!("{}</svg>", html.0);
       }
       println!("</td>");
index 6b8635da7b84b6f1b6f11624dd56d21979ae82c4..1e233fe09cda4a949e0263cb23e31c9be8e9ea2d 100644 (file)
@@ -144,7 +144,7 @@ pub trait PieceTrait: OutlineTrait + Send + Debug + 'static {
 
   // #[throws] doesn't work here - fehler #todo
   fn svg_piece(&self, f: &mut Html, gpc: &GPiece,
-               id: VisiblePieceId) -> Result<(),IE>;
+               id: VisiblePieceId, y: ShowUnocculted) -> Result<(),IE>;
 
   fn describe_html(&self, gpc: &GPiece) -> Result<Html,IE>;
 
index e8f0917d64438168f1e503e9118bcb97ffd67a95..7776e2ff7cd44f1216820acb86f850f1b41c27b5 100644 (file)
@@ -87,7 +87,8 @@ impl Hand {
 impl PieceTrait for Hand {
   fn nfaces(&self) -> RawFaceId { 1 }
   #[throws(IE)]
-  fn svg_piece(&self, f: &mut Html, gpc: &GPiece, _vpid: VisiblePieceId) {
+  fn svg_piece(&self, f: &mut Html, gpc: &GPiece,
+               _vpid: VisiblePieceId, _: ShowUnocculted) {
     self.shape.svg_piece_raw(f, gpc.face, &mut |f: &mut String| {
       if_chain!{
         if let Some(xdata) = gpc.xdata.get::<HandState>()?;
index ad82efe645788ed696736d68438c7389432c601a..7973891e23432a6f3228ceb3abec3203066ccd0e 100644 (file)
@@ -15,6 +15,9 @@ visible_slotmap_key!{ OccId(b'H') }
 
 // ========== data structures ==========
 
+#[derive(Copy,Clone,Debug)]
+pub struct ShowUnocculted(());
+
 #[derive(Clone,Debug,Default,Serialize,Deserialize)]
 pub struct GameOccults {
   occults: DenseSlotMap<OccId, Occultation>,
@@ -192,7 +195,7 @@ pub fn piece_pri(
       trace_dbg!("piece_pri", player, piece, occk, gpc);
       return None;
     }
-    OccKG::Visible            => PriOcculted::Visible,
+    OccKG::Visible            => PriOcculted::Visible(ShowUnocculted(())),
     OccKG::Scrambled          => PriOcculted::Occulted,
     OccKG::Displaced((pos,z)) => PriOcculted::Displaced(pos, z),
   };
@@ -201,6 +204,23 @@ pub fn piece_pri(
   Some(PieceRenderInstructions { vpid, occulted })
 }
 
+impl ShowUnocculted {
+  /// override
+  pub fn new_visible() -> ShowUnocculted {
+    ShowUnocculted(())
+  }
+}
+
+impl PieceRenderInstructions {
+  /// override
+  pub fn new_visible(vpid: VisiblePieceId) -> PieceRenderInstructions {
+    PieceRenderInstructions {
+      vpid,
+      occulted: PriOcculted::Visible(ShowUnocculted(())),
+    }
+  }
+}
+
 pub fn piece_at_all_occulted(gpc: &GPiece) -> bool {
   gpc.occult.passive.is_some()
 }
index 08928c56ea097531462647f5ac31b97e2ab711e6..34c9237568981e922c5df3ef4cf7c75bbc7b1b23 100644 (file)
@@ -18,7 +18,11 @@ pub struct PieceRenderInstructions {
 }
 
 #[derive(Debug,Clone)]
-pub enum PriOcculted { Visible, Occulted, Displaced(Pos, ZLevel) }
+pub enum PriOcculted {
+  Visible(ShowUnocculted),
+  Occulted,
+  Displaced(Pos, ZLevel),
+}
 
 impl VisiblePieceAngle {
   pub fn to_transform(self) -> VisibleAngleTransform {
@@ -49,7 +53,7 @@ impl PieceRenderInstructions {
       ! Delete() | Insert(_) | Modify(_) | ModifyQuiet(_),
     ) {
       match self.occulted {
-        Visible | Occulted => (),
+        Visible(_) | Occulted => (),
         Displaced(..) => return None,
       }
     }
@@ -85,18 +89,14 @@ impl PieceRenderInstructions {
     }
   }
 
-  pub fn new_visible(vpid: VisiblePieceId) -> PieceRenderInstructions {
-    PieceRenderInstructions { vpid, occulted: PriOcculted::Visible }
-  }
-
   #[throws(IE)]
   fn instead<'p>(&self, ioccults: &'p IOccults, p: &'p IPiece)
-                 -> Option<&'p dyn OccultedPieceTrait>
+                 -> Either<ShowUnocculted, &'p dyn OccultedPieceTrait>
   {
     match self.occulted {
-      PriOcculted::Visible                               => None,
+      PriOcculted::Visible(v) => Left(v),
       PriOcculted::Occulted | PriOcculted::Displaced(..) => {
-        Some({
+        Right({
           let occilk = p.occilk.as_ref()
             .ok_or_else(|| internal_logic_error(format!(
               "occulted non-occultable {:?}", p)))?
@@ -112,7 +112,7 @@ impl PieceRenderInstructions {
 
   pub fn angle(&self, gpc: &GPiece) -> VisiblePieceAngle {
     match self.occulted {
-      PriOcculted::Visible                               => gpc.angle,
+      PriOcculted::Visible(_)                            => gpc.angle,
       PriOcculted::Occulted | PriOcculted::Displaced(..) => default(),
     }
   }
@@ -120,7 +120,7 @@ impl PieceRenderInstructions {
   pub fn pos_zlevel<'r>(&'r self, gpc: &'r GPiece) -> (Pos, &'r ZLevel) {
     use PriOcculted as PO;
     match &self.occulted {
-      PO::Visible | PO::Occulted => (gpc.pos, &gpc.zlevel),
+      PO::Visible(_) | PO::Occulted => (gpc.pos, &gpc.zlevel),
       PO::Displaced(pos, zlevel) => (*pos, &zlevel),
     }
   }
@@ -133,8 +133,8 @@ impl PieceRenderInstructions {
     let instead = pri.instead(ioccults, ipc)?;
 
     let o: &dyn OutlineTrait = match instead {
-      None => Borrow::<dyn PieceTrait>::borrow(&ipc.p).dyn_upcast(),
-      Some(i) => i.dyn_upcast(),
+      Left(_) => Borrow::<dyn PieceTrait>::borrow(&ipc.p).dyn_upcast(),
+      Right(i) => i.dyn_upcast(),
     };
 
     let angle = pri.angle(gpc);
@@ -153,10 +153,10 @@ impl PieceRenderInstructions {
            pri.vpid, &transform.0, dragraise)?;
 
     match instead {
-      None => {
-        ipc.p.svg_piece(&mut defs, gpc, pri.vpid)?;
+      Left(y) => {
+        ipc.p.svg_piece(&mut defs, gpc, pri.vpid, y)?;
       },
-      Some(i) => {
+      Right(i) => {
         i.svg(&mut defs, pri.vpid)?;
       },
     };
@@ -182,8 +182,8 @@ impl PieceRenderInstructions {
   pub fn describe_fallible(&self, ioccults: &IOccults,
                            gpc: &GPiece, ipc: &IPiece) -> Html {
     match self.instead(ioccults, ipc)? {
-      None => ipc.p.describe_html(gpc)?,
-      Some(i) => i.describe_html()?,
+      Left(_y) => ipc.p.describe_html(gpc)?,
+      Right(i) => i.describe_html()?,
     }
   }
 
@@ -192,7 +192,7 @@ impl PieceRenderInstructions {
                    -> Vec<UoDescription>
   {
     match self.occulted {
-      PriOcculted::Visible                               => (),
+      PriOcculted::Visible(_)                            => (),
       PriOcculted::Occulted | PriOcculted::Displaced(..) => return vec![],
     };
 
index 370fccdebc143644f77f8bf853984ca1d0f3b730..675258fa4edc2765f8ee2e5e79c8f38dff543d0f 100644 (file)
@@ -129,7 +129,8 @@ impl<Desc, Outl:'static> OutlineTrait for GenericSimpleShape<Desc, Outl>
 #[typetag::serde]
 impl PieceTrait for SimpleShape {
   #[throws(IE)]
-  fn svg_piece(&self, f: &mut Html, gpc: &GPiece, _vpid: VisiblePieceId) {
+  fn svg_piece(&self, f: &mut Html, gpc: &GPiece,
+               _vpid: VisiblePieceId, _: ShowUnocculted) {
     self.svg_piece_raw(f, gpc.face, &mut |_|Ok(()))?;
   }
   #[throws(IE)]
index 4364042143d973b7a43a29712b2757bb6f4e06f5..df1977eedb944319c8adb194c37496e8ceb75e2e 100644 (file)
@@ -214,7 +214,8 @@ impl PieceTrait for Item {
   fn nfaces(&self) -> RawFaceId { self.faces.len().try_into().unwrap() }
 
   #[throws(IE)]
-  fn svg_piece(&self, f: &mut Html, gpc: &GPiece, _vpid: VisiblePieceId) {
+  fn svg_piece(&self, f: &mut Html, gpc: &GPiece,
+               _vpid: VisiblePieceId, _: ShowUnocculted) {
     let face = &self.faces[gpc.face];
     let svgd = &self.svgs[face.svg];
     face.xform.write_svgd(f, svgd)?;