From: Ian Jackson Date: Thu, 14 Apr 2022 19:03:34 +0000 (+0100) Subject: Introduce PieceBaseTrait and put nfaces() in it X-Git-Tag: otter-1.1.0~607 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=24b36cec0ab0012bc05941d6573b6322e95a2fa6;p=otter.git Introduce PieceBaseTrait and put nfaces() in it Signed-off-by: Ian Jackson --- diff --git a/src/clock.rs b/src/clock.rs index 69bcf432..5547309f 100644 --- a/src/clock.rs +++ b/src/clock.rs @@ -432,10 +432,12 @@ impl OutlineTrait for Clock { } } -#[typetag::serde(name="ChessClock")] -impl PieceTrait for Clock { +impl PieceBaseTrait for Clock { fn nfaces(&self) -> RawFaceId { 1 } +} +#[typetag::serde(name="ChessClock")] +impl PieceTrait for Clock { #[throws(IE)] fn svg_piece(&self, f: &mut Html, gpc: &GPiece, gs: &GameState, vpid: VisiblePieceId) { diff --git a/src/deck.rs b/src/deck.rs index e5eae521..f03f1c63 100644 --- a/src/deck.rs +++ b/src/deck.rs @@ -96,9 +96,12 @@ impl Deck { } } +impl PieceBaseTrait for Deck { + fn nfaces(&self) -> RawFaceId { 1 } +} + #[typetag::serde] impl PieceTrait for Deck { - fn nfaces(&self) -> RawFaceId { 1 } #[throws(IE)] fn svg_piece(&self, f: &mut Html, gpc: &GPiece, gs: &GameState, _vpid: VisiblePieceId) { diff --git a/src/gamestate.rs b/src/gamestate.rs index ec7361df..4d060587 100644 --- a/src/gamestate.rs +++ b/src/gamestate.rs @@ -173,11 +173,13 @@ pub struct UoDescription { pub wrc: WhatResponseToClientOp, } -#[typetag::serde] // usual variable: p -pub trait PieceTrait: OutlineTrait + Send + Debug + 'static { - /// by convention, occult face is nfaces-1 +pub trait PieceBaseTrait: OutlineTrait + Send + Debug + 'static { + /// By convention, occult face is nfaces-1 fn nfaces(&self) -> RawFaceId; +} +#[typetag::serde] // usual variable: p +pub trait PieceTrait: PieceBaseTrait + Send + Debug + 'static { #[throws(InternalError)] fn add_ui_operations(&self, _: ShowUnocculted, _upd: &mut Vec, @@ -236,9 +238,7 @@ pub trait PieceTrait: OutlineTrait + Send + Debug + 'static { } #[typetag::serde] -pub trait InertPieceTrait: OutlineTrait { - fn nfaces(&self) -> RawFaceId; - +pub trait InertPieceTrait: PieceBaseTrait { /// When used for occultated version of another object, /// face used is always default, regardless of nfaces. fn svg(&self, f: &mut Html, id: VisiblePieceId, face: FaceId) diff --git a/src/hand.rs b/src/hand.rs index a0725413..78256507 100644 --- a/src/hand.rs +++ b/src/hand.rs @@ -146,9 +146,12 @@ impl Behaviour { } } +impl PieceBaseTrait for Hand { + fn nfaces(&self) -> RawFaceId { 1 } +} + #[typetag::serde] impl PieceTrait for Hand { - fn nfaces(&self) -> RawFaceId { 1 } #[throws(IE)] fn svg_piece(&self, f: &mut Html, gpc: &GPiece, gs: &GameState, _vpid: VisiblePieceId) { diff --git a/src/pieces.rs b/src/pieces.rs index f1130d8a..e96072bf 100644 --- a/src/pieces.rs +++ b/src/pieces.rs @@ -126,6 +126,10 @@ impl OutlineTrait for GenericSimpleShape } // let edge_attrs = format!(r##"stroke-width="" stroke"## +impl PieceBaseTrait for SimpleShape { + fn nfaces(&self) -> RawFaceId { self.count_faces() } +} + #[typetag::serde] impl PieceTrait for SimpleShape { #[throws(IE)] @@ -143,17 +147,12 @@ impl PieceTrait for SimpleShape { }; r } - fn nfaces(&self) -> RawFaceId { - self.count_faces() - } fn itemname(&self) -> &str { self.itemname() } } #[typetag::serde] impl InertPieceTrait for SimpleShape { - fn nfaces(&self) -> RawFaceId { PieceTrait::nfaces(self) } - #[throws(IE)] fn svg(&self, f: &mut Html, _vpid: VisiblePieceId, face: FaceId) { self.svg_piece_raw(f, face, &mut |_|Ok(()))?; diff --git a/src/shapelib.rs b/src/shapelib.rs index a3b83dd2..4f06ef74 100644 --- a/src/shapelib.rs +++ b/src/shapelib.rs @@ -223,10 +223,11 @@ impl OutlineTrait for ItemInertForOcculted { delegate! { to self.outline { fn thresh_dragraise(&self) -> Result, IE>; fn bbox_approx(&self) -> Result; }}} +impl PieceBaseTrait for ItemInertForOcculted { + fn nfaces(&self) -> RawFaceId { 1 } +} #[typetag::serde(name="Lib")] impl InertPieceTrait for ItemInertForOcculted { - fn nfaces(&self) -> RawFaceId { 1 } - #[throws(IE)] fn svg(&self, f: &mut Html, _: VisiblePieceId, face: FaceId) { if face != FaceId::default() { @@ -364,14 +365,16 @@ impl Item { } } -#[typetag::serde(name="Lib")] -impl PieceTrait for Item { +impl PieceBaseTrait for Item { fn nfaces(&self) -> RawFaceId { (self.faces.len() + self.back.iter().count()) .try_into().unwrap() } +} +#[typetag::serde(name="Lib")] +impl PieceTrait for Item { #[throws(IE)] fn svg_piece(&self, f: &mut Html, gpc: &GPiece, _gs: &GameState, vpid: VisiblePieceId) { @@ -388,8 +391,6 @@ impl PieceTrait for Item { #[typetag::serde(name="LibItem")] impl InertPieceTrait for Item { - fn nfaces(&self) -> RawFaceId { ::nfaces(self) } - #[throws(IE)] fn svg(&self, f: &mut Html, id: VisiblePieceId, face: FaceId) { self.svg_face(f, face, id)?;