From 11ea34aca1ba1cef7a510b6f0141053a33607ebc Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Fri, 15 Apr 2022 23:30:44 +0100 Subject: [PATCH] die: Prepare for extracting text template too Signed-off-by: Ian Jackson --- .gitignore | 2 +- Makefile | 4 +-- ...e-extractor => overlay-template-extractor} | 5 ++- src/dice.rs | 36 +++++++++++-------- 4 files changed, 28 insertions(+), 19 deletions(-) rename dice/{cooldown-template-extractor => overlay-template-extractor} (94%) diff --git a/.gitignore b/.gitignore index b665e109..d9e68d79 100644 --- a/.gitignore +++ b/.gitignore @@ -7,7 +7,7 @@ templates/CC-BY-SA-3.0 templates/CC-BY-SA-4.0 templates/otter_wasm.ns.d.ts templates/shapelib.html -nwtemplates/die-cooldown.tera +nwtemplates/die-overlay.tera save/lock /examples/test-bundle.zip /examples/big-bundle diff --git a/Makefile b/Makefile index 88f86e62..65fbe652 100644 --- a/Makefile +++ b/Makefile @@ -130,7 +130,7 @@ TXTFILES= CC-BY-SA-3.0 CC-BY-SA-4.0 FILEASSETS = $(addprefix templates/, libre shapelib.html script.js \ $(LITFILES) $(TXTFILES)) \ $(wildcard templates/*.tera) \ - nwtemplates/die-cooldown.tera + nwtemplates/die-overlay.tera WASM := wasm32-unknown-unknown # ^ todo: Is this still right after @@ -371,7 +371,7 @@ templates/shapelib.html: $(TARGET_DIR)/debug/otterlib $(LIBRARY_FILES) --libs '$(addprefix $(PWD)/, $(addsuffix .toml, $(LIBRARIES)))' \ preview >$@.tmp && mv -f $@.tmp $@ -nwtemplates/die-cooldown.tera: dice/cooldown-template-extractor dice/die.svg +nwtemplates/die-overlay.tera: dice/overlay-template-extractor dice/die.svg ./$< $@.tmp && mv -f $@.tmp $@ #---------- examples ---------- diff --git a/dice/cooldown-template-extractor b/dice/overlay-template-extractor similarity index 94% rename from dice/cooldown-template-extractor rename to dice/overlay-template-extractor index 2179a1e1..3ca02b8f 100755 --- a/dice/cooldown-template-extractor +++ b/dice/overlay-template-extractor @@ -24,7 +24,10 @@ sub process_node ($$) { die "$label ?" unless @$nodes == 1; $node = $nodes->[0]; $fn->($node); - print $node->toString, "\n" or die $!; + print + "{% if cooldown_active %}", + $node->toString, + "{% endif %}\n" or die $!; } our $nom_radius; diff --git a/src/dice.rs b/src/dice.rs index f9f6da79..a368fdf3 100644 --- a/src/dice.rs +++ b/src/dice.rs @@ -75,7 +75,8 @@ impl PieceXData for State { } #[derive(Serialize, Debug)] -struct CooldownTemplateContext<'c> { +struct OverlayTemplateContext<'c> { + cooldown_active: bool, radius: f64, remprop: f64, path_d: &'c str, @@ -342,23 +343,28 @@ impl InertPieceTrait for Die { self.image.svg(f, vpid, face, xdata)?; let remprop = self.cooldown_remprop(state)?; - if remprop != 0. { + let cooldown_active = remprop != 0.; + + let (path_d, cd_elid) = if cooldown_active { let mut path_d = String::new(); die_cooldown_path(&mut path_d, self.cooldown_radius, remprop)?; - let cd_elid = format!("def.{}.die.cd", vpid); - - let tc = CooldownTemplateContext { - radius: self.cooldown_radius, - path_d: &path_d, - cd_elid: &cd_elid, - total_ms: self.cooldown_time.as_secs_f64() * 1000., - remprop, - }; - - write!(f.as_html_string_mut(), "{}", - nwtemplates::render("die-cooldown.tera", &tc)?)?; - } + (path_d, format!("def.{}.die.cd", vpid)) + } else { + default() + }; + + let tc = OverlayTemplateContext { + cooldown_active, + radius: self.cooldown_radius, + path_d: &path_d, + cd_elid: &cd_elid, + total_ms: self.cooldown_time.as_secs_f64() * 1000., + remprop, + }; + + write!(f.as_html_string_mut(), "{}", + nwtemplates::render("die-overlay.tera", &tc)?)?; } // add throw operation -- 2.30.2