Guest User

startup

a guest
Jan 27th, 2015
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.84 KB | None | 0 0
  1. -- locals
  2. local logs = {}
  3. local openTime = 5
  4. local textSize = 1
  5. local minimumNameLength = 4
  6. local buttonPadding = 1 --Min 1, Space before/after
  7. local centerPadding = 0 --Min 0, Space before/after
  8.  
  9. local buttonColor = colors.lightGray
  10. local buttonActiveColor = colors.orange
  11. local pageBack = "Prev Page"
  12. local pageFore = "Next Page"
  13. local resetButton = "Refresh"
  14. -- Hub Portals
  15. local portals = {
  16.  
  17.   { name="Nether"; address="Dehsetcro Migomcor Ormpra" },
  18.   { name="DS7"; address="Sileston Alsetbrei Lanulf" },
  19.   { name="Rapture"; address="Sileston Amecar Othgon"},
  20.   { name="Spawn"; address="Sileston Protaset Erpulf"},
  21.   { name="Reign"; address="Sileston Vamaclos Othas"},
  22.   { name="Moon"; address="Croursal Gonarva Ecsil"},
  23.   { name="The End"; address="Erpclaus Ommaculf Lanal"}
  24. }
  25.  
  26. --local portals = {
  27. --  { name="Hub"; address="Dehsetcro Migomcor Ormpra" }
  28. --}
  29.  
  30. --internal
  31. local monitor, touch
  32. local gate
  33. local monitorLocation
  34. local monX = 0
  35. local monY = 0
  36. local monMidL = 0
  37. local monMidR = 0
  38. local maxNameLength = 0
  39. local problem = false
  40. local names = {}
  41. local curPage = 1
  42. local lastPage = 1
  43. local namesPerPage = 1
  44. local pages = {}
  45. local gateType = 'Abstract Bus Adapter'
  46.  
  47.  
  48. function writeLog(text)
  49.   print(text)
  50. end
  51.  
  52. function init()
  53.   if not fs.exists('apis') then
  54.     fs.makeDir('apis')
  55.   end
  56.   if not fs.exists('apis/touchpoint') then
  57.     writeLog("You don't have the touchpoint API. Beaming it down.")
  58.     shell.run('pastebin get pFHeia96 apis/touchpoint')
  59.   end
  60.   os.loadAPI('apis/touchpoint')
  61.  
  62.  
  63.   if not fs.exists('apis/json') then
  64.     writeLog("You don't have the JSON API. Grabboring it.")
  65.     shell.run("pastebin get 4nRg9CHU apis/json")
  66.   end
  67.   os.loadAPI('apis/json')
  68.  
  69.   monitorLocation = findSideFor('monitor')
  70.   monitor = peripheral.wrap(monitorLocation)
  71.   gate    = peripheral.find(gateType)
  72.  
  73.   setupScreen()
  74. end
  75.  
  76. function handleMonitorResize()
  77.   writeLog("Screen resized. Setting up screen!")
  78.   setupScreen()
  79.   refresh()
  80. end
  81.  
  82.  
  83. function determineLength(myString)
  84.   local theLength = 0
  85.   if string.len(myString) > maxNameLength then
  86.     theLength = maxNameLength + buttonPadding * 2
  87.   else
  88.     theLength = string.len(myString) + buttonPadding * 2
  89.   end
  90.  
  91.   return theLength
  92. end
  93.  
  94.  
  95. function setupScreen()
  96.   monitor.clear()
  97.   monitor.setTextScale(textSize)
  98.   monX, monY = monitor.getSize()
  99.  
  100.   writeLog("Screen Dimensions: (x: "..monX..", y: "..monY..")")
  101.  
  102.   if (monY < 5) or (monX < (minimumNameLength * 2) + 4) then
  103.     monitor.setBackgroundColor(colors.red)
  104.     monitor.setTextColor(colors.black)
  105.     centerText("!!!", 3)
  106.     centerText("ERROR", 4)
  107.     centerText("VIEW COMPUTER", 5)
  108.     writeLog("Screen too small with current resolution, please adjust.")
  109.     problem = true
  110.   else
  111.     if math.floor(monX/2) == math.ceil(monX/2) then --Even
  112.       monMidR = monX/2 + 1
  113.       monMidL = monMidR - 1
  114.       maxNameLength = (monX - 4)/2 - (centerPadding + buttonPadding) * 2
  115.     else --Odd
  116.       monMidR = math.floor(monX/2) + 1
  117.       monMidL = monMidR
  118.       maxNameLength = (monX - 3)/2 - (centerPadding + buttonPadding) * 2
  119.     end
  120.     namesPerPage = math.ceil((monY - 4) / 2) * 2
  121.   end
  122. end
  123.  
  124. function rebuildPages(p1)
  125.   pages[curPage]:toggleButton(resetButton)
  126.   refresh()
  127. end
  128.  
  129. function doAction(buttonName)
  130.   local pressedButtonDesc
  131.  
  132.   writeLog("doAction("..buttonName..")")
  133.  
  134.   for k,v in ipairs(portals) do
  135.     if v["name"] == buttonName then
  136.       pressedButtonDesc = v
  137.       break
  138.     end
  139.   end
  140.   dialGate(pressedButtonDesc["address"])
  141.   pages[curPage]:flash(buttonName, openTime)
  142. end
  143.  
  144. function prevPage()
  145.   lastPage = curPage
  146.  
  147.   pages[lastPage]:flash(pageBack, 0.125)
  148.  
  149.   curPage = curPage - 1
  150.   if curPage < 1 then
  151.     curPage = #pages
  152.   end
  153.   pages[curPage]:draw()
  154.  
  155. end
  156.  
  157. function nextPage()
  158.   lastPage = curPage
  159.  
  160.   pages[lastPage]:flash(pageFore, 0.125)
  161.  
  162.   curPage = curPage + 1
  163.   if curPage > #pages then
  164.     curPage = 1
  165.   end
  166.   pages[curPage]:draw()
  167. end
  168.  
  169. function displayError(error)
  170.   monitor.clear()
  171.   monitor.setBackgroundColor(colors.red)
  172.   monitor.setTextColor(colors.black)
  173.   centerText("!!!", 3)
  174.   centerText(error, 4)
  175.   writeLog("Error: "..error)
  176. end
  177.  
  178. function buildPage(pN)
  179.   pages[pN] = touchpoint.new(monitorLocation)
  180.   pages[pN]:add(pageBack, prevPage, 1, 1, determineLength(pageBack), 1, buttonColor, buttonActiveColor)
  181.   pages[pN]:add(pageFore, nextPage, monX - determineLength(pageFore) + 1, 1, monX, 1, buttonColor, buttonActiveColor)
  182.  
  183.   if #portals > 0 then
  184.     local curIndex = (pN - 1) * namesPerPage + 1
  185.     for y = 3, monY - 2, 2 do
  186.       if curIndex < #portals + 1 then
  187.         pages[pN]:add(portals[curIndex]["name"], doAction, 2, y, monMidL - 1 - centerPadding, y, buttonColor, buttonActiveColor)
  188.       else
  189.         break
  190.       end
  191.       curIndex = curIndex + 1
  192.       if curIndex < #portals + 1 then
  193.         pages[pN]:add(portals[curIndex]["name"], doAction, monMidR + 1 + centerPadding, y, monX - 1, y, buttonColor, buttonActiveColor)
  194.       else
  195.         break
  196.       end
  197.       curIndex = curIndex + 1
  198.     end
  199.   end
  200.  
  201.   pages[pN]:add(resetButton, rebuildPages, 1, monY, determineLength(resetButton), monY, buttonColor, buttonActiveColor)
  202.  
  203. end
  204.  
  205. function refresh()
  206.   writeLog("Refreshing stargate list.")
  207.   pages = {}
  208.   local myPage = 1
  209.  
  210.   writeLog("# stargates: "..#portals)
  211.   if #portals> 0 then
  212.     writeLog('Names/Page: '..namesPerPage)
  213.     while (myPage - 1) * namesPerPage < #portals do
  214.       writeLog('Populating Page'..myPage..'.')
  215.       buildPage(myPage)
  216.       myPage = myPage + 1
  217.      
  218.     end
  219.   else
  220.     buildPage(1)
  221.   end
  222.  
  223.   if curPage > #pages then
  224.     curPage = #pages
  225.   end
  226.  
  227.   writeLog('Current Page:'..curPage)
  228.   pages[curPage]:draw()
  229. end
  230.  
  231. function dialGate(address)
  232.   writeLog('----')
  233.   writeLog("Dialing "..address)  
  234.   result = gate.sendPacket("FFFF", "action: dial", "timeout: "..openTime, "address: "..address)
  235.   writeLog("Gate told to dial. Result: "..result)
  236.   return result
  237. end
  238.  
  239. function closeGate()
  240.   writeLog("Force closing the gate.")
  241.   result = gate.sendPacket("FFFF", "action: disconnect")
  242.   writeLog("Gate told to close. Result: "..result)
  243. end
  244.  
  245.  
  246. function centerText(text, row)
  247.   local size = string.len(text)
  248.   offset = math.floor((monX - size) / 2) + 1
  249.   monitor.setCursorPos(offset, row)
  250.   monitor.write(text)
  251. end
  252.  
  253. function findSideFor(typeToFind)
  254.   local sides = peripheral.getNames()
  255.   for i, side in ipairs(sides) do
  256.     local type = peripheral.getType(side)
  257.     if type == typeToFind then
  258.       return(side)
  259.     end
  260.   end
  261. end
  262.  
  263.  
  264. init()
  265.  
  266. if not problem then
  267.   refresh()
  268.   while true do
  269.     event, p1 = pages[curPage]:handleEvents(os.pullEvent())
  270.     if event == "button_click" then
  271.       pages[curPage].buttonList[p1].func(p1)
  272.     elseif event == "monitor_resize" then
  273.       handleMonitorResize()
  274.     end
  275.   end
  276. end
Advertisement
Add Comment
Please, Sign In to add comment