2012-09-02 00:56:32 +00:00
|
|
|
--[[------------------------------------------------
|
2012-09-29 23:20:41 +00:00
|
|
|
-- Love Frames - A GUI library for LOVE --
|
|
|
|
-- Copyright (c) 2012 Kenny Shields --
|
2012-09-02 00:56:32 +00:00
|
|
|
--]]------------------------------------------------
|
|
|
|
|
|
|
|
-- tabs class
|
2012-11-24 22:42:16 +00:00
|
|
|
local newobject = loveframes.NewObject("tabs", "loveframes_object_tabs", true)
|
2012-09-02 00:56:32 +00:00
|
|
|
|
|
|
|
--[[---------------------------------------------------------
|
|
|
|
- func: initialize()
|
|
|
|
- desc: initializes the object
|
|
|
|
--]]---------------------------------------------------------
|
2012-11-24 22:42:16 +00:00
|
|
|
function newobject:initialize()
|
2012-09-02 00:56:32 +00:00
|
|
|
|
2012-11-05 13:53:22 +00:00
|
|
|
self.type = "tabs"
|
|
|
|
self.width = 100
|
|
|
|
self.height = 50
|
|
|
|
self.clickx = 0
|
|
|
|
self.clicky = 0
|
|
|
|
self.offsetx = 0
|
|
|
|
self.tab = 1
|
|
|
|
self.tabnumber = 1
|
|
|
|
self.padding = 5
|
|
|
|
self.tabheight = 25
|
|
|
|
self.previoustabheight = 25
|
|
|
|
self.autosize = true
|
|
|
|
self.internal = false
|
|
|
|
self.tooltipfont = loveframes.basicfontsmall
|
|
|
|
self.tabs = {}
|
|
|
|
self.internals = {}
|
|
|
|
self.children = {}
|
2012-09-02 00:56:32 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[---------------------------------------------------------
|
|
|
|
- func: update(deltatime)
|
|
|
|
- desc: updates the element
|
|
|
|
--]]---------------------------------------------------------
|
2012-11-24 22:42:16 +00:00
|
|
|
function newobject:update(dt)
|
2012-09-02 00:56:32 +00:00
|
|
|
|
2012-09-29 23:20:41 +00:00
|
|
|
local visible = self.visible
|
2012-09-02 00:56:32 +00:00
|
|
|
local alwaysupdate = self.alwaysupdate
|
|
|
|
|
2012-09-29 23:20:41 +00:00
|
|
|
if not visible then
|
|
|
|
if not alwaysupdate then
|
2012-09-02 00:56:32 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-05 13:53:22 +00:00
|
|
|
local x, y = love.mouse.getPosition()
|
|
|
|
local tabheight = self.tabheight
|
|
|
|
local padding = self.padding
|
|
|
|
local autosize = self.autosize
|
|
|
|
local padding = self.padding
|
|
|
|
local autosize = self.autosize
|
|
|
|
local children = self.children
|
|
|
|
local numchildren = #children
|
|
|
|
local internals = self.internals
|
|
|
|
local tab = self.tab
|
|
|
|
local parent = self.parent
|
|
|
|
local autosize = self.autosize
|
|
|
|
local base = loveframes.base
|
|
|
|
local update = self.Update
|
2012-09-02 00:56:32 +00:00
|
|
|
|
|
|
|
-- move to parent if there is a parent
|
2012-09-29 23:20:41 +00:00
|
|
|
if parent ~= base then
|
2012-09-02 00:56:32 +00:00
|
|
|
self.x = self.parent.x + self.staticx
|
|
|
|
self.y = self.parent.y + self.staticy
|
|
|
|
end
|
|
|
|
|
|
|
|
self:CheckHover()
|
|
|
|
|
|
|
|
if numchildren > 0 and tab == 0 then
|
|
|
|
self.tab = 1
|
|
|
|
end
|
|
|
|
|
|
|
|
local pos = 0
|
|
|
|
|
|
|
|
for k, v in ipairs(internals) do
|
|
|
|
v:update(dt)
|
|
|
|
if v.type == "tabbutton" then
|
|
|
|
v.y = (v.parent.y + v.staticy)
|
|
|
|
v.x = (v.parent.x + v.staticx) + pos + self.offsetx
|
|
|
|
pos = pos + v.width - 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
for k, v in ipairs(children) do
|
|
|
|
v:update(dt)
|
|
|
|
v:SetPos(padding, tabheight + padding)
|
|
|
|
end
|
|
|
|
|
2012-09-29 23:20:41 +00:00
|
|
|
if update then
|
|
|
|
update(self, dt)
|
2012-09-02 00:56:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[---------------------------------------------------------
|
|
|
|
- func: draw()
|
|
|
|
- desc: draws the object
|
|
|
|
--]]---------------------------------------------------------
|
2012-11-24 22:42:16 +00:00
|
|
|
function newobject:draw()
|
2012-09-02 00:56:32 +00:00
|
|
|
|
|
|
|
local visible = self.visible
|
|
|
|
|
2012-09-29 23:20:41 +00:00
|
|
|
if not visible then
|
2012-09-02 00:56:32 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2012-09-29 23:20:41 +00:00
|
|
|
local internals = self.internals
|
|
|
|
local tabheight = self:GetHeightOfButtons()
|
|
|
|
local stencilfunc = function() love.graphics.rectangle("fill", self.x, self.y, self.width, tabheight) end
|
|
|
|
local stencil = love.graphics.newStencil(stencilfunc)
|
|
|
|
local internals = self.internals
|
|
|
|
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.DrawTabPanel or skins[defaultskin].DrawTabPanel
|
|
|
|
local draw = self.Draw
|
|
|
|
local drawcount = loveframes.drawcount
|
|
|
|
|
2012-10-22 14:19:02 +00:00
|
|
|
-- set the object's draw order
|
|
|
|
self:SetDrawOrder()
|
2012-09-02 00:56:32 +00:00
|
|
|
|
2012-09-29 23:20:41 +00:00
|
|
|
if draw then
|
|
|
|
draw(self)
|
2012-09-02 00:56:32 +00:00
|
|
|
else
|
|
|
|
drawfunc(self)
|
|
|
|
end
|
|
|
|
|
|
|
|
love.graphics.setStencil(stencil)
|
|
|
|
|
|
|
|
for k, v in ipairs(internals) do
|
|
|
|
v:draw()
|
|
|
|
end
|
|
|
|
|
|
|
|
love.graphics.setStencil()
|
|
|
|
|
|
|
|
if #self.children > 0 then
|
|
|
|
self.children[self.tab]:draw()
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[---------------------------------------------------------
|
|
|
|
- func: mousepressed(x, y, button)
|
|
|
|
- desc: called when the player presses a mouse button
|
|
|
|
--]]---------------------------------------------------------
|
2012-11-24 22:42:16 +00:00
|
|
|
function newobject:mousepressed(x, y, button)
|
2012-09-02 00:56:32 +00:00
|
|
|
|
|
|
|
local visible = self.visible
|
|
|
|
|
2012-09-29 23:20:41 +00:00
|
|
|
if not visible then
|
2012-09-02 00:56:32 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2012-09-29 23:20:41 +00:00
|
|
|
local children = self.children
|
|
|
|
local numchildren = #children
|
|
|
|
local tab = self.tab
|
|
|
|
local internals = self.internals
|
2012-09-02 00:56:32 +00:00
|
|
|
local numinternals = #internals
|
2012-09-29 23:20:41 +00:00
|
|
|
local hover = self.hover
|
2012-09-02 00:56:32 +00:00
|
|
|
|
2012-09-29 23:20:41 +00:00
|
|
|
if hover then
|
2012-09-02 00:56:32 +00:00
|
|
|
|
|
|
|
if button == "l" then
|
|
|
|
|
|
|
|
local baseparent = self:GetBaseParent()
|
|
|
|
|
|
|
|
if baseparent and baseparent.type == "frame" then
|
|
|
|
baseparent:MakeTop()
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
if button == "wu" then
|
|
|
|
|
|
|
|
local buttonheight = self:GetHeightOfButtons()
|
2012-09-29 23:20:41 +00:00
|
|
|
local col = loveframes.util.BoundingBox(self.x, x, self.y, y, self.width, 1, buttonheight, 1)
|
|
|
|
local visible = internals[numinternals - 1]:GetVisible()
|
2012-09-02 00:56:32 +00:00
|
|
|
|
2012-09-29 23:20:41 +00:00
|
|
|
if col and visible then
|
2012-09-02 00:56:32 +00:00
|
|
|
self.offsetx = self.offsetx + 5
|
|
|
|
if self.offsetx > 0 then
|
|
|
|
self.offsetx = 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
if button == "wd" then
|
|
|
|
|
|
|
|
local buttonheight = self:GetHeightOfButtons()
|
2012-09-29 23:20:41 +00:00
|
|
|
local col = loveframes.util.BoundingBox(self.x, x, self.y, y, self.width, 1, buttonheight, 1)
|
|
|
|
local visible = internals[numinternals]:GetVisible()
|
2012-09-02 00:56:32 +00:00
|
|
|
|
2012-09-29 23:20:41 +00:00
|
|
|
if col and visible then
|
2012-09-02 00:56:32 +00:00
|
|
|
local bwidth = self:GetWidthOfButtons()
|
|
|
|
if (self.offsetx + bwidth) < self.width then
|
|
|
|
self.offsetx = bwidth - self.width
|
|
|
|
else
|
|
|
|
self.offsetx = self.offsetx - 5
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
for k, v in ipairs(internals) do
|
|
|
|
v:mousepressed(x, y, button)
|
|
|
|
end
|
|
|
|
|
|
|
|
if numchildren > 0 then
|
|
|
|
children[tab]:mousepressed(x, y, button)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[---------------------------------------------------------
|
|
|
|
- func: mousereleased(x, y, button)
|
|
|
|
- desc: called when the player releases a mouse button
|
|
|
|
--]]---------------------------------------------------------
|
2012-11-24 22:42:16 +00:00
|
|
|
function newobject:mousereleased(x, y, button)
|
2012-09-02 00:56:32 +00:00
|
|
|
|
2012-09-29 23:20:41 +00:00
|
|
|
local visible = self.visible
|
|
|
|
local children = self.children
|
2012-09-02 00:56:32 +00:00
|
|
|
local numchildren = #children
|
2012-09-29 23:20:41 +00:00
|
|
|
local tab = self.tab
|
|
|
|
local internals = self.internals
|
2012-09-02 00:56:32 +00:00
|
|
|
|
2012-09-29 23:20:41 +00:00
|
|
|
if not visible then
|
2012-09-02 00:56:32 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
for k, v in ipairs(internals) do
|
|
|
|
v:mousereleased(x, y, button)
|
|
|
|
end
|
|
|
|
|
|
|
|
if numchildren > 0 then
|
|
|
|
children[tab]:mousereleased(x, y, button)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[---------------------------------------------------------
|
|
|
|
- func: AddTab(name, object, tip, image)
|
|
|
|
- desc: adds a new tab to the tab panel
|
|
|
|
--]]---------------------------------------------------------
|
2012-11-24 22:42:16 +00:00
|
|
|
function newobject:AddTab(name, object, tip, image)
|
2012-09-02 00:56:32 +00:00
|
|
|
|
2012-11-05 13:53:22 +00:00
|
|
|
local padding = self.padding
|
|
|
|
local autosize = self.autosize
|
|
|
|
local tabnumber = self.tabnumber
|
|
|
|
local tabheight = self.tabheight
|
|
|
|
local internals = self.internals
|
2012-09-02 00:56:32 +00:00
|
|
|
|
|
|
|
object:Remove()
|
|
|
|
object.parent = self
|
|
|
|
object.staticx = 0
|
|
|
|
object.staticy = 0
|
|
|
|
|
|
|
|
table.insert(self.children, object)
|
2012-11-24 22:42:16 +00:00
|
|
|
internals[tabnumber] = loveframes.objects["tabbutton"]:new(self, name, tabnumber, tip, image)
|
2012-09-02 00:56:32 +00:00
|
|
|
self.tabnumber = tabnumber + 1
|
|
|
|
|
|
|
|
for k, v in ipairs(internals) do
|
|
|
|
self:SwitchToTab(k)
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
|
|
|
self:AddScrollButtons()
|
|
|
|
|
2012-09-29 23:20:41 +00:00
|
|
|
if autosize and not retainsize then
|
2012-09-02 00:56:32 +00:00
|
|
|
object:SetSize(self.width - padding*2, (self.height - tabheight) - padding*2)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[---------------------------------------------------------
|
|
|
|
- func: AddScrollButtons()
|
|
|
|
- desc: creates scroll buttons fot the tab panel
|
|
|
|
- note: for internal use only
|
|
|
|
--]]---------------------------------------------------------
|
2012-11-24 22:42:16 +00:00
|
|
|
function newobject:AddScrollButtons()
|
2012-09-02 00:56:32 +00:00
|
|
|
|
|
|
|
local internals = self.internals
|
|
|
|
|
|
|
|
for k, v in ipairs(internals) do
|
|
|
|
if v.type == "scrollbutton" then
|
|
|
|
table.remove(internals, k)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-24 22:42:16 +00:00
|
|
|
local leftbutton = loveframes.objects["scrollbutton"]:new("left")
|
2012-09-02 00:56:32 +00:00
|
|
|
leftbutton.parent = self
|
|
|
|
leftbutton:SetPos(0, 0)
|
|
|
|
leftbutton:SetSize(15, 25)
|
|
|
|
leftbutton:SetAlwaysUpdate(true)
|
|
|
|
leftbutton.Update = function(object, dt)
|
|
|
|
if self.offsetx ~= 0 then
|
|
|
|
object.visible = true
|
|
|
|
else
|
|
|
|
object.visible = false
|
|
|
|
object.down = false
|
|
|
|
object.hover = false
|
|
|
|
end
|
|
|
|
|
|
|
|
if object.down == true then
|
|
|
|
if self.offsetx ~= 0 then
|
|
|
|
self.offsetx = self.offsetx + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-24 22:42:16 +00:00
|
|
|
local rightbutton = loveframes.objects["scrollbutton"]:new("right")
|
2012-09-02 00:56:32 +00:00
|
|
|
rightbutton.parent = self
|
|
|
|
rightbutton:SetPos(self.width - 15, 0)
|
|
|
|
rightbutton:SetSize(15, 25)
|
|
|
|
rightbutton:SetAlwaysUpdate(true)
|
|
|
|
rightbutton.Update = function(object, dt)
|
|
|
|
local bwidth = self:GetWidthOfButtons()
|
|
|
|
if (self.offsetx + bwidth) > self.width then
|
|
|
|
object.visible = true
|
|
|
|
else
|
|
|
|
object.visible = false
|
|
|
|
object.down = false
|
|
|
|
object.hover = false
|
|
|
|
end
|
|
|
|
|
|
|
|
if object.down == true then
|
|
|
|
if ((self.x + self.offsetx) + bwidth) ~= (self.x + self.width) then
|
|
|
|
self.offsetx = self.offsetx - 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
table.insert(internals, leftbutton)
|
|
|
|
table.insert(internals, rightbutton)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[---------------------------------------------------------
|
|
|
|
- func: GetWidthOfButtons()
|
|
|
|
- desc: gets the total width of all of the tab buttons
|
|
|
|
--]]---------------------------------------------------------
|
2012-11-24 22:42:16 +00:00
|
|
|
function newobject:GetWidthOfButtons()
|
2012-09-02 00:56:32 +00:00
|
|
|
|
2012-09-29 23:20:41 +00:00
|
|
|
local width = 0
|
2012-09-02 00:56:32 +00:00
|
|
|
local internals = self.internals
|
|
|
|
|
|
|
|
for k, v in ipairs(internals) do
|
|
|
|
if v.type == "tabbutton" then
|
|
|
|
width = width + v.width
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return width
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[---------------------------------------------------------
|
|
|
|
- func: GetHeightOfButtons()
|
|
|
|
- desc: gets the height of one tab button
|
|
|
|
--]]---------------------------------------------------------
|
2012-11-24 22:42:16 +00:00
|
|
|
function newobject:GetHeightOfButtons()
|
2012-09-02 00:56:32 +00:00
|
|
|
|
|
|
|
return self.tabheight
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[---------------------------------------------------------
|
|
|
|
- func: SwitchToTab(tabnumber)
|
|
|
|
- desc: makes the specified tab the active tab
|
|
|
|
--]]---------------------------------------------------------
|
2012-11-24 22:42:16 +00:00
|
|
|
function newobject:SwitchToTab(tabnumber)
|
2012-09-02 00:56:32 +00:00
|
|
|
|
|
|
|
local children = self.children
|
|
|
|
|
|
|
|
for k, v in ipairs(children) do
|
|
|
|
v.visible = false
|
|
|
|
end
|
|
|
|
|
|
|
|
self.tab = tabnumber
|
|
|
|
self.children[tabnumber].visible = true
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[---------------------------------------------------------
|
|
|
|
- func: SetScrollButtonSize(width, height)
|
|
|
|
- desc: sets the size of the scroll buttons
|
|
|
|
--]]---------------------------------------------------------
|
2012-11-24 22:42:16 +00:00
|
|
|
function newobject:SetScrollButtonSize(width, height)
|
2012-09-02 00:56:32 +00:00
|
|
|
|
|
|
|
local internals = self.internals
|
|
|
|
|
|
|
|
for k, v in ipairs(internals) do
|
|
|
|
if v.type == "scrollbutton" then
|
|
|
|
v:SetSize(width, height)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[---------------------------------------------------------
|
|
|
|
- func: SetPadding(paddint)
|
|
|
|
- desc: sets the padding for the tab panel
|
|
|
|
--]]---------------------------------------------------------
|
2012-11-24 22:42:16 +00:00
|
|
|
function newobject:SetPadding(padding)
|
2012-09-02 00:56:32 +00:00
|
|
|
|
|
|
|
self.padding = padding
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[---------------------------------------------------------
|
|
|
|
- func: SetPadding(paddint)
|
|
|
|
- desc: gets the padding of the tab panel
|
|
|
|
--]]---------------------------------------------------------
|
2012-11-24 22:42:16 +00:00
|
|
|
function newobject:GetPadding()
|
2012-09-02 00:56:32 +00:00
|
|
|
|
|
|
|
return self.padding
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[---------------------------------------------------------
|
|
|
|
- func: SetTabHeight(height)
|
|
|
|
- desc: sets the height of the tab buttons
|
|
|
|
--]]---------------------------------------------------------
|
2012-11-24 22:42:16 +00:00
|
|
|
function newobject:SetTabHeight(height)
|
2012-09-02 00:56:32 +00:00
|
|
|
|
2012-11-05 13:53:22 +00:00
|
|
|
local autosize = self.autosize
|
|
|
|
local padding = self.padding
|
|
|
|
local previoustabheight = self.previoustabheight
|
|
|
|
local children = self.children
|
|
|
|
local internals = self.internals
|
2012-09-02 00:56:32 +00:00
|
|
|
|
|
|
|
self.tabheight = height
|
|
|
|
|
2012-11-05 13:53:22 +00:00
|
|
|
local tabheight = self.tabheight
|
|
|
|
|
|
|
|
if tabheight ~= previoustabheight then
|
|
|
|
for k, v in ipairs(children) do
|
|
|
|
local retainsize = v.retainsize
|
|
|
|
if autosize and not retainsize then
|
|
|
|
v:SetSize(self.width - padding*2, (self.height - tabheight) - padding*2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
self.previoustabheight = tabheight
|
|
|
|
end
|
|
|
|
|
2012-09-02 00:56:32 +00:00
|
|
|
for k, v in ipairs(internals) do
|
|
|
|
if v.type == "tabbutton" then
|
|
|
|
v:SetHeight(self.tabheight)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[---------------------------------------------------------
|
|
|
|
- func: SetToolTipFont(font)
|
|
|
|
- desc: sets the height of the tab buttons
|
|
|
|
--]]---------------------------------------------------------
|
2012-11-24 22:42:16 +00:00
|
|
|
function newobject:SetToolTipFont(font)
|
2012-09-02 00:56:32 +00:00
|
|
|
|
|
|
|
local internals = self.internals
|
|
|
|
|
|
|
|
for k, v in ipairs(internals) do
|
|
|
|
if v.type == "tabbutton" and v.tooltip then
|
|
|
|
v.tooltip:SetFont(font)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[---------------------------------------------------------
|
|
|
|
- func: GetTabNumber()
|
|
|
|
- desc: gets the object's tab number
|
|
|
|
--]]---------------------------------------------------------
|
2012-11-24 22:42:16 +00:00
|
|
|
function newobject:GetTabNumber()
|
2012-09-02 00:56:32 +00:00
|
|
|
|
2012-09-29 23:20:41 +00:00
|
|
|
return self.tab
|
2012-09-02 00:56:32 +00:00
|
|
|
|
2012-05-06 00:24:42 +00:00
|
|
|
end
|