Add an example of how to use a closure in Lua in order to bind user data to an event handler

git-svn-id: svn+ssh://svn.gna.org/svn/warzone/trunk@5966 4a71c877-e1ca-e34f-864e-861f7616d084
master
Giel van Schijndel 2008-09-08 00:06:04 +00:00
parent f81ac77f91
commit 966e1bfedc
1 changed files with 9 additions and 3 deletions

View File

@ -2,10 +2,16 @@ wnd = betawidget.window("myWindow", 400, 400)
wnd:reposition(400, 50)
wnd:show()
wnd:addEventHandler(betawidget.EVT_MOUSE_CLICK,
function (self, evt, handlerId)
print("Clicked")
-- Closure example: The variable `clicked' is bound to the closure of the
-- anonymous function which we return here.
(function ()
local clicked = 0
return function (self, evt, handlerId)
clicked = clicked + 1
print(string.format("Clicked %d times", clicked))
self:reposition(0, 0)
end
end
end)()
)
wnd:addEventHandler(betawidget.EVT_KEY_DOWN,
function (self, evt, handlerId)