2012-05-06 00:24:42 +00:00
|
|
|
--[[------------------------------------------------
|
2012-09-29 23:20:41 +00:00
|
|
|
-- Love Frames - A GUI library for LOVE --
|
|
|
|
-- Copyright (c) 2012 Kenny Shields --
|
2012-05-06 00:24:42 +00:00
|
|
|
--]]------------------------------------------------
|
|
|
|
|
2012-09-29 23:20:41 +00:00
|
|
|
-- collapsiblecategory class
|
2012-05-06 00:24:42 +00:00
|
|
|
collapsiblecategory = class("collapsiblecategory", base)
|
|
|
|
collapsiblecategory:include(loveframes.templates.default)
|
|
|
|
|
|
|
|
--[[---------------------------------------------------------
|
|
|
|
- func: initialize()
|
|
|
|
- desc: initializes the object
|
|
|
|
--]]---------------------------------------------------------
|
|
|
|
function collapsiblecategory:initialize()
|
|
|
|
|
2012-09-29 23:20:41 +00:00
|
|
|
self.type = "collapsiblecategory"
|
|
|
|
self.text = "Category"
|
|
|
|
self.width = 200
|
|
|
|
self.height = 25
|
|
|
|
self.closedheight = 25
|
|
|
|
self.padding = 5
|
|
|
|
self.internal = false
|
|
|
|
self.open = false
|
|
|
|
self.down = false
|
|
|
|
self.children = {}
|
|
|
|
self.OnOpenedClosed = nil
|
2012-05-06 00:24:42 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[---------------------------------------------------------
|
|
|
|
- func: update(deltatime)
|
|
|
|
- desc: updates the object
|
|
|
|
--]]---------------------------------------------------------
|
|
|
|
function collapsiblecategory:update(dt)
|
|
|
|
|
2012-09-29 23:20:41 +00:00
|
|
|
local visible = self.visible
|
2012-05-22 07:10:18 +00:00
|
|
|
local alwaysupdate = self.alwaysupdate
|
|
|
|
|
2012-09-29 23:20:41 +00:00
|
|
|
if not visible then
|
|
|
|
if not alwaysupdate then
|
2012-05-06 00:24:42 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-09-29 23:20:41 +00:00
|
|
|
local open = self.open
|
|
|
|
local children = self.children
|
2012-05-22 07:10:18 +00:00
|
|
|
local curobject = children[1]
|
2012-09-29 23:20:41 +00:00
|
|
|
local parent = self.parent
|
|
|
|
local base = loveframes.base
|
|
|
|
local update = self.Update
|
2012-05-22 07:10:18 +00:00
|
|
|
|
2012-05-06 00:24:42 +00:00
|
|
|
self:CheckHover()
|
|
|
|
|
|
|
|
-- move to parent if there is a parent
|
2012-09-29 23:20:41 +00:00
|
|
|
if parent ~= base and parent.type ~= "list" then
|
2012-05-06 00:24:42 +00:00
|
|
|
self.x = self.parent.x + self.staticx
|
|
|
|
self.y = self.parent.y + self.staticy
|
|
|
|
end
|
|
|
|
|
2012-05-22 07:10:18 +00:00
|
|
|
if open == true then
|
2012-09-29 23:20:41 +00:00
|
|
|
curobject:SetWidth(self.width - self.padding * 2)
|
2012-09-02 00:56:32 +00:00
|
|
|
curobject:update(dt)
|
2012-05-06 00:24:42 +00:00
|
|
|
end
|
|
|
|
|
2012-09-29 23:20:41 +00:00
|
|
|
if update then
|
|
|
|
update(self, dt)
|
2012-05-06 00:24:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[---------------------------------------------------------
|
|
|
|
- func: draw()
|
|
|
|
- desc: draws the object
|
|
|
|
--]]---------------------------------------------------------
|
|
|
|
function collapsiblecategory:draw()
|
|
|
|
|
2012-05-22 07:10:18 +00:00
|
|
|
local visible = self.visible
|
|
|
|
|
|
|
|
if visible == false then
|
2012-05-06 00:24:42 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2012-09-29 23:20:41 +00:00
|
|
|
local open = self.open
|
|
|
|
local children = self.children
|
|
|
|
local curobject = children[1]
|
|
|
|
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.DrawCollapsibleCategory or skins[defaultskin].DrawCollapsibleCategory
|
|
|
|
local draw = self.Draw
|
|
|
|
local drawcount = loveframes.drawcount
|
|
|
|
|
|
|
|
loveframes.drawcount = drawcount + 1
|
2012-05-22 07:10:18 +00:00
|
|
|
self.draworder = loveframes.drawcount
|
2012-09-02 00:56:32 +00:00
|
|
|
|
2012-09-29 23:20:41 +00:00
|
|
|
if draw then
|
|
|
|
draw(self)
|
2012-05-06 00:24:42 +00:00
|
|
|
else
|
2012-09-02 00:56:32 +00:00
|
|
|
drawfunc(self)
|
2012-05-06 00:24:42 +00:00
|
|
|
end
|
|
|
|
|
2012-09-29 23:20:41 +00:00
|
|
|
if open then
|
2012-05-22 07:10:18 +00:00
|
|
|
curobject:draw()
|
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 collapsiblecategory:mousepressed(x, y, button)
|
|
|
|
|
2012-05-22 07:10:18 +00:00
|
|
|
local visible = self.visible
|
|
|
|
|
2012-09-29 23:20:41 +00:00
|
|
|
if not visible then
|
2012-05-06 00:24:42 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2012-09-29 23:20:41 +00:00
|
|
|
local hover = self.hover
|
|
|
|
local open = self.open
|
|
|
|
local children = self.children
|
2012-05-22 07:10:18 +00:00
|
|
|
local curobject = children[1]
|
|
|
|
|
2012-09-29 23:20:41 +00:00
|
|
|
if hover then
|
2012-05-06 00:24:42 +00:00
|
|
|
|
|
|
|
local col = loveframes.util.BoundingBox(self.x, x, self.y, y, self.width, 1, self.closedheight, 1)
|
|
|
|
|
2012-09-29 23:20:41 +00:00
|
|
|
if button == "l" and col then
|
2012-05-06 00:24:42 +00:00
|
|
|
|
|
|
|
local baseparent = self:GetBaseParent()
|
|
|
|
|
|
|
|
if baseparent and baseparent.type == "frame" then
|
|
|
|
baseparent:MakeTop()
|
|
|
|
end
|
2012-05-13 03:57:27 +00:00
|
|
|
|
|
|
|
self.down = true
|
|
|
|
loveframes.hoverobject = self
|
2012-05-06 00:24:42 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2012-09-29 23:20:41 +00:00
|
|
|
if open then
|
2012-05-22 07:10:18 +00:00
|
|
|
curobject:mousepressed(x, y, button)
|
2012-05-06 00:24:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[---------------------------------------------------------
|
|
|
|
- func: mousereleased(x, y, button)
|
|
|
|
- desc: called when the player releases a mouse button
|
|
|
|
--]]---------------------------------------------------------
|
|
|
|
function collapsiblecategory:mousereleased(x, y, button)
|
|
|
|
|
2012-05-22 07:10:18 +00:00
|
|
|
local visible = self.visible
|
|
|
|
|
2012-09-29 23:20:41 +00:00
|
|
|
if not visible then
|
2012-05-06 00:24:42 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2012-09-29 23:20:41 +00:00
|
|
|
local hover = self.hover
|
|
|
|
local down = self.down
|
2012-05-06 00:24:42 +00:00
|
|
|
local clickable = self.clickable
|
2012-09-29 23:20:41 +00:00
|
|
|
local enabled = self.enabled
|
|
|
|
local open = self.open
|
|
|
|
local col = loveframes.util.BoundingBox(self.x, x, self.y, y, self.width, 1, self.closedheight, 1)
|
|
|
|
local children = self.children
|
2012-05-22 07:10:18 +00:00
|
|
|
local curobject = children[1]
|
2012-05-06 00:24:42 +00:00
|
|
|
|
2012-09-29 23:20:41 +00:00
|
|
|
if hover and col and down and button == "l" then
|
2012-05-06 00:24:42 +00:00
|
|
|
|
2012-09-29 23:20:41 +00:00
|
|
|
if open then
|
2012-05-06 00:24:42 +00:00
|
|
|
self:SetOpen(false)
|
|
|
|
else
|
|
|
|
self:SetOpen(true)
|
|
|
|
end
|
|
|
|
|
2012-05-13 03:57:27 +00:00
|
|
|
self.down = false
|
|
|
|
|
2012-05-06 00:24:42 +00:00
|
|
|
end
|
|
|
|
|
2012-09-29 23:20:41 +00:00
|
|
|
if open then
|
2012-05-22 07:10:18 +00:00
|
|
|
curobject:mousepressed(x, y, button)
|
2012-05-06 00:24:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[---------------------------------------------------------
|
|
|
|
- func: SetText(text)
|
|
|
|
- desc: sets the object's text
|
|
|
|
--]]---------------------------------------------------------
|
|
|
|
function collapsiblecategory:SetText(text)
|
|
|
|
|
|
|
|
self.text = text
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[---------------------------------------------------------
|
|
|
|
- func: GetText()
|
|
|
|
- desc: gets the object's text
|
|
|
|
--]]---------------------------------------------------------
|
|
|
|
function collapsiblecategory:GetText()
|
|
|
|
|
|
|
|
return self.text
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[---------------------------------------------------------
|
|
|
|
- func: SetObject(object)
|
|
|
|
- desc: sets the category's object
|
|
|
|
--]]---------------------------------------------------------
|
|
|
|
function collapsiblecategory:SetObject(object)
|
|
|
|
|
2012-09-29 23:20:41 +00:00
|
|
|
local children = self.children
|
2012-05-22 07:10:18 +00:00
|
|
|
local curobject = children[1]
|
|
|
|
|
|
|
|
if curobject then
|
|
|
|
curobject:Remove()
|
2012-05-06 00:24:42 +00:00
|
|
|
self.children = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
object:Remove()
|
|
|
|
object.parent = self
|
|
|
|
object:SetWidth(self.width - self.padding*2)
|
|
|
|
object:SetPos(self.padding, self.closedheight + self.padding)
|
|
|
|
|
|
|
|
table.insert(self.children, object)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[---------------------------------------------------------
|
|
|
|
- func: SetObject(object)
|
|
|
|
- desc: sets the category's object
|
|
|
|
--]]---------------------------------------------------------
|
|
|
|
function collapsiblecategory:GetObject()
|
|
|
|
|
2012-09-29 23:20:41 +00:00
|
|
|
local children = self.children
|
2012-05-22 07:10:18 +00:00
|
|
|
local curobject = children[1]
|
|
|
|
|
|
|
|
if curobject then
|
|
|
|
return curobject
|
2012-05-06 00:24:42 +00:00
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[---------------------------------------------------------
|
|
|
|
- func: SetSize(width, height)
|
|
|
|
- desc: sets the object's size
|
|
|
|
--]]---------------------------------------------------------
|
|
|
|
function collapsiblecategory:SetSize(width, height)
|
|
|
|
|
|
|
|
self.width = width
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[---------------------------------------------------------
|
|
|
|
- func: SetHeight(height)
|
|
|
|
- desc: sets the object's height
|
|
|
|
--]]---------------------------------------------------------
|
|
|
|
function collapsiblecategory:SetHeight(height)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[---------------------------------------------------------
|
|
|
|
- func: SetClosedHeight(height)
|
|
|
|
- desc: sets the object's closed height
|
|
|
|
--]]---------------------------------------------------------
|
|
|
|
function collapsiblecategory:SetClosedHeight(height)
|
|
|
|
|
|
|
|
self.closedheight = height
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[---------------------------------------------------------
|
|
|
|
- func: GetClosedHeight()
|
|
|
|
- desc: gets the object's closed height
|
|
|
|
--]]---------------------------------------------------------
|
|
|
|
function collapsiblecategory:GetClosedHeight()
|
|
|
|
|
|
|
|
return self.closedheight
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[---------------------------------------------------------
|
|
|
|
- func: SetOpen(bool)
|
|
|
|
- desc: sets whether the object is opened or closed
|
|
|
|
--]]---------------------------------------------------------
|
|
|
|
function collapsiblecategory:SetOpen(bool)
|
|
|
|
|
2012-09-29 23:20:41 +00:00
|
|
|
local children = self.children
|
|
|
|
local curobject = children[1]
|
|
|
|
local closedheight = self.closedheight
|
|
|
|
local padding = self.padding
|
|
|
|
local curobjectheight = curobject.height
|
|
|
|
local onopenedclosed = self.OnOpenedClosed
|
2012-05-22 07:10:18 +00:00
|
|
|
|
2012-05-06 00:24:42 +00:00
|
|
|
self.open = bool
|
|
|
|
|
2012-09-29 23:20:41 +00:00
|
|
|
if not bool then
|
|
|
|
self.height = closedheight
|
2012-05-22 07:10:18 +00:00
|
|
|
if curobject then
|
|
|
|
curobject:SetVisible(false)
|
2012-05-06 00:24:42 +00:00
|
|
|
end
|
|
|
|
else
|
2012-09-29 23:20:41 +00:00
|
|
|
self.height = closedheight + padding * 2 + curobjectheight
|
2012-05-22 07:10:18 +00:00
|
|
|
if curobject then
|
|
|
|
curobject:SetVisible(true)
|
2012-05-06 00:24:42 +00:00
|
|
|
end
|
|
|
|
end
|
2012-09-29 23:20:41 +00:00
|
|
|
|
|
|
|
-- call the on opened closed callback if it exists
|
|
|
|
if onopenedclosed then
|
|
|
|
onopenedclosed(self)
|
2012-05-06 00:24:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[---------------------------------------------------------
|
|
|
|
- func: GetOpen()
|
|
|
|
- desc: gets whether the object is opened or closed
|
|
|
|
--]]---------------------------------------------------------
|
|
|
|
function collapsiblecategory:GetOpen()
|
|
|
|
|
|
|
|
return self.opened
|
|
|
|
|
|
|
|
end
|