Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[Not yet finished: Not advised for real use just now and subject to heavy change]]
- function dPadMenu(t, yStart, yEnd, indent, highlight, background)
- if yStart == nil or yStart < 1 then
- yStart = 1
- elseif yStart >= mY then
- yStart = mY-1
- end
- if yEnd == nil or yEnd > mY then
- yEnd = mY
- end
- indent = indent or 3
- highlight = highlight or colors.magenta
- background = background or colors.black
- local menu = compileListIntoPages(t, yStart, yEnd)
- local pageS = 1 -- The default selected page
- local unitS = 1 -- The default selected item in the page
- local numberOfPages = #menu
- local returnVal = nil
- while true do
- menu[pageS][unitS]["selected"] = true -- Sets the default selection
- writeListWithHighlights(menu[pageS], indent, mX, yStart, yEnd, highlight, background)
- local event = {os.pullEvent("key")}
- if event[2] == 200 then -- up arrow. Shift unitS down if room. Shift original unitS to false
- if unitS > 1 then
- menu[pageS][unitS]["selected"] = false
- unitS = unitS - 1
- elseif pageS > 1 and unitS == 1 then
- menu[pageS][unitS]["selected"] = false
- pageS = pageS - 1
- unitS = #menu[pageS]
- end
- elseif event[2] == 208 then -- down arrow. Shift unitS up if room. Shift original unitS to false
- if unitS < #menu[pageS] then
- menu[pageS][unitS]["selected"] = false
- unitS = unitS + 1
- elseif pageS < numberOfPages and unitS == #menu[pageS] then
- menu[pageS][unitS]["selected"] = false
- pageS = pageS + 1
- unitS = 1
- end
- elseif event[2] == 203 then -- left arrow. Shift pageS down if room and reset unitS to one. Shift original unitS to false and original pageS to false
- if pageS > 1 then
- menu[pageS][unitS]["selected"] = false
- pageS = pageS - 1
- unitS = 1
- end
- elseif event[2] == 205 then -- right arrow. Shift pageS up if room and reset unitS to one. Shift original unitS to false and original pageS to false
- if pageS < numberOfPages then
- menu[pageS][unitS]["selected"] = false
- pageS = pageS + 1
- unitS = 1
- end
- elseif event[2] == 28 then -- enter. Set returnVal to current menu[pageS][unitS]["data"] and break
- returnVal = menu[pageS][unitS]["data"]
- term.setBackgroundColor(background)
- break
- end
- term.setBackgroundColor(background)
- end
- return returnVal
- end
- -- Add a menu option to your menu with this function.
- function constructSelectionPart(targetSelectionMenu, message, data)
- local tLength = #targetSelectionMenu
- data = data or message
- targetSelectionMenu[tLength+1] = {
- ["message"] = message,
- ["data"] = data,
- ["key"] = targetSelectionMenu,
- ["selected"] = false,
- }
- table.insert(targetSelectionMenu, 0, t)
- end
- -- You could just make the table yourself, which is better probably, but this is here for the sake of completeness
- function constructSelectionMenu()
- local t = {}
- return t
- end
- function clearScreenAtLineRange(minY, maxY)
- if minY == nil then
- minY = 1
- end
- if maxY == nil then
- maxY = mY
- end
- for i = minY, maxY do
- term.setCursorPos(1,i)
- term.clearLine()
- end
- term.setCursorPos(1,minY)
- end
- function stringToPointInLine(point, char, prefix, suffix, start) -- tweak
- prefix = prefix or ""
- suffix = suffix or ""
- char = char or " "
- local cX = start
- if start == nil then
- local x,y = term.getCursorPos()
- cX = x
- end
- local length = mX - ((2*cX) + string.len(prefix..suffix))
- prefix = prefix..(string.rep(char, length))..suffix
- return prefix
- end
- function writeListWithHighlights(list, indent, tWidth, yStart, yEnd, highlight, background, track, withHold) -- tweak
- local yShift = 0
- clearScreenAtLineRange(yStart, yEnd)
- for i,v in pairs(list) do
- term.setCursorPos(indent, yShift + yStart)
- yShift = yShift + 1
- if track == true then
- local x, y = term.getCursorPos()
- v["pos"] = {["x"] = x, ["y"] = y}
- end
- if withHold ~= true then
- term.setBackgroundColor(background)
- if v["selected"] == true then
- term.setBackgroundColor(highlight)
- end
- local str = stringToPointInLine(tWidth-(1+indent), nil, " "..v["message"])
- write(str)
- end
- end
- return list
- end
- function compileListIntoPages(list, yStart, yEnd) -- tweak
- local pageNumber = 1
- local menu = {}
- for i,v in pairs(list) do
- if menu[pageNumber] == nil then
- menu[pageNumber] = {}
- end
- table.insert(menu[pageNumber], 0, v)
- if #menu[pageNumber] == (yEnd - yStart) then
- pageNumber = pageNumber + 1
- end
- end
- return menu
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement