--[[
file name : 技能模块第一版
author : Clark/陈泽丹
created : 2012-11-07
purpose :
--]]
module("工具库_事件机", package.seeall)
--查找obj位置
function find_obj_pos(_t, _p)
local len = table.getn( _t )
for i=1, len do
if _t[i] == _p then
return i
end
end
return nil
end
--否决事件监听
function new_vote_evt_listener()
local public = {}
function public.on_vote( _t_evt )
myPrint("未实现 new_vote_evt_listener", 1)
return false
end
return public
end
--执行消息监听
function new_action_evt_listener()
local public = {}
function public.on_action( _t_evt )
myPrint("未实现 new_action_evt_listener", 1)
end
return public
end
--完毕消息监听
function new_response_evt_listener()
local public = {}
function public.on_response( _t_evt )
myPrint("未实现 new_response_evt_listener", 1)
end
return public
end
--消息处理机
function new_evt_server()
local function new_event_map()
local this_public = {}
this_public.map = {}
function this_public.add_listener(_evt_iid, _p_recv)
if nil == this_public.map[_evt_iid] then
this_public.map[_evt_iid] = {}
end
local t = this_public.map[_evt_iid]
if nil == find_obj_pos(t, _p_recv) then
t[ table.getn(t) + 1 ] = _p_recv
end
end
function this_public.remove_listener(_evt_iid, _p_recv)
if nil ~= this_public.map[_evt_iid] then
local t = this_public.map[_evt_iid]
local id = find_obj_pos(t, _p_recv)
if nil ~= id then
local len = table.getn(t)
t[id] = t[len]
t[len] = nil
end
if table.getn(t) <= 0 then
this_public.map[_evt_iid] = nil
end
end
end
function this_public.clear()
this_public.map = {}
end
return this_public
end
local public = {}
public.vote_map = new_event_map()
public.action_map = new_event_map()
public.response_map = new_event_map()
function public.clear()
public.vote_map.clear()
public.action_map.clear()
public.response_map.clear()
end
function public.dispatch_evt( _evt_iid, _t_evt )
--记录当前服,用于回复消息
_t_evt.from_evt_svr = public
--否决
if nil ~= public.vote_map.map[ _evt_iid ] then
local t = public.vote_map.map[ _evt_iid ]
local len = table.getn( t )
for i=1, len do
if t[i].on_vote( _t_evt ) then
return
end
end
end
--触发
if nil ~= public.action_map.map[ _evt_iid ] then
local t = public.action_map.map[ _evt_iid ]
local len = table.getn( t )
for i=1, len do
t[i].on_action( _t_evt )
end
end
--完毕
if nil ~= public.response_map.map[ _evt_iid ] then
local t = public.response_map.map[ _evt_iid ]
local len = table.getn( t )
for i=1, len do
t[i].on_response( _t_evt )
end
end
end
return public
end
--[[
file name : 技能模块第一版
author : Clark/陈泽丹
created : 2012-11-07
purpose :
--]]
module("工具库_调试工具", package.seeall)
PACK_EvtSvr = require"工具库_事件机"
--技能服
G_trace_svr = PACK_EvtSvr.new_evt_server()
--输出
function do_print_on_trace_svr()
local public = PACK_EvtSvr.new_action_evt_listener()
function public.on_action( _evt )
print( _evt.evt_iid, _evt.text )
end
return public
end
trace_obj = do_print_on_trace_svr()
--打开输出开关
function open_trace( _evt_iid )
G_trace_svr.action_map.add_listener( _evt_iid, trace_obj )
end
--关闭输出开关
function close_trace( _evt_iid )
G_trace_svr.action_map.remove_listener( _evt_iid, trace_obj )
end
--输出调试语句
function trace_text( _evt_iid, _text )
G_trace_svr.dispatch_evt( _evt_iid, { evt_iid = _evt_iid, text = _text} )
end
--[[
open_trace( "fefd" )
trace_text( "fefd", "ttte4es")
close_trace( "fefd" )
trace_text( "fefd", "ttte4es")
--]]