Guest User

phpscreenshot lua-part v1

a guest
May 7th, 2013
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.04 KB | None | 0 0
  1. term.clear()
  2. term.setCursorPos(1,1)
  3.  
  4.  
  5. -- Uses Gopher's redirect buffer api v1.2
  6. -- http://www.computercraft.info/forums2/index.php?/topic/5130-gophers-apis-new-ggui-beta-goroutine-redirect-ctrlkeys/page__hl__gopheratl
  7.  
  8. local trueCursor={term.getCursorPos()}
  9.  
  10. local redirectBufferBase = {
  11.     write=
  12.       function(buffer,...)
  13.         local cy=buffer.curY
  14.         if cy>0 and cy<=buffer.height then
  15.           local text=table.concat({...}," ")
  16.           local cx=buffer.curX
  17.           local px, py
  18.           if buffer.isActive and not buffer.cursorBlink then
  19.             term.native.setCursorPos(cx+buffer.scrX, cy+buffer.scrY)
  20.           end
  21.           for i=1,#text do
  22.             if cx<=buffer.width then
  23.               local curCell=buffer[cy][cx]
  24.               local char,textColor,backgroundColor=string.char(text:byte(i)),buffer.textColor,buffer.backgroundColor
  25.               if buffer[cy].isDirty or curCell.char~=char or curCell.textColor~=textColor or curCell.backgroundColor~=backgroundColor then
  26.                 buffer[cy][cx].char=char
  27.                 buffer[cy][cx].textColor=textColor
  28.                 buffer[cy][cx].backgroundColor=backgroundColor
  29.                 buffer[cy].isDirty=true
  30.               end
  31.             end
  32.             cx=cx+1
  33.           end
  34.           buffer.curX=cx
  35.           if buffer.isActive then
  36.             buffer.drawDirty()
  37.             if not buffer.cursorBlink then
  38.               trueCursor={cx+buffer.scrX-1,cy+buffer.scrY-1}
  39.               term.native.setCursorPos(unpack(trueCursor))
  40.             end
  41.           end
  42.         end
  43.       end,
  44.      
  45.     setCursorPos=
  46.       function(buffer,x,y)
  47.         buffer.curX=math.floor(x)
  48.         buffer.curY=math.floor(y)
  49.         if buffer.isActive and buffer.cursorBlink then
  50.           term.native.setCursorPos(x+buffer.scrX-1,y+buffer.scrY-1)
  51.           trueCursor={x+buffer.scrX-1,y+buffer.scrY-1}
  52.         end
  53.       end,
  54.      
  55.     getCursorPos=
  56.       function(buffer)
  57.         return buffer.curX,buffer.curY
  58.       end,
  59.      
  60.     scroll=
  61.       function(buffer,offset)
  62.         for j=1,offset do
  63.           local temp=table.remove(buffer,1)
  64.           table.insert(buffer,temp)
  65.           for i=1,#temp do
  66.             temp[i].char=" "
  67.             temp[i].textColor=buffer.textColor
  68.             temp[i].backgroundColor=buffer.backgroundColor
  69.           end
  70.         end
  71.         if buffer.isActive then
  72.           term.redirect(term.native)
  73.           buffer.blit()
  74.           term.restore()
  75.         end
  76.       end,
  77.      
  78.     isColor=
  79.       function(buffer)
  80.         return buffer._isColor
  81.       end,
  82.      
  83.     isColour=
  84.       function(buffer)
  85.         return buffer._isColor
  86.       end,
  87.      
  88.     clear=
  89.       function(buffer)
  90.         for y=1,buffer.height do
  91.           for x=1,buffer.width do
  92.             buffer[y][x]={char=" ",textColor=buffer.textColor,backgroundColor=buffer.backgroundColor}
  93.           end
  94.         end
  95.         if buffer.isActive then
  96.           term.redirect(term.native)
  97.           buffer.blit()
  98.           term.restore()
  99.         end
  100.       end,
  101.      
  102.     clearLine=
  103.       function(buffer)
  104.         local line=buffer[buffer.curY]
  105.         local fg,bg = buffer.textColor, buffer.backgroundColor
  106.         for x=1,buffer.width do
  107.           line[x]={char=" ",textColor=fg,backgroundColor=bg}
  108.         end
  109.         buffer[buffer.curY].isDirty=true
  110.         if buffer.isActive then
  111.           buffer.drawDirty()
  112.         end
  113.       end,
  114.      
  115.     setCursorBlink=
  116.       function(buffer,onoff)
  117.         buffer.cursorBlink=onoff
  118.         if buffer.isActive then
  119.           term.native.setCursorBlink(onoff)
  120.           if onoff then
  121.             term.native.setCursorPos(buffer.curX,buffer.curY)
  122.             trueCursor={buffer.curX,buffer.curY}
  123.           end
  124.         end
  125.       end,
  126.      
  127.     getSize=
  128.       function(buffer)
  129.         return buffer.width, buffer.height
  130.       end,
  131.      
  132.     setTextColor=
  133.       function(buffer,color)
  134.         buffer.textColor=color
  135.         if buffer.isActive then
  136.           if term.native.isColor() or color==colors.black or color==colors.white then
  137.             term.native.setTextColor(color)
  138.           end
  139.         end
  140.       end,
  141.      
  142.     setTextColour=
  143.       function(buffer,color)
  144.         buffer.textColor=color
  145.         if buffer.isActive then
  146.           if term.native.isColor() or color==colors.black or color==colors.white then
  147.             term.native.setTextColor(color)
  148.           end
  149.         end
  150.       end,
  151.      
  152.     setBackgroundColor=
  153.       function(buffer,color)
  154.         buffer.backgroundColor=color
  155.         if buffer.isActive then
  156.           if term.native.isColor() or color==colors.black or color==colors.white then
  157.         term.native.setBackgroundColor(color)
  158.           end
  159.         end
  160.       end,
  161.      
  162.     setBackgroundColour=
  163.       function(buffer,color)
  164.         buffer.backgroundColor=color
  165.         if buffer.isActive then
  166.           if term.native.isColor() or color==colors.black or color==colors.white then
  167.         term.native.setBackgroundColor(color)
  168.           end
  169.         end
  170.       end,
  171.    
  172.     resize=
  173.       function(buffer,width,height)
  174.         if buffer.width~=width or buffer.height~=height then
  175.           local fg, bg=buffer.textColor, buffer.backgroundColor
  176.           if width>buffer.width then
  177.             for y=1,buffer.height do
  178.               for x=#buffer[y]+1,width do
  179.                 buffer[y][x]={char=" ",textColor=fg,backgroundColor=bg}
  180.               end
  181.             end
  182.           end
  183.  
  184.           if height>buffer.height then
  185.             local w=width>buffer.width and width or buffer.width
  186.             for y=#buffer+1,height do
  187.               local row={}          
  188.               for x=1,width do
  189.                 row[x]={char=" ",textColor=fg,backgroundColor=bg}
  190.               end
  191.               buffer[y]=row
  192.             end
  193.           end
  194.           buffer.width=width
  195.           buffer.height=height
  196.         end
  197.       end,
  198.      
  199.     blit=
  200.       function(buffer,sx,sy,dx, dy, width,height)
  201.         sx=sx or 1
  202.         sy=sy or 1
  203.         dx=dx or buffer.scrX
  204.         dy=dy or buffer.scrY
  205.         width=width or buffer.width
  206.         height=height or buffer.height
  207.        
  208.         local h=sy+height>buffer.height and buffer.height-sy or height-1
  209.         for y=0,h do
  210.           local row=buffer[sy+y]
  211.           local x=0
  212.           local cell=row[sx]
  213.           local fg,bg=cell.textColor,cell.backgroundColor
  214.           local str=""
  215.           local tx=x
  216.           while true do
  217.             str=str..cell.char
  218.             x=x+1
  219.             if x==width or sx+x>buffer.width then
  220.               break
  221.             end
  222.             cell=row[sx+x]
  223.             if cell.textColor~=fg or cell.backgroundColor~=bg then
  224.               --write
  225.               term.setCursorPos(dx+tx,dy+y)
  226.               term.setTextColor(fg)
  227.               term.setBackgroundColor(bg)
  228.               term.write(str)
  229.               str=""
  230.               tx=x
  231.               fg=cell.textColor
  232.               bg=cell.backgroundColor              
  233.             end
  234.           end
  235.           term.setCursorPos(dx+tx,dy+y)
  236.           term.setTextColor(fg)
  237.           term.setBackgroundColor(bg)
  238.           term.write(str)
  239.         end
  240.       end,
  241.      
  242.     drawDirty =
  243.       function(buffer)
  244.         term.redirect(term.native)
  245.         for y=1,buffer.height do
  246.           if buffer[y].isDirty then
  247.             term.redirect(term.native)
  248.             buffer.blit(1,y,buffer.scrX,buffer.scrY+y-1,buffer.width,buffer.height)
  249.             term.restore()
  250.             buffer[y].isDirty=false
  251.           end
  252.         end
  253.         term.restore()
  254.       end,
  255.      
  256.     makeActive =
  257.       function(buffer,posX, posY)
  258.         posX=posX or 1
  259.         posY=posY or 1
  260.         buffer.scrX=posX
  261.         buffer.scrY=posY
  262.         term.redirect(term.native)
  263.         buffer.blit(1,1,posX,posY,buffer.width,buffer.height)
  264.         term.setCursorPos(buffer.curX,buffer.curY)
  265.         term.setCursorBlink(buffer.cursorBlink)
  266.         term.setTextColor(buffer.textColor)
  267.         term.setBackgroundColor(buffer.backgroundColor)
  268.         buffer.isActive=true
  269.         term.restore()
  270.       end,
  271.      
  272.     isBuffer = true,
  273.    
  274.   }
  275.    
  276.  
  277. function createRedirectBuffer(width,height,fg,bg,isColor)
  278.    bg=bg or colors.black
  279.    fg=fg or colors.white
  280.    isColor=isColor~=nil and isColor or term.isColor()
  281.    local buffer={}
  282.    
  283.    do
  284.      local w,h=term.getSize()
  285.      width,height=width or w,height or h
  286.    end
  287.    
  288.    for y=1,height do
  289.      local row={}
  290.      for x=1,width do
  291.        row[x]={char=" ",textColor=fg,backgroundColor=bg}
  292.      end
  293.      buffer[y]=row
  294.    end
  295.    buffer.scrX=1
  296.    buffer.scrY=1
  297.    buffer.width=width
  298.    buffer.height=height
  299.    buffer.cursorBlink=false
  300.    buffer.textColor=fg
  301.    buffer.backgroundColor=bg
  302.    buffer._isColor=isColor
  303.    buffer.curX=1
  304.    buffer.curY=1
  305.    
  306.    local meta={}
  307.    local function wrap(f,o)
  308.      return function(...)
  309.          return f(o,...)
  310.        end
  311.    end
  312.    for k,v in pairs(redirectBufferBase) do
  313.      if type(v)=="function" then
  314.        meta[k]=wrap(v,buffer)
  315.      else
  316.        meta[k]=v
  317.      end
  318.    end
  319.    setmetatable(buffer,{__index=meta})
  320.    return buffer
  321. end
  322.  
  323.  
  324.  
  325. local w,h = term.getSize()
  326. local buff = createRedirectBuffer(w,h)
  327. term.redirect(buff)
  328. buff.makeActive(1,1)
  329.  
  330. function screenshot()
  331.   local text = ""
  332.   for y,row in ipairs(buff) do
  333.     for x,char in ipairs(row) do
  334.       text=text..char.char
  335.     end
  336.   end
  337.   local file = io.open("screen.txt","a")
  338.   file:write("\nhttp://janvanrosmalen.nl/ccrender/?w="..w.."&h="..h.."&t="..textutils.urlEncode(text))
  339.   file:close()
  340. end
  341.  
  342.  
  343. local roll=true
  344. function go()
  345.   print("Press f1 to make a screenshot.")
  346.   print("Type exit to leave screenshot mode")
  347.   shell.run("rom/programs/shell")
  348.   roll=false
  349. end
  350.  
  351. local route = coroutine.create(go)
  352. os.queueEvent("derp")
  353. while roll do
  354.   local evt = {os.pullEventRaw()}
  355.   if (evt[1]=="key") and (evt[2]==keys.f1) then
  356.     screenshot()
  357.   end
  358.   coroutine.resume(route,unpack(evt))
  359. end
  360.  
  361. term.restore()
  362. term.clear()
  363. term.setCursorPos(1,1)
  364. print("Left screenshot mode")
Advertisement
Add Comment
Please, Sign In to add comment