 |
forums.ps2dev.org Homebrew PS2, PSP & PS3 Development Discussions
|
| View previous topic :: View next topic |
| Author |
Message |
Durante
Joined: 02 Oct 2005 Posts: 65 Location: Austria
|
Posted: Mon Oct 31, 2005 5:01 am Post subject: [code] Global callbacks (cool!) |
|
|
I wanted to be able to make screenshots and do some debugging stuff from anywhere in my program without cluttering up my UI/game loop code, so I came up with this:
globalCallbacks.lua
| Code: | ---- An object implementing global key functions ----
-------------------------------------------------------------------------------
GlobalCallbacks = {
funcs = {},
ctrlread = Controls.read
}
function GlobalCallbacks:register(f)
table.insert(self.funcs, f)
end
function GlobalCallbacks:remove(f)
for i, v in pairs(self.funcs) do
if v == f then
table.remove(self.funcs, i)
end
end
end
function GlobalCallbacks:read()
local ctrl = self.ctrlread()
for _, v in pairs(self.funcs) do
v(ctrl)
end
return ctrl
end
Controls.read = function()
return GlobalCallbacks:read()
end |
You use it like this:
| Code: | dofile("src/globalcallbacks.lua")
GlobalCallbacks:register(function(ctrl)
if ctrl:select() then
screen:save("screenshot.png")
end
end) |
Simple, but effective. (i.e. with the above code you can now make a screenshot by pressing select, EVERYWHERE controls are read)
This is why I love dynamic languages. |
|
| Back to top |
|
 |
Durante
Joined: 02 Oct 2005 Posts: 65 Location: Austria
|
Posted: Mon Oct 31, 2005 10:04 am Post subject: |
|
|
Here's an improved version that allows you to use Controls.read normally in the handlers without causing a stack overflow ;)
| Code: |
---- An object implementing global key functions ----
-------------------------------------------------------------------------------
GlobalCallbacks = {
funcs = {},
ctrlread = Controls.read,
fakeread = nil
}
function GlobalCallbacks:register(f)
table.insert(self.funcs, f)
end
function GlobalCallbacks:remove(f)
for i, v in pairs(self.funcs) do
if v == f then
table.remove(self.funcs, i)
end
end
end
function GlobalCallbacks:clear()
self.funcs = {}
end
function GlobalCallbacks:read()
local ctrl = self.ctrlread()
Controls.read = self.ctrlread
for _, v in pairs(self.funcs) do
v(ctrl)
end
Controls.read = self.fakeread
return ctrl
end
function GlobalCallbacks:cleanup()
Controls.read = self.ctrlread
end
GlobalCallbacks.fakeread = function()
return GlobalCallbacks:read()
end
Controls.read = GlobalCallbacks.fakeread |
You should call GlobalCallbacks:cleanup() before exiting if you want your program to play nicely with other Lua stuff launched in the same session. |
|
| Back to top |
|
 |
chaos
Joined: 10 Apr 2005 Posts: 135
|
Posted: Mon Oct 31, 2005 2:40 pm Post subject: |
|
|
very nice idea..! _________________ Chaosmachine Studios: High Quality Homebrew. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|