LoveFrames/objects/internal/treenode.lua

307 lines
6.8 KiB
Lua
Raw Normal View History

2014-06-29 05:16:25 +00:00
--[[------------------------------------------------
-- Love Frames - A GUI library for LOVE --
-- Copyright (c) 2012-2014 Kenny Shields --
--]]------------------------------------------------
-- get the current require path
local path = string.sub(..., 1, string.len(...) - string.len(".objects.internal.treenode"))
local loveframes = require(path .. ".libraries.common")
2014-06-29 05:16:25 +00:00
-- button object
local newobject = loveframes.NewObject("treenode", "loveframes_object_treenode", true)
--[[---------------------------------------------------------
- func: initialize()
- desc: initializes the object
--]]---------------------------------------------------------
function newobject:initialize()
self.type = "treenode"
self.text = "Node"
self.width = 250
self.height = 16
self.level = 0
self.leftpadding = 0
self.lastclick = 0
2014-06-29 05:16:25 +00:00
self.open = false
self.internal = true
self.internals = {}
self.icon = nil
self.OnOpen = nil
self.OnClose = nil
2014-06-29 05:16:25 +00:00
end
--[[---------------------------------------------------------
- func: update(deltatime)
- desc: updates the object
--]]---------------------------------------------------------
function newobject:update(dt)
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
return
end
end
self:CheckHover()
local parent = self.parent
local base = loveframes.base
local update = self.Update
local tree = self.tree
self:SetClickBounds(tree.x, tree.y, tree.width, tree.height)
for k, v in ipairs(self.internals) do
if v.type == "treenode" then
if self.open then
v.x = v.tree.x - v.tree.offsetx
v.y = (v.tree.y + self.tree.itemheight) - v.tree.offsety
if v.width > self.tree.itemwidth then
self.tree.itemwidth = v.width
end
self.tree.itemheight = self.tree.itemheight + v.height
v:update(dt)
end
else
v:update(dt)
end
end
if update then
update(self, dt)
end
end
--[[---------------------------------------------------------
- func: draw()
- desc: draws the object
--]]---------------------------------------------------------
function newobject:draw()
local state = loveframes.state
local selfstate = self.state
if state ~= selfstate then
return
end
local visible = self.visible
if not visible then
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.DrawTreeNode or skins[defaultskin].DrawTreeNode
local draw = self.Draw
local drawcount = loveframes.drawcount
-- set the object's draw order
self:SetDrawOrder()
if draw then
draw(self)
else
drawfunc(self)
end
for k, v in ipairs(self.internals) do
if v.type == "treenode" then
if self.open then
v:draw()
end
else
v:draw()
end
end
end
--[[---------------------------------------------------------
- func: mousepressed(x, y, button)
- desc: called when the player presses a mouse button
--]]---------------------------------------------------------
function newobject:mousepressed(x, y, button)
local state = loveframes.state
local selfstate = self.state
if state ~= selfstate then
return
end
local visible = self.visible
if not visible then
return
end
for k, v in ipairs(self.internals) do
v:mousepressed(x, y, button)
end
2015-10-31 13:40:19 +00:00
if self.hover and button == 1 then
local time = os.time()
if self.lastclick + 0.40 > time then
self.open = not self.open
end
self.lastclick = time
2014-07-06 03:19:33 +00:00
local onselectnode = self.tree.OnSelectNode
self.tree.selectednode = self
2014-07-06 03:19:33 +00:00
if onselectnode then
onselectnode(self.parent, self)
end
end
2014-06-29 05:16:25 +00:00
end
--[[---------------------------------------------------------
- func: mousereleased(x, y, button)
- desc: called when the player releases a mouse button
--]]---------------------------------------------------------
function newobject:mousereleased(x, y, button)
local state = loveframes.state
local selfstate = self.state
if state ~= selfstate then
return
end
local visible = self.visible
if not visible then
return
end
for k, v in ipairs(self.internals) do
v:mousereleased(x, y, button)
end
end
2014-07-02 01:08:45 +00:00
--[[---------------------------------------------------------
- func: SetIcon(icon)
- desc: sets the object's icon
--]]---------------------------------------------------------
2014-06-29 05:16:25 +00:00
function newobject:SetIcon(icon)
if type(icon) == "string" then
self.icon = love.graphics.newImage(icon)
else
self.icon = icon
end
2014-06-29 05:16:25 +00:00
return self
end
2014-07-02 01:08:45 +00:00
--[[---------------------------------------------------------
- func: GetIcon()
- desc: gets the object's icon
--]]---------------------------------------------------------
2014-07-02 00:30:03 +00:00
function newobject:GetIcon()
return self.icon
end
2014-07-02 01:08:45 +00:00
--[[---------------------------------------------------------
- func: SetText(text)
- desc: sets the object's text
--]]---------------------------------------------------------
function newobject:SetText(text)
self.text = text
return self
end
2014-07-02 01:08:45 +00:00
--[[---------------------------------------------------------
- func: GetText()
- desc: gets the object's text
--]]---------------------------------------------------------
function newobject:GetText()
return self.text
end
2014-07-02 01:08:45 +00:00
--[[---------------------------------------------------------
- func: AddNode(text)
- desc: adds a new node to the object
--]]---------------------------------------------------------
2014-06-29 05:16:25 +00:00
function newobject:AddNode(text)
if not self.internals[1] then
local openbutton = loveframes.objects["treenodebutton"]:new()
openbutton.parent = self
openbutton.staticx = 2
openbutton.staticy = 2
table.insert(self.internals, openbutton)
end
local node = loveframes.objects["treenode"]:new()
node.parent = self
node.tree = self.tree
node.text = text
node.level = self.level + 1
table.insert(self.internals, node)
return node
end
2014-10-24 21:19:54 +00:00
--[[---------------------------------------------------------
- func: RemoveNode(id)
- desc: removes a node from the object
--]]---------------------------------------------------------
function newobject:RemoveNode(id)
id = id + 1
for k, v in ipairs(self.internals) do
if k == id then
v:Remove()
break
end
end
end
2014-07-02 01:08:45 +00:00
--[[---------------------------------------------------------
- func: SetOpen(bool)
- desc: sets whether or not the object is open
--]]---------------------------------------------------------
2014-06-29 05:16:25 +00:00
function newobject:SetOpen(bool)
self.open = bool
return self
2014-07-02 01:10:46 +00:00
end
--[[---------------------------------------------------------
- func: GetOpen()
- desc: gets whether or not the object is open
--]]---------------------------------------------------------
function newobject:GetOpen()
return self.open
2014-11-09 21:24:10 +00:00
end