TreePanel.lua 3.7 KB
Newer Older
林新发 已提交
1 2 3 4 5 6
-- 树界面
TreePanel = TreePanel or {}
TreePanel.__index = TreePanel

local this = TreePanel
local instance = nil
林新发 已提交
7
this.uiNodeTb = nil
林新发 已提交
8 9 10 11 12 13 14 15 16 17 18 19

function TreePanel.Show()
    instance = UITool.CreatePanelObj(instance, TreePanel, 'TreePanel', PANEL_ID.TREE_PANEL_ID, GlobalObjs.s_gamePanel)
end

function TreePanel.Hide()
    UITool.HidePanel(instance)
end

function TreePanel:OnShow(parent)
    local panelObj = UITool.Instantiate(parent, 16)
    self.panelObj = panelObj
林新发 已提交
20
    this.uiNodeTb = {}
林新发 已提交
21 22 23 24 25 26 27 28 29 30 31 32
    local binder = panelObj:GetComponent("PrefabBinder")
    self:SetUi(binder)
end

-- UI交互
function TreePanel:SetUi(binder)
    UGUITool.SetButton(binder, "backBtn", function(btn)
        self.Hide()
    end)

    this.tiemForClone = binder:GetObj("itemForClone")
    LuaUtil.SafeActiveObj(this.tiemForClone, false)
林新发 已提交
33 34

    local tree = TreeLogic.GetTree()
林新发 已提交
35 36 37 38 39 40 41 42 43 44 45
    this.ExpanNode(tree)

end

-- 展开节点
function TreePanel.ExpanNode(node)
    if nil == node.child then
        return
    end
    local index = 1
    for _, child_node in pairs(node.child) do
林新发 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
        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()
林新发 已提交
74

林新发 已提交
75 76 77 78 79 80 81 82
                if not child_node.isopen then
                    child_node.isopen = true
                    -- 递归, 展开子节点
                    this.ExpanNode(child_node)
                else
                    -- 关闭子节点
                    this.CloseNode(child_node, false)
                end
林新发 已提交
83

林新发 已提交
84 85 86 87 88
                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
林新发 已提交
89
        end
林新发 已提交
90
        -- 更新展开文本
林新发 已提交
91
        if type(child_node.value) == 'table' then
林新发 已提交
92
            uiUnit.text.text = (child_node.isopen and '▼ ' or '► ') .. child_node.name
林新发 已提交
93
        else
林新发 已提交
94
            uiUnit.text.text = '● ' .. child_node.name .. ': ' .. child_node.value
林新发 已提交
95 96 97 98 99
        end
    end
end

-- 关闭子节点
林新发 已提交
100
function TreePanel.CloseNode(node, onlyHide)
林新发 已提交
101 102 103
    if LuaUtil.IsNilOrNull(node.child) then
        return
    end
林新发 已提交
104 105 106
    if not onlyHide then
        node.isopen = false
    end
林新发 已提交
107 108

    for _, child in pairs(node.child) do
林新发 已提交
109
        LuaUtil.SafeActiveObj(child.uiObj, false)
林新发 已提交
110 111
        if nil ~= child.child then
            -- 递归关闭子节点
林新发 已提交
112
            this.CloseNode(child, true)
林新发 已提交
113 114 115 116 117 118
        end
    end
end

function TreePanel:OnHide()
    LuaUtil.SafeDestroyObj(self.panelObj)
林新发 已提交
119
    this.uiNodeTb = nil
林新发 已提交
120 121
    instance = nil
end