chiark / gitweb /
dice: Allow setting label colour
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 15 May 2022 15:30:50 +0000 (16:30 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 15 May 2022 22:38:57 +0000 (23:38 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
dice/overlay-template-extractor
docs/gamespec.rst
src/dice.rs

index d31d9f2567c2048a53557de3ddf431239ee71984..4f0cbfcb03089b5d5a867cc3447e1192dd038293 100755 (executable)
@@ -59,6 +59,7 @@ sub filter_text () {
   filter_element('text', qw(x y font-family text-align text-anchor));
   $node->setAttribute('font-size', "{{ label_font_size }}px");
   $node->setAttribute('y', "{{ label_y_adjust }}");
+  $node->setAttribute('fill', "{{ label_colour }}");
   $node->setAttribute('pointer-events', 'none');
   $node->removeChildNodes();
   $node->appendText('{{ label_text }}');
index 4a390cc7cfa0e44754debd2ad2520d28ac721ceb..b9e89f088943d2ec4380647533e3a4d5313927ee 100644 (file)
@@ -344,6 +344,9 @@ Parameters:
     - [list of two numbers] Label faces numerically (inclusive).
     - [single number] Label faces numerically from 1 to n (inclusive).
 
+ * ``label.colour`` [string, defaults to black]:
+   Colour to write the ``labels`` Text strings.
+
  * ``cooldown``: Duration of the cooldown time.
    [duration - number(s) with units; default "4s"]
 
index a55c68bb6d4aaed366d4390f598d69562f482526..ede7a2b87a3fb44ce66e64f529639c562548085f 100644 (file)
@@ -31,6 +31,7 @@ pub struct Spec {
   // must be >1 faces on image, or >1 texts, and if both, same number
   image: Box<dyn PieceSpec>,
   #[serde(default)] labels: SpecLabels,
+  #[serde(default)] label: SpecLabelSection,
   occult: Option<OccultSpec>,
   // 1.0 means base outline circle size on most distant corner of bounding box
   // minimum is 0.5; maximum is 1.5
@@ -46,6 +47,11 @@ pub struct OccultSpec {
   #[serde(default)] label: String,
 }
 
+#[derive(Debug,Default,Clone,Serialize,Deserialize)]
+pub struct SpecLabelSection {
+  pub colour: Option<ColourSpec>,
+}
+
 #[derive(Debug,Clone,Serialize,Deserialize)]
 #[serde(untagged)]
 pub enum SpecLabels {
@@ -68,6 +74,7 @@ struct Die {
   itemname: String,
   labels: IndexVec<FaceId, String>, // if .len()==1, always use [0]
   image: Arc<dyn InertPieceTrait>, // if image.nfaces()==1, always use face 0
+  label_colour: Colour,
   surround_outline: CircleOutline,
   cooldown_radius: f64,
   cooldown_time: Duration,
@@ -88,6 +95,7 @@ struct OverlayTemplateContext<'c> {
   label_text: &'c str,
   label_font_size: f64,
   label_y_adjust: f64,
+  label_colour: &'c Html,
 
   cooldown_active: bool,
   radius: f64,
@@ -149,6 +157,10 @@ impl PieceSpec for Spec {
       index_vec!["".into()]
     };
 
+    let label_colour: Colour = self.label.colour.as_ref()
+      .map(TryInto::try_into).transpose()?
+      .unwrap_or_else(|| Html::lit("black").into());
+
     if_let!{ Some((nfaces,_)) = nfaces;
              else throw!(SpecError::MultipleFacesRequired) };
 
@@ -218,6 +230,7 @@ impl PieceSpec for Spec {
       let occ_ilk = LOI::Distinct;
       let our_occ_image = Arc::new(Die {
         nfaces, cooldown_time, cooldown_radius, surround_outline,
+        label_colour: label_colour.clone(),
         itemname: itemname.clone(),
         desc: desc.clone(),
         image: occ_image,
@@ -229,7 +242,7 @@ impl PieceSpec for Spec {
 
     let die = Die {
       nfaces, cooldown_time, cooldown_radius, surround_outline,
-      itemname, labels, desc,
+      itemname, labels, desc, label_colour,
       image: image.into()
     };
 
@@ -507,6 +520,7 @@ impl InertPieceTrait for Die {
 
     let tc = OverlayTemplateContext {
       label_text: label,
+      label_colour: &self.label_colour,
       label_font_size,
       label_y_adjust: label_font_size * SVG_FONT_Y_ADJUST_OF_FONT_SIZE,