Advertisement
PaymentOption

Updated QuickBuffer

Sep 8th, 2015
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.71 KB | None | 0 0
  1. ---------------------------------------------------------------------
  2. local tFrameBufferMetatable = { __index = getfenv() }
  3. ---------------------------------------------------------------------
  4. function getTextColor(self)
  5.     return 2 ^ tonumber(self.sTextColor, 16)
  6. end
  7. getTextColour = getTextColor
  8. ---------------------------------------------------------------------
  9. function getBackgroundColor(self)
  10.     return 2 ^ tonumber(self.sBackColor, 16)
  11. end
  12. getBackgroundColour = getBackgroundColor
  13. ---------------------------------------------------------------------
  14. function getSize (self)
  15.     return self.nWidth, self.nHeight, self
  16. end
  17. ---------------------------------------------------------------------
  18. function getCursorPos (self)
  19.     return self.nCursorX, self.nCursorY, self
  20. end
  21. ---------------------------------------------------------------------
  22. function setCursorPos (self, x, y)
  23.     self.nCursorX = math.floor (x) or self.nCursorX
  24.     self.nCursorY = math.floor (y) or self.nCursorY
  25.  
  26.     return self
  27. end
  28. ---------------------------------------------------------------------
  29. function isColor (self)
  30.     return self.tTerm.isColor(), self
  31. end
  32. ---------------------------------------------------------------------
  33. function setTextColor (self, nTextColor)
  34.     self.sTextColor = string.format ("%x", math.log (nTextColor) / math.log (2)) or self.sTextColor
  35.     return self
  36. end
  37. ---------------------------------------------------------------------
  38. function setBackgroundColor (self, nBackColor)
  39.     self.sBackColor = string.format ("%x", math.log (nBackColor) / math.log (2)) or self.sBackColor
  40.     return self
  41. end
  42. ---------------------------------------------------------------------
  43. function setCursorBlink (self, bCursorBlink)
  44.     self.bCursorBlink = bCursorBlink
  45.     return self
  46. end
  47. ---------------------------------------------------------------------
  48. isColour            = isColor
  49. setTextColour       = setTextColor
  50. setBackgroundColour = setBackgroundColor
  51. ---------------------------------------------------------------------
  52. function clearLine (self, nLineNumber)
  53.     nLineNumber = nLineNumber or self.nCursorY
  54.  
  55.     if nLineNumber >= 1 and nLineNumber <= self.nHeight then
  56.         self.tText[nLineNumber]       = (" "):rep (self.nWidth)
  57.         self.tTextColors[nLineNumber] = self.sTextColor:rep (self.nWidth)
  58.         self.tBackColors[nLineNumber] = self.sBackColor:rep (self.nWidth)
  59.     end
  60. end
  61. ---------------------------------------------------------------------
  62. function clear (self)
  63.     for nLineNumber = 1, self.nHeight do
  64.         self:clearLine (nLineNumber)
  65.     end
  66.    
  67.     if not self.tRedirectthen then
  68.         self.tRedirect = {}
  69.  
  70.         for sFunctionName, _ in pairs (self.tTerm) do
  71.             self.tRedirect[sFunctionName] = function (...)
  72.                 return self[sFunctionName] (self, ...)
  73.             end
  74.         end
  75.     end
  76.  
  77.     return self
  78. end
  79. ---------------------------------------------------------------------
  80. function scroll (self, nTimesToScroll)
  81.     for nTimesScrolled = 1, math.abs (nTimesToScroll) do
  82.         if nTimesToScroll > 0 then
  83.             for nLineNumber = 1, self.nHeight do
  84.                 self.tText[nLineNumber]       = self.tText[nLineNumber + 1] or string.rep (" ", self.nWidth)
  85.                 self.tTextColors[nLineNumber] = self.tTextColors[nLineNumber + 1] or string.rep (self.sTextColor, self.nWidth)
  86.                 self.tBackColors[nLineNumber] = self.tBackColors[nLineNumber + 1] or string.rep (self.sBackColor, self.nWidth)
  87.             end
  88.         else
  89.             for nLineNumber = self.nHeight, 1, -1 do
  90.                 self.tText[nLineNumber]       = self.tText[nLineNumber - 1] or string.rep (" ", self.nWidth)
  91.                 self.tTextColors[nLineNumber] = self.tTextColors[nLineNumber - 1] or string.rep (self.sTextColor, self.nWidth)
  92.                 self.tBackColors[nLineNumber] = self.tBackColors[nLineNumber - 1] or string.rep (self.sBackColor, self.nWidth)
  93.             end
  94.         end
  95.     end
  96. end
  97. ---------------------------------------------------------------------
  98. function write (self, sText)
  99.     return self:blit(sText, string.rep(self.sTextColor, sText:len()), string.rep(self.sBackColor, sText:len()))
  100. end
  101. ---------------------------------------------------------------------
  102. function blit(self, sText, sTextColors, sBackColors)
  103.     local nCursorX, nCursorY = self.nCursorX, self.nCursorY
  104.    
  105.     if nCursorY >= 1 and nCursorY <= self.nHeight then
  106.         sText = tostring(sText):gsub("\t", " "):gsub("%c", "?")
  107.        
  108.         local nStartX, nStopX = nCursorX, nCursorX + sText:len()
  109.        
  110.         if nCursorX + sText:len() < 1 or nCursorX > self.nWidth then
  111.             self.nCursorX = self.nCursorX + sText:len()
  112.             return self
  113.         end
  114.        
  115.         if nCursorX + sText:len() > self.nWidth + 1 then
  116.             sText = sText:sub(1, self.nWidth - nCursorX + 1)
  117.             sTextColors = sTextColors:sub(1, self.nWidth - nCursorX + 1)
  118.             sBackColors = sBackColors:sub(1, self.nWidth - nCursorX + 1)
  119.            
  120.             nStopX = self.nWidth
  121.         end
  122.         if nCursorX < 1 then
  123.             sText = sText:sub(2 + math.abs(nCursorX))
  124.             sTextColors = sTextColors:sub(2 + math.abs(nCursorX))
  125.             sBackColors = sBackColors:sub(2 + math.abs(nCursorX))
  126.            
  127.             nStartX = 1
  128.         end
  129.        
  130.         local sTextLine = self.tText[nCursorY]
  131.         local sTextColorLine = self.tTextColors[nCursorY]
  132.         local sBackColorLine = self.tBackColors[nCursorY]
  133.        
  134.         sTextLine = sTextLine:sub(1, nStartX - 1) .. sText .. sTextLine:sub(nStopX + 1)
  135.         sTextColorLine = sTextColorLine:sub(1, nStartX - 1) .. sTextColors .. sTextColorLine:sub(nStopX + 1)
  136.         sBackColorLine = sBackColorLine:sub(1, nStartX - 1) .. sBackColors .. sBackColorLine:sub(nStopX + 1)
  137.        
  138.         self.tText[nCursorY] = sTextLine
  139.         self.tTextColors[nCursorY] = sTextColorLine
  140.         self.tBackColors[nCursorY] = sBackColorLine
  141.     end
  142.    
  143.     self.nCursorX = self.nCursorX + sText:len()
  144. end
  145. ---------------------------------------------------------------------
  146. function render(self)
  147.     local redirect     = term.redirect
  148.     local tCurrentTerm = redirect(self.tTerm)
  149.  
  150.     local blit         = term.blit
  151.     local setCursorPos = term.setCursorPos
  152.  
  153.     local tText       = self.tText
  154.     local tTextColors = self.tTextColors
  155.     local tBackColors = self.tBackColors
  156.  
  157.     local x = self.x
  158.     local y = self.y
  159.  
  160.     for nLine = 1, self.nHeight do
  161.         setCursorPos(x, nLine + y - 1)
  162.         blit(tText[nLine], tTextColors[nLine], tBackColors[nLine])
  163.     end
  164.  
  165.     local tonumber = tonumber
  166.  
  167.     term.setTextColor(2 ^ tonumber(self.sTextColor, 16))
  168.     term.setBackgroundColor(2 ^ tonumber(self.sBackColor, 16))
  169.     term.setCursorBlink(self.bCursorBlink)
  170.  
  171.     setCursorPos(self.nCursorX + x - 1, self.nCursorY + y - 1)
  172.     redirect(tCurrentTerm)
  173.  
  174.     return self, false
  175. end
  176. ---------------------------------------------------------------------
  177. function new (nWidth, nHeight, x, y, tCurrentTerm)
  178.     return setmetatable (
  179.         {
  180.             nWidth  = nWidth,
  181.             nHeight = nHeight,
  182.  
  183.             tRedirect = false,
  184.  
  185.             x = x,
  186.             y = y,
  187.  
  188.             tTerm = tCurrentTerm or term.current(),
  189.  
  190.             nCursorX = 1,
  191.             nCursorY = 1,
  192.  
  193.             sTextColor = "0",
  194.             sBackColor = "f",
  195.  
  196.             tText       = {},
  197.             tTextColors = {},
  198.             tBackColors = {},
  199.  
  200.             bCursorBlink = false
  201.         }
  202.     , tFrameBufferMetatable):clear()
  203. end
  204. ---------------------------------------------------------------------
  205. function redirect (self)
  206.     return self.tRedirect
  207. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement