Preview - new event handling

All about writing scripts for Celestia in Lua and the .cel system
Topic author
chris
Site Admin
Posts: 4211
Joined: 28.01.2002
With us: 22 years 9 months
Location: Seattle, Washington, USA

Preview - new event handling

Post #1by chris » 17.07.2006, 07:36

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

Vincent
Developer
Posts: 1356
Joined: 07.01.2005
With us: 19 years 10 months
Location: Nancy, France

Post #2by Vincent » 07.12.2007, 18:17

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...)
Last edited by Vincent on 07.12.2007, 21:10, edited 3 times in total.
@+
Vincent

Celestia Qt4 SVN / Celestia 1.6.1 + Lua Edu Tools v1.2
GeForce 8600 GT 1024MB / AMD Athlon 64 Dual Core / 4Go DDR2 / XP SP3

Avatar
selden
Developer
Posts: 10192
Joined: 04.09.2002
With us: 22 years 2 months
Location: NY, USA

Post #3by selden » 07.12.2007, 18:50

It doesn't work for me for any of the versions of Celestia that I have: pre3, pre4 or current cvs.
Selden

Topic author
chris
Site Admin
Posts: 4211
Joined: 28.01.2002
With us: 22 years 9 months
Location: Seattle, Washington, USA

Post #4by chris » 07.12.2007, 18:54

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

Vincent
Developer
Posts: 1356
Joined: 07.01.2005
With us: 19 years 10 months
Location: Nancy, France

Post #5by Vincent » 07.12.2007, 19:04

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
@+
Vincent

Celestia Qt4 SVN / Celestia 1.6.1 + Lua Edu Tools v1.2
GeForce 8600 GT 1024MB / AMD Athlon 64 Dual Core / 4Go DDR2 / XP SP3

Topic author
chris
Site Admin
Posts: 4211
Joined: 28.01.2002
With us: 22 years 9 months
Location: Seattle, Washington, USA

Post #6by chris » 08.12.2007, 01:26

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

Vincent
Developer
Posts: 1356
Joined: 07.01.2005
With us: 19 years 10 months
Location: Nancy, France

Post #7by Vincent » 08.12.2007, 10:57

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...
@+
Vincent

Celestia Qt4 SVN / Celestia 1.6.1 + Lua Edu Tools v1.2
GeForce 8600 GT 1024MB / AMD Athlon 64 Dual Core / 4Go DDR2 / XP SP3

Avatar
Chuft-Captain
Posts: 1779
Joined: 18.12.2005
With us: 18 years 11 months

Re: Preview - new event handling

Post #8by Chuft-Captain » 10.06.2010, 07:33

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? )
"Is a planetary surface the right place for an expanding technological civilization?"
-- Gerard K. O'Neill (1969)

CATALOG SYNTAX HIGHLIGHTING TOOLS LAGRANGE POINTS

Vincent
Developer
Posts: 1356
Joined: 07.01.2005
With us: 19 years 10 months
Location: Nancy, France

Re: Preview - new event handling

Post #9by Vincent » 10.06.2010, 16:34

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:

Code: Select all

keyhandlers = {};
@+
Vincent

Celestia Qt4 SVN / Celestia 1.6.1 + Lua Edu Tools v1.2
GeForce 8600 GT 1024MB / AMD Athlon 64 Dual Core / 4Go DDR2 / XP SP3

Avatar
Chuft-Captain
Posts: 1779
Joined: 18.12.2005
With us: 18 years 11 months

Re: Preview - new event handling

Post #10by Chuft-Captain » 10.06.2010, 23:53

Vincent wrote:I haven't tried it yet, but simply cleaning up the keyhandlers table should do it:

Code: Select all

keyhandlers = {};
Of course it should! I'm embarrassed I didn't try that. :oops:
I'll give it a go now and report back.

Thanks Vincent.
CC
"Is a planetary surface the right place for an expanding technological civilization?"
-- Gerard K. O'Neill (1969)

CATALOG SYNTAX HIGHLIGHTING TOOLS LAGRANGE POINTS

Avatar
Chuft-Captain
Posts: 1779
Joined: 18.12.2005
With us: 18 years 11 months

Re: Preview - new event handling

Post #11by Chuft-Captain » 11.06.2010, 00:56

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
"Is a planetary surface the right place for an expanding technological civilization?"
-- Gerard K. O'Neill (1969)

CATALOG SYNTAX HIGHLIGHTING TOOLS LAGRANGE POINTS

Vincent
Developer
Posts: 1356
Joined: 07.01.2005
With us: 19 years 10 months
Location: Nancy, France

Re: Preview - new event handling

Post #12by Vincent » 11.06.2010, 16:50

CC,

You may want to try the following line:

Code: Select all

celestia:registereventhandler("key", nil)
@+
Vincent

Celestia Qt4 SVN / Celestia 1.6.1 + Lua Edu Tools v1.2
GeForce 8600 GT 1024MB / AMD Athlon 64 Dual Core / 4Go DDR2 / XP SP3

Avatar
Chuft-Captain
Posts: 1779
Joined: 18.12.2005
With us: 18 years 11 months

Re: Preview - new event handling

Post #13by Chuft-Captain » 13.06.2010, 08:59

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. :wink:
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
"Is a planetary surface the right place for an expanding technological civilization?"
-- Gerard K. O'Neill (1969)

CATALOG SYNTAX HIGHLIGHTING TOOLS LAGRANGE POINTS


Return to “Scripting”