From: Ian Jackson Date: Sun, 15 May 2022 00:36:30 +0000 (+0100) Subject: dice: Get size right in preview with a special entrypoint X-Git-Tag: otter-1.1.0~146 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=a29049d94fc6cca9303d8a071cea3eae621ebc87;p=otter.git dice: Get size right in preview with a special entrypoint 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 --- diff --git a/src/bin/otterlib.rs b/src/bin/otterlib.rs index d9d4e3a5..120e74a8 100644 --- a/src/bin/otterlib.rs +++ b/src/bin/otterlib.rs @@ -119,7 +119,7 @@ fn preview(opts: &Opts, items: Vec) { 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::>()) diff --git a/src/dice.rs b/src/dice.rs index 59ec135a..a55c68bb 100644 --- a/src/dice.rs +++ b/src/dice.rs @@ -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; fn shape(&self) -> Option; } @@ -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")] diff --git a/src/gamestate.rs b/src/gamestate.rs index 3a3d118f..b4200be0 100644 --- a/src/gamestate.rs +++ b/src/gamestate.rs @@ -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 { 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 diff --git a/support/imports.rs b/support/imports.rs index 60d03c72..f244656d 100644 --- a/support/imports.rs +++ b/support/imports.rs @@ -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;