From 4563553fcfe398f12d1a3bdf67878b5d6f433757 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Thu, 14 Apr 2022 20:45:42 +0100 Subject: [PATCH] Move itemname into PieceBaseTrait We want this becasue dice want to use inert pieces as images, and use their images' item names in their own item names. This involves providing an itemname for ItemInertForOcculted. Happily the SvgBaseName will do nicely. Signed-off-by: Ian Jackson --- src/clock.rs | 4 ++-- src/deck.rs | 12 ++++++------ src/gamestate.rs | 3 ++- src/hand.rs | 12 ++++++------ src/pieces.rs | 4 ++-- src/shapelib.rs | 6 +++++- 6 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/clock.rs b/src/clock.rs index 5547309f..9bc52e7f 100644 --- a/src/clock.rs +++ b/src/clock.rs @@ -434,6 +434,8 @@ impl OutlineTrait for Clock { impl PieceBaseTrait for Clock { fn nfaces(&self) -> RawFaceId { 1 } + + fn itemname(&self) -> &str { "chess-clock" } } #[typetag::serde(name="ChessClock")] @@ -747,6 +749,4 @@ impl PieceTrait for Clock { let state = gpc.xdata_mut(|| State::new(&self.spec))?; state.do_start_or_stop(piece, None, None, held, &self.spec, ig)?; } - - fn itemname(&self) -> &str { "chess-clock" } } diff --git a/src/deck.rs b/src/deck.rs index f03f1c63..f653a11c 100644 --- a/src/deck.rs +++ b/src/deck.rs @@ -98,6 +98,12 @@ impl Deck { impl PieceBaseTrait for Deck { fn nfaces(&self) -> RawFaceId { 1 } + + delegate!{ + to self.shape { + fn itemname(&self) -> &str; + } + } } #[typetag::serde] @@ -129,12 +135,6 @@ impl PieceTrait for Deck { }.into() } - delegate!{ - to self.shape { - fn itemname(&self) -> &str; - } - } - #[throws(InternalError)] fn add_ui_operations(&self, _: ShowUnocculted, upd: &mut Vec, gs: &GameState, gpc: &GPiece) { diff --git a/src/gamestate.rs b/src/gamestate.rs index 4d060587..74c5670b 100644 --- a/src/gamestate.rs +++ b/src/gamestate.rs @@ -176,6 +176,8 @@ pub struct UoDescription { pub trait PieceBaseTrait: OutlineTrait + Send + Debug + 'static { /// By convention, occult face is nfaces-1 fn nfaces(&self) -> RawFaceId; + + fn itemname(&self) -> &str; } #[typetag::serde] // usual variable: p @@ -220,7 +222,6 @@ pub trait PieceTrait: PieceBaseTrait + Send + Debug + 'static { ExecuteGameChangeUpdates{ pcs: vec![], log: vec![], raw: None } } - fn itemname(&self) -> &str; fn sortkey(&self) -> Option<&str> { None } fn occultation_notify_hook(&self, _piece: PieceId) -> UnpreparedUpdates { diff --git a/src/hand.rs b/src/hand.rs index 78256507..6430c2a7 100644 --- a/src/hand.rs +++ b/src/hand.rs @@ -148,6 +148,12 @@ impl Behaviour { impl PieceBaseTrait for Hand { fn nfaces(&self) -> RawFaceId { 1 } + + delegate!{ + to self.shape { + fn itemname(&self) -> &str; + } + } } #[typetag::serde] @@ -195,12 +201,6 @@ impl PieceTrait for Hand { self.behaviour.describe_html_inner(xdata) } - delegate!{ - to self.shape { - fn itemname(&self) -> &str; - } - } - #[throws(InternalError)] fn add_ui_operations(&self, _: ShowUnocculted, upd: &mut Vec, _gs: &GameState, gpc: &GPiece) { diff --git a/src/pieces.rs b/src/pieces.rs index e96072bf..ac3e093e 100644 --- a/src/pieces.rs +++ b/src/pieces.rs @@ -128,6 +128,8 @@ impl OutlineTrait for GenericSimpleShape impl PieceBaseTrait for SimpleShape { fn nfaces(&self) -> RawFaceId { self.count_faces() } + + fn itemname(&self) -> &str { self.itemname() } } #[typetag::serde] @@ -147,8 +149,6 @@ impl PieceTrait for SimpleShape { }; r } - - fn itemname(&self) -> &str { self.itemname() } } #[typetag::serde] diff --git a/src/shapelib.rs b/src/shapelib.rs index 4f06ef74..39e9baad 100644 --- a/src/shapelib.rs +++ b/src/shapelib.rs @@ -178,6 +178,7 @@ pub struct Item { #[derive(Debug,Serialize,Deserialize)] struct ItemInertForOcculted { + itemname: Arc, desc: Html, svgd: Arc, xform: FaceTransform, @@ -225,6 +226,7 @@ impl OutlineTrait for ItemInertForOcculted { delegate! { to self.outline { }}} impl PieceBaseTrait for ItemInertForOcculted { fn nfaces(&self) -> RawFaceId { 1 } + fn itemname(&self) -> &str { &self.itemname.as_str() } } #[typetag::serde(name="Lib")] impl InertPieceTrait for ItemInertForOcculted { @@ -371,6 +373,8 @@ impl PieceBaseTrait for Item { + self.back.iter().count()) .try_into().unwrap() } + + fn itemname(&self) -> &str { &self.itemname } } #[typetag::serde(name="Lib")] @@ -385,7 +389,6 @@ impl PieceTrait for Item { self.describe_face(gpc.face)? } - fn itemname(&self) -> &str { &self.itemname } fn sortkey(&self) -> Option<&str> { self.sort.as_ref().map(AsRef::as_ref) } } @@ -591,6 +594,7 @@ impl Contents { }).clone()?; let it = Arc::new(ItemInertForOcculted { svgd, + itemname: occ_name.clone().into_inner(), xform: occ.xform.clone(), desc: occ.desc.clone(), outline: occ.outline.clone(), -- 2.30.2