Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---------------
- -- Variables --
- ---------------
- local termWidth, termHeight = term.getSize ()
- local selectedItem = 1
- local inMainMenu = true
- local inLightsMenu = false
- ---------------
- -- Functions --
- ---------------
- function Choice1 ()
- term.clear ()
- term.setCursorPos (1, 1)
- term.write ("Hello, my name is "..os.getComputerLabel ())
- sleep (3)
- end
- function Choice2 ()
- inLightsMenu = true
- selectedItem = 1
- while inLightsMenu do
- term.clear ()
- term.setCursorPos (1, 1)
- printMenu (lightsMenu)
- event, key = os.pullEvent ("key")
- onKeyPressed (key, lightsMenu)
- end
- end
- function LightsOn ()
- inLightsMenu = false
- end
- function LightsOff ()
- inLightsMenu = false
- end
- function Exit ()
- inMainMenu = false
- end
- ---------------
- -- Main Menu --
- ---------------
- local mainMenu = {
- [1] = {text = "Choice 1", handler = Choice1},
- [2] = {text = "Choice 2", handler = Choice2},
- [3] = {text = "Exit" , handler = Exit},
- }
- local lightsMenu = {
- [1] = {text = "Lights On" , handler = LightsOn},
- [2] = {text = "Lights Off", handler = LightsOff},
- }
- --DebugLine, says lightsMenu is not nil, but error thinks it's nil???
- if (lightsMenu == nil) then print ("True") else print ("Not nil")end
- sleep (3)
- --------------------
- -- Screen Refresh --
- --------------------
- function printMenu (menu)
- for i = 1, #menu do
- if (i == selectedItem) then
- print ("> "..menu[i].text)
- else
- print (" "..menu[i].text)
- end
- end
- end
- --------------
- -- Handlers --
- --------------
- function onKeyPressed (key, menu)
- if (key == keys.enter) then
- onItemSelected (menu)
- elseif (key == keys.up and selectedItem > 1) then
- selectedItem = selectedItem - 1
- elseif (key == keys.down and selectedItem < #menu) then
- selectedItem = selectedItem + 1
- end
- end
- function onItemSelected (menu)
- menu[selectedItem].handler ()
- end
- -----------------
- -- Main Method --
- -----------------
- function main ()
- while inMainMenu do
- term.clear ()
- term.setCursorPos (1, 1)
- printMenu (mainMenu)
- event, key = os.pullEvent ("key")
- onKeyPressed (key, mainMenu)
- end
- end
- main ()
Advertisement
Add Comment
Please, Sign In to add comment