chiark / gitweb /
labels: wip generalisation - break out method
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 24 Mar 2021 15:50:38 +0000 (15:50 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 24 Mar 2021 15:50:49 +0000 (15:50 +0000)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/hand.rs
src/pieces.rs

index 36226dd1cbf1104e8a1aa1bf25b781d643aff864..b83589190b6145e0a13ca58384df6bbbba0bac27 100644 (file)
@@ -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##"<text x="{}" y="{}" font-size="{}" fill="{}">{}</text>"##,
-               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)))?;
       }
     }
   }
index c647744e3c8fc345880a960f90e4d38986cefe04..0705c0f0adfca1ed6827010a14fda0155efd1f0c 100644 (file)
@@ -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##"<text x="{}" y="{}" font-size="{}" fill="{}">{}</text>"##,
+               x, y, fontsz, colour, &text.0
+    )?;
+  }  
+}
+
 impl<Desc, Outl:'static> GenericSimpleShape<Desc, Outl>
     where Desc: Debug + Send + Sync + 'static,
           Outl: OutlineTrait,