Advertisement
BigSHinyToys

terminal redirect controll system v0.2

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