From 98c826e762b13c862d58e05442232542ac9d34bf Mon Sep 17 00:00:00 2001 From: Mikayla Fischler Date: Sat, 10 Sep 2022 15:14:48 -0400 Subject: [PATCH] start/stop animations with show/hide and pass show/hide down children --- graphics/element.lua | 18 ++++++++++++++++++ graphics/elements/animations/waiting.lua | 21 ++++++++++++++++++--- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/graphics/element.lua b/graphics/element.lua index bad2a80..6ed3434 100644 --- a/graphics/element.lua +++ b/graphics/element.lua @@ -141,6 +141,14 @@ function element.new(args) return nil end + -- start animations + function protected.start_anim() + end + + -- stop animations + function protected.stop_anim() + end + -- get public interface ---@return graphics_element element, element_id id function protected.get() return public, self.id end @@ -272,10 +280,20 @@ function element.new(args) -- show the element function public.show() protected.window.setVisible(true) + protected.start_anim() + + for i = 1, #self.children do + self.children[i].show() + end end -- hide the element function public.hide() + protected.stop_anim() + for i = 1, #self.children do + self.children[i].hide() + end + protected.window.setVisible(false) end diff --git a/graphics/elements/animations/waiting.lua b/graphics/elements/animations/waiting.lua index 031b0da..5c03d09 100644 --- a/graphics/elements/animations/waiting.lua +++ b/graphics/elements/animations/waiting.lua @@ -16,6 +16,7 @@ local element = require("graphics.element") ---@return graphics_element element, element_id id local function waiting(args) local state = 0 + local run_animation = false args.width = 4 args.height = 3 @@ -28,7 +29,8 @@ local function waiting(args) local blit_fg_2x = e.fg_bg.blit_fgd .. e.fg_bg.blit_fgd local blit_bg_2x = e.fg_bg.blit_bkg .. e.fg_bg.blit_bkg - local function update() + -- tick the animation + local function animate() e.window.clear() if state >= 0 and state < 7 then @@ -82,10 +84,23 @@ local function waiting(args) state = state + 1 if state >= 12 then state = 0 end - tcd.dispatch(0.5, update) + if run_animation then + tcd.dispatch(0.5, animate) + end end - update() + -- start the animation + function e.start_anim() + run_animation = true + animate() + end + + -- stop the animation + function e.stop_anim() + run_animation = false + end + + e.start_anim() return e.get() end