Skip to content

Instantly share code, notes, and snippets.

@blam23
Created July 21, 2013 19:36
Show Gist options
  • Select an option

  • Save blam23/6049671 to your computer and use it in GitHub Desktop.

Select an option

Save blam23/6049671 to your computer and use it in GitHub Desktop.
A little and basic animations library and example usage.
-- (c) 2013 Blam
-- Animations.lua
animation = { }
animation.__index = animation
animation.all = { }
function animation.delay(frames)
local a = animation:new{
a = 0,
b = 0,
func = function(a, b, i) return 1 end,
frameStep = 1/frames,
}
return a
end
function animation.createLinear(startValue, endValue, frames)
local a = animation:new{
a = startValue,
b = endValue,
func = animation.linear,
frameStep = 1/frames,
}
return a
end
function animation.createCosine(startValue, endValue, frames)
local a = animation:new{
a = startValue,
b = endValue,
func = animation.cosine,
frameStep = 1/frames,
}
return a
end
function animation.linear(a, b, i)
return (a*(1-i)+b*i)
end
function animation.cosine(a, b, i)
local i2 = (1-math.cos(i*math.pi))/2
return animation.linear(a,b,i2)
end
function animation:new(base)
local anim = base
anim.i = 0
setmetatable(anim, animation)
return anim
end
function animation:start()
table.insert(animation.all, self)
return self
end
function animation:step()
self.i = self.i + self.frameStep
local v = self.func(self.a, self.b, self.i)
return v
end
animationController = { }
animationController.__index = animationController
function animationController:new()
local ac = { items = { }, object = nil }
setmetatable(ac, animationController)
return ac
end
function animationController:addAnimation(value, a)
table.insert(self.items, {value, a})
end
function animationController:stop(a)
for i = 1,#self.items do self.items[i] = nil end
self.items = { }
end
function animationController:update()
for i = 1,#self.items do
local a = self.items[i][2]
local v = a:step()
if(self.items[i][1] ~= nil) then
self.object[self.items[i][1]] = v
end
if(a.i >= 1) then
if(a.onEnd ~= nil) then a.onEnd(a) end
table.remove(self.items,i)
end
end
end
function animationController:attach(object)
object["__animC"] = self
object["addAnimation"] = function(v, a) animationController.addAnimation(self, v, a) end
object["stopAnimation"] = function() animationController.stop(self) end
object["stepAnimation"] = function() animationController.update(self) end
self.object = object
return self
end
-- EXAMPLE OF USAGE
gui = { }
gui.bar = { X = 0, Y = 0, width = -1, height = 20, hover = false}
gui.bar.Y = -gui.bar.height
gui.bar.__index = gui.bar
animationController:new():attach(gui.bar)
function gui.bar:show()
self:stopAnimation()
local sy = self.Y
anim = animation.createCosine(sy, 0, 15)
self.addAnimation("Y", anim)
end
function gui.bar:hide()
self:stopAnimation()
local sy = self.Y
delay = animation.delay(15)
delay.onEnd = function(a)
a2 = animation.createCosine(sy, -self.height, 15)
self.addAnimation("Y", a2)
end
self.addAnimation(nil, delay)
end
function gui.bar:update()
self:stepAnimation()
self.opacity = (self.Y+(self.height))/(self.height)
if(self.width == -1) then
self.width, h = get_window_size()
end
end
function gui.bar:draw()
set_color(0,0,0,self.opacity/2)
draw_quad(self.X, self.Y+1, self.width, self.height)
set_color(1,0,0,self.opacity/2)
draw_quad(self.X, self.Y, self.width, self.height)
set_color(1,1,1,1)
draw_text("Test Application 01", self.X+5, self.Y+1, 1)
end
add_hook("draw2d", "drawconsole", function()
gui.bar:update()
gui.bar:draw()
end)
add_hook("mouse_move", "showconsole", function(x,y)
if(y <= gui.bar.height) then
if(not gui.bar.hover) then
gui.bar:show()
gui.bar.hover = true
end
else
if(gui.bar.hover) then
gui.bar:hide()
gui.bar.hover = false
end
end
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment