diff options
| -rw-r--r-- | html/examples/animate_gif.wlua | 86 | 
1 files changed, 56 insertions, 30 deletions
| diff --git a/html/examples/animate_gif.wlua b/html/examples/animate_gif.wlua index 4334f37..04d69e0 100644 --- a/html/examples/animate_gif.wlua +++ b/html/examples/animate_gif.wlua @@ -7,8 +7,7 @@ require"iuplua"  require"iupluacd"  require"iupluaimglib" -images={} -frame=1 +anim={}  function print_error(err)    local msg = {} @@ -34,30 +33,55 @@ function load_frames(file_name)        return    end -  images={} -  images.delays={} -  images.disposal={} -  frame=1 +  anim.images={} +  anim.delays={} +  anim.disposal={} +  anim.pos={} +  anim.frame=1 +   +  local ScreenWidth = 0  +  local ScreenHeight = 0    for i=1, select(3,ifile:GetInfo()) do -    images[i]=ifile:LoadBitmap(i-1) -    err, images[i] = im.ConvertColorSpaceNew(images[i], im.RGB, true) -    local delay=images[i]:GetAttribute("Delay") -- time to wait betweed frames in 1/100 of a second] +   +    anim.images[i]=ifile:LoadBitmap(i-1) +    err, anim.images[i] = im.ConvertColorSpaceNew(anim.images[i], im.RGB, true) +     +    local delay=anim.images[i]:GetAttribute("Delay") -- time to wait betweed frames in 1/100 of a second]      if delay then -      images.delays[i]=delay[1]*10 -- timer in miliseconds +      anim.delays[i]=delay[1]*10 -- timer in miliseconds      else        if (i == 1) then  -        images.delays[i]=100  +        anim.delays[i]=100         else -        images.delays[i]=images.delays[i-1] +        anim.delays[i]=anim.delays[i-1]        end      end -    images.disposal[i]=images[i]:GetAttribute("Disposal", true) --  [UNDEF, LEAVE, RBACK, RPREV] +     +    anim.disposal[i]=anim.images[i]:GetAttribute("Disposal", true) --  [UNDEF, LEAVE, RBACK, RPREV] +     +    local w = anim.images[i]:Width() +    local h = anim.images[i]:Height() +    if (w > ScreenWidth) then ScreenWidth = w end +    if (h > ScreenHeight) then ScreenHeight = h end +    w = anim.images[i]:GetAttribute("ScreenWidth") +    h = anim.images[i]:GetAttribute("ScreenHeight") +    if (w and w[1] > ScreenWidth) then ScreenWidth = w[1] end +    if (h and h[1] > ScreenHeight) then ScreenHeight = h[1] end +     +    local X = anim.images[i]:GetAttribute("XScreen") +    local Y = anim.images[i]:GetAttribute("YScreen") +    anim.pos[i] = {x=0, y=0} +    if (X) then anim.pos[i].x = X[1] end +    if (Y) then anim.pos[i].y = Y[1] end    end    ifile:Close() -  cnv.rastersize = images[1]:Width().."x"..images[1]:Height() +  anim.ScreenHeight = ScreenHeight +  anim.ScreenWidth = ScreenWidth +   +  cnv.rastersize = ScreenWidth.."x"..ScreenHeight    dlg.size=nil    iup.Refresh(cnv)  end @@ -68,33 +92,33 @@ function start_timer()    dlg.title = "Animated Gif"    dlg.play_bt.image="IUP_MediaPause" dlg.play_bt.title="Pause"    t.run = "NO" -  t.time = images.delays[frame] +  t.time = anim.delays[anim.frame]    t.run = "YES"    iup.Update(cnv)  end  function stop_timer() -  dlg.title = "Animated Gif "..frame.."/"..#images +  dlg.title = "Animated Gif "..anim.frame.."/"..#anim    dlg.play_bt.image="IUP_MediaPlay" dlg.play_bt.title="Play"     t.run="NO"    iup.Update(cnv)  end  function set_frame(f) -  frame = f -  if frame > #images then -    frame = #images +  anim.frame = f +  if anim.frame > #anim.images then +    anim.frame = #anim.images    end -  if frame < 1 then -    frame = 1 +  if anim.frame < 1 then +    anim.frame = 1    end    stop_timer()  end  function t:action_cb() -  frame = frame + 1 -  if frame == #images+1 then -    frame = 1 +  anim.frame = anim.frame + 1 +  if anim.frame == #anim.images+1 then +    anim.frame = 1    end    start_timer() @@ -111,10 +135,12 @@ end  function cnv:action()-- called everytime the IUP canvas needs to be repainted    canvas:Activate() -  if (images.disposal[frame] == "RBACK") then +  if (anim.disposal[anim.frame] == "RBACK") then      canvas:Clear()    end -  images[frame]:cdCanvasPutImageRect(canvas, 0, 0, 0, 0, 0, 0, 0, 0) -- use default values +  local x = anim.pos[anim.frame].x +  local y = anim.ScreenHeight - anim.pos[anim.frame].y - anim.images[anim.frame]:Height() +  anim.images[anim.frame]:cdCanvasPutImageRect(canvas, x, y, 0, 0, 0, 0, 0, 0) -- use default values  end  function cnv:resize_cb() @@ -138,17 +164,17 @@ end  buts = iup.hbox{    iup.button{title="First", image="IUP_MediaGotoBegin", action=function(self) set_frame(1) end},  -  iup.button{title="Previous", image="IUP_MediaRewind", action=function(self) set_frame(frame-1) end},  +  iup.button{title="Previous", image="IUP_MediaRewind", action=function(self) set_frame(anim.frame-1) end},     iup.button{title="Pause", image="IUP_MediaPause", action=function(self) if (t.run=="YES") then stop_timer() else start_timer() end end},  -  iup.button{title="Next", image="IUP_MediaForward", action=function(self) set_frame(frame+1) end},  -  iup.button{title="Last", image="IUP_MediaGoToEnd", action=function(self) set_frame(#images) end},  +  iup.button{title="Next", image="IUP_MediaForward", action=function(self) set_frame(anim.frame+1) end},  +  iup.button{title="Last", image="IUP_MediaGoToEnd", action=function(self) set_frame(#anim) end},     }  dlg = iup.dialog{iup.vbox{cnv, buts},title="Animated Gif", margin="5x5", gap=10}  dlg.play_bt = dlg[1][2][3]  function dlg:close_cb()    iup.Destroy(t) -  images=nil --Destroys will be called by the garbage collector +  anim=nil --Destroys will be called by the garbage collector    canvas:Kill()    self:destroy()    return iup.IGNORE -- because we destroy the dialog | 
