Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Local Variables
- local sItem = 1
- local cFloor = 1
- local inFloorMenu = true
- local w, h = term.getSize()
- -- Text Functions
- function printCentered( text ) -- Centers text on terminal screen
- term.clear()
- term.setCursorPos(w/2-#text/2, h/2)
- print (text)
- sleep(1)
- end
- -- Handler Functions
- function s1() -- Sends rednet message to the control computer
- rednet.open("back") -- Opens the wireless modem
- rednet.send(2, "1", true) -- Sends the message
- rednet.close("back") -- Closes the modem
- cFloor = 1 -- Rewrites the current floor variable for later use
- end
- function s2() -- Same as above
- rednet.open("back")
- rednet.send(2, "2", true)
- rednet.close("back")
- cFloor = 2
- end
- function s3() -- Same as above
- rednet.open("back")
- rednet.send(2, "3", true)
- rednet.close("back")
- cFloor = 3
- end
- --[[ Menu Table
- Table containing menu text and the the corresponding functions
- ]]--
- floorMenu = {
- [1] = { text = "1st Floor", handler = s1 },
- [2] = { text = "2nd Floor", handler = s2 },
- [3] = { text = "3rd Floor", handler = s3 }
- }
- -- Draw Menu
- function printMenu( menu ) -- Displays the menu with >> << markers to select floors with.
- for i=1,#menu do
- term.setCursorPos(w/2-#menu[i].text/2-2, h/2-1+i) -- centers the menu
- if i == sItem then
- print (">> "..menu[i].text.." <<") -- Displays markers around the selected floor
- else
- print (" "..menu[i].text) -- Displays the other floors
- end
- end
- end
- -- Handler
- function onKey( key, menu ) -- Finds out which key was pressed
- if key == keys.enter then -- starts the onItem function
- onItem(menu)
- elseif key == keys.up then -- Moves up in the menu, ie. from floor 3 to 2
- if sItem > 1 then -- Unless it's already at the top
- sItem = sItem - 1
- end
- elseif key == keys.down then -- Moves down in the menu
- if sItem < #menu then -- Unless it's already on the last item in the menu
- sItem = sItem + 1
- end
- end
- end
- function onItem( menu ) -- starts the menu function(s1, s2 and s3 dependent on which was selected)
- menu[sItem].handler()
- end
- -- Run Program
- function runProgram() -- Main function which starts the program
- while inFloorMenu do -- Runs a loop as long as the inFloorMenu variable is true
- term.clear()
- term.setCursorPos(1,1)
- printMenu(floorMenu) -- calls the print menu function for the floor menu specifically.
- event, key = os.pullEvent("key") -- Checks whether a key has been pressed
- onKey(key,floorMenu) -- Runs the onKey function for the floor menu.
- end
- end
- runProgram()
Advertisement
Add Comment
Please, Sign In to add comment