From 5ee5b193198ffa8fa674f1362c7a011077fa1c9d Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Wed, 24 Mar 2021 15:50:38 +0000 Subject: [PATCH] labels: wip generalisation - break out method Signed-off-by: Ian Jackson --- src/hand.rs | 31 ++++++------------------------- src/pieces.rs | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 25 deletions(-) diff --git a/src/hand.rs b/src/hand.rs index 36226dd1..b8358919 100644 --- a/src/hand.rs +++ b/src/hand.rs @@ -3,7 +3,7 @@ // There is NO WARRANTY. use crate::prelude::*; -use piece_specs::{PieceLabel, PieceLabelPlace}; +use piece_specs::PieceLabel; pub const UNCLAIMED_DESC: &str = "a hand repository"; @@ -107,31 +107,12 @@ impl PieceTrait for Hand { if_chain! { if let Some(owned) = owned; if let Some(gpl) = gs.players.get(owned.player); - if let Some(spec) = &self.label; - let colour = { - if let Some(c) = &spec.colour { &c.0 } - else if let Some(c) = self.shape.edges.get(0) { &c.0 } - else { "black" } - }; - let fontsz = 4.; - let PosC([x,y]) = { - use PieceLabelPlace::*; - let eff_size = (self.shape.outline.xy - PosC([2.,2.]))?; - let mut pos = (eff_size * -0.5)?; - let y = &mut pos.0[1]; - *y += 0.5 * fontsz; - match spec.place { - BottomLeft => { *y *= -1. }, - TopLeft => { } - }; - *y += 0.5 * fontsz; - pos - }; + if let Some(label) = &self.label; then { - write!(f.0, - r##"{}"##, - x, y, fontsz, colour, - htmlescape::encode_minimal(&gpl.nick))?; + label.svg(f, + &self.shape.outline, + self.shape.edges.get(0), + &Html(htmlescape::encode_minimal(&gpl.nick)))?; } } } diff --git a/src/pieces.rs b/src/pieces.rs index c647744e..0705c0f0 100644 --- a/src/pieces.rs +++ b/src/pieces.rs @@ -149,6 +149,38 @@ impl PieceTrait for SimpleShape { fn itemname(&self) -> &str { self.itemname() } } +impl piece_specs::PieceLabel { + #[throws(IE)] + pub fn svg(&self, f: &mut Html, + outline: &shapelib::Rectangle, + def_colour: Option<&Colour>, + text: &Html) { + let colour = { + if let Some(c) = &self.colour { &c.0 } + else if let Some(c) = def_colour { &c.0 } + else { "black" } + }; + let fontsz = 4.; + let PosC([x,y]) = { + use piece_specs::PieceLabelPlace::*; + let eff_size = (outline.xy - PosC([2.,2.]))?; + let mut pos = (eff_size * -0.5)?; + let y = &mut pos.0[1]; + *y += 0.5 * fontsz; + match self.place { + BottomLeft => { *y *= -1. }, + TopLeft => { } + }; + *y += 0.5 * fontsz; + pos + }; + write!(f.0, + r##"{}"##, + x, y, fontsz, colour, &text.0 + )?; + } +} + impl GenericSimpleShape where Desc: Debug + Send + Sync + 'static, Outl: OutlineTrait, -- 2.30.2