Advertisement
BigSHinyToys

terminal redirect controll system v0.3

Jun 2nd, 2013
461
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.79 KB | None | 0 0
  1. --[[
  2.         Monitor and terminal Display controll system.
  3.         by BigShinyToys
  4. ]]--
  5.  
  6. local ver = "0.3"
  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" then
  155.             local tList = peripheral.getMethods(v)
  156.             hasWire = false
  157.             for i = 1,#tList do
  158.                 if tList[i] == "isWireless" then
  159.                     if not peripheral.call(v,"isWireless") then
  160.                         tree[v] = {["type"] = "side",["status"] = "closed",["sub"] = {},}
  161.                     end
  162.                     hasWire = true
  163.                     break
  164.                 end
  165.             end
  166.             if not hasWire then
  167.                 error("Requires a Computer Craft ver 1.51 or above and Peripheral cables")
  168.             end
  169.         elseif peripheral.getType(v) == "monitor" then
  170.             table.insert(data,{v,v,"monitor",false,"direct","best"})
  171.         end
  172.     end
  173.     print("Found "..#tree.." wired access points.")
  174.  
  175.     for k,v in pairs(tree) do
  176.         print(type(k),k," ",type(v))
  177.         local devices = peripheral.call(k,"getNamesRemote")
  178.         for a,b in pairs(devices) do
  179.             print(type(a),tostring(a)," ",type(b),tostring(b))
  180.             tree[k].sub[b] = peripheral.call(k,"getTypeRemote",b)
  181.         end
  182.     end
  183.  
  184.     term.clear()
  185.     term.setCursorPos(1,1)
  186.    
  187.     for k,v in pairs(tree) do
  188.        
  189.         print("--- Side "..k.."---")
  190.         for j,l in pairs(v.sub) do
  191.             if l == "monitor" then
  192.                 table.insert(data,{k,j,l,false,"wire","best"})
  193.             end
  194.         end
  195.        
  196.     end
  197.    
  198.     return data
  199. end
  200.  
  201. local titleTX = term.isColor() and colors.white or colors.black
  202. local titleBC = term.isColor() and colors.gray or colors.white
  203.  
  204. local function drawGrid(grid)
  205.     local sBlank = ""
  206.     for k,v in pairs(grid.colWidth) do
  207.         sBlank = sBlank..string.rep(" ",v-1).."|"
  208.     end
  209.     for i = grid.posY,grid.hight+grid.posY do
  210.         term.setCursorPos(grid.posX,i)
  211.         local sel = i - grid.posY + 1 + grid.selOffY
  212.         if grid.data[sel] then
  213.             if sel == 1 then
  214.                 term.setTextColor(titleTX)
  215.                 term.setBackgroundColor(titleBC)
  216.             else
  217.                 term.setTextColor(colors.white)
  218.                 term.setBackgroundColor(colors.black)
  219.             end
  220.             for k,v in pairs(grid.data[sel]) do
  221.                 term.write(string.sub(tostring(v)..string.rep(" ",grid.colWidth[k] - 1),1,grid.colWidth[k] - 1).."|")
  222.             end
  223.         else
  224.             term.write(sBlank)
  225.         end
  226.     end
  227. end
  228.  
  229. local function manipulateGrid(grid)
  230.     local lastEvent = nil
  231.     local lastBlock = nil
  232.     local list = {}
  233.     local function applyScreen()
  234.         term.clear()
  235.         term.setCursorPos(1,1)
  236.         term.restore()
  237.         term.redirect(connect(list))
  238.     end
  239.     local function reList()
  240.         list = {}
  241.         for no,value in pairs(grid.data) do
  242.             if no ~= 1 and value[4] then
  243.                 table.insert(list,{["type"] = value[5],["side"] = value[1],["modemSide"] = value[1],["deviceID"] = value[2],["scale"] = value[6]})
  244.             end
  245.         end
  246.     end
  247.     applyScreen()
  248.     while true do
  249.         term.clear()
  250.         drawGrid(grid)
  251.         local event = {os.pullEvent()}
  252.         if event[1] == "mouse_click" then
  253.             --if event[2] == 1 then -- left button
  254.                 local row = event[4] - grid.posY + grid.selOffY + 1
  255.                
  256.                 local total = grid.posX - 1
  257.                 lastBlock = nil
  258.                 for i = 1,#grid.colWidth do
  259.                     if event[3] > total and event[3] <= total + grid.colWidth[i] then
  260.                         lastBlock = i
  261.                         break
  262.                     end
  263.                     total = total + grid.colWidth[i]
  264.                 end
  265.                
  266.                 if event[4] == grid.posY - grid.selOffY then -- X line
  267.                    
  268.                 elseif grid.data[row] then
  269.                     if lastBlock == 4 then
  270.                         grid.data[row][4] = not grid.data[row][4]
  271.                         reList()
  272.                     elseif lastBlock == 6 then
  273.                         if event[2]== 1 then
  274.                             if grid.data[row][6] == "best" then
  275.                                 grid.data[row][6] = 0.5
  276.                             else
  277.                                 grid.data[row][6] = grid.data[row][6] + 0.5
  278.                             end
  279.                             if grid.data[row][6] > 5 then
  280.                                 grid.data[row][6] = "best"
  281.                             end
  282.                         elseif event[2] == 2 then
  283.                             if grid.data[row][6] == "best" then
  284.                                 grid.data[row][6] = 5
  285.                             else
  286.                                 grid.data[row][6] = grid.data[row][6] - 0.5
  287.                             end
  288.                             if grid.data[row][6] < 0.5 then
  289.                                 grid.data[row][6] = "best"
  290.                             end
  291.                         end
  292.                         reList()
  293.                     end
  294.                     applyScreen()
  295.                 end
  296.             --end
  297.         elseif event[1] == "mouse_drag" then
  298.             if event[2] == 1 then -- left button
  299.                 if event[4] == grid.posY - grid.selOffY and lastBlock then -- X line
  300.                     local nTest = event[3] - lastEvent[3]
  301.                     if nTest == 1 or nTest == -1 then
  302.                         grid.colWidth[lastBlock] = grid.colWidth[lastBlock] + nTest
  303.                     else
  304.                         lastBlock = nil
  305.                     end
  306.                     if lastBlock and grid.colWidth[lastBlock] < 1 then
  307.                         grid.colWidth[lastBlock] = 1
  308.                         lastBlock = nil
  309.                     end
  310.                 else
  311.                     lastBlock = nil
  312.                 end
  313.             end
  314.         elseif event[1] == "mouse_scroll" then
  315.             grid.selOffY = grid.selOffY + event[2]
  316.         elseif event[1] == "key" then
  317.             if event[2] == 14 then
  318.                 return
  319.             elseif event[2] == 200 then -- up
  320.                 grid.posY = grid.posY - 1
  321.             elseif event[2] == 208 then -- down
  322.                 grid.posY = grid.posY + 1
  323.             elseif event[2] == 203 then -- left
  324.                 grid.posX = grid.posX - 1
  325.             elseif event[2] == 205 then -- right
  326.                 grid.posX = grid.posX + 1
  327.             elseif event[2] == 28 then -- enter
  328.                 applyScreen()
  329.                 return
  330.             end
  331.         elseif event[1] == "peripheral" or event[1] == "peripheral_detach" then
  332.             grid.data = getperpheralData()
  333.             reList()
  334.             applyScreen()
  335.         end
  336.         lastEvent = event
  337.     end
  338. end
  339.  
  340. testGrid.data = getperpheralData()
  341.  
  342. manipulateGrid(testGrid)
  343. term.setCursorPos(1,1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement