Celx menu Object

This variant of the menu script is not so much a base for further scripting as it is a working solution. Put it in a library, import it into a script (using "dofile()") and all that needs to be done to create a menu is something like this:

--[[ Load libraries
========================================================================
    import supporting scripts, etc.
========================================================================
dofile("scripts/celx libs/menulib.celx")                 -- the "import"

--[[ Function: keyEvent()

------------------------------------------------------------------------

the keyboard event handler

------------------------------------------------------------------------]]

function keyEvent(eventInfo)

if menu:wantsKey(eventInfo) then

return menu:processKey(eventInfo)

-- elseif otherMenu:wantsKey(eventInfo) then

-- return otherMenu:processKey(eventInfo)

else

return false

end

end

--[[ Function: Main()

------------------------------------------------------------------------

the function Main

------------------------------------------------------------------------]]

function Main()

menu = menuObject:new() -- create a menu object

menu:addKey("M", displayMenu) -- add the key value to activate the menu and your function that prints it

menu:addChoice("j", printLower) -- add a menu choice and your function that handles it

menu:addChoice("J", printUpper) -- add a second menu choice and it's function

menu:addChoice("C-j", printControl) -- add a third menu choice and it's function

menu:start() -- initialise the menu's state

celestia:registereventhandler("key", keyEvent)

celestia:print("Menu is active", 2)

end

As the snippet shows, the object supports [Ctrl]+[Key] combinations. You can have as many different menu's in one script as you want and with half the amount of code. The code for the menu object is actually even simpler than the previous variants; there is more comment in the script than actual lines of code. For a complete example of how to use it, see "Celx menu example" in the downloads.

For details about the celx key event see: Key event

Downloads

Installation and use

Installation

    1. download "menulib.celx" to the folder Celestia/scripts/celx libs/
    2. - or to your existing celx library folder if you already have one
    3. - in that case don't forget to adjust the name of the folder in the menu example (menuObject)
    4. download "menuObject.celx" to your Celestia/scripts/ folder.

Use

    1. Start Celestia and choose "File/Scripts/menuObject.celx" from the menubar. Or choose "File/Open Script...", navigate to your scripts folder, select "menuObject.celx" and choose "Open". The script will confirm it's running status by printing "Menu is active" onscreen.
    2. Press [Shift] + [M] and follow the onscreen messages.

Remarks

    • Celestia can only run 1 script at a time. When you execute a second script, you will have to 'restart' menuObject again.

History

Version 1, 8 feb 2011

Initial version