【功能】Lua层的全局事件管理系统

1.EventManager 全局的管理类

2.EventType 事件类型

3..Lua层Common工具目录去require对应文件目录的脚本文件

--事件类型
local EventType = {
    TestMsg = 1,
}

return EventType
local EventManager = class();
EventManager.msgMap = {}

local function HaveSameFunc(tagTable, func)
    for i=0, #tagTable do
        if tagTable[i] == func then
            return true
        end
    end
    return false
end

local function GetFuncIndex(table, func)
    if table == nil then
        return
    end
    for i=1, #table do
        if func == table[i] then
            return i
        end
    end
    return
end

--注册
function EventManager.RegisterMsg(msgid, func)
    if EventManager.msgMap[msgid] ~= nil then
        local callfunctable = EventManager.msgMap[msgid]
        if callfunctable == nil then
            callfunctable = {}
            table.insert(callfunctable, func)
            return
        end
        local findfunc = HaveSameFunc(callfunctable, func)
        if not findfunc then
            table.insert(callfunctable, func)
            return
        end
        return
    end
    EventManager.msgMap[msgid] = {}
    table.insert(EventManager.msgMap[msgid], func)
end

--反注册
function EventManager.UnRegisterMsg(msgid, func)
    if EventManager.msgMap[msgid] ~= nil then
        local callfunctable = EventManager.msgMap[msgid]
        if callfunctable == nil then
            return
        end

        local findfunc = HaveSameFunc(callfunctable, func)
        if findfunc then
           local delIndex = GetFuncIndex(callfunctable, func)
           if delIndex then
               table.remove(callfunctable, delIndex)
           end
        end
    end
end

--抛事件
function EventManager.DispatchMsg(msgid,...)
    if EventManager.msgMap[msgid]~= nil then
        local callfunctable = EventManager.msgMap[msgid]
        if callfunctable == nil then
            return
        end
        for i=1, #callfunctable do
            callfunctable[i](...)
        end
    end
end

return EventManager

业务层的使用

local AccountPanelUI = BaseClass("AccountPanelUI",UIBaseView)
local base = UIBaseView

function AccountPanelUI.OnCreate(self)
    base.OnCreate(self)

    self:RegMsg()
end

functiion AccountPanelUI:RegMsg()
    self.funTable = {}

    self.funTable.OnClick= function ()
        print("test event.............")
    end

    EventManager.RegisterMsg(EventType.TestMsg, self.funTable.OnClick)
end

function AccountPanelUI:UnRegMsg()
    EventManager.UnRegisterMsg(EventType.TestMsg, self.funTable.OnClick)
    self.funTable = nil
end

return AccountPanelUI

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值