Advertisement
BigSHinyToys

cc glass term redirect

Jun 5th, 2013
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.10 KB | None | 0 0
  1. local function class(tInput)
  2.     local tClass = tInput
  3.    
  4.     local mt = {
  5.         __index = tClass
  6.     }
  7.        
  8.     tClass.new = function(o)
  9.         local tObj = o or {}
  10.         setmetatable(tObj,mt)
  11.         return tObj
  12.     end
  13.    
  14.     return tClass
  15. end
  16.  
  17. local screen = class {
  18.     screen = {},
  19.     realX = 30,
  20.     realY = 10,
  21.     posX = 1,
  22.     posY = 1,
  23.     isBlink = false,
  24.     screenTX = colors.white,
  25.     screenBC = colors.black,
  26.    
  27.     --[[
  28.     scp = function(...) pcall(term.native.setCursorPos,...) end,
  29.     scb = function(...) pcall(term.native.setCursorBlink,...) end,
  30.     stc = function(...) pcall(term.native.setTextColor,...) end,
  31.     sbc = function(...) pcall(term.native.setBackgroundColor,...) end,
  32.     wrt = function(...) pcall(term.native.write,...) end,
  33.     ]]--
  34.    
  35.     scp = term.native.setCursorPos,
  36.     scb = term.native.setCursorBlink,
  37.     stc = term.native.setTextColor,
  38.     sbc = term.native.setBackgroundColor,
  39.     wrt = term.native.write,
  40.    
  41.     drawFlag,
  42.     speed = 0.01,
  43.     offX = 0,
  44.     offY = 0,
  45.     triger = function(s)
  46.         s.drawFlag = true
  47.     end,
  48.     clearDrawFlag = function(s)
  49.         s.drawFlag = false
  50.     end,
  51.     getDrawFlag = function(s)
  52.         return s.drawFlag
  53.     end,
  54.     resize = function(s,width,hight) -- probble could be cleaner
  55.         local difX = s.realX - width
  56.         local difY = s.realY - hight
  57.         if difY ~= 0 then
  58.             if difY < 0 then
  59.                 for y = s.realY,hight do
  60.                     s.screen[y] = {}
  61.                     for x = 1,width do
  62.                         s.screen[y][x] = {s.screenBC,s.screenTX," "}
  63.                     end
  64.                 end
  65.             else
  66.                 for y = s.realY + difY,s.realY do
  67.                     s.screen[y] = nil
  68.                 end
  69.             end
  70.         end
  71.         if difX ~= 0 then
  72.             if difX < 0 then
  73.                 for y = 1,hight do
  74.                     for x = s.realX,width do  -- dont know
  75.                         if not s.screen[y][x] then
  76.                             s.screen[y][x] = {s.screenBC,s.screenTX," "}
  77.                         end
  78.                     end
  79.                 end
  80.             else
  81.                 for y = 1,hight do
  82.                     for x = s.realX,s.realX + difX do -- keep an eye on this
  83.                         s.screen[y][x] = nil
  84.                     end
  85.                 end
  86.             end
  87.         end
  88.         s.realX = width
  89.         s.realY = hight
  90.     end,
  91.     setPos = function(s,x,y)
  92.         s.offX = x
  93.         s.offY = y
  94.     end,
  95.    
  96.     newLine = function(s)
  97.         local tab = {}
  98.         for x = 1,s.realX do
  99.             tab[x] = {s.screenBC,s.screenTX," "}
  100.         end
  101.         return tab
  102.     end,
  103.    
  104.     slowDraw = function(s)
  105.         s.drawFlag = false
  106.         s.scb(false)
  107.         for y = 1,s.realY do
  108.             s.scp(1 + s.offX,y + s.offY)
  109.             for x = 1,s.realX do
  110.                 s.sbc(s.screen[y][x][1])
  111.                 s.stc(s.screen[y][x][2])
  112.                 s.wrt(s.screen[y][x][3])
  113.             end
  114.         end
  115.         if s.isBlink then
  116.             s.scp(s.posX + s.offX,s.posY + s.offY)
  117.             s.scb(s.isBlink)
  118.         else
  119.             s.scb(s.isBlink)
  120.         end
  121.     end,
  122.    
  123.     draw = function(s) -- faster
  124.         --pcall(function() -- test
  125.         s.drawFlag = false
  126.         s.scb(false)
  127.         for y = 1,s.realY do
  128.             s.scp(1 + s.offX,y + s.offY)
  129.             local lastTC
  130.             local lastBC
  131.             local TX = {}
  132.             for x = 1,s.realX do
  133.                 local tempBC = s.screen[y][x][1]
  134.                 local tempTC = s.screen[y][x][2]
  135.                 if lastTC == tempTC and lastBC == tempBC then
  136.                     TX[#TX + 1] = s.screen[y][x][3]
  137.                 elseif lastBC then
  138.                     s.sbc(lastBC or tempBC)
  139.                     s.stc(lastTC or tempTC)
  140.                     s.wrt(table.concat(TX,""))
  141.                     TX = {}
  142.                     lastTC = tempTC
  143.                     lastBC = tempBC
  144.                     TX[#TX + 1] = s.screen[y][x][3]
  145.                 else
  146.                     lastTC = tempTC
  147.                     lastBC = tempBC
  148.                     TX[#TX + 1] = s.screen[y][x][3]
  149.                 end
  150.             end
  151.             s.sbc(lastBC)
  152.             s.stc(lastTC)
  153.             s.wrt(table.concat(TX,""))
  154.         end
  155.         if s.isBlink then
  156.             s.scp(s.posX + s.offX,s.posY + s.offY)
  157.             s.scb(s.isBlink)
  158.         else
  159.             s.scb(s.isBlink)
  160.         end
  161.         --end) -- test
  162.     end,
  163.     isOverlap = function(s,inX,inY,w,h)
  164.         if inX and inY and w and h then
  165.             local rX1 = s.offX + (s.realX / 2) + 1
  166.             local rY1 = s.offY + (s.realY / 2) + 1
  167.             local rX2 = inX + (w/2)
  168.             local rY2 = inY + (h/2)
  169.        
  170.             if math.abs(rX1 - rX2) <= ((s.realX / 2) + (w/2)) and math.abs(rY1 - rY2) <= ((s.realY / 2) + (h/2)) then
  171.        
  172.                 return true
  173.             end
  174.                 return false
  175.         else
  176.             return error("not correct values")
  177.         end
  178.     end,
  179.    
  180.     isColor = function(s)
  181.         return true
  182.     end,
  183.     isColour = function(s)
  184.         return true
  185.     end,
  186.    
  187.     setTextColor = function(s,color)
  188.         s.screenTX = color
  189.     end,
  190.     setTextColour = function(s,color)
  191.         s.screenTX = color
  192.     end,
  193.    
  194.     setBackgroundColor = function(s,color)
  195.         s.screenBC = color
  196.     end,
  197.     setBackgroundColour = function(s,color)
  198.         s.screenBC = color
  199.     end,
  200.    
  201.     getSize = function(s)
  202.         return s.realX,s.realY
  203.     end,
  204.     clearLine = function(s)
  205.         s.screen[s.posY] = s:newLine()
  206.         s:triger()
  207.     end,
  208.     setCursorBlink = function(s,bool)
  209.         s.isBlink = bool
  210.         s:triger()
  211.     end,
  212.     getCursorPos = function(s)
  213.         return s.posX,s.posY
  214.     end,
  215.     clear = function(s)
  216.         s.screen = {}
  217.         for y = 1,s.realY do
  218.             s.screen[y] = {}
  219.             for x = 1,s.realX do
  220.                 s.screen[y][x] = {s.screenBC,s.screenTX," "}
  221.             end
  222.         end
  223.         s:triger()
  224.     end,
  225.     scroll = function(s,n)
  226.         for i = 1,n do
  227.             table.remove(s.screen,1)
  228.             table.insert(s.screen,s:newLine())
  229.         end
  230.         s:triger()
  231.     end,
  232.     setCursorPos = function(s,x,y)
  233.         s.posX = math.floor(x)
  234.         s.posY = math.floor(y)
  235.         s:triger()
  236.     end,
  237.     write = function(s,line)
  238.         if line and type(line) == "string" then
  239.             for i = 0,#line - 1 do
  240.                 if s.screen[s.posY] and s.screen[s.posY][s.posX + i] then
  241.                     s.screen[s.posY][s.posX + i] = {s.screenBC,s.screenTX,string.sub(line,i + 1,i + 1)}
  242.                 end
  243.             end
  244.             s.posX = s.posX + #line
  245.         end
  246.         s:triger()
  247.     end,
  248.     getRedirect = function(s)
  249.         local list = {
  250.             "isColor","isColour","setTextColor","setTextColour","setBackgroundColor","setBackgroundColour","getSize",
  251.             "clearLine","setCursorBlink","getCursorPos","clear","scroll","setCursorPos","write",
  252.         }
  253.         local fakeTerm = {}
  254.         local screen = s
  255.         for i = 1,#list do
  256.             fakeTerm[list[i]] = function(...)
  257.                 --return select(2,pcall(screen[list[i]],screen,...))
  258.                 return screen[list[i]](screen,...)
  259.             end
  260.         end
  261.         return fakeTerm
  262.     end,
  263.     setOutput = function(s,input)
  264.         if type(input) == "table" then
  265.             local req = {"scp","scb","stc","sbc","wrt"}
  266.             local install = true
  267.             for i = 1,#req do
  268.                 if not input[req[i]] then
  269.                     install = false
  270.                     error("missing "..tostring(req[i]))
  271.                 end
  272.             end
  273.             if install then
  274.                 for i = 1,#req do
  275.                     s[req[i]] = input[req[i]]
  276.                 end
  277.             end
  278.         end
  279.     end,
  280. }
  281.  
  282. local function findDevice(sType)
  283.     for _,sSide in pairs(peripheral.getNames()) do
  284.         if peripheral.isPresent(sSide) and peripheral.getType(sSide) == sType then
  285.             return sSide,peripheral.wrap(sSide)
  286.         end
  287.     end
  288. end
  289.  
  290. local id,glass = findDevice("terminal_glasses_bridge")
  291.  
  292. local fakeX,fakeY = 51,19
  293. local grid = {}
  294.  
  295. local col = {
  296. 15790320,
  297. 15435844,
  298. 12801229,
  299. 6719955,
  300. 14605932,
  301. 4312372,
  302. 14188952,
  303. 4408131,
  304. 10066329,
  305. 2651799,
  306. 8073150,
  307. 2437522,
  308. 5320730,
  309. 3887386,
  310. 11743532,
  311. 0,
  312. }
  313. local change = {}
  314. for i = 1,16 do
  315.     change[2^(i-1)] = col[i]
  316. end
  317.  
  318. local glasses = screen.new()
  319.  
  320. local offsetX,offsetY = 0,0
  321.  
  322. if id then
  323.     glass.clear()
  324.     for y = 1,fakeY do
  325.         grid[y] = {}
  326.         for x = 1,fakeX do
  327.             grid[y][x] = {
  328.                 back = glass.addBox((x*6) - 6 + offsetX, (y*9) - 9 + offsetY,6,9, 0x000000, 1),
  329.                 text = glass.addText((x*6) - 6 + offsetX, (y*9) - 9 + offsetY, "@", 0xCC0000)
  330.             }
  331.             grid[y][x].text.setZIndex(2)
  332.         end
  333.     end
  334. else
  335.     error("can't find glasses")
  336. end
  337.  
  338. local scp = term.native.setCursorPos
  339. local scb = term.native.setCursorBlink
  340. local stc = term.native.setTextColor
  341. local sbc = term.native.setBackgroundColor
  342. local wrt = term.native.write
  343.  
  344. local curX,curY = 1,1
  345. local textCol = 0
  346. local backCol = 0
  347.  
  348. local newMon = {
  349.     scp = function(x,y)
  350.         if x and y then
  351.             curX,curY = x,y
  352.             scp(x,y)
  353.         end
  354.     end,
  355.     scb = function(input)
  356.         -- unneeded for now
  357.         scb(input)
  358.     end,
  359.     stc = function(col)
  360.         if change[col] then
  361.             textCol = change[col]
  362.             stc(col)
  363.         else
  364.             error("not color "..tostring(col))
  365.         end
  366.     end,
  367.     sbc = function(col)
  368.         if change[col] then
  369.             backCol = change[col]
  370.             sbc(col)
  371.         else
  372.             error("not color "..tostring(col))
  373.         end
  374.     end,
  375.     wrt = function(text)
  376.         for i = 1,#text do
  377.             if grid[curY] and grid[curY][curX + i - 1] then
  378.                 grid[curY][curX + i - 1].text.setColor(textCol)
  379.                 grid[curY][curX + i - 1].text.setText(text:sub(i,i))
  380.                 grid[curY][curX + i - 1].back.setColor(backCol)
  381.             end
  382.         end
  383.         curX = curX + #text
  384.         wrt(text)
  385.     end
  386. }
  387.  
  388. sleep(0.01)
  389. glasses:clear()
  390. glasses:resize(51,19)
  391. glasses:setOutput(newMon)
  392. sleep(0.01)
  393. term.redirect(glasses:getRedirect())
  394. sleep(0.01)
  395. local oldRaw = os.pullEventRaw
  396. os.pullEventRaw = function()
  397.     if glasses:getDrawFlag() then
  398.         glasses:draw()
  399.     end
  400.     return oldRaw()
  401. end
  402. --[[
  403. glasses:clear()
  404. glasses:resize(51,19)
  405. term.redirect(glasses:getRedirect())
  406. ]]--
  407. --[[
  408. glasses:clear()
  409. glasses:resize(51,19)
  410. glasses:setOutput(newMon)
  411. glasses:setCursorPos(1,1)
  412. glasses:write("&")
  413. glasses:setCursorPos(4,4)
  414. glasses:setBackgroundColor(colors.red)
  415. glasses:setTextColor(colors.blue)
  416. glasses:write("+")
  417. glasses:draw()
  418. --{"scp","scb","stc","sbc","wrt"}
  419. ]]--
  420. --[==[
  421. for i = 1,#colors do
  422. grid[1][i].back.setColor(colors[i])
  423. grid[1][i].back.setAlpha(1)
  424. end
  425. --[[
  426. for y = 1,termY do
  427.     for x = 1,termX do
  428.         print("BACK : ",grid[y][x].back.getZIndex()," TEXT : ",grid[y][x].text.getZIndex())
  429.         sleep(0.5)
  430.     end
  431. end
  432. ]]--
  433. print(#colors)
  434. os.pullEvent()
  435. ]==]--
  436. --[[
  437. for i = 1,0xff do
  438.     grid[8][25].back.setColor(i)
  439.     sleep(0.1)
  440. end
  441. ]]--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement