From 6a6969532bde17850e8e28aa47b2d733284ea694 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sun, 15 May 2022 16:30:50 +0100 Subject: [PATCH] dice: Allow setting label colour Signed-off-by: Ian Jackson --- dice/overlay-template-extractor | 1 + docs/gamespec.rst | 3 +++ src/dice.rs | 16 +++++++++++++++- 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/dice/overlay-template-extractor b/dice/overlay-template-extractor index d31d9f25..4f0cbfcb 100755 --- a/dice/overlay-template-extractor +++ b/dice/overlay-template-extractor @@ -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 }}'); diff --git a/docs/gamespec.rst b/docs/gamespec.rst index 4a390cc7..b9e89f08 100644 --- a/docs/gamespec.rst +++ b/docs/gamespec.rst @@ -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"] diff --git a/src/dice.rs b/src/dice.rs index a55c68bb..ede7a2b8 100644 --- a/src/dice.rs +++ b/src/dice.rs @@ -31,6 +31,7 @@ pub struct Spec { // must be >1 faces on image, or >1 texts, and if both, same number image: Box, #[serde(default)] labels: SpecLabels, + #[serde(default)] label: SpecLabelSection, occult: Option, // 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, +} + #[derive(Debug,Clone,Serialize,Deserialize)] #[serde(untagged)] pub enum SpecLabels { @@ -68,6 +74,7 @@ struct Die { itemname: String, labels: IndexVec, // if .len()==1, always use [0] image: Arc, // 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, -- 2.30.2