Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Syntax:
- -- selectionMenu(title, item1, item2, ...)
- -- [01] item1
- -- [02] item2
- -- selectionMenu(title, {item1, ...})
- -- [01] item1
- -- [02] item2
- -- selectionMenu(title, {item1, ...}, {itemA, ...})
- -- [01] item1 itemA
- -- [02] item2 itemB
- function selectionMenu(title, ... )
- -------------------
- --Made by Yarillo--
- -------------------
- tChoices = {}
- if type(arg[1]) == "table" then
- for i=1, #arg do
- tChoices[i] = arg[i]
- -- tChoices[1] = {a,b,c,d,e}
- -- tChoices[2] = {1,2,3,4,5}
- end
- else
- tChoices[1] = arg
- -- tChoices[1] = {a,b,c,d,e}
- end
- local nTermX, nTermY = term.getSize()
- local sSeperator = ("-"):rep(nTermX) -- Create a seperator string with the size of the terminal
- local tSeparatorLength = math.floor((nTermX-string.len(tostring(title)))/2)-1
- local tSeparator = (" "):rep(tSeparatorLength)
- print(tSeparator)
- -- Do the above for the remaining
- local nSelection = 1 -- The current selection defaults at 1
- while true do
- term.setCursorPos(1, 1)
- term.clear()
- print(sSeperator)
- term.write("|"..(" "):rep(tSeparatorLength))
- term.write(tostring(title))
- if tSeparatorLength%2 == 0 then
- print(tSeparator.."|")
- else
- print(tSeparator.." |")
- end
- print(sSeperator)
- for nLine = 1, #tChoices[1] do -- Iterate through the possible potions, and print them, marking the chosen one
- local sLine = " "
- if nSelection == nLine then
- sLine = ">"
- end
- local sLineNum = tostring(nLine)
- if #sLineNum < 2 then
- sLineNum = "0" .. sLineNum -- Prepend a 0 if it's too short
- end
- sLine = sLine .. "[" .. sLineNum .. "] "
- for i=1,#tChoices do
- sLine = sLine .. tostring(tChoices[i][nLine]) .. " "
- -- [0] choix1 choix2 choixN
- end
- print(sLine) -- Print it
- end
- -- os.pullEvent keys: up - 200, down - 208, enter - 28
- local sEvent, nKey = os.pullEvent("key") -- Using the 1.3 filtering; this will mean only "key" events will pass
- if nKey == 200 or nKey == 17 then -- Up/w key: move up the menu
- if tChoices[1][nSelection - 1] then -- Check if we can move up
- nSelection = nSelection - 1
- end
- -- Ignore it otherwise
- elseif nKey == 208 or nKey == 31 then -- Down/s key: move down the menu
- if tChoices[1][nSelection + 1] then -- Check if we can move down
- nSelection = nSelection + 1
- end
- elseif nKey == 28 then -- Enter key: Selecting a choice
- if tChoices[1][nSelection] then
- return nSelection, tChoices[1][nSelection] -- Run the function associated with the action.
- else
- print("Error: Selection out of bounds: ", nSelection)
- return false
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement