SuperMcBrent

PortalApp

Feb 14th, 2017
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.00 KB | None | 0 0
  1. c = peripheral.wrap('bottom')
  2. m = peripheral.wrap('top')
  3. local controllerX, controllerY
  4. buttons = {}
  5. local currentApp = "keypad"
  6. displayOffset = 0
  7. local targetCount = 0
  8. local destinations
  9. local activeButton = 0
  10.  
  11. function resizeMon()
  12.     controllerX, controllerY = m.getSize()
  13.     m.setTextScale(0.5)
  14. end
  15.  
  16. function clearAll()
  17.     empty(m)
  18. end
  19.  
  20. function empty()
  21.     m.setBackgroundColor(colors.black)
  22.     m.clear()
  23.     m.setCursorPos(1,1)
  24. end
  25.  
  26. function listDestinations()
  27.     destinations={}
  28.     for i=1,27 do
  29.         if c.getStackInSlot(i) ~= nil then
  30.             name = tostring(c.getStackInSlot(i).myst_book.destination)
  31.             destinations[i]={}
  32.             destinations[i]['name'] = name
  33.             destinations[i]['favorite'] = false
  34.             destinations[i]['active'] = false
  35.             print(i.." - "..name)
  36.         end
  37.     end
  38.     return destinations
  39. end
  40.  
  41. function moveBook(InOrOut,bookNumber)
  42.     local aaa = true
  43.     if InOrOut == "in" then
  44.         if c.pushItem('west',bookNumber,1) == 0 then
  45.             print("There is already a book inside")
  46.             aaa = false
  47.         end
  48.     elseif InOrOut == "out" then
  49.         c.pullItem('west',1,1)
  50.     end
  51.     return aaa
  52. end
  53.  
  54. function drawTitle(x, y, text, color_txt, color_bg, monitor)
  55.     monitor.setBackgroundColor(color_bg)
  56.     monitor.setTextColor(color_txt)
  57.     monitor.setCursorPos(x,y)
  58.     monitor.write(text)
  59.     monitor.setCursorPos(1,1)
  60.     monitor.setBackgroundColor(colors.black)
  61. end
  62.  
  63. function drawLine(x, y, length, size, color_bar, monitor)
  64.     for yPos = y, y+size-1 do
  65.         monitor.setBackgroundColor(color_bar)
  66.         monitor.setCursorPos(x, yPos)
  67.         monitor.write(string.rep(" ", length))
  68.         monitor.setBackgroundColor(colors.black)
  69.     end
  70. end
  71.  
  72. function drawProg(x, y, length, size, minVal, maxVal, color_bar, color_bg, monitor)
  73.     drawLine(x, y, length, size, color_bg, monitor)
  74.     local barSize = math.floor((minVal/maxVal)*length)
  75.     drawLine(x, y, barSize, size, color_bar, monitor)
  76. end
  77.  
  78. function drawContent(x, y, height, width, minVal, maxVal, color_bar, color_bg, monitor)
  79.     drawGraph(x, y, height, width, color_bg, monitor)
  80.     local a = math.floor((minVal/maxVal)*height)
  81.     drawGraph(x, y, a, width, color_bar, monitor)
  82. end
  83.  
  84. function drawGraph(x, y, height, width, color_bar, monitor)
  85.     drawLine(x, y-height+1, width, height, color_bar, monitor)
  86. end
  87.  
  88. function addButton(x,y,w,h,name,colorOn,colorOff,state,textOn,textOff,textX,textY,header)
  89.     buttons[name] = {["x"] = x,["y"] = y,["width"] = w,["height"] = h,
  90.                         ["colorOn"] = colorOn,["colorOff"] = colorOff,["state"] = state,
  91.                         ["textOn"] = textOn,["textOff"] = textOff,
  92.                         ["textX"] = textX,["textY"] = textY,["header"] = header}
  93. end
  94.  
  95. function addButtons()
  96.     for i=0,3 do
  97.         addButton(2,6+i*4,13,3,"buttonPos"..i,colors.lime,colors.red,false,"TARGET","TARGET",3,7+i*4,"")
  98.     end
  99.     addButton(2,4,13,1,"buttonUP",colors.cyan,colors.lightGray,false,"     / \\","     / \\",2,4,"")
  100.     addButton(2,22,13,1,"buttonDOWN",colors.cyan,colors.lightGray,true,"     \\ /","     \\ /",2,22,"")
  101. end
  102.  
  103. function generateGUI(monitor)
  104.     if currentApp == "keypad" then
  105.  
  106.         drawTitle(2, 2, 'Select Target', colors.white, colors.black, m)
  107.  
  108.         if targetCount-(displayOffset*4) >= 1 then
  109.             buttons['buttonPos0'].textOn = destinations[1+(displayOffset*4)]['name']
  110.             buttons['buttonPos0'].textOff = buttons['buttonPos0'].textOn
  111.             if 1+(displayOffset*4) == activeButton then
  112.                 buttons['buttonPos0'].state = true
  113.             else
  114.                 buttons['buttonPos0'].state = false
  115.             end
  116.             drawButton("buttonPos0",m)
  117.         end
  118.         if targetCount-(displayOffset*4) >= 2 then
  119.             buttons['buttonPos1'].textOn = destinations[2+(displayOffset*4)]['name']
  120.             buttons['buttonPos1'].textOff = buttons['buttonPos1'].textOn
  121.             if 2+(displayOffset*4) == activeButton then
  122.                 buttons['buttonPos1'].state = true
  123.             else
  124.                 buttons['buttonPos1'].state = false
  125.             end
  126.             drawButton("buttonPos1",m)
  127.         end
  128.         if targetCount-(displayOffset*4) >= 3 then
  129.             buttons['buttonPos2'].textOn = destinations[3+(displayOffset*4)]['name']
  130.             buttons['buttonPos2'].textOff = buttons['buttonPos2'].textOn
  131.             if 3+(displayOffset*4) == activeButton then
  132.                 buttons['buttonPos2'].state = true
  133.             else
  134.                 buttons['buttonPos2'].state = false
  135.             end
  136.             drawButton("buttonPos2",m)
  137.         end
  138.         if targetCount-(displayOffset*4) >= 4 then
  139.             buttons['buttonPos3'].textOn = destinations[4+(displayOffset*4)]['name']
  140.             buttons['buttonPos3'].textOff = buttons['buttonPos3'].textOn
  141.             if 4+(displayOffset*4) == activeButton then
  142.                 buttons['buttonPos3'].state = true
  143.             else
  144.                 buttons['buttonPos3'].state = false
  145.             end
  146.             drawButton("buttonPos3",m)
  147.         end
  148.  
  149.         if displayOffset == 0 then
  150.             buttons['buttonUP'].state = false
  151.         else
  152.             buttons['buttonUP'].state = true
  153.         end
  154.         if displayOffset + 4 == targetCount or targetCount < 4 then
  155.             buttons['buttonDOWN'].state = false
  156.         else
  157.             buttons['buttonDOWN'].state = true
  158.         end
  159.  
  160.         drawButton("buttonUP",m)
  161.         drawButton("buttonDOWN",m)
  162.  
  163.     else error("App not found") end
  164. end
  165.  
  166. function drawButton(button,monitor)
  167.     local color = colors.red
  168.     local text = ""
  169.     if buttons[button].state==true then color = buttons[button].colorOn else color = buttons[button].colorOff end
  170.     if buttons[button].state==true then text = buttons[button].textOn else text = buttons[button].textOff end
  171.     drawLine(buttons[button].x, buttons[button].y, buttons[button].width, buttons[button].height, color, monitor)
  172.     if buttons[button].header == "" then else
  173.     drawTitle(buttons[button].x, buttons[button].y - 1, buttons[button].header, colors.white, colors.black, monitor) end
  174.     drawTitle(buttons[button].textX, buttons[button].textY, text, colors.white, color, monitor)
  175. end
  176.  
  177. function setupTargets()
  178.     moveBook('out',0)
  179.     destinations = listDestinations()
  180.     targetCount = table.getn(destinations)
  181. end
  182.  
  183. function touchAreas(x,y,side)
  184.     if x >= buttons["buttonPos0"].x and x < buttons["buttonPos0"].x + buttons["buttonPos0"].width and
  185.         y >= buttons["buttonPos0"].y and y < buttons["buttonPos0"].y + buttons["buttonPos0"].height then
  186.             if buttons["buttonPos0"].textOn ~= "TARGET" then
  187.                 if activeButton == 0 then
  188.                     if moveBook('in',1+(displayOffset*4)) then
  189.                         activeButton = 1+(displayOffset*4)
  190.                     end
  191.                 elseif activeButton == 1+(displayOffset*4) then
  192.                     moveBook('out',1+(displayOffset*4))
  193.                     activeButton = 0
  194.                 end
  195.             end
  196.     elseif x >= buttons["buttonPos1"].x and x < buttons["buttonPos1"].x + buttons["buttonPos1"].width and
  197.         y >= buttons["buttonPos1"].y and y < buttons["buttonPos1"].y + buttons["buttonPos1"].height then
  198.             if buttons["buttonPos1"].textOn ~= "TARGET" then
  199.                 if activeButton == 0 then
  200.                     if moveBook('in',2+(displayOffset*4)) then
  201.                         activeButton = 2+(displayOffset*4)
  202.                     end
  203.                 elseif activeButton == 2+(displayOffset*4) then
  204.                     moveBook('out',2+(displayOffset*4))
  205.                     activeButton = 0
  206.                 end
  207.             end
  208.     elseif x >= buttons["buttonPos2"].x and x < buttons["buttonPos2"].x + buttons["buttonPos2"].width and
  209.         y >= buttons["buttonPos2"].y and y < buttons["buttonPos2"].y + buttons["buttonPos2"].height then
  210.             if buttons["buttonPos2"].textOn ~= "TARGET" then
  211.                 if activeButton == 0 then
  212.                     if moveBook('in',3+(displayOffset*4)) then
  213.                         activeButton = 3+(displayOffset*4)
  214.                     end
  215.                 elseif activeButton == 3+(displayOffset*4) then
  216.                     moveBook('out',3+(displayOffset*4))
  217.                     activeButton = 0
  218.                 end
  219.             end
  220.     elseif x >= buttons["buttonPos3"].x and x < buttons["buttonPos3"].x + buttons["buttonPos3"].width and
  221.         y >= buttons["buttonPos3"].y and y < buttons["buttonPos3"].y + buttons["buttonPos3"].height then
  222.             if buttons["buttonPos3"].textOn ~= "TARGET" then
  223.                 if activeButton == 0 then
  224.                     if moveBook('in',4+(displayOffset*4)) then
  225.                         activeButton = 4+(displayOffset*4)
  226.                     end
  227.                 elseif activeButton == 4+(displayOffset*4) then
  228.                     moveBook('out',4+(displayOffset*4))
  229.                     activeButton = 0
  230.                 end
  231.             end
  232.     elseif x >= buttons["buttonUP"].x and x < buttons["buttonUP"].x + buttons["buttonUP"].width and
  233.         y >= buttons["buttonUP"].y and y < buttons["buttonUP"].y + buttons["buttonUP"].height then
  234.             if displayOffset ~= 0 then displayOffset = displayOffset - 1 end
  235.     elseif x >= buttons["buttonDOWN"].x and x < buttons["buttonDOWN"].x + buttons["buttonDOWN"].width and
  236.         y >= buttons["buttonDOWN"].y and y < buttons["buttonDOWN"].y + buttons["buttonDOWN"].height then
  237.             if 4 + displayOffset < targetCount then
  238.                 displayOffset = displayOffset + 1
  239.             end
  240.     else
  241.        
  242.     end
  243.     clearAll()
  244. end
  245.  
  246. function waitForInput()
  247.     parallel.waitForAny(wait,getTouch)
  248. end
  249.  
  250. function getTouch()
  251.     event, side, xPos, yPos = os.pullEvent("monitor_touch")
  252.     touchAreas(xPos, yPos, side)
  253. end
  254.  
  255. function wait()
  256.  sleep(1)
  257. end
  258.  
  259. function init()
  260.     clearAll()
  261.     resizeMon()
  262.     addButtons()
  263.     clearAll()
  264.     setupTargets()
  265.     while true do
  266.         generateGUI(m)
  267.         waitForInput()
  268.         clearAll()
  269.     end
  270. end
  271.  
  272. init()
Advertisement
Add Comment
Please, Sign In to add comment