chiark / gitweb /
hand: text seems to work in tests
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Tue, 23 Mar 2021 23:37:25 +0000 (23:37 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Tue, 23 Mar 2021 23:37:25 +0000 (23:37 +0000)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
specs/mao.game.toml
src/hand.rs
src/spec.rs

index 004e0a3a59c537e28fb061e1a99d582de654336a..d1440ddb174578f933730f2fed32b7f9722b2e5c 100644 (file)
@@ -112,6 +112,8 @@ shape.xy = [97,25]
 [[pieces]]
 pos = [250, 65]
 colour = "yellow"
+label.place = "TopLeft"
+label.colour = "black"
 
 type = "Hand"
 edge = "darkgrey"
@@ -172,6 +174,8 @@ shape.xy = [97,25]
 [[pieces]]
 pos = [50, 135]
 colour = "white"
+label.place = "BottomLeft"
+label.colour = "black"
 
 type = "Hand"
 edge = "darkgrey"
index 71c79fda96f63cb7a277176ad048c9b6ecb7401a..4679ed8692a6835e69331d0429e1ececdd43dcee 100644 (file)
@@ -3,6 +3,7 @@
 // There is NO WARRANTY.
 
 use crate::prelude::*;
+use piece_specs::{HandLabel, HandLabelPlace};
 
 pub const UNCLAIMED_DESC: &str = "a hand repository";
 
@@ -16,6 +17,7 @@ struct MagicOwner {
 #[derive(Debug,Serialize,Deserialize)]
 struct Hand {
   shape: GenericSimpleShape<(), shapelib::Rectangle>,
+  label: Option<HandLabel>,
 }
 
 #[derive(Debug,Clone,Default,Serialize,Deserialize)]
@@ -67,6 +69,7 @@ impl PieceSpec for piece_specs::Hand {
       &common)?;
     let p = Box::new(Hand {
       shape,
+      label: self.label.clone(),
     }) as Box<dyn PieceTrait>;
     PieceSpecLoaded { p, occultable: None }
   }
@@ -104,20 +107,30 @@ impl PieceTrait for Hand {
     if_chain! {
       if let Some(owned) = owned;
       if let Some(gpl) = gs.players.get(owned.player);
-      if let Some(colour) = self.shape.edges.get(0);
-      let signum = if false { -1. } else { 1. };
+      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]) = {
-        let mut pos = (self.shape.outline.xy * -0.5)?;
-        pos.0[1] -= 0.5 * fontsz;
-        pos.0[1] *= signum;
-        pos.0[1] += 0.5 * fontsz;
+        use HandLabelPlace::*;
+        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 {
+          TopLeft => { *y *= -1. },
+          BottomLeft => { },
+        };
+        *y += 0.5 * fontsz;
         pos
       };
       then {
         write!(f.0,
  r##"<text x="{}" y="{}" font-size="{}" fill="{}">{}</text>"##,
-               x, y, fontsz, &colour.0,
+               x, y, fontsz, colour,
                htmlescape::encode_minimal(&gpl.nick))?;
       }
     }
index d1589708673182508520fcc4046fbffb77efdaf4..316d655d061263a320aebe387d20818ad89cf021 100644 (file)
@@ -246,6 +246,19 @@ pub mod piece_specs {
     pub edge: Option<ColourSpec>,
     pub edge_width: Option<f64>,
     pub shape: Outline,
+    pub label: Option<HandLabel>,
+  }
+
+  #[derive(Debug,Clone,Serialize,Deserialize)]
+  pub struct HandLabel {
+    #[serde(default)] pub place: HandLabelPlace,
+    pub colour: Option<ColourSpec>,
+  }
+
+  #[derive(Debug,Copy,Clone,Serialize,Deserialize,Eq,PartialEq)]
+  pub enum HandLabelPlace {
+    BottomLeft,
+    TopLeft,
   }
 
   #[derive(Debug,Serialize,Deserialize)]
@@ -424,6 +437,10 @@ pub mod implementation {
     }
   }
 
+  impl Default for piece_specs::HandLabelPlace {
+    fn default() -> Self { Self::BottomLeft }
+  }
+
   impl<P: Eq + Hash> Default for Acl<P> {
     fn default() -> Self { Acl { ents: default() } }
   }