Advertisement
PaymentOption

Frames API

Feb 17th, 2013
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.01 KB | None | 0 0
  1. --[[
  2.     Frames      Trystan Cannon
  3.                 12 February 2013
  4.                
  5.     This is a redesign of an API
  6.     which I had written and didn't
  7.     function properly because of the
  8.     faulty design.
  9.    
  10.     This API implements a method of recording
  11.     the screen in frames. However, saving
  12.     an entire recording isn't practical because
  13.     of how large the file size would be.
  14.    
  15.     A decent use for this API is to take
  16.     screenshots in game.
  17. ]]--
  18.  
  19.  
  20. -- Returns the true size of a table using pairs().
  21. local function getTableSize (myTable)
  22.     local size = 0
  23.    
  24.     for _, __ in pairs (myTable) do
  25.         size = size + 1
  26.     end
  27.    
  28.     return size
  29. end
  30.  
  31. -- Hooks the given screen buffer to the current terminal API.
  32. function hookTerminal (screenBuffer)
  33.     local hookedTerminal = {}
  34.     for index, item in pairs (term.native) do
  35.         hookedTerminal[index] = item
  36.     end
  37.    
  38.    
  39.     -- TODO: Implement this function!
  40.     hookedTerminal["scroll"] = function (linesToScroll)
  41.         local lines = {}
  42.        
  43.         for line = linesToScroll + 1, screenBuffer.screenHeight do
  44.             lines[#lines + 1] = screenBuffer[line]
  45.         end
  46.         -- Fill in any remaining lines with empty space.
  47.         for line = (getTableSize (lines) == 1 and getTableSize (lines) or 1), screenBuffer.screenHeight do
  48.             lines[#lines + 1] = {}
  49.            
  50.             for xPos = 1, screenBuffer.screenWidth do
  51.                 lines[#lines][xPos] = {text = ' ', backgroundColor = colors.black, textColor = colors.white}
  52.             end
  53.         end
  54.        
  55.         -- Insert the lines table as the current screen buffer lines.
  56.         for line = 1, screenBuffer.screenHeight do
  57.             screenBuffer[line] = lines[line]
  58.         end
  59.         return term.native.scroll (linesToScroll)
  60.     end
  61.    
  62.     hookedTerminal["clearLine"] = function()
  63.         local _, line = term.native.getCursorPos()
  64.        
  65.         -- Clear the line by replacing every cell in it with spaces.
  66.         for xPos = 1, screenBuffer.screenWidth do
  67.             screenBuffer[line][xPos] = {text = ' ', backgroundColor = screenBuffer.backgroundColor, textColor = screenBuffer.textColor}
  68.         end
  69.        
  70.         return term.native.clearLine()
  71.     end
  72.    
  73.     hookedTerminal["clear"] = function()
  74.         -- Clear the screen buffer by, instead of dumping it, replace every line with spaces.
  75.         for line = 1, screenBuffer.screenHeight do
  76.             for xPos = 1, screenBuffer.screenWidth do
  77.                 screenBuffer[line][xPos] = {text = ' ', backgroundColor = screenBuffer.backgroundColor, textColor = screenBuffer.textColor}
  78.             end
  79.         end
  80.        
  81.         return term.native.clear()
  82.     end
  83.    
  84.     hookedTerminal["setTextColor"] = function (textColor)
  85.         screenBuffer.textColor = textColor
  86.         return term.native.setTextColor (textColor)
  87.     end
  88.    
  89.     hookedTerminal["setBackgroundColor"] = function (backgroundColor)
  90.         screenBuffer.backgroundColor = backgroundColor
  91.         return term.native.setBackgroundColor (backgroundColor)
  92.     end
  93.    
  94.     hookedTerminal["setCursorBlink"] = function (cursorBlink)
  95.         screenBuffer.cursorBlink = cursorBlink
  96.         return term.native.setCursorBlink (cursorBlink)
  97.     end
  98.    
  99.     hookedTerminal["write"] = function (text)
  100.         local currentX, currentY = term.native.getCursorPos()
  101.         local currentX_offset    = currentX
  102.         local backgroundColor    = screenBuffer.backgroundColor
  103.         local textColor          = screenBuffer.textColor
  104.        
  105.         local subOffset = 1
  106.         while currentX < currentX_offset + text:len() do
  107.             screenBuffer[currentY][currentX] = {text = text:sub (subOffset, subOffset), backgroundColor = backgroundColor, textColor = textColor}
  108.            
  109.             subOffset = subOffset + 1
  110.             currentX  = currentX  + 1
  111.         end
  112.        
  113.         return term.native.write (text)
  114.     end
  115.    
  116.     term.redirect (hookedTerminal)
  117. end
  118.  
  119. -- Initializes the frame environment.
  120. function initFrameEnvironment()
  121.     local screenWidth, screenHeight = term.getSize()
  122.     _G.frameEnvironment              = {}
  123.    
  124.    
  125.     _G.frameEnvironment.screenBuffer = {
  126.         cursorBlink     = true,
  127.         backgroundColor = colors.black,
  128.         textColor       = colors.white,
  129.         screenWidth     = screenWidth,
  130.         screenHeight    = screenHeight
  131.     }
  132.     -- Initialize the screen buffer by adding all of the cell tables.
  133.     for line = 1, _G.frameEnvironment.screenBuffer.screenHeight do
  134.         _G.frameEnvironment.screenBuffer[line] = {}
  135.        
  136.         for xPos = 1, _G.frameEnvironment.screenBuffer.screenWidth do
  137.             _G.frameEnvironment.screenBuffer[line][xPos] = {text = ' ', backgroundColor = _G.frameEnvironment.screenBuffer.backgroundColor, textColor = _G.frameEnvironment.screenBuffer.textColor}
  138.         end
  139.     end
  140.    
  141.    
  142.     -- Returns a copy of the screen buffer and the current os.clock() time. This is a frame.
  143.     -- NOTE: A frame object always has a 'cursorBlink' field too!
  144.     _G.frameEnvironment.getCurrentFrame = function()
  145.         local currentX, currentY = term.native.getCursorPos()
  146.        
  147.         local currentFrame = {
  148.             cursorBlink = _G.frameEnvironment.screenBuffer.cursorBlink,
  149.             cursorPos   = {x = currentX, y = currentY}
  150.         }
  151.        
  152.         for line = 1, _G.frameEnvironment.screenBuffer.screenHeight do
  153.             currentFrame[line] = {}
  154.            
  155.             for xPos = 1, _G.frameEnvironment.screenBuffer.screenWidth do
  156.                 currentFrame[line][xPos] = {}
  157.                
  158.                 for index, item in pairs (_G.frameEnvironment.screenBuffer[line][xPos]) do
  159.                     currentFrame[line][xPos][index] = item
  160.                 end
  161.             end
  162.         end
  163.        
  164.         return currentFrame, os.clock()
  165.     end
  166.    
  167.     -- Draws the given frame.
  168.     -- NOTE: This function does not return the cursor position, blink, or screen colors to their original state!
  169.     _G.frameEnvironment.drawFrame = function (frame)
  170.         term.native.setCursorBlink (false)
  171.        
  172.         for line = 1, _G.frameEnvironment.screenBuffer.screenHeight do
  173.             for xPos = 1, _G.frameEnvironment.screenBuffer.screenWidth do
  174.                 local cell = frame[line][xPos]
  175.                
  176.                 if getTableSize (cell) > 0 then
  177.                     term.native.setCursorPos (xPos, line)
  178.                     term.native.setBackgroundColor (cell.backgroundColor)
  179.                     term.native.setTextColor (cell.textColor)
  180.                     term.write (cell.text)
  181.                 end
  182.             end
  183.         end
  184.        
  185.         term.native.setCursorBlink (frame.cursorBlink)
  186.         term.native.setCursorPos (frame.cursorPos.x, frame.cursorPos.y)
  187.     end
  188.    
  189.    
  190.    
  191.     -- Ensure that the terminal API is writing to the screen buffer by hooking the screen buffer to the terminal API.
  192.     hookTerminal (_G.frameEnvironment.screenBuffer)
  193. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement