Lua学习笔记Day5-Lua实现窗口类

Lua学习笔记Day5-Lua实现窗口类

目录

  • Lua实现窗口类

Lua实现窗口类

  • Lua实现窗口类

    窗口类需要具备如下特性:
    1) 有自己的名字
    2) 有自己的布局文件路径
    3) 初始化、析构、显示、隐藏
    4) 可以拥有一个父窗口,多个子窗口
    5) 需要方便通过名字查找其子窗口
    6) 具备添加子窗口
    7) 脱离父窗口的方法
    8) 递归查找指定名字的子窗口
    9) 递归删除指定名字的子窗口
    10) 控制台打印出某个窗口的整个父子关系


Base_Windows = {}
function Base_Windows:Create()
    local windows = {name,layoutPath,parent,children}
    --初始化
    function windows:Init(name)
        self.name = name
        self.layoutPath = ''
        self.parent = {}
        self.children = {}
    end
    --添加子窗口
    function windows:AddWindows(name)
        local child = Base_Windows:Create()
        child.name = name
        child.parent  = self
        child.children = {}
        table.insert(self.children,child)
        return child
    end
    --脱离父窗口
    function windows:Leave()
        local parent = self.parent
        for key,child in pairs(parent.children) do
            if child.name == self.name then
                parent.children[key] = nil
            end
        end
        self.parent = nil
    end
    --查找子窗口
    function windows:FindChild(name)
        local children = self.children
        if children~=nil then
            for key,child in pairs(children) do
                if child.name == name then
                    print('success find child :'..child.name..' '..child.parent.name)
                    return child
                else
                    child:FindChild(name)
                end
            end
        end
    end
    --删除子窗口
    function windows:DeleChild(name)
        local children = self.children
        if children~=nil then
            for key,child in pairs(children) do
                if child.name == name then
                    child:Destory()
                else
                    child:DeleChild(name)
                end
            end
        end
    end
    --打印层级关系
    function windows:PrintRelation()
        print(self.name)
        local children = self.children
        if children~=nil then
            for key,child in pairs(children) do
                child:PrintRelation()
            end
        end
    end
    --销毁窗口
    function windows:Destory()
        local children = self.children
        if children~=nil then
            for key,child in pairs(children) do
                child:Destory()
            end
        end

        self.name = nil
        self.layoutPath = nil
        self.parent = nil
        self.children = nil
        self = nil
    end
    --展示与隐藏,见下题
    function windows:Display()
    end
    function windows:Hide()
    end

    return windows
end

调用方法
local windows = Base_Windows:Create()
windows:Init('windows')
local windows1 = windows:AddWindows('windows1')
local windows2 = windows:AddWindows('windows2')
windows1:AddWindows('windows11')
windows2:AddWindows('windows21')
windows:FindChild('windows21')
windows:Display()
windows1:Leave()
windows2:DeleChild('windows21')
windows:Display()
windows:Destory()

猜你喜欢

转载自blog.csdn.net/Teddy_k/article/details/51429530