start/stop animations with show/hide and pass show/hide down children

This commit is contained in:
Mikayla Fischler 2022-09-10 15:14:48 -04:00
parent dcf275784c
commit 98c826e762
2 changed files with 36 additions and 3 deletions

View File

@ -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

View File

@ -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