local isLog = false local function __DebugLog(str) if isLog then Log("" .. str .. "") end end -- 红点状态 local _data = {} -- 红点树关系结构数据 local _parentIndex = {} local _childNum = {} local _childIndex = {} -- 红点检测方法 local _checkFunc = {} -- 服务器保存的红点信息 local _serverRedData = {} -- 向父级检测设置红点值 local function _doSetRedValue(node, value) if _data[node] ~= value then _data[node] = value __DebugLog(Language[11409].. node .. ", value == ".. value) --如果有父级,更新父级 local _parent = _parentIndex[node] if _parent then local index = _childIndex[node] local _parentValue = 0 --- BitMath.lShiftOp(1, (index - 1)) 后为:: 1,2,4,8 .。。 既:0001 0010 0100 1000。。。 local _IndexBitValue = BitMath.lShiftOp(1, (index - 1)) if value > 0 then --- 父结点值为:或运算,将相应二进制位置上的 0 置为 1 _parentValue = BitMath.orOp(_data[_parent], _IndexBitValue) else --- BitMath.notOp(_IndexBitValue) 取反运算,值为: 1110 1101 1011 0111 .。。 --- 父节点值为:与运算,将相应二进制位置上的 1 置为 0 _parentValue = BitMath.andOp(_data[_parent], BitMath.notOp(_IndexBitValue)) end -- 向上一层级递归 _doSetRedValue(_parent, _parentValue) end end end local RedPointData = {} function RedPointData:SetParent(child, parent) -- 不能相反 if _parentIndex[parent] == child then LogError("试图将一个红点的父红点注册为其子红点, parent == ".. parent.. ", child == ".. child) return end -- 一个子节点只能有一个父节点 if _parentIndex[child] then -- LogError("红点id == " .. child.. ",重复设置了父级,请检查") return end -- 设置子节点对应的父节点 _parentIndex[child] = parent -- 记录父节点对应的子节点数量 if not _childNum[parent] then _childNum[parent] = 0 end _childNum[parent] = _childNum[parent] + 1 -- 记录子节点在父节点中的位置 _childIndex[child] = _childNum[parent] -- 设置数据 if not _data[child] then _data[child] = 0 end if not _data[parent] then _data[parent] = 0 end end -- 获取红点的父节点 function RedPointData:GetRedParent(node) return _parentIndex[node] end function RedPointData:GetRedChildren(node) local list = {} for c, p in pairs(_parentIndex) do if p == node then table.insert(list, c) end end -- 排序 table.sort(list, function(a, b) return _childIndex[a] < _childIndex[b] end) return list end -- 对叶子节点设置红点状态 function RedPointData:SetRedValue(node, value) if _childNum[node] and _childNum[node] > 0 then Log("子结点数量大于1,说明是高级结点,不能操作") return false end -- 设置红点数值 _doSetRedValue(node, value) return true end -- 获取红点状态 function RedPointData:GetRedValue(node) return _data[node] end -- 获取所有红点数据 function RedPointData:GetAllRedData() return _data end -- 注册红点检测方法 function RedPointData:AddCheckFunc(node, func, openType) if _childNum[node] and _childNum[node] > 0 then Log("红点id == ".. node .. ", 此节点是父节点,不可以注册检测方法") return end if _checkFunc[node] then Log("红点id == ".. node .. ", 重复注册了事件,请检查") return end if not RedPointData:GetServerRed(node) and not func then Log("红点id == ".. node .. ", 不是服务器红点,并注册事件为空,请检查") return end __DebugLog(Language[11410].. node .. Language[11411]) _checkFunc[node] = { func = func, openType = openType } end -- 删除检测方法 function RedPointData:RemoveCheckFunc(node) _checkFunc[node] = nil end -- 检测红点,成功返回true,失败返回 false function RedPointData:CheckRedPoint(node) local value = nil -- 判断是否解锁 if _checkFunc[node] and _checkFunc[node].openType then local isOpen = ActTimeCtrlManager.SingleFuncState(_checkFunc[node].openType) if not isOpen then value = 0 end end if not value then -- 判断是否是服务器红点 if RedPointData:GetServerRed(node) then value = RedPointData:GetServerRed(node) -- 判断是否注册了检测方法 elseif _checkFunc[node] and _checkFunc[node].func then value = _checkFunc[node].func(node) and 1 or 0 -- 都没有提示,出错 else Log("红点id == ".. node .. ", 没有注册检测事件,也不是服务器保存的红点,请检查") return false end end -- 数据正确性检测 if not value then Log("红点id == ".. node .. ", 数据错误,请检查") return false end -- 判断红点状态是否改变 if value == RedPointData:GetRedValue(node) then __DebugLog(Language[11410].. node .. ", value = ".. value ..Language[11412]) return false end -- 设置红点值 RedPointData:SetRedValue(node, value) return true end -- 获取红点检测方法list function RedPointData:GetCheckList() return _checkFunc end ---------服务器红点相关----------------- -- 设置服务器红点数据 function RedPointData:SetServerRedData(list) _serverRedData = list -- debug用 __DebugLog("++++++++++++++++") __DebugLog("++++++++++++++++") __DebugLog("++++++++++++++++") for rpType, value in ipairs(list) do __DebugLog(Language[11413]..rpType..", value == "..value) end __DebugLog("++++++++++++++++") __DebugLog("++++++++++++++++") __DebugLog("++++++++++++++++") end function RedPointData:GetServerRedData() return _serverRedData end -- 更新服务器红点状态 function RedPointData:SetServerRed(node, value) if not _serverRedData[node] then Log("红点id == "..node..",不是服务器保存红点,请检查!") return false end _serverRedData[node] = value return true end -- 获取服务器红点值 function RedPointData:GetServerRed(node) if not _serverRedData[node] then return end return _serverRedData[node] end return RedPointData