Advertisement
casillero

terminal draft

May 9th, 2014
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.22 KB | None | 0 0
  1. --[[Not yet finished: Not advised for real use just now and subject to heavy change]]
  2.  
  3. function dPadMenu(t, yStart, yEnd, indent, highlight, background)
  4.     if yStart == nil or yStart < 1 then
  5.         yStart = 1
  6.     elseif yStart >= mY then
  7.         yStart = mY-1
  8.     end
  9.     if yEnd == nil or yEnd > mY then
  10.         yEnd = mY
  11.     end
  12.     indent = indent or 3
  13.     highlight = highlight or colors.magenta
  14.     background = background or colors.black
  15.     local menu = compileListIntoPages(t, yStart, yEnd)
  16.     local pageS = 1 -- The default selected page
  17.     local unitS = 1 -- The default selected item in the page
  18.     local numberOfPages = #menu
  19.     local returnVal = nil
  20.     while true do
  21.         menu[pageS][unitS]["selected"] = true -- Sets the default selection
  22.         writeListWithHighlights(menu[pageS], indent, mX, yStart, yEnd, highlight, background)
  23.         local event = {os.pullEvent("key")}
  24.         if event[2] == 200 then -- up arrow. Shift unitS down if room. Shift original unitS to false
  25.             if unitS > 1 then
  26.                 menu[pageS][unitS]["selected"] = false
  27.                 unitS = unitS - 1
  28.             elseif pageS > 1 and unitS == 1 then
  29.                 menu[pageS][unitS]["selected"] = false
  30.                 pageS = pageS - 1
  31.                 unitS = #menu[pageS]
  32.             end
  33.         elseif event[2] == 208 then -- down arrow. Shift unitS up if room. Shift original unitS to false
  34.             if unitS < #menu[pageS] then
  35.                 menu[pageS][unitS]["selected"] = false
  36.                 unitS = unitS + 1
  37.             elseif pageS < numberOfPages and unitS == #menu[pageS] then
  38.                 menu[pageS][unitS]["selected"] = false
  39.                 pageS = pageS + 1
  40.                 unitS = 1
  41.             end
  42.         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
  43.             if pageS > 1 then
  44.                 menu[pageS][unitS]["selected"] = false
  45.                 pageS = pageS - 1
  46.                 unitS = 1
  47.             end
  48.         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
  49.             if pageS < numberOfPages then
  50.                 menu[pageS][unitS]["selected"] = false
  51.                 pageS = pageS + 1
  52.                 unitS = 1
  53.             end
  54.         elseif event[2] == 28 then -- enter. Set returnVal to current menu[pageS][unitS]["data"] and break
  55.             returnVal = menu[pageS][unitS]["data"]  
  56.             term.setBackgroundColor(background)
  57.             break
  58.         end  
  59.         term.setBackgroundColor(background)
  60.     end  
  61.     return returnVal
  62. end
  63.  
  64. -- Add a menu option to your menu with this function.
  65.  
  66. function constructSelectionPart(targetSelectionMenu, message, data)
  67.     local tLength = #targetSelectionMenu
  68.     data = data or message
  69.     targetSelectionMenu[tLength+1] = {
  70.         ["message"] = message,
  71.         ["data"] = data,
  72.         ["key"] = targetSelectionMenu,
  73.         ["selected"] = false,
  74.         }
  75.     table.insert(targetSelectionMenu, 0, t)
  76. end
  77.  
  78. -- You could just make the table yourself, which is better probably, but this is here for the sake of completeness
  79.  
  80. function constructSelectionMenu()
  81.     local t = {}
  82.     return t
  83. end
  84.  
  85. function clearScreenAtLineRange(minY, maxY)
  86.     if minY == nil then
  87.         minY = 1
  88.     end
  89.     if maxY == nil then
  90.        
  91.         maxY = mY
  92.     end
  93.     for i = minY, maxY do
  94.         term.setCursorPos(1,i)
  95.         term.clearLine()
  96.     end
  97.     term.setCursorPos(1,minY)
  98. end
  99.  
  100. function stringToPointInLine(point, char, prefix, suffix, start) -- tweak
  101.     prefix = prefix or ""
  102.     suffix = suffix or ""
  103.     char = char or " "
  104.     local cX = start
  105.     if start == nil then
  106.         local x,y = term.getCursorPos()
  107.         cX = x
  108.     end
  109.     local length = mX - ((2*cX) + string.len(prefix..suffix))
  110.     prefix = prefix..(string.rep(char, length))..suffix
  111.     return prefix  
  112. end
  113.  
  114. function writeListWithHighlights(list, indent, tWidth, yStart, yEnd, highlight, background, track, withHold) -- tweak
  115.     local yShift = 0
  116.     clearScreenAtLineRange(yStart, yEnd)
  117.     for i,v in pairs(list) do
  118.         term.setCursorPos(indent, yShift + yStart)
  119.         yShift = yShift + 1
  120.         if track == true then
  121.             local x, y = term.getCursorPos()
  122.             v["pos"] = {["x"] = x, ["y"] = y}
  123.         end
  124.         if withHold ~= true then
  125.             term.setBackgroundColor(background)
  126.             if v["selected"] == true then
  127.                 term.setBackgroundColor(highlight)
  128.             end
  129.             local str = stringToPointInLine(tWidth-(1+indent), nil, "   "..v["message"])
  130.             write(str)
  131.         end
  132.     end
  133.     return list
  134. end
  135.  
  136. function compileListIntoPages(list, yStart, yEnd) -- tweak
  137.     local pageNumber = 1
  138.     local menu = {}
  139.     for i,v in pairs(list) do
  140.             if menu[pageNumber] == nil then
  141.                 menu[pageNumber] = {}
  142.             end
  143.             table.insert(menu[pageNumber], 0, v)
  144.             if #menu[pageNumber] == (yEnd - yStart) then
  145.                 pageNumber = pageNumber + 1
  146.             end
  147.     end
  148.     return menu
  149. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement