It 's a simplified version of the menu structure I used in "goto for spacecraft" that can easily be adapted to
other menu needs.
Menu.celx
Code: Select all
-- Title: Menu example
--[[ Description
This is a simple menu that can serve as an example for how to create
a menu using Celx scripting. The example can be easily adjusted to
your own needs.
The script displays a menu when the key M (Shift + M) is pressed and
prints a message when one of the key's J, K or L (Uppercase !!!) is
pressed in response to the menu.
The script uses lua's facility to create an (object oriented) type of
object; in this case the menu object. As long as the script runs,
the menu object is in one of two states:
1. menu.display: waiting for the key "M" to be pressed
- this is also the default state
2. menu.choice: waiting for a menu-choice to be pressed
- this state ends when any key is pressed
]]
--[[ Menu Structure
=======================================================================
a structure/object that holds all the data and logic (functions
and methods) to form a base for a 'menu' functionality
=======================================================================]]
--[[ Table: menu
------------------------------------------------------------------------
holds menu data, functions, and state
------------------------------------------------------------------------]]
menu = {
state = nil,
display = {
keys = "M",
functions = {
M = displayMenu
}
},
choice = {
keys = "JKL",
functions = {
J = printJ,
K = printK,
L = printL
}
}
}
-- set state to default!!!
-- -----------------------
menu.state = menu.display
-- -----------------------
--[[ State method: menu.display:wantsKey()
------------------------------------------------------------------------
current state == display; if key has correct value, accept key as input
------------------------------------------------------------------------]]
function menu.display:wantsKey(eventInfo)
if string.find(self.keys, eventInfo.char) == nil then
menu:stateReset()
return false
else
return true
end
end
--[[ State method: menu.display:processKey()
------------------------------------------------------------------------
current state == display; process the key entered by the user
------------------------------------------------------------------------]]
function menu.display:processKey(eventInfo)
return self.functions[eventInfo.char](self)
-- ".functions[](self)" is needed here; Lua will n?t resolve: ":functions[]()" correctly
end
--[[ State method: menu.choice:wantsKey()
------------------------------------------------------------------------
current state == choice; if key has correct value, accept key as input
------------------------------------------------------------------------]]
function menu.choice:wantsKey(eventInfo)
if string.find(self.keys, eventInfo.char) == nil then
menu:stateReset()
return false
else
return true
end
end
--[[ State method: menu.choice:processKey()
------------------------------------------------------------------------
current state == choice; process the key entered by the user
------------------------------------------------------------------------]]
function menu.choice:processKey(eventInfo)
return self.functions[eventInfo.char](self)
-- ".functions[](self)" is needed here; Lua will n?t resolve: ":functions[]()" correctly
end
--[[ Method: menu:stateReset()
------------------------------------------------------------------------
resets state to default
------------------------------------------------------------------------]]
function menu:stateReset()
self.state = self.display
end
--[[ Method: menu:wantsKey()
------------------------------------------------------------------------
check if menu wants to process the key the user pressed
------------------------------------------------------------------------]]
function menu:wantsKey(eventInfo)
celestia:print("") -- remove possible previously printed text
if self.state == nil then
return false
else
return self.state:wantsKey(eventInfo)
end
end
--[[ Method: menu:processKey()
------------------------------------------------------------------------
allow menu to process the key the user pressed
------------------------------------------------------------------------]]
function menu:processKey(eventInfo)
if self.state == nil then
return false
else
return self.state:processKey(eventInfo)
end
end
--[[ Testers
========================================================================
Test functions to show how the menu structure can be used
=======================================================================]]
-- print the menu
function displayMenu()
celestia:print("J = Choice 1\nK = Choice 2\nL = Choice 3\n")
menu.state = menu.choice
return true
end
-- confirm menu choice J
function printJ()
celestia:print("Menu choice = J")
menu:stateReset()
return true
end
-- confirm menu choice K
function printK()
celestia:print("Menu choice = K")
menu:stateReset()
return true
end
-- confirm menu choice L
function printL()
celestia:print("Menu choice = L")
menu:stateReset()
return true
end
--[[ Script Main:
=======================================================================
The main functions
=======================================================================]]
--[[ 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()
celestia:registereventhandler("key", keyEvent)
celestia:print("Menu is active", 2)
end
--[[ Script
------------------------------------------------------------------------
start
------------------------------------------------------------------------]]
Main()
Have fun using it.
Jan