In Corona SDK, event listeners can only be added to the global Runtime object or to display objects. In order to broadcast messages to event listeners, the event dispatcher is limited to either one or the other. This limitation places messaging in the two scopes instead of between objects that are supposed to be talking to each other. I’ve seen many examples with display objects that are created solely for the purpose of dispatching events. This just doesn’t feel right to me; so I’m releasing my EventDispatcher, perhaps other developers may find it useful too.
Those who came from the good old Flash 5 days may remember FLEM (FLash Event Model). It was the first listener event model for Flash and ActionScript, and was created by Branden Hall and further developed and maintained by me. I’ve adapted the basic event model mechanism found in ActionScript 2/3 to Lua. This EventDispatcher has a similar interface as the listener model in Corona SDK, with some extra features thrown in (such as optional extra parameters when dispatching events, and returning status).
EventDispatcher provides custom event broadcaster/listener mechanism to regular Lua objects, it works as regular Lua 5.1 / 5.2 code, in Corona SDK, and likely other Lua-based frameworks.
[ Update: See http://swfoo.com/?p=718 ]
Basic usage:
local EvtD = require "EventDispatcher"
local listener = {
eventName = function(event)
print(event.name)
end
}
local broadcaster = EvtD()
broadcaster:addEventListener( "eventName", listener )
broadcaster:hasEventListener( "eventName", listener )
broadcaster:dispatchEvent( { name="eventName" } )
broadcaster:removeEventListener( "eventName", listener )
Sample code below demonstrates how it can be used:
local EvtD = require "EventDispatcher"
---------------------------------------------------------------------------
-- shared function for cowboys; shows the use of event.target
local function cowboyDraw(event, ...)
if event.subject then
print(event.target.name .." is ".. event.name .."ing a gun and shooting a ".. event.subject)
else
print(event.target.name .." is ".. event.name .."ing a gun")
end
end
---------------------------------------------------------------------------
-- table listeners
local cowboy1 = {
name = "Cowboy1",
draw = cowboyDraw
}
local cowboy2 = {
name = "Cowboy2",
draw = cowboyDraw
}
---------------------------------------------------------------------------
-- listener as table; shows the use of event.source
local iPad = {
turnOn = function(event, ...)
print("iPad is turned on by ".. event.source.name .." (table)")
end,
turnOff = function(event, ...)
print("iPad is turned off by ".. event.source.name .." (table)")
end
}
-- listener as function
local function turnOniPad(event, ...)
print("iPad is turned on by ".. event.source.name .." (function)")
end
---------------------------------------------------------------------------
-- basic artist draw function
local function artistDraw(event, ...)
print(event.target.name .." is ".. event.name .."ing a picture")
end
---------------------------------------------------------------------------
-- artist1 is both a listener and a event dispatcher
local artist1 = EvtD{
name = "Artist1",
draw = artistDraw,
-- responds to the 'rest' message, and sends a message to the iPad
rest = function(event, ...)
print(event.target.name .." is ".. event.name .."ing")
-- event.target is artist1
event.target:dispatchEvent( { name="turnOff" } )
end
}
-- artist1 tells iPad to listen to the 'turnOff' message
artist1:addEventListener( "turnOff", iPad)
---------------------------------------------------------------------------
-- artist2 is both a listener and a event dispatcher
local artist2 = EvtD{
name = "Artist2",
draw = function(event, ...)
-- event.target is artist2
event.target:dispatchEvent( { name="turnOn" } )
if event.subject then
print(event.target.name .." is ".. event.name .."ing a ".. event.subject .." on the iPad")
-- shows the use of extra arguments
local func,pieces,name = ...
func(pieces,name)
else
print(event.target.name .." is ".. event.name .."ing on the iPad")
end
end,
rest = function(event, ...)
event.target:dispatchEvent( { name="turnOff" } )
print(event.target.name .." is ".. event.name .."ing")
end
}
-- shows the use of table and function listeners
artist2:addEventListener( "turnOff", iPad)
artist2:addEventListener( "turnOn", turnOniPad)
---------------------------------------------------------------------------
-- mayor is a event dispatcher who tells others what to do
local mayor = EvtD()
-- mayor shows how much gold is collected
mayor.collectGold = function(nPieces, fromName)
print("Mayor collected ".. nPieces .." pieces of gold from ".. fromName)
end
-- mayor tells these four people to pay attention to different messages
mayor:addEventListener( "draw", cowboy1 )
mayor:addEventListener( "draw", cowboy2 )
mayor:addEventListener( "draw", artist1 )
mayor:addEventListener( "draw", artist2 )
mayor:addEventListener( "rest", artist2 )
-- mayor sends the 'rest' message
mayor:dispatchEvent( { name="rest" } )
print("Rested 1")
-- mayor tells everyone to draw
mayor:dispatchEvent( { name="draw" } )
-- mayor tells these people to stop listening to the 'draw' message
mayor:removeEventListener( "draw", cowboy1 )
mayor:removeEventListener( "draw", artist1 )
print("Removed")
-- mayor tells artist1 to listen to the 'rest' message
mayor:addEventListener( "rest", artist1 )
-- mayor tells whoever is still listening to rest
mayor:dispatchEvent( { name="rest" } )
print("Rested 2")
-- mayor tells whoever is still listening to draw, with a subject and extra parameters
mayor:dispatchEvent( { name="draw", subject="bandit" }, mayor.collectGold, 42, "Dave" )
Here is the output from the code:
iPad is turned off by Artist2 (table)
Artist2 is resting
Rested 1
Cowboy1 is drawing a gun
Cowboy2 is drawing a gun
Artist1 is drawing a picture
iPad is turned on by Artist2 (function)
Artist2 is drawing on the iPad
Removed
iPad is turned off by Artist2 (table)
Artist2 is resting
Artist1 is resting
iPad is turned off by Artist1 (table)
Rested 2
Cowboy2 is drawing a gun and shooting a bandit
iPad is turned on by Artist2 (function)
Artist2 is drawing a bandit on the iPad
Mayor collected 42 pieces of gold from Dave
And here is the EventDispatcher module:
Update (Sept 12, 2014): Added aliases ‘on’ and ’emit’ for ‘addEventListener’ and ‘dispatchEvent’ for Coronium GS.
-- EventDispatcher.lua
--
-- Provides custom event broadcaster/listener mechanism to regular Lua objects.
--
-- Created by: Dave Yang / Quantumwave Interactive Inc.
--
-- http://qwmobile.com | http://swfoo.com/?p=632
--
-- Version: 1.1.4
--
-- Basic usage:
-- local EvtD = require "EventDispatcher"
-- local listener = {
-- eventName = function(event)
-- print(event.name)
-- end
-- }
-- local broadcaster = EvtD()
-- broadcaster:addEventListener( "eventName", listener ) or broadcaster:on( "eventName", listener )
-- broadcaster:hasEventListener( "eventName", listener )
-- broadcaster:dispatchEvent( { name="eventName" } ) or broadcaster:emit( { name="eventName" } )
-- broadcaster:removeEventListener( "eventName", listener )
---------------------------------------------------------------------------
local EventDispatcher = {}
function EventDispatcher:init(o)
local o = o or {}
o._listeners = {}
self.__index = self
return setmetatable(o, self)
end
---------------------------------------------------------------------------
-- Check if the event dispatcher has registered listener for the event eventName
-- Return boolean (true/false), and if found also return the index of listener object
function EventDispatcher:hasEventListener(eventName, listener)
if eventName==nil or #eventName==0 or listener==nil then return false end
local a = self._listeners
if a==nil then return false end
for i,o in next,a do
if o~=nil and o.evt==eventName and o.obj==listener then
return true, i
end
end
return false
end
---------------------------------------------------------------------------
-- Add a listener for event eventName (a string).
-- Return addition status (true/false), position of listener is also returned if false; position=0 if failed
function EventDispatcher:addEventListener(eventName, listener)
local found,pos = self:hasEventListener(eventName, listener)
if found then return false,pos end
local a = self._listeners
if a==nil then return false,0 end
a[#a+1] = { evt=eventName, obj=listener }
return true
end
-- 'on' is an alias of 'addEventListener'
EventDispatcher.on = EventDispatcher.addEventListener
---------------------------------------------------------------------------
-- Dispatch event (a table, must have a 'name' key), with optional extra parameters.
-- Return dispatched status (true/false).
function EventDispatcher:dispatchEvent(event, ...)
if event==nil or event.name==nil or type(event.name)~="string" or #event.name==0 then return false end
local a = self._listeners
if a==nil then return false end
local dispatched = false
for i,o in next,a do
if o~=nil and o.obj~=nil and o.evt==event.name then
event.target = o.obj
event.source = self
if type(o.obj)=="function" then
o.obj(event, ...)
dispatched = true
elseif type(o.obj)=="table" then
local f = o.obj[event.name]
if f~= nil then
f(event, ...)
dispatched = true
end
end
end
end
return dispatched
end
-- 'emit' is an alias of 'dispatchEvent'
EventDispatcher.emit = EventDispatcher.dispatchEvent
---------------------------------------------------------------------------
-- Remove listener with eventName event from the event dispatcher.
-- Return removal status (true/false).
function EventDispatcher:removeEventListener(eventName, listener)
local found,pos = self:hasEventListener(eventName, listener)
if found then
table.remove(self._listeners, pos)
end
return found
end
---------------------------------------------------------------------------
-- create syntactic sugar to automatically call init()
setmetatable(EventDispatcher, { __call = function(_, ...) return EventDispatcher:init(...) end })
return EventDispatcher
EventDispatcher provides a broadcaster/listener event mechanism to regular Lua objects. Corona developers can write cleaner object-oriented messaging code that doesn’t rely on display objects or send messages from the global Runtime.
For the latest code, check out EventDispatcher at github.
One reply on “Send custom events with EventDispatcher for Corona SDK / Lua”
Thanks for sharing.