So im making a small gui library for myself which as of now only has buttons. But i cant get multiple animations to update correctly. If theres one animation/button then it works but if theres 2 they just hang at the starting frame. Im including my code.
gui.lua ( the library )
Code: Select all
local anim8 = require "anim8"
local gui = {}
local pi = math.pi
local button = {}
local buttonmt = { __index = button }
--Helper functions
local function drawImageorAnimation(image,x,y,r,sx,sy,ox,oy,kx,ky,animation)
if type(animation) == "table" then animation:draw(image,x,y,r,sx,sy,ox,oy,kx,ky)
else love.graphics.draw(image,x,y,r,sx,sy,ox,oy,kx,ky) end
end
----------------------------------------------------------------------------------------------------------------------------------
function newButtonSystem(center,edge,corner,centerON,edgeON,cornerON) --If theres an animation include in a table with the according image
local t = {}
for i=1, 6 do
t[i] = 0
end
t[1] = center
t[2] = edge
t[3] = corner
t[4] = centerON
t[5] = edgeON
t[6] = cornerON
local temp = {}
for k=1,6 do
if type(t[k]) == "table" then
for i, v in pairs(t[k]) do
if type(v) == "table" then t[k+6] = v
else temp[k] = v end
end
end
end
for i,v in pairs(temp) do
t[i] = v
end
return t
end
function newButton(text,x,y,w,h,ox,oy,funct,system)
local yc = math.ceil((h-18)/9)
local xc = math.ceil((w-18)/9)
for i=7, 12 do
if system[i] then
system[i] = system[i]:clone()
end
end
return setmetatable({text=text,x=x,y=y,w=w,h=h,ox=ox,oy=oy,yc=yc,xc=xc,hover=false,click=funct,system=system},buttonmt)
end
function button:update(dt)
for i = 7, 12 do
if self["system"][i] then
self["system"][i]:update(dt)
if self.hover then
self["system"][i]:resume()
else
self["system"][i]:pauseAtStart()
end
end
end
if checkCoordinBox( love.mouse.getX() , love.mouse.getY() , self.x-self.ox , self.y-self.oy , self.w , self.h ) then self.hover = true else self.hover = false end
end
function button:mousepressed(x,y)
if checkCoordinBox( x , y , self.x-self.ox , self.y-self.oy , self.w , self.h ) then self.click() end
end
function button:draw()
love.graphics.push()
love.graphics.translate(-self.ox,-self.oy)
if self.hover then
self:drawActive()
else
self:drawDeactive()
end
self:drawText()
love.graphics.pop()
end
function button:drawText()
love.graphics.printf(self.text,self.x+self.w/2,self.y+self.h/2,self.w/2,"left",0,1,1,font:getWidth(self.text)/2,font:getHeight()/2)
end
function button:drawActive()
for i=1,self.yc,1 do
for j=1,self.yc,1 do
drawImageorAnimation(self['system'][4],self.x+9*j,self.y+9*i,0,sx,sy,ox,oy,kx,ky,self['system'][10])
drawImageorAnimation(self['system'][5],self.x+9*j+9,self.y,0.5*pi,sx,sy,ox,oy,kx,ky,self['system'][11])
drawImageorAnimation(self['system'][5],self.x+9*j,self.y+self.h,-0.5*pi,sx,sy,ox,oy,kx,ky,self['system'][11])
end
drawImageorAnimation(self['system'][5],self.x,self.y+9*i,0,sx,sy,ox,oy,kx,ky,self['system'][11])
drawImageorAnimation(self['system'][5],self.x+self.w,self.y+9*i+9,pi,sx,sy,ox,oy,kx,ky,self['system'][11])
drawImageorAnimation(self['system'][6],self.x,self.y,0,sx,sy,ox,oy,kx,ky,self['system'][12])
drawImageorAnimation(self['system'][6],self.x+self.w,self.y,0.5*pi,sx,sy,ox,oy,kx,ky,self['system'][12])
drawImageorAnimation(self['system'][6],self.x,self.y+self.h,-0.5*pi,sx,sy,ox,oy,kx,ky,self['system'][12])
drawImageorAnimation(self['system'][6],self.x+self.w,self.y+self.h,pi,sx,sy,ox,oy,kx,ky,self['system'][12])
end
end
function button:drawDeactive()
for i=1,self.yc,1 do
for j=1,self.xc,1 do
drawImageorAnimation(self['system'][1],self.x+9*j,self.y+9*i,0,sx,sy,ox,oy,kx,ky,self['system'][7])
drawImageorAnimation(self['system'][2],self.x+9*j+9,self.y,0.5*pi,sx,sy,ox,oy,kx,ky,self['system'][8])
drawImageorAnimation(self['system'][2],self.x+9*j,self.y+self.h,-0.5*pi,sx,sy,ox,oy,kx,ky,self['system'][8])
end
drawImageorAnimation(self['system'][2],self.x,self.y+9*i,0,sx,sy,ox,oy,kx,ky,self['system'][8])
drawImageorAnimation(self['system'][2],self.x+self.w,self.y+9*i+9,pi,sx,sy,ox,oy,kx,ky,self['system'][8])
drawImageorAnimation(self['system'][3],self.x,self.y,0,sx,sy,ox,oy,kx,ky,self['system'][9])
drawImageorAnimation(self['system'][3],self.x+self.w,self.y,0.5*pi,sx,sy,ox,oy,kx,ky,self['system'][9])
drawImageorAnimation(self['system'][3],self.x,self.y+self.h,-0.5*pi,sx,sy,ox,oy,kx,ky,self['system'][9])
drawImageorAnimation(self['system'][3],self.x+self.w,self.y+self.h,pi,sx,sy,ox,oy,kx,ky,self['system'][9])
end
end
return gui
ATM My library assumes that all the images are 9x9(expect the animation spritesheets of course)