LoveFrames/objects/list.lua

603 lines
14 KiB
Lua
Raw Normal View History

2012-05-06 00:24:42 +00:00
--[[------------------------------------------------
-- Love Frames - A GUI library for LOVE --
-- Copyright (c) 2012 Kenny Shields --
2012-05-06 00:24:42 +00:00
--]]------------------------------------------------
-- list class
local newobject = loveframes.NewObject("list", "loveframes_object_list", 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 = "list"
self.display = "vertical"
self.width = 300
self.height = 150
self.clickx = 0
self.clicky = 0
self.padding = 0
self.spacing = 0
self.offsety = 0
self.offsetx = 0
self.extrawidth = 0
self.extraheight = 0
self.buttonscrollamount = 0.10
self.mousewheelscrollamount = 5
self.internal = false
self.hbar = false
self.vbar = false
self.autoscroll = false
self.internals = {}
self.children = {}
self.OnScroll = 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 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 internals = self.internals
local children = self.children
local display = self.display
local parent = self.parent
local base = loveframes.base
local update = self.Update
2012-05-06 00:24:42 +00:00
-- 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
self:CheckHover()
for k, v in ipairs(internals) do
2012-05-06 00:24:42 +00:00
v:update(dt)
end
for k, v in ipairs(children) do
2012-05-06 00:24:42 +00:00
v:update(dt)
v:SetClickBounds(self.x, self.y, self.width, self.height)
v.y = (v.parent.y + v.staticy) - self.offsety
v.x = (v.parent.x + v.staticx) - self.offsetx
if display == "vertical" then
2012-05-06 00:24:42 +00:00
if v.lastheight ~= v.height then
self:CalculateSize()
self:RedoLayout()
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 visible = self.visible
if not visible then
2012-05-06 00:24:42 +00:00
return
end
local internals = self.internals
local children = self.children
local stencilfunc = function() love.graphics.rectangle("fill", self.x, self.y, self.width, self.height) end
local stencil = love.graphics.newStencil(stencilfunc)
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.DrawList or skins[defaultskin].DrawList
local drawoverfunc = skin.DrawOverList or skins[defaultskin].DrawOverList
local draw = self.Draw
local drawcount = loveframes.drawcount
-- 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
love.graphics.setStencil(stencil)
for k, v in ipairs(children) do
2012-05-06 00:24:42 +00:00
local col = loveframes.util.BoundingBox(self.x, v.x, self.y, v.y, self.width, v.width, self.height, v.height)
if col == true then
v:draw()
end
end
love.graphics.setStencil()
for k, v in ipairs(internals) do
2012-05-06 00:24:42 +00:00
v:draw()
end
if not draw then
drawoverfunc(self)
2012-05-06 00:24:42 +00:00
end
end
--[[---------------------------------------------------------
- func: mousepressed(x, y, button)
- desc: called when the player presses a mouse button
--]]---------------------------------------------------------
function newobject:mousepressed(x, y, button)
2012-05-06 00:24:42 +00:00
local visible = self.visible
if not visible then
2012-05-06 00:24:42 +00:00
return
end
local toplist = self:IsTopList()
local hover = self.hover
local vbar = self.vbar
local hbar = self.hbar
local scrollamount = self.mousewheelscrollamount
local children = self.children
local internals = self.internals
2012-05-06 00:24:42 +00:00
if hover and button == "l" then
2012-05-06 00:24:42 +00:00
local baseparent = self:GetBaseParent()
if baseparent and baseparent.type == "frame" then
baseparent:MakeTop()
end
end
if vbar or hbar then
2012-05-06 00:24:42 +00:00
if toplist then
2012-05-06 00:24:42 +00:00
local bar = self:GetScrollBar()
if button == "wu" then
bar:Scroll(-scrollamount)
2012-05-06 00:24:42 +00:00
elseif button == "wd" then
bar:Scroll(scrollamount)
2012-05-06 00:24:42 +00:00
end
end
end
for k, v in ipairs(internals) do
2012-05-06 00:24:42 +00:00
v:mousepressed(x, y, button)
end
for k, v in ipairs(children) do
2012-05-06 00:24:42 +00:00
v:mousepressed(x, y, button)
end
end
--[[---------------------------------------------------------
- func: AddItem(object)
- desc: adds an item to the object
--]]---------------------------------------------------------
function newobject:AddItem(object)
2012-05-06 00:24:42 +00:00
if object.type == "frame" then
return
end
local children = self.children
2012-05-06 00:24:42 +00:00
-- remove the item object from it's current parent and make it's new parent the list object
2012-05-06 00:24:42 +00:00
object:Remove()
object.parent = self
-- insert the item object into the list object's children table
table.insert(children, object)
2012-05-06 00:24:42 +00:00
-- resize the list and redo it's layout
2012-05-06 00:24:42 +00:00
self:CalculateSize()
self:RedoLayout()
end
--[[---------------------------------------------------------
- func: RemoveItem(object)
- desc: removes an item from the object
--]]---------------------------------------------------------
function newobject:RemoveItem(object)
2012-05-06 00:24:42 +00:00
object:Remove()
self:CalculateSize()
self:RedoLayout()
end
--[[---------------------------------------------------------
- func: CalculateSize()
- desc: calculates the size of the object's children
--]]---------------------------------------------------------
function newobject:CalculateSize()
2012-05-06 00:24:42 +00:00
local numitems = #self.children
local height = self.height
local width = self.width
local padding = self.padding
local spacing = self.spacing
local itemheight = self.padding
local itemwidth = self.padding
local display = self.display
local vbar = self.vbar
local hbar = self.hbar
local internals = self.internals
local children = self.children
2012-05-06 00:24:42 +00:00
if display == "vertical" then
for k, v in ipairs(self.children) do
itemheight = itemheight + v.height + spacing
end
self.itemheight = (itemheight - spacing) + padding
local itemheight = self.itemheight
if itemheight > height then
2012-05-06 00:24:42 +00:00
self.extraheight = itemheight - height
2012-05-06 00:24:42 +00:00
if not vbar then
local scrollbar = loveframes.objects["scrollbody"]:new(self, display)
table.insert(internals, scrollbar)
2012-05-06 00:24:42 +00:00
self.vbar = true
self:GetScrollBar().autoscroll = self.autoscroll
2012-05-06 00:24:42 +00:00
end
else
if vbar then
local bar = internals[1]
bar:Remove()
2012-05-06 00:24:42 +00:00
self.vbar = false
self.offsety = 0
end
end
elseif display == "horizontal" then
for k, v in ipairs(children) do
2012-05-06 00:24:42 +00:00
itemwidth = itemwidth + v.width + spacing
end
self.itemwidth = (itemwidth - spacing) + padding
local itemwidth = self.itemwidth
if itemwidth > width then
2012-05-06 00:24:42 +00:00
self.extrawidth = itemwidth - width
2012-05-06 00:24:42 +00:00
if not hbar then
local scrollbar = loveframes.objects["scrollbody"]:new(self, display)
table.insert(internals, scrollbar)
2012-05-06 00:24:42 +00:00
self.hbar = true
self:GetScrollBar().autoscroll = self.autoscroll
2012-05-06 00:24:42 +00:00
end
else
if hbar then
local bar = internals[1]
bar:Remove()
2012-05-06 00:24:42 +00:00
self.hbar = false
self.offsetx = 0
end
end
end
end
--[[---------------------------------------------------------
- func: RedoLayout()
- desc: used to redo the layour of the object
--]]---------------------------------------------------------
function newobject:RedoLayout()
2012-05-06 00:24:42 +00:00
local children = self.children
local padding = self.padding
local spacing = self.spacing
local starty = padding
local startx = padding
local vbar = self.vbar
local hbar = self.hbar
local display = self.display
2012-05-06 00:24:42 +00:00
if #children > 0 then
for k, v in ipairs(children) do
if display == "vertical" then
local height = v.height
v.staticx = padding
v.staticy = starty
v.lastheight = v.height
if vbar then
2012-05-06 00:24:42 +00:00
if v.width + padding > (self.width - self.internals[1].width) then
v:SetWidth((self.width - self.internals[1].width) - (padding*2))
2012-05-06 00:24:42 +00:00
end
if not v.retainsize then
v:SetWidth((self.width - self.internals[1].width) - (padding*2))
2012-05-06 00:24:42 +00:00
end
self.internals[1].staticx = self.width - self.internals[1].width
self.internals[1].height = self.height
else
if not v.retainsize then
v:SetWidth(self.width - (padding*2))
2012-05-06 00:24:42 +00:00
end
end
starty = starty + v.height
starty = starty + spacing
elseif display == "horizontal" then
v.staticx = startx
v.staticy = padding
if hbar then
2012-05-06 00:24:42 +00:00
if v.height + padding > (self.height - self.internals[1].height) then
v:SetHeight((self.height - self.internals[1].height) - (padding*2))
2012-05-06 00:24:42 +00:00
end
if not v.retainsize then
v:SetHeight((self.height - self.internals[1].height) - (padding*2))
2012-05-06 00:24:42 +00:00
end
self.internals[1].staticy = self.height - self.internals[1].height
self.internals[1].width = self.width
else
if not v.retainsize then
v:SetHeight(self.height - (padding*2))
2012-05-06 00:24:42 +00:00
end
end
startx = startx + v.width
startx = startx + spacing
end
end
end
end
--[[---------------------------------------------------------
- func: SetDisplayType(type)
- desc: sets the object's display type
--]]---------------------------------------------------------
function newobject:SetDisplayType(type)
2012-05-06 00:24:42 +00:00
local children = self.children
local numchildren = #children
self.display = type
self.vbar = false
self.hbar = false
self.offsetx = 0
self.offsety = 0
2012-05-06 00:24:42 +00:00
self.internals = {}
if numchildren > 0 then
2012-05-06 00:24:42 +00:00
self:CalculateSize()
self:RedoLayout()
end
end
--[[---------------------------------------------------------
- func: GetDisplayType()
- desc: gets the object's display type
--]]---------------------------------------------------------
function newobject:GetDisplayType()
2012-05-06 00:24:42 +00:00
return self.display
end
--[[---------------------------------------------------------
- func: SetPadding(amount)
- desc: sets the object's padding
--]]---------------------------------------------------------
function newobject:SetPadding(amount)
2012-05-06 00:24:42 +00:00
local children = self.children
local numchildren = #children
2012-05-06 00:24:42 +00:00
self.padding = amount
if numchildren > 0 then
2012-05-06 00:24:42 +00:00
self:CalculateSize()
self:RedoLayout()
end
end
--[[---------------------------------------------------------
- func: SetSpacing(amount)
- desc: sets the object's spacing
--]]---------------------------------------------------------
function newobject:SetSpacing(amount)
2012-05-06 00:24:42 +00:00
local children = self.children
local numchildren = #children
2012-05-06 00:24:42 +00:00
self.spacing = amount
if numchildren > 0 then
2012-05-06 00:24:42 +00:00
self:CalculateSize()
self:RedoLayout()
end
end
--[[---------------------------------------------------------
- func: Clear()
- desc: removes all of the object's children
--]]---------------------------------------------------------
function newobject:Clear()
2012-05-06 00:24:42 +00:00
self.children = {}
self:CalculateSize()
self:RedoLayout()
end
--[[---------------------------------------------------------
- func: SetWidth(width)
- desc: sets the object's width
--]]---------------------------------------------------------
function newobject:SetWidth(width)
2012-05-06 00:24:42 +00:00
self.width = width
self:CalculateSize()
self:RedoLayout()
end
--[[---------------------------------------------------------
- func: SetHeight(height)
- desc: sets the object's height
--]]---------------------------------------------------------
function newobject:SetHeight(height)
2012-05-06 00:24:42 +00:00
self.height = height
self:CalculateSize()
self:RedoLayout()
end
--[[---------------------------------------------------------
- func: GetSize()
- desc: gets the object's size
--]]---------------------------------------------------------
function newobject:SetSize(width, height)
2012-05-06 00:24:42 +00:00
self.width = width
self.height = height
self:CalculateSize()
self:RedoLayout()
end
--[[---------------------------------------------------------
- func: GetScrollBar()
- desc: gets the object's scroll bar
--]]---------------------------------------------------------
function newobject:GetScrollBar()
2012-05-06 00:24:42 +00:00
local vbar = self.vbar
local hbar = self.hbar
local internals = self.internals
if vbar or hbar then
local scrollbody = internals[1]
local scrollarea = scrollbody.internals[1]
local scrollbar = scrollarea.internals[1]
return scrollbar
else
return false
end
2012-05-06 00:24:42 +00:00
end
--[[---------------------------------------------------------
- func: SetAutoScroll(bool)
- desc: sets whether or not the list's scrollbar should
auto scroll to the bottom when a new object is
added to the list
--]]---------------------------------------------------------
function newobject:SetAutoScroll(bool)
2012-05-06 00:24:42 +00:00
local scrollbar = self:GetScrollBar()
2012-05-06 00:24:42 +00:00
self.autoscroll = bool
if scrollbar then
scrollbar.autoscroll = bool
end
end
--[[---------------------------------------------------------
- func: SetButtonScrollAmount(speed)
- desc: sets the scroll amount of the object's scrollbar
buttons
--]]---------------------------------------------------------
function newobject:SetButtonScrollAmount(amount)
self.buttonscrollamount = amount
end
--[[---------------------------------------------------------
- func: GetButtonScrollAmount()
- desc: gets the scroll amount of the object's scrollbar
buttons
--]]---------------------------------------------------------
function newobject:GetButtonScrollAmount()
return self.buttonscrollamount
end
--[[---------------------------------------------------------
- func: SetMouseWheelScrollAmount(amount)
- desc: sets the scroll amount of the mouse wheel
--]]---------------------------------------------------------
function newobject:SetMouseWheelScrollAmount(amount)
self.mousewheelscrollamount = amount
end
--[[---------------------------------------------------------
- func: GetMouseWheelScrollAmount()
- desc: gets the scroll amount of the mouse wheel
--]]---------------------------------------------------------
function newobject:GetButtonScrollAmount()
return self.mousewheelscrollamount
2012-05-06 00:24:42 +00:00
end