红点系统架构(Lua篇)

一、数据
使用一个树管理红点的关系,每个节点中的数据结构如下:
treenod:{
_data:红点中的相关数据
parent:父节点
childlist:子节点列表
}

_data:{
name:用于识别同层级的节点
type:红点类型
isshow:红点显示标志位

	checkreddotfun:更新红点函数,返回值true/false
	handlereddotfun:显示红点时触发的函数

}
红点类型:
reddottype = {
none = 0,–无状态
onlyreddot = 1,–只显示红点
numreddot = 2,–存在数据的红点
}
二、具体代码

local reddottype = reddottype

local reddotdata = {
   }
reddotdata.treedic = {
   }

reddotdata.newreddotdata = function(name, type, checkreddotfun, handlereddotfun)
    local o = {
   
        name = name, 
        type = type or reddottype.none, 
        num = 0, 
        isshow = false, 
        checkreddotfun = checkreddotfun, 
        handlereddotfun = handlereddotfun,
    }
    return o
end


local treenode = {
   }
treenode.__index = treenode
function treenode:new(data)
    local o = {
   childlist = {
   }, parent = nil, _data = data}
    setmetatable(o, treenode)
    return o
end

function treenode:addchild(name, checktype, checkreddotfun, handlereddotfun)
    local data = reddotdata.newreddotdata(name, checktype, checkreddotfun, handlereddotfun)
    local node = treenode(data)
    node.parent = self
    node.index = #self.childlist + 1 
    self.childlist[#self.childlist + 1] = node  

    --添加的时候自动检测一次
    if checkreddotfun then
        self:check()
    end
    return node
end

--nodepath: 节点路径(不包含self节点的名称)
function treenode:findchild(nodepath)
    log.d(nodepath)
    nodepath = utils.string.split(nodepath, "[.]")
    local node = self
    local isfindchild = false
    local isfinishfind = false
    for i = 1,  #nodepath do
        local name = nodepath[i]
        isfindchild = false
        for _, child in ipairs(node.childlist) do
            if child._data.name == name then
                node = child
                isfindchild = true
                break
            end
        end

        --如果没找到当前的子节点,则不成功
        if not isfindchild then
            break
        end

        --如果全部节点都找到,则查找成功
        if i == #nodepath then
            isfinishfind = true
        end
    end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值