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

树界面节点逻辑优化

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