Lyqyd

Framebuffer API

Oct 20th, 2013
2,202
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function new(_sizeX, _sizeY, _color)
  2.     local redirect = {buffer = {text = {}, textColor = {}, backColor = {}, cursorX = 1, cursorY = 1, cursorBlink = false, curTextColor = "0", curBackColor = "f", sizeX = _sizeX or 51, sizeY = _sizeY or 19, color = _color}}
  3.     redirect.write = function(text)
  4.         text = tostring(text)
  5.         local pos = redirect.buffer.cursorX
  6.         if redirect.buffer.cursorY > redirect.buffer.sizeY or redirect.buffer.cursorY < 1 then
  7.             redirect.buffer.cursorX = pos + #text
  8.             return
  9.         end
  10.         local writeText
  11.         if pos + #text <= 1 then
  12.             --skip entirely.
  13.             redirect.buffer.cursorX = pos + #text
  14.             return
  15.         elseif pos < 1 then
  16.             --adjust text to fit on screen starting at one.
  17.             writeText = string.sub(text, math.abs(redirect.buffer.cursorX) + 2)
  18.             redirect.buffer.cursorX = 1
  19.         elseif pos > redirect.buffer.sizeX then
  20.             --if we're off the edge to the right, skip entirely.
  21.             redirect.buffer.cursorX = pos + #text
  22.             return
  23.         else
  24.             writeText = text
  25.         end
  26.         local lineText = redirect.buffer.text[redirect.buffer.cursorY]
  27.         local lineColor = redirect.buffer.textColor[redirect.buffer.cursorY]
  28.         local lineBack = redirect.buffer.backColor[redirect.buffer.cursorY]
  29.         local preStop = redirect.buffer.cursorX - 1
  30.         local preStart = math.min(1, preStop)
  31.         local postStart = redirect.buffer.cursorX + string.len(writeText)
  32.         local postStop = redirect.buffer.sizeX
  33.         redirect.buffer.text[redirect.buffer.cursorY] = string.sub(lineText, preStart, preStop)..writeText..string.sub(lineText, postStart, postStop)
  34.         redirect.buffer.textColor[redirect.buffer.cursorY] = string.sub(lineColor, preStart, preStop)..string.rep(redirect.buffer.curTextColor, #writeText)..string.sub(lineColor, postStart, postStop)
  35.         redirect.buffer.backColor[redirect.buffer.cursorY] = string.sub(lineBack, preStart, preStop)..string.rep(redirect.buffer.curBackColor, #writeText)..string.sub(lineBack, postStart, postStop)
  36.         redirect.buffer.cursorX = pos + string.len(text)
  37.     end
  38.     redirect.clear = function()
  39.         for i=1, redirect.buffer.sizeY do
  40.             redirect.buffer.text[i] = string.rep(" ", redirect.buffer.sizeX)
  41.             redirect.buffer.textColor[i] = string.rep(redirect.buffer.curTextColor, redirect.buffer.sizeX)
  42.             redirect.buffer.backColor[i] = string.rep(redirect.buffer.curBackColor, redirect.buffer.sizeX)
  43.         end
  44.     end
  45.     redirect.clearLine = function()
  46.         redirect.buffer.text[redirect.buffer.cursorY] = string.rep(" ", redirect.buffer.sizeX)
  47.         redirect.buffer.textColor[redirect.buffer.cursorY] = string.rep(redirect.buffer.curTextColor, redirect.buffer.sizeX)
  48.         redirect.buffer.backColor[redirect.buffer.cursorY] = string.rep(redirect.buffer.curBackColor, redirect.buffer.sizeX)
  49.     end
  50.     redirect.getCursorPos = function()
  51.         return redirect.buffer.cursorX, redirect.buffer.cursorY
  52.     end
  53.     redirect.setCursorPos = function(x, y)
  54.         redirect.buffer.cursorX = math.floor(tonumber(x)) or redirect.buffer.cursorX
  55.         redirect.buffer.cursorY = math.floor(tonumber(y)) or redirect.buffer.cursorY
  56.     end
  57.     redirect.setCursorBlink = function(b)
  58.         redirect.buffer.cursorBlink = b
  59.     end
  60.     redirect.getSize = function()
  61.         return redirect.buffer.sizeX, redirect.buffer.sizeY
  62.     end
  63.     redirect.scroll = function(n)
  64.         n = tonumber(n) or 1
  65.         if n > 0 then
  66.             for i = 1, redirect.buffer.sizeY - n do
  67.                 if redirect.buffer.text[i + n] then
  68.                     redirect.buffer.text[i] = redirect.buffer.text[i + n]
  69.                     redirect.buffer.textColor[i] = redirect.buffer.textColor[i + n]
  70.                     redirect.buffer.backColor[i] = redirect.buffer.backColor[i + n]
  71.                 end
  72.             end
  73.             for i = redirect.buffer.sizeY, redirect.buffer.sizeY - n + 1, -1 do
  74.                 redirect.buffer.text[i] = string.rep(" ", redirect.buffer.sizeX)
  75.                 redirect.buffer.textColor[i] = string.rep(redirect.buffer.curTextColor, redirect.buffer.sizeX)
  76.                 redirect.buffer.backColor[i] = string.rep(redirect.buffer.curBackColor, redirect.buffer.sizeX)
  77.             end
  78.         elseif n < 0 then
  79.             for i = redirect.buffer.sizeY, math.abs(n) + 1, -1 do
  80.                 if redirect.buffer.text[i + n] then
  81.                     redirect.buffer.text[i] = redirect.buffer.text[i + n]
  82.                     redirect.buffer.textColor[i] = redirect.buffer.textColor[i + n]
  83.                     redirect.buffer.backColor[i] = redirect.buffer.backColor[i + n]
  84.                 end
  85.             end
  86.             for i = 1, math.abs(n) do
  87.                 redirect.buffer.text[i] = string.rep(" ", redirect.buffer.sizeX)
  88.                 redirect.buffer.textColor[i] = string.rep(redirect.buffer.curTextColor, redirect.buffer.sizeX)
  89.                 redirect.buffer.backColor[i] = string.rep(redirect.buffer.curBackColor, redirect.buffer.sizeX)
  90.             end
  91.         end
  92.     end
  93.     redirect.setTextColor = function(clr)
  94.         if clr and clr <= 32768 and clr >= 1 then
  95.             if redirect.buffer.color then
  96.                 redirect.buffer.curTextColor = string.format("%x", math.floor(math.log(clr) / math.log(2)))
  97.             elseif clr == 1 or clr == 32768 then
  98.                 redirect.buffer.curTextColor = string.format("%x", math.floor(math.log(clr) / math.log(2)))
  99.             else
  100.                 return nil, "Colour not supported"
  101.             end
  102.         end
  103.     end
  104.     redirect.setTextColour = redirect.setTextColor
  105.     redirect.setBackgroundColor = function(clr)
  106.         if clr and clr <= 32768 and clr >= 1 then
  107.             if redirect.buffer.color then
  108.                 redirect.buffer.curBackColor = string.format("%x", math.floor(math.log(clr) / math.log(2)))
  109.             elseif clr == 32768 or clr == 1 then
  110.                 redirect.buffer.curBackColor = string.format("%x", math.floor(math.log(clr) / math.log(2)))
  111.             else
  112.                 return nil, "Colour not supported"
  113.             end
  114.         end
  115.     end
  116.     redirect.setBackgroundColour = redirect.setBackgroundColor
  117.     redirect.isColor = function()
  118.         return redirect.buffer.color == true
  119.     end
  120.     redirect.isColour = redirect.isColor
  121.     redirect.render = function(inputBuffer)
  122.         for i = 1, redirect.buffer.sizeY do
  123.             redirect.buffer.text[i] = inputBuffer.text[i]
  124.             redirect.buffer.textColor[i] = inputBuffer.textColor[i]
  125.             redirect.buffer.backColor[i] = inputBuffer.backColor[i]
  126.         end
  127.     end
  128.     redirect.clear()
  129.     return redirect
  130. end
  131.  
  132. function draw(buffer, current)
  133.     for i=1, buffer.sizeY do
  134.         term.setCursorPos(1,i)
  135.         if (current and (buffer.text[i] ~= current.text[i] or buffer.textColor[i] ~= current.textColor[i] or buffer.backColor[i] ~= current.backColor[i])) or not current then
  136.             local lineEnd = false
  137.             local offset = 1
  138.             while not lineEnd do
  139.                 local textColorString = string.match(string.sub(buffer.textColor[i], offset), string.sub(buffer.textColor[i], offset, offset).."*")
  140.                 local backColorString = string.match(string.sub(buffer.backColor[i], offset), string.sub(buffer.backColor[i], offset, offset).."*")
  141.                 term.setTextColor(2 ^ tonumber(string.sub(textColorString, 1, 1), 16))
  142.                 term.setBackgroundColor(2 ^ tonumber(string.sub(backColorString, 1, 1), 16))
  143.                 term.write(string.sub(buffer.text[i], offset, offset + math.min(#textColorString, #backColorString) - 1))
  144.                 offset = offset + math.min(#textColorString, #backColorString)
  145.                 if offset > buffer.sizeX then lineEnd = true end
  146.             end
  147.             if current then
  148.                 current.text[i] = buffer.text[i]
  149.                 current.textColor[i] = buffer.textColor[i]
  150.                 current.backColor[i] = buffer.backColor[i]
  151.             end
  152.         end
  153.     end
  154.     term.setCursorPos(buffer.cursorX, buffer.cursorY)
  155.     term.setTextColor(2 ^ tonumber(buffer.curTextColor, 16))
  156.     term.setBackgroundColor(2 ^ tonumber(buffer.curBackColor, 16))
  157.     term.setCursorBlink(buffer.cursorBlink)
  158.     return current
  159. end
RAW Paste Data