2014-06-29 05:16:25 +00:00
|
|
|
--[[------------------------------------------------
|
|
|
|
-- Love Frames - A GUI library for LOVE --
|
|
|
|
-- Copyright (c) 2012-2014 Kenny Shields --
|
|
|
|
--]]------------------------------------------------
|
|
|
|
|
2019-03-06 23:19:58 +00:00
|
|
|
return function(loveframes)
|
|
|
|
---------- module start ----------
|
2014-10-23 22:36:16 +00:00
|
|
|
|
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
|
2014-09-20 14:58:36 +00:00
|
|
|
self.lastclick = 0
|
2014-06-29 05:16:25 +00:00
|
|
|
self.open = false
|
|
|
|
self.internal = true
|
|
|
|
self.internals = {}
|
|
|
|
self.icon = nil
|
2014-07-02 01:04:58 +00:00
|
|
|
self.OnOpen = nil
|
|
|
|
self.OnClose = nil
|
2014-06-29 05:16:25 +00:00
|
|
|
|
2019-03-06 23:19:58 +00:00
|
|
|
self:SetDrawFunc()
|
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()
|
2019-03-06 23:19:58 +00:00
|
|
|
if loveframes.state ~= self.state then
|
2014-06-29 05:16:25 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2019-03-06 23:19:58 +00:00
|
|
|
if not self.visible then
|
2014-06-29 05:16:25 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- set the object's draw order
|
|
|
|
self:SetDrawOrder()
|
2019-03-06 23:19:58 +00:00
|
|
|
|
|
|
|
local drawfunc = self.Draw or self.drawfunc
|
|
|
|
if drawfunc then
|
2014-06-29 05:16:25 +00:00
|
|
|
drawfunc(self)
|
|
|
|
end
|
|
|
|
|
2019-03-06 23:19:58 +00:00
|
|
|
local internals = self.internals
|
|
|
|
if internals then
|
|
|
|
for k, v in ipairs(internals) do
|
|
|
|
if v.type == "treenode" then
|
|
|
|
if self.open then
|
|
|
|
v:draw()
|
|
|
|
end
|
|
|
|
else
|
2014-06-29 05:16:25 +00:00
|
|
|
v:draw()
|
|
|
|
end
|
|
|
|
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
|
2014-09-20 14:58:36 +00:00
|
|
|
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
|
2014-07-06 03:15:51 +00:00
|
|
|
self.tree.selectednode = self
|
2014-07-06 03:19:33 +00:00
|
|
|
if onselectnode then
|
|
|
|
onselectnode(self.parent, self)
|
|
|
|
end
|
2014-07-06 03:15:51 +00:00
|
|
|
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)
|
|
|
|
|
2014-07-02 00:28:07 +00:00
|
|
|
if type(icon) == "string" then
|
|
|
|
self.icon = love.graphics.newImage(icon)
|
2019-03-06 23:19:58 +00:00
|
|
|
self.icon:setFilter("nearest", "nearest")
|
2014-07-02 00:28:07 +00:00
|
|
|
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
|
|
|
|
--]]---------------------------------------------------------
|
2014-07-02 00:49:48 +00:00
|
|
|
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
|
|
|
|
--]]---------------------------------------------------------
|
2014-07-02 00:49:48 +00:00
|
|
|
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
|
2019-03-06 23:19:58 +00:00
|
|
|
|
|
|
|
---------- module end ----------
|
|
|
|
end
|