Advertisement
BigSHinyToys

terminal redirect controll system 0.1alpha

Apr 7th, 2013
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.43 KB | None | 0 0
  1. --[[
  2.         Monitor and terminal Display controll system.
  3.         by BigShinyToys
  4. ]]--
  5.  
  6. local ver = "0.1aplha"
  7.  
  8. local function connect(tMonitor)
  9.  
  10.     local termX,termY = term.getSize()
  11.     local tFunctions = {"write","scroll","setCursorPos","setCursorBlink","getCursorPos",
  12.     "getSize","clear","clearLine","setTextColour","setTextColor", -- setTextScale
  13.     "setBackgroundColour","setBackgroundColor","isColour","isColor",}
  14.     local tPassed = {}
  15.     tPassed.direct = {}
  16.     tPassed.wire = {}
  17.     -- evluate candidats
  18.     for _,device in pairs(tMonitor) do
  19.         for size = 1.5,0.5,-0.5 do
  20.             local monX,monY
  21.             if device.type == "direct" then
  22.                 peripheral.call(device.side,"setTextScale",size)
  23.                 monX,monY = peripheral.call(device.side,"getSize")
  24.             elseif device.type == "wire" then
  25.                 peripheral.call(device.modemSide,"callRemote",device.deviceID,"setTextScale",size)
  26.                 monX,monY = peripheral.call(device.modemSide,"callRemote",device.deviceID,"getSize")
  27.             end
  28.             if monX >= termX and monY >= termY then
  29.                 table.insert(tPassed[device.type],device)
  30.                 break
  31.             end
  32.         end
  33.     end
  34.    
  35.     local tOut = {}
  36.    
  37.     --print("Running pack process")
  38.    
  39.     for _,item in pairs(tFunctions) do
  40.         local tempFunction =  function()
  41.             return function(...)
  42.                 for k,v in pairs(tPassed.direct) do
  43.                     peripheral.call(v.side,item,...)
  44.                 end
  45.                 for k,v in pairs(tPassed.wire) do
  46.                     peripheral.call(v.modemSide,"callRemote",v.deviceID,item,...)
  47.                 end
  48.                 return term.native[item](...)
  49.             end
  50.         end
  51.         tOut[item] = tempFunction()
  52.     end
  53.    
  54.     return tOut
  55.    
  56. end
  57.  
  58. --[[
  59. local tArg = {...}
  60.  
  61. if tArg[1] then
  62.     term.clear()
  63.     term.setCursorPos(1,1)
  64.     term.restore()
  65. else
  66.     local newTerm = connect(grabScreens())
  67.     term.redirect(newTerm)
  68.     term.clear()
  69.     term.setCursorPos(1,1)
  70.     --term.write("test")
  71.     --os.pullEvent()
  72. end
  73. ]]--
  74.  
  75.  
  76.  
  77. local function test(...)
  78.     term.setCursorPos(1,1)
  79.     print(...)
  80.     local event,number = os.pullEvent()
  81.     if event == "key" and number == 14 then
  82.         error()
  83.     end
  84. end
  85.  
  86. --[[
  87.         {"type","player","other","y/n","number",},
  88.         {"random type","Offline","I dont know what",true,7},
  89.         {"random type","Offline","I dont know what",true,7},
  90.         {"random type","Offline","I dont know what",true,7},
  91.         {"random type","Offline","I dont know what",true,7},
  92.         {"random type","Offline","I dont know what",true,7},
  93.         {"random type","Offline","I dont know what",true,7},
  94.         {"random type","Offline","I dont know what",true,7},
  95.         {"random type","Offline","I dont know what",true,7},
  96.         {"random type","Offline","I dont know what",true,7},
  97. ]]--
  98.  
  99. local testGrid = {
  100.     posX = 2,
  101.     posY = 2,
  102.     width = 50,
  103.     hight = 16,
  104.     selOffX = 0,
  105.     selOffY = 0,
  106.     colWidth = {8,11,9,7,},
  107.     data = {}
  108. }
  109.  
  110. local function getperpheralData()
  111.     local tree = {}
  112.     local data = {}
  113.     print("Welcome to netSearch")
  114.     for k,v in pairs(rs.getSides()) do
  115.         if peripheral.getType(v) == "modem" and not peripheral.call(v,"isWireless") then
  116.             tree[v] = {["type"] = "side",["status"] = "closed",["sub"] = {},}
  117.         end
  118.     end
  119.     print("Found "..#tree.." wired access points.")
  120.  
  121.     for k,v in pairs(tree) do
  122.         print(type(k),k," ",type(v))
  123.         local devices = peripheral.call(k,"getNamesRemote")
  124.         for a,b in pairs(devices) do
  125.             print(type(a),tostring(a)," ",type(b),tostring(b))
  126.             tree[k].sub[b] = peripheral.call(k,"getTypeRemote",b)
  127.         end
  128.     end
  129.  
  130.     term.clear()
  131.     term.setCursorPos(1,1)
  132.    
  133.     table.insert(data,{"Side","Name","Type","Active"})
  134.    
  135.     for k,v in pairs(tree) do
  136.        
  137.         print("--- Side "..k.."---")
  138.         for j,l in pairs(v.sub) do
  139.             if l == "monitor" then
  140.                 table.insert(data,{k,j,l,false})
  141.             end
  142.         end
  143.        
  144.     end
  145.    
  146.     return data
  147. end
  148.  
  149. local function drawGrid(grid)
  150.     local sBlank = ""
  151.     for k,v in pairs(grid.colWidth) do
  152.         sBlank = sBlank..string.rep(" ",v-1).."|"
  153.     end
  154.     for i = grid.posY,grid.hight+grid.posY do
  155.         term.setCursorPos(grid.posX,i)
  156.         local sel = i - grid.posY + 1 + grid.selOffY
  157.         if grid.data[sel] then
  158.             if sel == 1 then
  159.                 term.setBackgroundColor(colors.gray)
  160.             else
  161.                 term.setBackgroundColor(colors.black)
  162.             end
  163.             for k,v in pairs(grid.data[sel]) do
  164.                 term.write(string.sub(tostring(v)..string.rep(" ",grid.colWidth[k] - 1),1,grid.colWidth[k] - 1).."|")
  165.             end
  166.         else
  167.             term.write(sBlank)
  168.         end
  169.     end
  170. end
  171.  
  172. local function manipulateGrid(grid)
  173.     local lastEvent = nil
  174.     local lastBlock = nil
  175.     local list = {}
  176.     local function applyScreen()
  177.         term.clear()
  178.         term.setCursorPos(1,1)
  179.         term.restore()
  180.         term.redirect(connect(list))
  181.     end
  182.     while true do
  183.         term.clear()
  184.         drawGrid(grid)
  185.         local event = {os.pullEvent()}
  186.         if event[1] == "mouse_click" then
  187.             if event[2] == 1 then -- left button
  188.                 local row = event[4] - grid.posY + grid.selOffY + 1
  189.                 if event[4] == grid.posY - grid.selOffY then -- X line
  190.                     local total = grid.posX - 1
  191.                     for i = 1,#grid.colWidth do
  192.                         total = total + grid.colWidth[i]
  193.                         if event[3] == total then
  194.                             lastBlock = i
  195.                         end
  196.                     end
  197.                 elseif grid.data[row] then
  198.                     grid.data[row][4] = not grid.data[row][4]
  199.                     list = {}
  200.                     for no,value in pairs(grid.data) do
  201.                         if no ~= 1 and value[4] then
  202.                             table.insert(list,{["type"] = "wire",["modemSide"] = value[1],["deviceID"] = value[2]})
  203.                         end
  204.                     end
  205.                     applyScreen()
  206.                 end
  207.             end
  208.         elseif event[1] == "mouse_drag" then
  209.             if event[2] == 1 then -- left button
  210.                 if event[4] == grid.posY - grid.selOffY and lastBlock then -- X line
  211.                     local nTest = event[3] - lastEvent[3]
  212.                     if nTest == 1 or nTest == -1 then
  213.                         grid.colWidth[lastBlock] = grid.colWidth[lastBlock] + nTest
  214.                     else
  215.                         lastBlock = nil
  216.                     end
  217.                     if lastBlock and grid.colWidth[lastBlock] < 1 then
  218.                         grid.colWidth[lastBlock] = 1
  219.                         lastBlock = nil
  220.                     end
  221.                 else
  222.                     lastBlock = nil
  223.                 end
  224.             end
  225.         elseif event[1] == "mouse_scroll" then
  226.             grid.selOffY = grid.selOffY + event[2]
  227.         elseif event[1] == "key" then
  228.             if event[2] == 14 then
  229.                 return
  230.             elseif event[2] == 200 then -- up
  231.                 grid.posY = grid.posY - 1
  232.             elseif event[2] == 208 then -- down
  233.                 grid.posY = grid.posY + 1
  234.             elseif event[2] == 203 then -- left
  235.                 grid.posX = grid.posX - 1
  236.             elseif event[2] == 205 then -- right
  237.                 grid.posX = grid.posX + 1
  238.             elseif event[2] == 28 then -- enter
  239.                 applyScreen()
  240.                 return
  241.             end
  242.         elseif event[1] == "peripheral" or event[1] == "peripheral_detach" then
  243.             grid.data = getperpheralData()
  244.             list = {}
  245.             applyScreen()
  246.         end
  247.         lastEvent = event
  248.     end
  249. end
  250.  
  251. testGrid.data = getperpheralData()
  252.  
  253. manipulateGrid(testGrid)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement