LoveFrames/loveframes/objects/internal/treenode.lua
rozenmad edab2be4c7 Added grid span feature, fixes
- Grid span feature
- Added offset from parent to update each element (for correct positioning relative to each parent)
- OnClick event rework
- Multichoice: new function GetChoiceIndex
- Added positioning of items in the grid using the align parameter in AddItem
- Correct positioning of multichoice-arrow image
- Fixed stencil test for list
- Fixed a bug that can trigger OnClick events again, even if the mouse cursor is not already hovering over the object
- Fixed a bug where "backspace" leads to an incorrect positioning of the entry if the text is overflowed 'textinput' element
2021-09-05 20:46:49 +03:00

316 lines
6.8 KiB
Lua

--[[------------------------------------------------
-- Love Frames - A GUI library for LOVE --
-- Copyright (c) 2012-2014 Kenny Shields --
--]]------------------------------------------------
return function(loveframes)
---------- module start ----------
-- 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
self.open = false
self.internal = true
self.internals = {}
self.icon = nil
self.OnOpen = nil
self.OnClose = nil
self:SetDrawFunc()
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()
if loveframes.state ~= self.state then
return
end
if not self.visible then
return
end
-- set the object's draw order
self:SetDrawOrder()
local drawfunc = self.Draw or self.drawfunc
if drawfunc then
drawfunc(self)
end
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
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
if self.hover and button == 1 then
local time = os.time()
if self.lastclick + 0.30 > time then
self.open = not self.open
self.lastclick = 0
else
self.lastclick = time
end
local onselectnode = self.tree.OnSelectNode
self.tree.selectednode = self
if onselectnode then
onselectnode(self.parent, self)
end
end
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
if self.hover then
if button == 2 then
local onselectnoderightclick = self.tree.OnSelectNodeRightClick
self.tree.selectednode = self
if onselectnoderightclick then
onselectnoderightclick(self.parent, self)
end
end
end
end
--[[---------------------------------------------------------
- func: SetIcon(icon)
- desc: sets the object's icon
--]]---------------------------------------------------------
function newobject:SetIcon(icon)
if type(icon) == "string" then
self.icon = love.graphics.newImage(icon)
self.icon:setFilter("nearest", "nearest")
else
self.icon = icon
end
return self
end
--[[---------------------------------------------------------
- func: GetIcon()
- desc: gets the object's icon
--]]---------------------------------------------------------
function newobject:GetIcon()
return self.icon
end
--[[---------------------------------------------------------
- func: SetText(text)
- desc: sets the object's text
--]]---------------------------------------------------------
function newobject:SetText(text)
self.text = text
return self
end
--[[---------------------------------------------------------
- func: GetText()
- desc: gets the object's text
--]]---------------------------------------------------------
function newobject:GetText()
return self.text
end
--[[---------------------------------------------------------
- func: AddNode(text)
- desc: adds a new node to the object
--]]---------------------------------------------------------
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
--[[---------------------------------------------------------
- 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
function newobject:SetSelected()
self.tree.selectednode = self
return self
end
--[[---------------------------------------------------------
- func: SetOpen(bool)
- desc: sets whether or not the object is open
--]]---------------------------------------------------------
function newobject:SetOpen(bool)
self.open = bool
return self
end
--[[---------------------------------------------------------
- func: GetOpen()
- desc: gets whether or not the object is open
--]]---------------------------------------------------------
function newobject:GetOpen()
return self.open
end
---------- module end ----------
end