Advertisement
BigSHinyToys

Term Redirect to table

May 14th, 2013
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.10 KB | None | 0 0
  1. --[[
  2.         terminal redirect
  3.         by BigShinyToys
  4. ]]--
  5.  
  6. local function class(tInput)
  7.     local tClass = tInput
  8.    
  9.     local mt = {
  10.         __index = tClass
  11.     }
  12.        
  13.     tClass.new = function(o)
  14.         local tObj = o or {}
  15.         setmetatable(tObj,mt)
  16.         return tObj
  17.     end
  18.    
  19.     return tClass
  20. end
  21.  
  22. local screen = class {
  23.     screen = {},
  24.     realX = 30,
  25.     realY = 10,
  26.     posX = 1,
  27.     posY = 1,
  28.     isBlink = false,
  29.     screenTX = colors.white,
  30.     screenBC = colors.black,
  31.    
  32.     scp = term.native.setCursorPos,
  33.     scb = term.native.setCursorBlink,
  34.     stc = term.native.setTextColor,
  35.     sbc = term.native.setBackgroundColor,
  36.     wrt = term.native.write,
  37.    
  38.     drawTime,
  39.     speed = 0.01,
  40.     offX = 0,
  41.     offY = 0,
  42.     triger = function(s)
  43.         if not s.drawTime then
  44.             s.drawTime = os.startTimer(s.speed)
  45.         end
  46.     end,
  47.     clearDrawTimer = function(s)
  48.         s.drawTime = false
  49.     end,
  50.     getDrawTimer = function(s)
  51.         return s.drawTime
  52.     end,
  53.     resize = function(s,width,hight) -- probble could be cleaner
  54.         local difX = s.realX - width
  55.         local difY = s.realY - hight
  56.         if difY ~= 0 then
  57.             if difY < 0 then
  58.                 for y = s.realY,hight do
  59.                     s.screen[y] = {}
  60.                     for x = 1,width do
  61.                         s.screen[y][x] = {s.screenBC,s.screenTX," "}
  62.                     end
  63.                 end
  64.             else
  65.                 for y = s.realY + difY,s.realY do
  66.                     s.screen[y] = nil
  67.                 end
  68.             end
  69.             if difX ~= 0 then
  70.                 if difX < 0 then
  71.                     for y = 1,hight do
  72.                         for x = s.realY,width do
  73.                             if not s.screen[y][x] then
  74.                                 s.screen[y][x] = {s.screenBC,s.screenTX," "}
  75.                             end
  76.                         end
  77.                     end
  78.                 else
  79.                     for y = 1,hight do
  80.                         for x = s.realY + difX,s.realY do
  81.                             s.screen[y][x] = nil
  82.                         end
  83.                     end
  84.                 end
  85.             end
  86.         end
  87.         s.realX = width
  88.         s.realY = hight
  89.     end,
  90.     setPos = function(s,x,y)
  91.         s.offX = x
  92.         s.offY = y
  93.     end,
  94.    
  95.     newLine = function(s)
  96.         local tab = {}
  97.         for x = 1,s.realX do
  98.             tab[x] = {s.screenBC,s.screenTX," "}
  99.         end
  100.         return tab
  101.     end,
  102.    
  103.     slowDraw = function(s)
  104.         s.drawTime = false
  105.         s.scb(false)
  106.         for y = 1,s.realY do
  107.             s.scp(1 + s.offX,y + s.offY)
  108.             for x = 1,s.realX do
  109.                 s.sbc(s.screen[y][x][1])
  110.                 s.stc(s.screen[y][x][2])
  111.                 s.wrt(s.screen[y][x][3])
  112.             end
  113.         end
  114.         if s.isBlink then
  115.             s.scp(s.posX + s.offX,s.posY + s.offY)
  116.             s.scb(s.isBlink)
  117.         else
  118.             s.scb(s.isBlink)
  119.         end
  120.     end,
  121.    
  122.     draw = function(s) -- faster
  123.         s.drawTime = false
  124.         s.scb(false)
  125.         for y = 1,s.realY do
  126.             s.scp(1 + s.offX,y + s.offY)
  127.             local lastTC
  128.             local lastBC
  129.             local TX = {}
  130.             for x = 1,s.realX do
  131.                 local tempBC = s.screen[y][x][1]
  132.                 local tempTC = s.screen[y][x][2]
  133.                 if lastTC == tempTC and lastBC == tempBC then
  134.                     TX[#TX + 1] = s.screen[y][x][3]
  135.                 elseif lastBC then
  136.                     s.sbc(lastBC or tempBC)
  137.                     s.stc(lastTC or tempTC)
  138.                     s.wrt(table.concat(TX,""))
  139.                     TX = {}
  140.                     lastTC = tempTC
  141.                     lastBC = tempBC
  142.                     TX[#TX + 1] = s.screen[y][x][3]
  143.                 else
  144.                     lastTC = tempTC
  145.                     lastBC = tempBC
  146.                     TX[#TX + 1] = s.screen[y][x][3]
  147.                 end
  148.             end
  149.             s.sbc(lastBC)
  150.             s.stc(lastTC)
  151.             s.wrt(table.concat(TX,""))
  152.         end
  153.         if s.isBlink then
  154.             s.scp(s.posX + s.offX,s.posY + s.offY)
  155.             s.scb(s.isBlink)
  156.         else
  157.             s.scb(s.isBlink)
  158.         end
  159.     end,
  160.    
  161.     isColor = function(s)
  162.         return true
  163.     end,
  164.     isColour = function(s)
  165.         return true
  166.     end,
  167.    
  168.     setTextColor = function(s,color)
  169.         s.screenTX = color
  170.     end,
  171.     setTextColour = function(s,color)
  172.         s.screenTX = color
  173.     end,
  174.    
  175.     setBackgroundColor = function(s,color)
  176.         s.screenBC = color
  177.     end,
  178.     setBackgroundColour = function(s,color)
  179.         s.screenBC = color
  180.     end,
  181.    
  182.     getSize = function(s)
  183.         return s.realX,s.realY
  184.     end,
  185.     clearLine = function(s)
  186.         s.screen[s.posY] = s:newLine()
  187.         s:triger()
  188.     end,
  189.     setCursorBlink = function(s,bool)
  190.         s.isBlink = bool
  191.         s:triger()
  192.     end,
  193.     getCursorPos = function(s)
  194.         return s.posX,s.posY
  195.     end,
  196.     clear = function(s)
  197.         s.screen = {}
  198.         for y = 1,s.realY do
  199.             s.screen[y] = {}
  200.             for x = 1,s.realX do
  201.                 s.screen[y][x] = {s.screenBC,s.screenTX," "}
  202.             end
  203.         end
  204.         s:triger()
  205.     end,
  206.     scroll = function(s,n)
  207.         for i = 1,n do
  208.             table.remove(s.screen,1)
  209.             table.insert(s.screen,s:newLine())
  210.         end
  211.         s:triger()
  212.     end,
  213.     setCursorPos = function(s,x,y)
  214.         s.posX = math.floor(x)
  215.         s.posY = math.floor(y)
  216.         s:triger()
  217.     end,
  218.     write = function(s,line)
  219.         if line and type(line) == "string" then
  220.             for i = 0,#line - 1 do
  221.                 if s.screen[s.posY] and s.screen[s.posY][s.posX + i] then
  222.                     s.screen[s.posY][s.posX + i] = {s.screenBC,s.screenTX,string.sub(line,i + 1,i + 1)}
  223.                 end
  224.             end
  225.             s.posX = s.posX + #line
  226.         end
  227.         s:triger()
  228.     end,
  229.     getRedirect = function(s)
  230.         local list = {
  231.             "isColor","isColour","setTextColor","setTextColour","setBackgroundColor","setBackgroundColour","getSize",
  232.             "clearLine","setCursorBlink","getCursorPos","clear","scroll","setCursorPos","write",
  233.         }
  234.         local fakeTerm = {}
  235.         local screen = s
  236.         for i = 1,#list do
  237.             fakeTerm[list[i]] = function(...)
  238.                 return screen[list[i]](screen,...)
  239.             end
  240.         end
  241.         return fakeTerm
  242.     end,
  243.    
  244. }
  245.  
  246.  
  247. --[[
  248. local myScreen = screen.new()
  249. myScreen:clear() -- required to inishilize grid
  250. myScreen:resize(40,18)
  251. myScreen:resize(30,10)
  252. myScreen:setPos(20,0)
  253. --[=[ test
  254. myScreen:write("Hello World!")
  255. myScreen:draw()
  256. ]=]--
  257.  
  258. local fTerm = myScreen:getRedirect()
  259. term.redirect(fTerm)
  260.  
  261. os.pullEventRaw = function(...)
  262.     while true do
  263.         local event = {coroutine.yield(...)}
  264.         if event[1] == "timer" and event[2] == myScreen:getDrawTimer() then
  265.         --if event[1] == "timer" then
  266.             myScreen:draw()
  267.         else
  268.             return unpack(event)
  269.         end
  270.     end
  271. end
  272. ]]--
  273.  
  274.  
  275.  
  276.  
  277. local tTask = {}
  278.  
  279. local newTask = function(path,x,y)
  280.     if fs.exists(path) then
  281.         local sc = screen.new()
  282.         sc:clear()
  283.         sc:resize(25,19)
  284.         sc:setPos(x,y)
  285.         local prog = coroutine.create(loadfile(path))
  286.         table.insert(tTask,{co = prog,sc = sc,path = path,fTerm = sc:getRedirect()})
  287.     end
  288. end
  289.  
  290. --[[ nothing to see here move along
  291. newTask("rom/user/NEW/CIA login.lua",-10,-10)
  292. newTask("rom/user/NEW/CIA login.lua",15,-10)
  293. newTask("rom/user/NEW/CIA login.lua",-10,10)
  294. newTask("rom/user/NEW/CIA login.lua",15,10)
  295. newTask("rom/user/NEW/CIA login.lua",40,-10)
  296. newTask("rom/user/NEW/CIA login.lua",40,10)
  297. ]]--
  298.  
  299. newTask("rom/programs/secret/alongtimeago",-10,-10)
  300. newTask("rom/programs/secret/alongtimeago",15,-10)
  301. newTask("rom/programs/secret/alongtimeago",-10,10)
  302. newTask("rom/programs/secret/alongtimeago",15,10)
  303. newTask("rom/programs/secret/alongtimeago",40,-10)
  304. newTask("rom/programs/secret/alongtimeago",40,10)
  305.  
  306. while true do
  307.     local event = {coroutine.yield()} -- add code to correctly divide up events like click
  308.     if event[1] == "timer" then
  309.         for i = 1,#tTask do
  310.             if event[2] == tTask[i].sc:getDrawTimer() then
  311.                 tTask[i].sc:draw()
  312.                 break
  313.             end
  314.         end
  315.         for i = 1,#tTask do
  316.             term.redirect(tTask[i].fTerm)
  317.             coroutine.resume(tTask[i].co,unpack(event))
  318.             term.restore()
  319.         end
  320.     else
  321.         for i = 1,#tTask do
  322.             term.redirect(tTask[i].fTerm)
  323.             coroutine.resume(tTask[i].co,unpack(event))
  324.             term.restore()
  325.             --[[
  326.                 yes i could have been smart and added a "term"
  327.                 to the envirmant but that would not affect print
  328.                 , write and other functions that affect the
  329.                 screen. so I'm using lyqyds method of redirection
  330.             ]]--
  331.         end
  332.     end
  333. end
  334.  
  335.  
  336. --term.redirect(tTask[1].fTerm)
  337. --[[
  338. os.pullEventRaw = function(...)
  339.     while true do
  340.         local event = {coroutine.yield(...)}
  341.         if event[1] == "timer" and event[2] == tTask[1].sc:getDrawTimer() then
  342.             tTask[1].sc:draw()
  343.         else
  344.             return unpack(event)
  345.         end
  346.     end
  347. end
  348. ]]--
  349. --[[
  350. for key,value in pairs(tTask[1].tEnv.term) do
  351.     print(key," ",value)
  352. end
  353. ]]--
  354.  
  355. --[[
  356. while true do
  357.     local event = {coroutine.yield(...)}
  358.     if event[1] == "timer" and event[2] == tTask[1].sc:getDrawTimer() then
  359.         term.redirect(tTask[1].fTerm)
  360.         tTask[1].sc:draw()
  361.         term.redirect()
  362.     else
  363.         coroutine.resume(tTask[1].co,unpack(event))
  364.     end
  365. end
  366. ]]--
  367. --[[
  368. while true do
  369.     local event = {coroutine.yield(...)}
  370.     if event[1] == "timer" then
  371.         for i = 1,#tTask do
  372.             if tTask[i].sc:getDrawTimer() == event[2] then
  373.                 tTask[i].sc:draw()
  374.                 break
  375.             end
  376.         end
  377.     else
  378.         for i = 1,#tTask do
  379.             coroutine.resume(tTask[i].co,unpack(event))
  380.         end
  381.     end
  382. end
  383. ]]--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement