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
Installation
Use
Remarks
Version 1, 8 feb 2011
Initial version