Update Treeview.lua

Small change for treeview
This commit is contained in:
Robert Jelic
2023-05-21 19:45:14 +02:00
parent 2293aacdec
commit 1b7f61323b

View File

@@ -28,11 +28,11 @@ return function(name, basalt)
local onSelect local onSelect
node = { node = {
getChildren = function() getChildren = function(self)
return children return children
end, end,
setParent = function(p) setParent = function(self, p)
if(parent~=nil)then if(parent~=nil)then
parent.removeChild(parent.findChildrenByText(node.getText())) parent.removeChild(parent.findChildrenByText(node.getText()))
end end
@@ -41,11 +41,11 @@ return function(name, basalt)
return node return node
end, end,
getParent = function() getParent = function(self)
return parent return parent
end, end,
addChild = function(text, expandable) addChild = function(self, text, expandable)
local childNode = newNode(text, expandable) local childNode = newNode(text, expandable)
childNode.setParent(node) childNode.setParent(node)
table.insert(children, childNode) table.insert(children, childNode)
@@ -53,7 +53,7 @@ return function(name, basalt)
return childNode return childNode
end, end,
setExpanded = function(exp) setExpanded = function(self, exp)
if(expandable)then if(expandable)then
expanded = exp expanded = exp
end end
@@ -61,11 +61,11 @@ return function(name, basalt)
return node return node
end, end,
isExpanded = function() isExpanded = function(self)
return expanded return expanded
end, end,
onSelect = function(...) onSelect = function(self, ...)
for _,v in pairs(table.pack(...))do for _,v in pairs(table.pack(...))do
if(type(v)=="function")then if(type(v)=="function")then
onSelect = v onSelect = v
@@ -74,23 +74,23 @@ return function(name, basalt)
return node return node
end, end,
callOnSelect = function() callOnSelect = function(self)
if(onSelect~=nil)then if(onSelect~=nil)then
onSelect(node) onSelect(node)
end end
end, end,
setExpandable = function(expandable) setExpandable = function(self, expandable)
expandable = expandable expandable = expandable
base:updateDraw() base:updateDraw()
return node return node
end, end,
isExpandable = function() isExpandable = function(self)
return expandable return expandable
end, end,
removeChild = function(index) removeChild = function(self, index)
if(type(index)=="table")then if(type(index)=="table")then
for k,v in pairs(index)do for k,v in pairs(index)do
if(v==index)then if(v==index)then
@@ -104,7 +104,7 @@ return function(name, basalt)
return node return node
end, end,
findChildrenByText = function(searchText) findChildrenByText = function(self, searchText)
local foundNodes = {} local foundNodes = {}
for _, child in ipairs(children) do for _, child in ipairs(children) do
if string.find(child.getText(), searchText) then if string.find(child.getText(), searchText) then
@@ -114,11 +114,11 @@ return function(name, basalt)
return foundNodes return foundNodes
end, end,
getText = function() getText = function(self)
return text return text
end, end,
setText = function(t) setText = function(self, t)
text = t text = t
base:updateDraw() base:updateDraw()
return node return node
@@ -129,7 +129,7 @@ return function(name, basalt)
end end
local root = newNode("Root", true) local root = newNode("Root", true)
root.setExpanded(true) root:setExpanded(true)
local object = { local object = {
init = function(self) init = function(self)
@@ -251,7 +251,7 @@ return function(name, basalt)
local function checkNodeClick(node, level) local function checkNodeClick(node, level)
if y == oby+currentLine-1 then if y == oby+currentLine-1 then
if x >= obx and x < obx + w then if x >= obx and x < obx + w then
node.setExpanded(not node.isExpanded()) node:setExpanded(not node:isExpanded())
self:selectionHandler(node) self:selectionHandler(node)
self:setValue(node) self:setValue(node)
self:updateDraw() self:updateDraw()
@@ -259,8 +259,8 @@ return function(name, basalt)
end end
end end
currentLine = currentLine + 1 currentLine = currentLine + 1
if node.isExpanded() then if node:isExpanded() then
for _, child in ipairs(node.getChildren()) do for _, child in ipairs(node:getChildren()) do
if checkNodeClick(child, level + 1) then if checkNodeClick(child, level + 1) then
return true return true
end end
@@ -269,7 +269,7 @@ return function(name, basalt)
return false return false
end end
for _, item in ipairs(root.getChildren()) do for _, item in ipairs(root:getChildren()) do
if checkNodeClick(item, 1) then if checkNodeClick(item, 1) then
return true return true
end end
@@ -291,14 +291,14 @@ return function(name, basalt)
local visibleLines = 0 local visibleLines = 0
local function countVisibleLines(node, level) local function countVisibleLines(node, level)
visibleLines = visibleLines + 1 visibleLines = visibleLines + 1
if node.isExpanded() then if node:isExpanded() then
for _, child in ipairs(node.getChildren()) do for _, child in ipairs(node:getChildren()) do
countVisibleLines(child, level + 1) countVisibleLines(child, level + 1)
end end
end end
end end
for _, item in ipairs(root.getChildren()) do for _, item in ipairs(root:getChildren()) do
countVisibleLines(item, 1) countVisibleLines(item, 1)
end end
@@ -335,14 +335,14 @@ return function(name, basalt)
currentLine = currentLine + 1 currentLine = currentLine + 1
if node.isExpanded() then if node:isExpanded() then
for _, child in ipairs(node.getChildren()) do for _, child in ipairs(node:getChildren()) do
drawNode(child, level + 1) drawNode(child, level + 1)
end end
end end
end end
for _, item in ipairs(root.getChildren()) do for _, item in ipairs(root:getChildren()) do
drawNode(item, 1) drawNode(item, 1)
end end
end) end)