LoveFrames/objects/progressbar.lua

355 lines
8.1 KiB
Lua
Raw Normal View History

2012-05-06 00:24:42 +00:00
--[[------------------------------------------------
-- Love Frames - A GUI library for LOVE --
2014-01-14 11:29:11 +00:00
-- Copyright (c) 2012-2014 Kenny Shields --
2012-05-06 00:24:42 +00:00
--]]------------------------------------------------
-- get the current require path
local path = string.sub(..., 1, string.len(...) - string.len(".objects.progressbar"))
local loveframes = require(path .. ".libraries.common")
2013-11-25 13:06:41 +00:00
-- progressbar object
local newobject = loveframes.NewObject("progressbar", "loveframes_object_progressbar", true)
2012-05-06 00:24:42 +00:00
--[[---------------------------------------------------------
- func: initialize()
- desc: initializes the object
--]]---------------------------------------------------------
function newobject:initialize()
2012-05-06 00:24:42 +00:00
self.type = "progressbar"
self.text = ""
self.width = 100
self.height = 25
self.min = 0
self.max = 10
self.value = 0
self.barwidth = 0
self.lerprate = 1000
self.lerpvalue = 0
self.lerpto = 0
self.lerpfrom = 0
self.completed = false
self.lerp = false
self.internal = false
self.OnComplete = nil
2012-05-06 00:24:42 +00:00
end
--[[---------------------------------------------------------
- func: update(deltatime)
- desc: updates the object
--]]---------------------------------------------------------
function newobject:update(dt)
2012-05-06 00:24:42 +00:00
local state = loveframes.state
local selfstate = self.state
if state ~= selfstate then
return
end
local visible = self.visible
local alwaysupdate = self.alwaysupdate
if not visible then
if not alwaysupdate then
2012-05-06 00:24:42 +00:00
return
end
end
local lerp = self.lerp
local lerprate = self.lerprate
local lerpvalue = self.lerpvalue
local lerpto = self.lerpto
local lerpfrom = self.lerpfrom
local value = self.value
local completed = self.completed
local parent = self.parent
local base = loveframes.base
local update = self.Update
local oncomplete = self.OnComplete
2012-05-06 00:24:42 +00:00
self:CheckHover()
-- caclulate barwidth
if lerp then
2012-05-06 00:24:42 +00:00
if lerpfrom < lerpto then
if lerpvalue < lerpto then
self.lerpvalue = lerpvalue + lerprate*dt
elseif lerpvalue > lerpto then
self.lerpvalue = lerpto
end
elseif lerpfrom > lerpto then
if lerpvalue > lerpto then
self.lerpvalue = lerpvalue - lerprate*dt
elseif lerpvalue < lerpto then
self.lerpvalue = lerpto
end
elseif lerpfrom == lerpto then
self.lerpvalue = lerpto
end
self.barwidth = self.lerpvalue/self.max * self.width
2012-05-06 00:24:42 +00:00
-- min check
if self.lerpvalue < self.min then
self.lerpvalue = self.min
end
-- max check
if self.lerpvalue > self.max then
self.lerpvalue = self.max
end
else
self.barwidth = value/self.max * self.width
-- min max check
if value < self.min then
2012-05-06 00:24:42 +00:00
self.value = self.min
elseif value > self.max then
2012-05-06 00:24:42 +00:00
self.value = self.max
end
end
-- move to parent if there is a parent
if parent ~= base then
2012-05-06 00:24:42 +00:00
self.x = self.parent.x + self.staticx
self.y = self.parent.y + self.staticy
end
-- completion check
if not completed then
2012-05-06 00:24:42 +00:00
if self.value >= self.max then
self.completed = true
if oncomplete then
oncomplete(self)
2012-05-06 00:24:42 +00:00
end
end
end
if update then
update(self, dt)
2012-05-06 00:24:42 +00:00
end
end
--[[---------------------------------------------------------
- func: draw()
- desc: draws the object
--]]---------------------------------------------------------
function newobject:draw()
2012-05-06 00:24:42 +00:00
local state = loveframes.state
local selfstate = self.state
if state ~= selfstate then
return
end
local visible = self.visible
if not visible then
2012-05-06 00:24:42 +00:00
return
end
local skins = loveframes.skins.available
local skinindex = loveframes.config["ACTIVESKIN"]
local defaultskin = loveframes.config["DEFAULTSKIN"]
local selfskin = self.skin
local skin = skins[selfskin] or skins[skinindex]
local drawfunc = skin.DrawProgressBar or skins[defaultskin].DrawProgressBar
local draw = self.Draw
local drawcount = loveframes.drawcount
2012-05-06 00:24:42 +00:00
-- set the object's draw order
self:SetDrawOrder()
2012-05-06 00:24:42 +00:00
if draw then
draw(self)
2012-05-06 00:24:42 +00:00
else
drawfunc(self)
2012-05-06 00:24:42 +00:00
end
end
--[[---------------------------------------------------------
- func: SetMax(max)
- desc: sets the object's maximum value
--]]---------------------------------------------------------
function newobject:SetMax(max)
2012-05-06 00:24:42 +00:00
self.max = max
2014-02-11 12:34:07 +00:00
return self
2012-05-06 00:24:42 +00:00
end
--[[---------------------------------------------------------
- func: GetMax()
- desc: gets the object's maximum value
--]]---------------------------------------------------------
function newobject:GetMax()
2012-05-06 00:24:42 +00:00
return self.max
end
--[[---------------------------------------------------------
- func: SetMin(min)
- desc: sets the object's minimum value
--]]---------------------------------------------------------
function newobject:SetMin(min)
2012-05-06 00:24:42 +00:00
self.min = min
2014-02-11 12:34:07 +00:00
return self
2012-05-06 00:24:42 +00:00
end
--[[---------------------------------------------------------
- func: GetMin()
- desc: gets the object's minimum value
--]]---------------------------------------------------------
function newobject:GetMin()
2012-05-06 00:24:42 +00:00
return self.min
end
--[[---------------------------------------------------------
- func: SetMinMax()
- desc: sets the object's minimum and maximum values
--]]---------------------------------------------------------
function newobject:SetMinMax(min, max)
2012-05-06 00:24:42 +00:00
self.min = min
self.max = max
2014-02-11 12:34:07 +00:00
return self
2012-05-06 00:24:42 +00:00
end
--[[---------------------------------------------------------
- func: GetMinMax()
- desc: gets the object's minimum and maximum values
--]]---------------------------------------------------------
function newobject:GetMinMax()
2012-05-06 00:24:42 +00:00
return self.min, self.max
end
--[[---------------------------------------------------------
- func: SetValue(value)
- desc: sets the object's value
--]]---------------------------------------------------------
function newobject:SetValue(value)
2012-05-06 00:24:42 +00:00
local lerp = self.lerp
if lerp then
2012-05-06 00:24:42 +00:00
self.lerpvalue = self.lerpvalue
self.lerpto = value
self.lerpfrom = self.lerpvalue
self.value = value
else
self.value = value
end
2014-02-11 12:34:07 +00:00
return self
2012-05-06 00:24:42 +00:00
end
--[[---------------------------------------------------------
- func: GetValue()
- desc: gets the object's value
--]]---------------------------------------------------------
function newobject:GetValue()
2012-05-06 00:24:42 +00:00
return self.value
end
--[[---------------------------------------------------------
- func: SetLerp(bool)
- desc: sets whether or not the object should lerp
when changing between values
--]]---------------------------------------------------------
function newobject:SetLerp(bool)
2012-05-06 00:24:42 +00:00
self.lerp = bool
self.lerpto = self:GetValue()
self.lerpvalue = self:GetValue()
2014-02-11 12:34:07 +00:00
return self
2012-05-06 00:24:42 +00:00
end
--[[---------------------------------------------------------
- func: GetLerp()
- desc: gets whether or not the object should lerp
when changing between values
--]]---------------------------------------------------------
function newobject:GetLerp()
2012-05-06 00:24:42 +00:00
return self.lerp
end
--[[---------------------------------------------------------
- func: SetLerpRate(rate)
- desc: sets the object's lerp rate
--]]---------------------------------------------------------
function newobject:SetLerpRate(rate)
2012-05-06 00:24:42 +00:00
self.lerprate = rate
2014-02-11 12:34:07 +00:00
return self
2012-05-06 00:24:42 +00:00
end
--[[---------------------------------------------------------
- func: GetLerpRate()
- desc: gets the object's lerp rate
--]]---------------------------------------------------------
function newobject:GetLerpRate()
2012-05-06 00:24:42 +00:00
return self.lerprate
end
--[[---------------------------------------------------------
- func: GetCompleted()
- desc: gets whether or not the object has reached its
maximum value
--]]---------------------------------------------------------
function newobject:GetCompleted()
return self.completed
end
--[[---------------------------------------------------------
- func: GetBarWidth()
- desc: gets the object's bar width
--]]---------------------------------------------------------
function newobject:GetBarWidth()
return self.barwidth
end
--[[---------------------------------------------------------
- func: SetText(text)
- desc: sets the object's text
--]]---------------------------------------------------------
function newobject:SetText(text)
self.text = text
2014-02-11 12:34:07 +00:00
return self
end
--[[---------------------------------------------------------
- func: GetText()
- desc: gets the object's text
--]]---------------------------------------------------------
function newobject:GetText()
return self.text
2014-11-09 21:24:10 +00:00
end