Advertisement
PaymentOption

Scrolling menu

Oct 4th, 2012
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.09 KB | None | 0 0
  1. --=VARIABLES========================--
  2. nScreenWidth, nScreenHeight = term.getSize()
  3.  
  4. nSelect = 1
  5. tTestMenu = {}
  6. --==================================--
  7.  
  8. -- Takes a menu table formatted as such:
  9. -- tMenu[n] = {sTitle (string), fAssociatedFunction (function)}
  10. -- Prints the menu with the correct highlighting of the options,
  11. -- and scrolls the menu as needed, beggining at xPos, yPos.
  12. function printMenu(tMenu, nSelection, xPos, yPos)
  13.     local nMenuItem_Index = 1 -- The item that is currently being printed by this method.
  14.     -- Check if the menu needs to be scrolled.
  15.     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.
  16.     -- If the selection is greater than the amount of available lines to print one.
  17.     if nSelection > nScreenHeight then
  18.         nScroll = nSelection - nScreenHeight -- Make sure that the scroll is the amount of lines that the screen cannot accomodate.
  19.     end
  20.  
  21.     -- Print the menu by looping through the table.
  22.     for nLine = 1, #tMenu do
  23.         nMenuItem_Index = nLine + nScroll
  24.         term.setCursorPos(xPos, yPos + nLine - 1) -- Subtract 1 from the current line to make sure we start on the proper line (yPos).
  25.  
  26.         -- If the selection is equal to the current option, draw brackets around it to show that it is selected.
  27.         if nSelection == nMenuItem_Index then
  28.             term.write("[" .. tMenu[nMenuItem_Index].sTitle .. "]") -- Print the option with brackets so that the user knows it is selected.
  29.         -- If the selection is not equal to the current option, do not draw brackets around it to show that it is not selected.
  30.         else
  31.             term.write(" " .. tMenu[nMenuItem_Index].sTitle .. " ") -- Print the title of the menu option with spaces around it to compensate for
  32.             -- the lack of brackets (this way all of the options are centered with one another.)
  33.         end
  34.  
  35.         if nMenuItem_Index == #tMenu then
  36.             return
  37.         end
  38.     end
  39. end
  40.  
  41. -- Takes a key code and performs the appropriate method respectively to the passed menu.
  42. function handleKeyPress(tMenu, nSelection, nKey)
  43.     -- If the enter key was pressed, then run the function associated with that option.
  44.     if nKey == 28 then
  45.         tMenu[nSelection].fAssociatedFunction()
  46.     -- If the up key was pressed, then move the selection one up.
  47.     elseif nKey == 200 and nSelection > 1 then
  48.         nSelection = nSelection - 1
  49.     -- If the down key was pressed, then move the selection one down.
  50.     elseif nKey == 208 and nSelection < #tMenu then
  51.         nSelection = nSelection + 1
  52.     end
  53.  
  54.     if nSelection then
  55.         return nSelection
  56.     else
  57.         return 1
  58.     end
  59. end
  60.  
  61. -- Sets up a dummy table for scroll testing.
  62. function createDummyMenu()
  63.     local tDummyTable = {}
  64.  
  65.     for nIndex = 1, nScreenHeight * 2 do
  66.         tDummyTable[#tDummyTable+1] = {sTitle = "Option" .. nIndex, fAssociatedFunction = os.shutdown}
  67.     end
  68.  
  69.     return tDummyTable
  70. end
  71.  
  72. tTestMenu = createDummyMenu()
  73.  
  74. function clearScreen()
  75.     term.clear()
  76.     term.setCursorPos(1, 1)
  77. end
  78.  
  79. local bRunning = true
  80.  
  81. while bRunning do
  82.     clearScreen()
  83.     printMenu(tTestMenu, nSelect, 1, 1)
  84.  
  85.     local sEvent, nKey = os.pullEvent("key")
  86.     nSelect = handleKeyPress(tTestMenu, nSelect, nKey)
  87. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement