Advertisement
HydrantHunter

showMethods

Jan 7th, 2017
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.91 KB | None | 0 0
  1. --[[     showMethods    ]]--
  2. --[[       by Dog       ]]--
  3. --[[ aka  HydrantHunter ]]--
  4. --[[ pastebin: FxyJJ9R9 ]]--
  5. local smVer = "1.0.03"
  6. if not term.isColor() or pocket or turtle then error("Advanced Computer required.", 0) end
  7. local termX, termY = term.getSize()
  8. if termX < 51 or termY < 18 then error("Screen size too small. 51x18 required.", 0) end
  9. local myID, myName = os.getComputerID(), os.getComputerLabel()
  10. if not myName then myName = "Computer " .. tostring(myID) os.setComputerLabel(myName) end
  11. local screen = "main" --# local, network, localDetail, netDetail
  12. local numPerPage, drawHelp, drawLocalDevices = 14
  13. local nPage, nPages, localList, networkList = { }, { }, { }, { }
  14. nPage.top, nPage.bottom, nPage.left, nPage.right, nPage.front, nPage.back = 1, 1, 1, 1, 1 ,1
  15. networkList.top, networkList.bottom, networkList.left, networkList.right, networkList.front, networkList.back = { }, { }, { }, { }, { }, { }
  16. print("")
  17. print("Searching for GPS signal...")
  18. local x, y, z = gps.locate(1)
  19.  
  20. local function inventoryDevices()
  21.   for _, side in pairs(rs.getSides()) do
  22.     localList[side] = nil                --# clear localList table
  23.     for i = #networkList[side], 1, -1 do --# clear networkList table
  24.       networkList[side][i] = nil
  25.     end
  26.     if peripheral.isPresent(side) then   --# populate localList & networkList tables
  27.       if peripheral.getType(side) == "modem" and not peripheral.call(side, "isWireless") then
  28.         localList[side] = "wired_modem"
  29.         for _, name in pairs(peripheral.call(side, "getNamesRemote")) do
  30.           networkList[side][#networkList[side] + 1] = name
  31.         end
  32.         nPages[side] = networkList[side][1] and math.ceil(#networkList[side] / numPerPage) or 1
  33.       else
  34.         localList[side] = peripheral.getType(side) == "modem" and "wireless_modem" or peripheral.getType(side)
  35.       end
  36.     end
  37.   end
  38. end
  39.  
  40. local function clearArea(yStart, bgColor, cursor)
  41.   paintutils.drawFilledBox(1, yStart, termX, termY, bgColor or colors.black)
  42.   if cursor then
  43.     term.setTextColor(colors.white)
  44.     term.setCursorPos(1, 1)
  45.   end
  46. end
  47.  
  48. do
  49.   local helpText = {
  50.     [4] = "Press F1 to open and close basic help.";
  51.     [6] = "Help can also be closed by clicking the X.";
  52.     [8] = "Click on devices to see their methods.";
  53.     [10] = "Click on wired modems to see connected devices.";
  54.     [12] = "CTRL-Click on wired modems to see their methods.";
  55.     [14] = "Close modem/method screens by clicking the X.";
  56.     [16] = "Change pages via mouse wheel or PGUP/PGDN.";
  57.   }
  58.  
  59.   drawHelp = function()
  60.     clearArea(3, colors.white)
  61.     term.setTextColor(colors.black)
  62.     for i = 4, 16, 2 do
  63.       term.setCursorPos(2, i)
  64.       term.write(helpText[i])
  65.     end
  66.   end
  67. end
  68.  
  69. local function drawDeviceMethods(sSide, sModem, sDevice)
  70.   screen = sDevice and "netDetail" or "localDetail"
  71.   sDevice = sDevice or sModem
  72.   term.setBackgroundColor(colors.gray)
  73.   term.setTextColor(colors.white)
  74.   term.setCursorPos(1, 2)
  75.   if screen == "netDetail" then
  76.     term.write(" " .. sSide .. " / " .. sModem .. " / " .. sDevice .. " methods" .. string.rep(" ", 36 - (#sSide + #sModem + #sDevice)))
  77.   else
  78.     term.write(" " .. sSide .. " / " .. sDevice .. " methods" .. string.rep(" ", 39 - (#sSide + #sDevice)))
  79.   end
  80.   clearArea(3)
  81.   term.setCursorPos(1, 4)
  82.   print(textutils.pagedTabulate(colors.yellow, screen == "netDetail" and peripheral.getMethods(sDevice) or peripheral.getMethods(sSide)))
  83. end
  84.  
  85. local function drawNetworkedDevices(sSide, sModem)
  86.   screen = "network"
  87.   local sName = sSide .. " / " .. sModem
  88.   term.setBackgroundColor(colors.gray)
  89.   term.setTextColor(colors.white)
  90.   term.setCursorPos(1, 2)
  91.   term.write(" " .. sName:sub(1, 34) .. string.rep(" ", termX - #sName:sub(1, 34)))
  92.   clearArea(3)
  93.   if networkList[sSide][1] then
  94.     local yPos, magicNumber = 3, ((nPage[sSide] - 1) * numPerPage) + 1
  95.     term.setTextColor(colors.orange)
  96.     for i = magicNumber, #networkList[sSide] do
  97.       yPos = yPos + 1
  98.       term.setCursorPos(1, yPos)
  99.       term.write(networkList[sSide][i])
  100.       if yPos - 3 >= numPerPage then break end
  101.     end
  102.   else
  103.     term.setTextColor(colors.gray)
  104.     term.write("No devices connected")
  105.   end
  106.   term.setCursorPos(1, termY)
  107.   term.setBackgroundColor(colors.gray)
  108.   term.setTextColor(colors.white)
  109.   term.write(string.rep(" ", termX))
  110.   term.setCursorPos(math.floor(termX - (#tostring(nPage[sSide]) + #tostring(nPages[sSide]) + 4)) / 2, termY)
  111.   term.write(tostring(nPage[sSide]) .. " of " .. tostring(nPages[sSide]))
  112. end
  113.  
  114. do
  115.   local localData = {
  116.     [5] = { "ID#:   ", tostring(myID) };
  117.     [6] = { "GPS:   ", x and "X: " .. tostring(x) .. " | Y: " .. tostring(y) .. " | Z: " .. tostring(z) or "No GPS fix" };
  118.   }
  119.  
  120.   drawLocalDevices = function()
  121.     screen = "main"
  122.     term.setBackgroundColor(colors.gray)
  123.     term.setTextColor(colors.white)
  124.     term.setCursorPos(1, 2)
  125.     term.write(" " .. myName:sub(1, 49) .. string.rep(" ", 50 - #myName:sub(1, 49)))
  126.     clearArea(3)
  127.     for i = 5, 6 do
  128.       term.setCursorPos(1, i)
  129.       term.setTextColor(colors.lightGray)
  130.       term.write(localData[i][1])
  131.       term.setTextColor(colors.white)
  132.       term.write(localData[i][2])
  133.     end
  134.     local yPos = 7
  135.     for _, side in pairs(rs.getSides()) do
  136.       yPos = yPos + 1
  137.       term.setCursorPos(1, yPos)
  138.       term.setTextColor(colors.green)
  139.       term.write(side)
  140.       term.setTextColor(colors.lightGray)
  141.       term.write(":" .. string.rep(" ", 7 - #side))
  142.       if localList[side] then
  143.         term.setTextColor(localList[side] == "wired_modem" and colors.orange or colors.blue)
  144.       else
  145.         term.setTextColor(colors.gray)
  146.       end
  147.       term.write(localList[side] or "no device present")
  148.     end
  149.   end
  150. end
  151.  
  152. local function mainLoop()
  153.   local control, help, sSide, sModem, sDevice, tDevice = false, false
  154.   local sideList, listCounter = { }, 0
  155.   local screens = {
  156.     main = { function() drawLocalDevices() end, function() clearArea(1, nil, true) error() end };
  157.     network = { function() drawNetworkedDevices(sSide, sModem) end, function() sSide = nil sModem = nil drawLocalDevices() end };
  158.     netDetail = { function() drawDeviceMethods(sSide, sModem, sDevice) end, function() sDevice = nil drawNetworkedDevices(sSide, sModem) end };
  159.     localDetail = { function() drawDeviceMethods(sSide, sDevice) end, function() sSide = nil sDevice = nil drawLocalDevices() end };
  160.   }
  161.   for _, side in pairs(rs.getSides()) do
  162.     listCounter = listCounter + 1
  163.     sideList[listCounter] = side
  164.   end
  165.   while true do
  166.     local event, data, mX, mY = os.pullEvent()
  167.     if event == "mouse_click" and data == 1 then
  168.       if mX == termX and mY == 1 then
  169.         if help then --# Exit help
  170.           help = false
  171.           screens[screen][1]()
  172.         else         --# Exit screen or program
  173.           screens[screen][2]()
  174.         end
  175.       elseif screen == "main" then
  176.         for i = 8, 13 do
  177.           if mY == i and localList[sideList[i - 7]] and mX > 8 and mX < #localList[sideList[i - 7]] + 9 then
  178.             sSide, tDevice = sideList[i - 7], localList[sideList[i - 7]]
  179.             if tDevice == "wired_modem" and not control then
  180.               sModem = tDevice
  181.               drawNetworkedDevices(sSide, sModem)
  182.             else
  183.               sDevice = tDevice
  184.               drawDeviceMethods(sSide, sDevice)
  185.             end
  186.             break
  187.           end
  188.         end
  189.       elseif screen == "network" then
  190.         if networkList[sSide][1] then
  191.           local yPos, magicNumber = 3, ((nPage[sSide] - 1) * numPerPage) + 1
  192.           for i = magicNumber, #networkList[sSide] do
  193.             yPos = yPos + 1
  194.             if mY == yPos and mX <= #networkList[sSide][i] then
  195.               sDevice = networkList[sSide][i]
  196.               drawDeviceMethods(sSide, sModem, sDevice)
  197.               break
  198.             end
  199.             if yPos - 3 >= numPerPage then break end
  200.           end
  201.         end
  202.       end
  203.     elseif event == "mouse_scroll" and screen == "network" and nPages[sSide] > 1 and not help then
  204.       nPage[sSide] = data == 1 and math.min(nPage[sSide] + 1, nPages[sSide]) or math.max(nPage[sSide] - 1, 1)
  205.       drawNetworkedDevices(sSide, sModem)
  206.     elseif event == "key" then
  207.       if data == keys.f1 then
  208.         help = not help
  209.         if help then
  210.           drawHelp()
  211.         else
  212.           screens[screen][1]()
  213.         end
  214.       elseif data == keys.leftCtrl or data == keys.rightCtrl then
  215.         control = true
  216.       elseif (data == keys.pageUp or data == keys.pageDown) and screen == "network" and nPages[sSide] > 1 and not help then
  217.         nPage[sSide] = data == keys.pageDown and math.min(nPage[sSide] + 1, nPages[sSide]) or math.max(nPage[sSide] - 1, 1)
  218.         drawNetworkedDevices(sSide, sModem)
  219.       elseif (data == keys.home or data == keys["end"]) and screen == "network" and nPages[sSide] > 1 and not help then
  220.         nPage[sSide] = data == keys.home and 1 or nPages[sSide]
  221.         drawNetworkedDevices(sSide, sModem)
  222.       end
  223.     elseif event == "key_up" and (data == keys.leftCtrl or data == keys.rightCtrl) then
  224.       control = false
  225.     elseif event == "char" then
  226.       if data:lower() == "q" then --# Exit program
  227.         clearArea(1, nil, true)
  228.         return
  229.       end
  230.     elseif event == "peripheral" or event == "peripheral_detach" then
  231.       inventoryDevices()
  232.       if event == "peripheral_detach" then
  233.         sSide, sModem, sDevice = nil, nil, nil
  234.         if help then
  235.           screen = "main"
  236.         else
  237.           drawLocalDevices()
  238.         end
  239.       end
  240.     end
  241.   end
  242. end
  243.  
  244. clearArea(3)
  245. paintutils.drawLine(1, 1, termX - 1, 1, colors.blue) --# draw header
  246. term.setCursorPos(math.floor((termX - 18) / 2), 1)
  247. term.setTextColor(colors.lightBlue)
  248. term.write("showMethods " .. smVer)
  249. --paintutils.drawFilledBox(termX - 2, 1, termX, 3, colors.red)
  250. term.setCursorPos(termX, 1)
  251. term.setTextColor(colors.white)
  252. term.setBackgroundColor(colors.red)
  253. term.write("X")
  254. inventoryDevices()
  255. drawLocalDevices()
  256. mainLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement