Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --=VARIABLES========================--
- nScreenWidth, nScreenHeight = term.getSize()
- nSelect = 1
- tTestMenu = {}
- --==================================--
- -- Takes a menu table formatted as such:
- -- tMenu[n] = {sTitle (string), fAssociatedFunction (function)}
- -- Prints the menu with the correct highlighting of the options,
- -- and scrolls the menu as needed, beggining at xPos, yPos.
- function printMenu(tMenu, nSelection, xPos, yPos)
- local nMenuItem_Index = 1 -- The item that is currently being printed by this method.
- -- Check if the menu needs to be scrolled.
- local nScroll = 0 -- Start out the scroll with an initial value of 0, so it won't affect the print loop if no scrolling is necessary.
- -- If the selection is greater than the amount of available lines to print one.
- if nSelection > nScreenHeight then
- nScroll = nSelection - nScreenHeight -- Make sure that the scroll is the amount of lines that the screen cannot accomodate.
- end
- -- Print the menu by looping through the table.
- for nLine = 1, #tMenu do
- nMenuItem_Index = nLine + nScroll
- term.setCursorPos(xPos, yPos + nLine - 1) -- Subtract 1 from the current line to make sure we start on the proper line (yPos).
- -- If the selection is equal to the current option, draw brackets around it to show that it is selected.
- if nSelection == nMenuItem_Index then
- term.write("[" .. tMenu[nMenuItem_Index].sTitle .. "]") -- Print the option with brackets so that the user knows it is selected.
- -- If the selection is not equal to the current option, do not draw brackets around it to show that it is not selected.
- else
- term.write(" " .. tMenu[nMenuItem_Index].sTitle .. " ") -- Print the title of the menu option with spaces around it to compensate for
- -- the lack of brackets (this way all of the options are centered with one another.)
- end
- if nMenuItem_Index == #tMenu then
- return
- end
- end
- end
- -- Takes a key code and performs the appropriate method respectively to the passed menu.
- function handleKeyPress(tMenu, nSelection, nKey)
- -- If the enter key was pressed, then run the function associated with that option.
- if nKey == 28 then
- tMenu[nSelection].fAssociatedFunction()
- -- If the up key was pressed, then move the selection one up.
- elseif nKey == 200 and nSelection > 1 then
- nSelection = nSelection - 1
- -- If the down key was pressed, then move the selection one down.
- elseif nKey == 208 and nSelection < #tMenu then
- nSelection = nSelection + 1
- end
- if nSelection then
- return nSelection
- else
- return 1
- end
- end
- -- Sets up a dummy table for scroll testing.
- function createDummyMenu()
- local tDummyTable = {}
- for nIndex = 1, nScreenHeight * 2 do
- tDummyTable[#tDummyTable+1] = {sTitle = "Option" .. nIndex, fAssociatedFunction = os.shutdown}
- end
- return tDummyTable
- end
- tTestMenu = createDummyMenu()
- function clearScreen()
- term.clear()
- term.setCursorPos(1, 1)
- end
- local bRunning = true
- while bRunning do
- clearScreen()
- printMenu(tTestMenu, nSelect, 1, 1)
- local sEvent, nKey = os.pullEvent("key")
- nSelect = handleKeyPress(tTestMenu, nSelect, nKey)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement