Advertisement
Wyvern67

Selection menu

Oct 15th, 2015
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.61 KB | None | 0 0
  1. -- Syntax:
  2.  
  3. -- selectionMenu(title, item1, item2, ...)
  4. -- [01] item1
  5. -- [02] item2
  6.  
  7. -- selectionMenu(title, {item1, ...})
  8. -- [01] item1
  9. -- [02] item2
  10.  
  11. -- selectionMenu(title, {item1, ...}, {itemA, ...})
  12. -- [01] item1 itemA
  13. -- [02] item2 itemB
  14.  
  15. function selectionMenu(title, ... )
  16.     -------------------
  17.     --Made by Yarillo--
  18.     -------------------
  19.     tChoices = {}
  20.     if type(arg[1]) == "table" then
  21.         for i=1, #arg do
  22.             tChoices[i] = arg[i]
  23.             -- tChoices[1] = {a,b,c,d,e}
  24.             -- tChoices[2] = {1,2,3,4,5}
  25.         end
  26.     else
  27.         tChoices[1] = arg
  28.         -- tChoices[1] = {a,b,c,d,e}
  29.     end
  30.    
  31.     local nTermX, nTermY = term.getSize()
  32.     local sSeperator = ("-"):rep(nTermX) -- Create a seperator string with the size of the terminal
  33.     local tSeparatorLength = math.floor((nTermX-string.len(tostring(title)))/2)-1
  34.     local tSeparator = (" "):rep(tSeparatorLength)
  35.     print(tSeparator)
  36.     -- Do the above for the remaining
  37.  
  38.     local nSelection = 1 -- The current selection defaults at 1
  39.     while true do
  40.         term.setCursorPos(1, 1)
  41.         term.clear()
  42.         print(sSeperator)
  43.         term.write("|"..(" "):rep(tSeparatorLength))
  44.         term.write(tostring(title))
  45.         if tSeparatorLength%2 == 0 then
  46.             print(tSeparator.."|")
  47.         else
  48.             print(tSeparator.." |")
  49.         end
  50.         print(sSeperator)
  51.  
  52.         for nLine = 1, #tChoices[1] do -- Iterate through the possible potions, and print them, marking the chosen one
  53.             local sLine = " "
  54.             if nSelection == nLine then
  55.                 sLine = ">"
  56.             end
  57.             local sLineNum = tostring(nLine)
  58.             if #sLineNum < 2 then
  59.                 sLineNum = "0" .. sLineNum -- Prepend a 0 if it's too short
  60.             end
  61.             sLine = sLine .. "[" .. sLineNum .. "] "
  62.             for i=1,#tChoices do
  63.                 sLine = sLine .. tostring(tChoices[i][nLine]) .. " "
  64.                 -- [0] choix1 choix2 choixN
  65.             end
  66.             print(sLine) -- Print it
  67.         end
  68.         -- os.pullEvent keys: up - 200, down - 208, enter - 28
  69.         local sEvent, nKey = os.pullEvent("key") -- Using the 1.3 filtering; this will mean only "key" events will pass
  70.  
  71.         if nKey == 200 or nKey == 17 then -- Up/w key: move up the menu
  72.             if tChoices[1][nSelection - 1] then -- Check if we can move up
  73.                 nSelection = nSelection - 1
  74.             end
  75.             -- Ignore it otherwise
  76.         elseif nKey == 208 or nKey == 31  then -- Down/s key: move down the menu
  77.             if tChoices[1][nSelection + 1] then -- Check if we can move down
  78.                 nSelection = nSelection + 1
  79.             end
  80.         elseif nKey == 28 then -- Enter key: Selecting a choice
  81.             if tChoices[1][nSelection] then
  82.                 return nSelection, tChoices[1][nSelection]  -- Run the function associated with the action.
  83.             else
  84.                 print("Error: Selection out of bounds: ", nSelection)
  85.                 return false
  86.             end
  87.         end
  88.     end
  89. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement