chiark / gitweb /
dice: Get size right in preview with a special entrypoint
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 15 May 2022 00:36:30 +0000 (01:36 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 15 May 2022 00:42:18 +0000 (01:42 +0100)
This is the code from
  dice: Get size right in preview by setting bbox, breaking other things
but in a new entrypoint used only for preview.  The comment explains why.

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

index d9d4e3a514d9a15a6dd6a9351340f66e796fa193..120e74a8d16dac9399c3292da8fc6f0ba3a1736b 100644 (file)
@@ -119,7 +119,7 @@ fn preview(opts: &Opts, items: Vec<ItemForOutput>) {
       let spec = spec.clone();
 
       let bbox = p
-        .bbox_approx()?;
+        .bbox_preview()?;
       let mut bbox = bbox.corners.iter()
         .map(|PosC{coords}| coords.iter().map(|&p| p as f64)
              .collect::<Vec<_>>())
index 59ec135a449919dc271ee4bc72205bbcccc8b35c..a55c68bb6d4aaed366d4390f598d69562f482526 100644 (file)
@@ -319,6 +319,8 @@ impl OutlineTrait for Die {
 
   delegate! {
     to self.surround_outline {
+      // see also PieceTraitbbox_preview (below, which would noormally
+      // ccall this, but we override it.
       fn bbox_approx(&self) -> Result<Rect, IE>;
       fn shape(&self) -> Option<Shape>;
     }
@@ -451,6 +453,26 @@ impl PieceTrait for Die {
     )?)
 //.map_err(|e| internal_error_bydebug(&e))?
   }
+
+  // This is not consistent with the surround_path: the surround_path
+  // does not include the cooldown circle, but this does.
+  //
+  // OutlineTrait::bbox_approx is used in lots of places, eg
+  //
+  //  * Pieces are added with the CLI.  (Size also returned via ListPieces.)
+  //  * "Organise" function.
+  //  * Finding pieces that have been clicked on in the client.
+  //  etc.
+  //
+  // So we have to have a different entrypoint for just this:
+  #[throws(IE)]
+  fn bbox_preview(&self) -> Rect {
+    let r: Coord = cast(self.cooldown_radius.ceil())
+      .ok_or(CoordinateOverflow)?;
+    let br = PosC::new(r,r);
+    let tl = (-br)?;
+    Rect{ corners: [tl,br] }
+  }
 }
 
 #[typetag::serde(name="Die")]
index 3a3d118f1f4dcd038ee9c3f4c20ec10daf36ecd9..b4200be053b062a31654c088cf4da3cd824ca565 100644 (file)
@@ -239,6 +239,8 @@ pub trait PieceTrait: PieceBaseTrait + Downcast + Send + Debug + 'static {
   fn loaded_hook_preview(&self, _gpc: &mut GPiece) -> Result<(),IE> {
     Ok(())
   }
+  // Specialised by dice.rs.
+  fn bbox_preview(&self) -> Result<Rect, IE> { self.bbox_approx() }
 
   /// Not called if the whole game is destroyed.
   /// You can use Drop of course but it's not usually much use since
index 60d03c7214d4a5bdd9542852cd9118d1372b1bb5..f244656d47e552123540b35590a8892ad54831e2 100644 (file)
@@ -42,7 +42,7 @@ pub use nix::unistd::{self, Uid};
 pub use nix::sys::time::TimeSpec;
 pub use nix::time::clock_gettime;
 pub use num_derive::{ToPrimitive, FromPrimitive};
-pub use num_traits::{Bounded, FromPrimitive, ToPrimitive};
+pub use num_traits::{cast, Bounded, FromPrimitive, ToPrimitive};
 pub use paste::paste;
 pub use rand::distributions::Alphanumeric;
 pub use rand::thread_rng;