Page 1 of 1
Preview - new event handling
Posted: 17.07.2006, 07:36
by chris
A while ago, I added new event handling to Celestia's celx scripting system. It will be enabled in the next prerelease. Among other things, it will let you write a script that replaces Celestia's default keyboard handling with behavior of the script author's choice. There are two new functions added on the celestia object: registereventhandler and geteventhandler. registereventhandler takes two parameters: a string specifying the event type and the event handler. An event handler is a function that accepts a single table parameter and returns a boolean indicating whether or not it handled the event. If no event handler is registered, or if the registered event handler returns false, Celestia reverts to the default behavior for that event.
Here's a sample celx script that creates a keyboard handler:
Code: Select all
-- A sample keyboard handling function
function showmessage()
celestia:flash("Key pressed")
end
-- The table mapping key names to handlers
keyhandlers =
{
p = showmessage,
q = showmessage,
}
function handlekey(k)
-- k is a table containing the event properties; in this simple sample,
-- we're only interested in char, the name of the key that was pressed.
handler = keyhandlers[k.char]
if (handler ~= nil) then
handler()
return true
else
return false
end
end
This sample binds the keys p and q to the handler showmessage(). The bindings will remain until either Celestia is restarted or another script is run to unbind them.
Here's a useful little handler function that I wrote to toggle cloud shadows on and off, because switching them on and off is not yet built into Celestia.
Code: Select all
function togglecloudshadows()
t = celestia:getrenderflags()
celestia:setrenderflags{cloudshadows = not t.cloudshadows}
end
There are also mouseup, mousedown, and tick events. I'll post more information about them soon.
--Chris
Posted: 07.12.2007, 18:17
by Vincent
The above script from Chris doesn't seem to work anymore for me with the current CVS build... I'm quite sure it was running great a while ago, though...
Could anybody confirm or invalidate, please ?
("Key pressed" should be displayed whenever the [P] or [Q] key is pressed...)
Posted: 07.12.2007, 18:50
by selden
It doesn't work for me for any of the versions of Celestia that I have: pre3, pre4 or current cvs.
Posted: 07.12.2007, 18:54
by chris
I'll have a chance to look at this in more detail later, but I did find an apparent bug with pre-1.5.0 style key event handling. See
http://www.celestiaproject.net/forum/viewtopic ... 6511#96511
I think it has to do with the change from Lua 5.0 to 5.1.
--Chris
Posted: 07.12.2007, 19:04
by Vincent
chris wrote:I think it has to do with the change from Lua 5.0 to 5.1.
Chris,
The script doesn't work with a CVS version built again Lua 5.0 that I use to test the Lua Tools.
It doesn't work either when ifdefing the following line in celx.cpp (as you suggested), neither with Lua 5.0 nor with Lua 5.1:
Code: Select all
#if LUA_VER < 0x050100
// cleanup stack - is this necessary?
lua_settop(costate, stack_top);
#endif
Posted: 08.12.2007, 01:26
by chris
I think the problem is that in the original sample, a key bit of code isn't shown. In order to active the key event handler, this line is required after handlekey is defined.
Code: Select all
celestia:registereventhandler("key", handlekey)
After adding that line, the sample works fine.
--Chris
Posted: 08.12.2007, 10:57
by Vincent
chris wrote:After adding that line, the sample works fine.
Thanks Chris, your script now works fine for me too.
I was indeed surprised that no link between the event handler and the "handlekey" function was appearing in the script. I thought for a while that the "handlekey" function could have been hardcoded, but couldn't find a reference to it in the code...
Re: Preview - new event handling
Posted: 10.06.2010, 07:33
by Chuft-Captain
chris wrote:The bindings will remain until either Celestia is restarted or another script is run to unbind them.
Sorry to resurrect this old thread, but I have a question:
Is there a method available to unbind a custom keyhandler from within the running script?
It would be useful to be able to de-register a keyhandler without having to exit the running script, or at least get it to drop it's own keyhandler before exiting (without having to run a second script to do so. ie. "Script, clean up your own mess")
Something like a celestia:
deregistereventhandler(...) method might do the job.
(Incidentally, whilst you mention unbinding with a second script, as far as I can see, you haven't described the method for doing so. What is the current syntax to unbind, even if it is from a second script? )
Re: Preview - new event handling
Posted: 10.06.2010, 16:34
by Vincent
Chuft-Captain wrote:chris wrote:The bindings will remain until either Celestia is restarted or another script is run to unbind them.
Sorry to resurrect this old thread, but I have a question:
Is there a method available to unbind a custom keyhandler from within the running script?
It would be useful to be able to de-register a keyhandler without having to exit the running script, or at least get it to drop it's own keyhandler before exiting (without having to run a second script to do so. ie. "Script, clean up your own mess")
Something like a celestia:
deregistereventhandler(...) method might do the job.
(Incidentally, whilst you mention unbinding with a second script, as far as I can see, you haven't described the method for doing so. What is the current syntax to unbind, even if it is from a second script? )
I haven't tried it yet, but simply cleaning up the keyhandlers table should do it:
Re: Preview - new event handling
Posted: 10.06.2010, 23:53
by Chuft-Captain
Vincent wrote:I haven't tried it yet, but simply cleaning up the keyhandlers table should do it:
Of course it should! I'm embarrassed I didn't try that.
I'll give it a go now and report back.
Thanks Vincent.
CC
Re: Preview - new event handling
Posted: 11.06.2010, 00:56
by Chuft-Captain
keyhandlers = {} works well Vincent... the events get passed through to Celestia (ie. not handled) but unfortunately the script (and the keyhandler) are still running (just passing keys through without handling).
After clearing the keyhandler I also want to terminate the script completely, and return to Celestia, rather than having it still running with an empty keyhandler.
Any ideas?
Cheers
CC
Re: Preview - new event handling
Posted: 11.06.2010, 16:50
by Vincent
CC,
You may want to try the following line:
Code: Select all
celestia:registereventhandler("key", nil)
Re: Preview - new event handling
Posted: 13.06.2010, 08:59
by Chuft-Captain
Thanks Vincent,
That seems to work. I had tried a couple of other techniques but this appears to be the best solution. Thanks.
The more CELX scripting I do, the more I realize that a celestia:getbuild() or getversion() method would be very useful, as it would allow scripts to be written in a more robust way so that they wouldn't necessarily fall apart on older versions of Celestia.
For example, allowing conditional coding such as ...
Code: Select all
if celestia:getbuild() >= ???? -- (version 1.6.1)
celestia:settextcolor(0.0, 1.0, 0.0)
end
CC