Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- #SimpleEventHandler 1.0.0.2
- Created in 2013 by Necro
- Licensed under CC0 1.0 Universal(CC0 1.0) (read below)
- ]]--
- --[[
- CC0 1.0 Universal (CC0 1.0) - Public Domain Dedication:
- The person who associated a work with this deed has dedicated the work to the public domain
- by waiving all of his or her rights to the work worldwide under copyright law, including all
- related and neighboring rights, to the extent allowed by law.
- You can copy, modify, distribute and perform the work, even for commercial purposes,
- all without asking permission. See Other Information below.
- ]]--
- SimpleEventHandler=SimpleEventHandler or {}
- SimpleEventHandler.listenerTable=SimpleEventHandler.listenerTable or {}
- function SimpleEventHandler.addEvent(e)
- if not SimpleEventHandler.listenerTable[e] then
- SimpleEventHandler.listenerTable[e]={}
- print("SEH:Added event "..tostring(e))
- return true
- else
- error("SEH exception:attempt to override an existing event")
- end
- end
- function SimpleEventHandler.removeEvent(e)
- if SimpleEventHandler.listenerTable[e] then
- SimpleEventHandler.listenerTable[e]=nil
- print("SEH:Removed event "..tostring(e))
- return true
- else
- error("SEH exception:attempt to remove a not existing event")
- end
- end
- --[[
- To be called by a script.
- ]]--
- function SimpleEventHandler.addListener(e,f)
- if not SimpleEventHandler.listenerTable[e] then
- error("SEH exception:attempt to add listener to a not existing event")
- end
- f = (type(f)=="string" and _G[f]) or f
- table.insert(SimpleEventHandler.listenerTable[e],f)
- print("SEH:Added listener "..tostring(f).." to "..tostring(e))
- return true
- end
- function SimpleEventHandler.callEvent(e,...)
- if not SimpleEventHandler.listenerTable[e] then
- error("SEH exception:attempt to call a not existing event")
- end
- local ret=nil
- for i,j in pairs(SimpleEventHandler.listenerTable[e]) do
- local temp=j(...)
- if ret==nil then
- ret=temp
- end
- end
- --print("SEH:Called event "..tostring(e))
- return ret
- end
- function SimpleEventHandler.removeListener(e,f)
- local ret=false
- for i,j in pairs(SimpleEventHandler.listenerTable[e]) do
- if j==f then
- table.remove(SimpleEventHandler.listenerTable[e],i)
- print("SEH:Removed listener "..tostring(f).." from "..tostring(e))
- ret=true
- end
- end
- if not ret then
- error("SEH exception:attempt to remove listener from not existing event")
- end
- return ret
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement