提交 00e36dc3 编写于 作者: 林新发

树界面节点逻辑优化

上级 3fae9d03
...@@ -34,6 +34,8 @@ function TreeLogic.Init() ...@@ -34,6 +34,8 @@ function TreeLogic.Init()
-- 根节点 -- 根节点
this.root = TreeNode.New("Root") this.root = TreeNode.New("Root")
this.root = this.MakeTree(data_table, this.root) this.root = this.MakeTree(data_table, this.root)
-- 打印树
local str = '' local str = ''
str = this.TreeToString(this.root, str) str = this.TreeToString(this.root, str)
log(str) log(str)
...@@ -67,18 +69,24 @@ function TreeLogic.MakeTree(tb, parent) ...@@ -67,18 +69,24 @@ function TreeLogic.MakeTree(tb, parent)
return parent return parent
end end
-- 把树转为字符串
function TreeLogic.TreeToString(node, str) function TreeLogic.TreeToString(node, str)
if nil ~= node.value then if nil ~= node.value then
local tabspace = ''
for i = 1, node.tab do
tabspace = tabspace .. ' '
end
if 'table' == type(node.value) then if 'table' == type(node.value) then
str = str .. string.format('▼ %s :', node.name) str = str .. string.format('%s▼ %s :\n', tabspace, node.name)
else else
str = str .. string.format('● %s : %s', node.name , tostring(node.value)) str = str .. string.format('%s● %s : %s\n', tabspace, node.name, tostring(node.value))
end end
end end
if nil ~= node.child then if nil ~= node.child then
for _, child_node in pairs(node.child) do for _, child_node in pairs(node.child) do
str = str .. this.TreeToString(child_node, str) -- 递归
str = this.TreeToString(child_node, str)
end end
end end
return str return str
......
...@@ -4,6 +4,7 @@ TreePanel.__index = TreePanel ...@@ -4,6 +4,7 @@ TreePanel.__index = TreePanel
local this = TreePanel local this = TreePanel
local instance = nil local instance = nil
this.uiNodeTb = nil
function TreePanel.Show() function TreePanel.Show()
instance = UITool.CreatePanelObj(instance, TreePanel, 'TreePanel', PANEL_ID.TREE_PANEL_ID, GlobalObjs.s_gamePanel) instance = UITool.CreatePanelObj(instance, TreePanel, 'TreePanel', PANEL_ID.TREE_PANEL_ID, GlobalObjs.s_gamePanel)
...@@ -16,6 +17,7 @@ end ...@@ -16,6 +17,7 @@ end
function TreePanel:OnShow(parent) function TreePanel:OnShow(parent)
local panelObj = UITool.Instantiate(parent, 16) local panelObj = UITool.Instantiate(parent, 16)
self.panelObj = panelObj self.panelObj = panelObj
this.uiNodeTb = {}
local binder = panelObj:GetComponent("PrefabBinder") local binder = panelObj:GetComponent("PrefabBinder")
self:SetUi(binder) self:SetUi(binder)
end end
...@@ -26,9 +28,10 @@ function TreePanel:SetUi(binder) ...@@ -26,9 +28,10 @@ function TreePanel:SetUi(binder)
self.Hide() self.Hide()
end) end)
local tree = TreeLogic.GetTree()
this.tiemForClone = binder:GetObj("itemForClone") this.tiemForClone = binder:GetObj("itemForClone")
LuaUtil.SafeActiveObj(this.tiemForClone, false) LuaUtil.SafeActiveObj(this.tiemForClone, false)
local tree = TreeLogic.GetTree()
this.ExpanNode(tree) this.ExpanNode(tree)
end end
...@@ -40,60 +43,79 @@ function TreePanel.ExpanNode(node) ...@@ -40,60 +43,79 @@ function TreePanel.ExpanNode(node)
end end
local index = 1 local index = 1
for _, child_node in pairs(node.child) do for _, child_node in pairs(node.child) do
local item = LuaUtil.CloneObj(this.tiemForClone) local uiUnit = {}
if this.uiNodeTb[child_node] then
-- 从缓存中取ui对象
uiUnit = this.uiNodeTb[child_node]
-- 显示
LuaUtil.SafeActiveObj(uiUnit.obj, true)
if child_node.isopen then
-- 递归, 展开子节点
this.ExpanNode(child_node)
end
else
-- 创建节点的UI对象
uiUnit.obj = LuaUtil.CloneObj(this.tiemForClone)
uiUnit.text = uiUnit.obj.transform:GetChild(0):GetComponent("Text")
uiUnit.btn = uiUnit.obj:GetComponent("Button")
-- 坐标缩进
uiUnit.text.transform.localPosition = uiUnit.text.transform.localPosition +
Vector3.New((child_node.tab - 1) * 50, 0, 0)
child_node.uiObj = uiUnit.obj
if not LuaUtil.IsNilOrNull(node.uiObj) then
-- 子节点塞在父节点下面
local siblingIndex = node.uiObj:GetComponent("RectTransform"):GetSiblingIndex()
child_node.uiObj:GetComponent("RectTransform"):SetSiblingIndex(siblingIndex + index)
index = index + 1
end
uiUnit.btn.onClick:AddListener(function()
local text = item.transform:GetChild(0):GetComponent("Text") if not child_node.isopen then
child_node.uiObj = item child_node.isopen = true
-- 递归, 展开子节点
this.ExpanNode(child_node)
else
-- 关闭子节点
this.CloseNode(child_node, false)
end
if not LuaUtil.IsNilOrNull(node.uiObj) then if type(child_node.value) == 'table' then
-- 子节点塞在父节点下面 uiUnit.text.text = (child_node.isopen and '▼ ' or '► ') .. child_node.name
local siblingIndex = node.uiObj:GetComponent("RectTransform"):GetSiblingIndex() end
child_node.uiObj:GetComponent("RectTransform"):SetSiblingIndex(siblingIndex + index) end)
index = index + 1 this.uiNodeTb[child_node] = uiUnit
end end
-- 更新展开文本
if type(child_node.value) == 'table' then if type(child_node.value) == 'table' then
text.text = (child_node.isopen and '▼ ' or '► ') .. child_node.name uiUnit.text.text = (child_node.isopen and '▼ ' or '► ') .. child_node.name
else else
text.text = '● ' .. child_node.name .. ': ' .. child_node.value uiUnit.text.text = '● ' .. child_node.name .. ': ' .. child_node.value
end end
-- 坐标缩进
text.transform.localPosition = text.transform.localPosition + Vector3.New((child_node.tab-1)*50, 0,0)
item:GetComponent("Button").onClick:AddListener(function()
if not child_node.isopen then
child_node.isopen = true
-- 递归, 展开子节点
this.ExpanNode(child_node)
else
-- 关闭子节点
this.CloseNode(child_node)
end
if type(child_node.value) == 'table' then
text.text = ( child_node.isopen and '▼ ' or '► ') .. child_node.name
end
end)
end end
end end
-- 关闭子节点 -- 关闭子节点
function TreePanel.CloseNode(node) function TreePanel.CloseNode(node, onlyHide)
if LuaUtil.IsNilOrNull(node.child) then if LuaUtil.IsNilOrNull(node.child) then
return return
end end
node.isopen = false if not onlyHide then
node.isopen = false
end
for _, child in pairs(node.child) do for _, child in pairs(node.child) do
child.isopen = false LuaUtil.SafeActiveObj(child.uiObj, false)
LuaUtil.SafeDestroyObj(child.uiObj)
if nil ~= child.child then if nil ~= child.child then
-- 递归关闭子节点 -- 递归关闭子节点
this.CloseNode(child) this.CloseNode(child, true)
end end
end end
end end
function TreePanel:OnHide() function TreePanel:OnHide()
LuaUtil.SafeDestroyObj(self.panelObj) LuaUtil.SafeDestroyObj(self.panelObj)
this.uiNodeTb = nil
instance = nil instance = nil
end end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册