local RedPointNode = {} local private = {} local EventSystem = require('Event/EventSystem') private.logicNodeList = {} private.redPointTreeList = {} ---NodeMT,通过元表的方式,把Add方法暴露给使用者 ---通过add方法直接添加该节点的孩子节点,以此类推 --region -- **************** NodeMT ******************* -- local NodeMT = {} NodeMT.__index = NodeMT ---AddChildRpNode方法可以用于初始化也可以用于更新节点状态 function NodeMT:AddChildRpNode(eventId, key, userData, logicTable, state) if not eventId then logging.error(LTAG.ERROR, "欲添加一个eventId为:", eventId, "的子红点节点!请程序检查!!!") end if self.childrenNodeList[key] then private.UpdateSingleNodeAndDispatch(self.childrenNodeList[key], state) return self.childrenNodeList[key] end local node = { parentNode = self, --父节点 eventId = eventId, --派发事件ID key = key, --使用者定义的key userData = userData, --派发事件携带的参数 childrenNodeList = {}, --子节点列表 state = state or false, --节点红点状态 } node = setmetatable(node, NodeMT) self.childrenNodeList[key] = node if logicTable then --建立映射表 local rootEventId = private.GetRootNode(node).eventId if not private.logicNodeList[rootEventId] then private.logicNodeList[rootEventId] = {} end local temp = private.logicNodeList[rootEventId] for i, v in ipairs(logicTable) do if i == #logicTable then temp[v] = node break end if not temp[v] then temp[v] = {} end temp = temp[v] end end return node end --endregion -- **************** NodeMT ******************* -- ---添加根节点的方法,需要先调用确定好根节点 ---然后在通过跟根节点的add方法添加相应的子节点 function RedPointNode.AddRootRpNode(eventId) local node = { eventId = eventId, childrenNodeList = {}, state = false, } node = setmetatable(node, NodeMT) private.redPointTreeList[eventId] = node return node end function RedPointNode.GetRootNode(eventId) return private.redPointTreeList[eventId] end ---通过logic参数的方式获得node节点 function private.GetNodeByLogicParams(rootEventId, ...) if MDL.Unlock.IsUnlock(MDL.Unlock.FunctionType.EntryRedPoint) == false then return nil end local value = private.logicNodeList[rootEventId] if value then for i = 1, select("#", ...) do if value then value = value[select(i, ...)] else return nil end end end return value end ---通过logic参数的方式获得node节点的状态 function RedPointNode.GetNodeStateByLogicParams(rootEventId, ...) local node = private.GetNodeByLogicParams(rootEventId, ...) if node then return node.state end return false end ---通过logic参数的方式获得node节点 function RedPointNode.GetNodeByLogicParams(rootEventId, ...) local node = private.GetNodeByLogicParams(rootEventId, ...) if node then return node end end ---通过eventid和index一层层找到某个节点的方法 function RedPointNode.GetNodeStateByMultiKey(rootEventId, ...) if MDL.Unlock.IsUnlock(MDL.Unlock.FunctionType.EntryRedPoint) == false then return nil end local node = private.redPointTreeList[rootEventId] for i = 1, select("#", ...) do local key = select(i, ...) if node then node = node.childrenNodeList[key] end end if node then return node.state else return nil end end ---单独更新节点事件并派发 function private.UpdateSingleNodeAndDispatch(node, state) if node.state ~= state then node.state = state EventSystem.Dispatch(node.eventId, node.state, node.userData) end end ---初始化一个根节点下的所有节点 function RedPointNode.InitAllNodes(rootEventId) local node = private.redPointTreeList[rootEventId] private.RefreshNodeFromParent(node) end function RedPointNode.RefreshAllNodes() for _,node in pairs(private.redPointTreeList) do private.RefreshNodeFromParent(node) end end ---从某个父节点开始更新其下的所有节点 function private.RefreshNodeFromParent(node) if MDL.Unlock.IsUnlock(MDL.Unlock.FunctionType.EntryRedPoint) == false then return end if next(node.childrenNodeList) == nil then return end local state = false for key, childNode in pairs(node.childrenNodeList) do private.RefreshNodeFromParent(childNode) if childNode.state == true then private.UpdateSingleNodeAndDispatch(node, true) state = true end end node.state = state end function RedPointNode.UpdateNodeStateByLogicParams(state, rootEventId, ...) if MDL.Unlock.IsUnlock(MDL.Unlock.FunctionType.EntryRedPoint) == false then return end local node = private.GetNodeByLogicParams(rootEventId, ...) if node then private.UpdateNodeState(node, state) end end function private.UpdateNodeState(node, state) private.UpdateSingleNodeAndDispatch(node, state) private.RefreshNodeByChild(node) end ---通过一个子节点,层层向上更新父节点并派发 function private.RefreshNodeByChild(childNode) local parent = childNode.parentNode if parent == nil then return end local newState = false if childNode.state == true then newState = true else newState = private.GetNodeStateByChildren(parent) end private.UpdateSingleNodeAndDispatch(parent, newState) private.RefreshNodeByChild(parent) end function private.GetNodeStateByChildren(node) for key, childNode in pairs(node.childrenNodeList) do if childNode.state == true then return true end end return false end ---获取某个节点的根节点 function private.GetRootNode(node) local parentNode = node.parentNode while(parentNode.parentNode ~= nil) do parentNode = parentNode.parentNode end return parentNode end --region 强制派发的各种方法 ---强制刷新并派发所有红点事件 function RedPointNode.ForceRefreshAllNodes() for _,node in pairs(private.redPointTreeList) do private.ForceRefreshNodeFromParent(node) end end ---从某个父节点开始更新其下的所有节点 function private.ForceRefreshNodeFromParent(node) if MDL.Unlock.IsUnlock(MDL.Unlock.FunctionType.EntryRedPoint) == false then return end if next(node.childrenNodeList) == nil then return end local state = false for key, childNode in pairs(node.childrenNodeList) do private.ForceRefreshNodeFromParent(childNode) if childNode.state == true then node.state = true EventSystem.Dispatch(node.eventId, node.state, node.userData) state = true end end node.state = state end --endregion return RedPointNode