VintageRice

[CC][Unfinished] - Scrolling Menu

Apr 28th, 2014
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.30 KB | None | 0 0
  1. local getSelected = 1
  2.  
  3. local menuOptions = {"Testing", "Menu", "Options"} --The actual menu options
  4. local controls = {"w", "s", "f"} --Controls (up, down, select)
  5. local width, height = term.getSize()
  6.  
  7. local function drawMenu()
  8.     for i = 1, #menuOptions do
  9.         term.setCursorPos(((width / 2) - (#menuOptions[i] / 2)), ((height / 2) - (#menuOptions / 2) + (i - 1))) --Some overcomplicated maths for the centering
  10.         print(menuOptions[i])
  11.        
  12.         if i == getSelected then --If the option number is selected, change the colour
  13.             term.setTextColor(colours.red)
  14.         else
  15.             term.setTextColor(colours.white)
  16.         end
  17.     end
  18. end
  19.  
  20. function monitorKeys()
  21.     while true do
  22.         event, char = os.pullEvent("char")
  23.        
  24.         for i = 1, 3 do
  25.             if char == controls[i] then
  26.                 if char == controls[1] then
  27.                     getSelected = getSelected - 1 --Decreases the cursor
  28.                 end
  29.                
  30.                 if char == controls[2] then
  31.                     getSelected = getSelected + 1 --Increases the cursor
  32.                 end
  33.                
  34.                 if char == controls[3] then
  35.                     print(menuOptions[getSelected]) --Prints the selected option
  36.                 end
  37.                
  38.                 if getSelected == 0 then
  39.                     getSelected = #menuOptions
  40.                 end
  41.                
  42.                 if getSelected == #menuOptions then
  43.                     getSelected = 1
  44.                 end
  45.                
  46.                 drawMenu()
  47.             end
  48.         end
  49.     end
  50. end
  51.  
  52. term.clear()
  53.  
  54. drawMenu()
  55. monitorKeys()
Advertisement
Add Comment
Please, Sign In to add comment