Advertisement
LDDestroier

Lyqyd's Framebuffer API modified

Dec 24th, 2016
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.66 KB | None | 0 0
  1. --[[
  2.     This is the Framebuffer API made by Lyqyd, but I modified it with a few, non-standard term functions.
  3.     pastebin get sMVQZJcx framebuffer
  4. --]]
  5.  
  6. --[[
  7. The MIT License (MIT)
  8.  
  9. Copyright (c) 2013 Lyqyd
  10.  
  11. Permission is hereby granted, free of charge, to any person obtaining a copy
  12. of this software and associated documentation files (the "Software"), to deal
  13. in the Software without restriction, including without limitation the rights
  14. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. copies of the Software, and to permit persons to whom the Software is
  16. furnished to do so, subject to the following conditions:
  17.  
  18. The above copyright notice and this permission notice shall be included in
  19. all copies or substantial portions of the Software.
  20.  
  21. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. THE SOFTWARE.
  28. --]]
  29.  
  30. function new(_sizeX, _sizeY, _color, _xOffset, _yOffset)
  31.     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, xOffset = _xOffset or 0, yOffset = _yOffset or 0}}
  32.     local function doWrite(text, textColor, backColor)
  33.         local pos = redirect.buffer.cursorX
  34.         if redirect.buffer.cursorY > redirect.buffer.sizeY or redirect.buffer.cursorY < 1 then
  35.             redirect.buffer.cursorX = pos + #text
  36.             return
  37.         end
  38.         local writeText, writeTC, writeBC
  39.         if pos + #text <= 1 then
  40.             --skip entirely.
  41.             redirect.buffer.cursorX = pos + #text
  42.             return
  43.         elseif pos < 1 then
  44.             --adjust text to fit on screen starting at one.
  45.             local len = math.abs(redirect.buffer.cursorX) + 2
  46.             writeText = string.sub(text, len)
  47.             writeTC = textColor and string.sub(textColor, len) or nil
  48.             writeBC = textColor and string.sub(backColor, len) or nil
  49.             redirect.buffer.cursorX = 1
  50.         elseif pos > redirect.buffer.sizeX then
  51.             --if we're off the edge to the right, skip entirely.
  52.             redirect.buffer.cursorX = pos + #text
  53.             return
  54.         else
  55.             writeText = text
  56.             writeTC = textColor or nil
  57.             writeBC = backColor or nil
  58.         end
  59.         local lineText = redirect.buffer.text[redirect.buffer.cursorY]
  60.         local lineColor = redirect.buffer.textColor[redirect.buffer.cursorY]
  61.         local lineBack = redirect.buffer.backColor[redirect.buffer.cursorY]
  62.         local preStop = redirect.buffer.cursorX - 1
  63.         local preStart = math.min(1, preStop)
  64.         local postStart = redirect.buffer.cursorX + string.len(writeText)
  65.         local postStop = redirect.buffer.sizeX
  66.         redirect.buffer.text[redirect.buffer.cursorY] = string.sub(lineText, preStart, preStop)..writeText..string.sub(lineText, postStart, postStop)
  67.         if textColor then redirect.buffer.textColor[redirect.buffer.cursorY] = string.sub(lineColor, preStart, preStop)..writeTC..string.sub(lineColor, postStart, postStop) end
  68.         if backColor then redirect.buffer.backColor[redirect.buffer.cursorY] = string.sub(lineBack, preStart, preStop)..writeBC..string.sub(lineBack, postStart, postStop) end
  69.         redirect.buffer.cursorX = pos + string.len(text)
  70.     end
  71.     redirect.write = function(text)
  72.         local text = tostring(text)
  73.         doWrite(text, string.rep(redirect.buffer.curTextColor, #text), string.rep(redirect.buffer.curBackColor, #text))
  74.     end
  75.     redirect.blit = function(text, textColor, backColor)
  76.         if type(text) ~= "string" or type(textColor) ~= "string" or type(backColor) ~= "string" then
  77.             error("Expected string, string, string", 2)
  78.         end
  79.         if #textColor ~= #text or #backColor ~= #text then
  80.             error("Arguments must be the same length", 2)
  81.         end
  82.         doWrite(text, textColor, backColor)
  83.     end
  84.     redirect.clear = function()
  85.         for i=1, redirect.buffer.sizeY do
  86.             redirect.buffer.text[i] = string.rep(" ", redirect.buffer.sizeX)
  87.             redirect.buffer.textColor[i] = string.rep(redirect.buffer.curTextColor, redirect.buffer.sizeX)
  88.             redirect.buffer.backColor[i] = string.rep(redirect.buffer.curBackColor, redirect.buffer.sizeX)
  89.         end
  90.     end
  91.     redirect.clearLine = function()
  92.         redirect.buffer.text[redirect.buffer.cursorY] = string.rep(" ", redirect.buffer.sizeX)
  93.         redirect.buffer.textColor[redirect.buffer.cursorY] = string.rep(redirect.buffer.curTextColor, redirect.buffer.sizeX)
  94.         redirect.buffer.backColor[redirect.buffer.cursorY] = string.rep(redirect.buffer.curBackColor, redirect.buffer.sizeX)
  95.     end
  96.     redirect.getCursorPos = function()
  97.         return redirect.buffer.cursorX, redirect.buffer.cursorY
  98.     end
  99.     redirect.setCursorPos = function(x, y)
  100.         redirect.buffer.cursorX = math.floor(tonumber(x) or redirect.buffer.cursorX)
  101.         redirect.buffer.cursorY = math.floor(tonumber(y) or redirect.buffer.cursorY)
  102.     end
  103.     redirect.setCursorBlink = function(b)
  104.         redirect.buffer.cursorBlink = b
  105.     end
  106.     redirect.getSize = function()
  107.         return redirect.buffer.sizeX, redirect.buffer.sizeY
  108.     end
  109.     redirect.scroll = function(n)
  110.         n = tonumber(n) or 1
  111.         if n > 0 then
  112.             for i = 1, redirect.buffer.sizeY - n do
  113.                 if redirect.buffer.text[i + n] then
  114.                     redirect.buffer.text[i] = redirect.buffer.text[i + n]
  115.                     redirect.buffer.textColor[i] = redirect.buffer.textColor[i + n]
  116.                     redirect.buffer.backColor[i] = redirect.buffer.backColor[i + n]
  117.                 end
  118.             end
  119.             for i = redirect.buffer.sizeY, redirect.buffer.sizeY - n + 1, -1 do
  120.                 redirect.buffer.text[i] = string.rep(" ", redirect.buffer.sizeX)
  121.                 redirect.buffer.textColor[i] = string.rep(redirect.buffer.curTextColor, redirect.buffer.sizeX)
  122.                 redirect.buffer.backColor[i] = string.rep(redirect.buffer.curBackColor, redirect.buffer.sizeX)
  123.             end
  124.         elseif n < 0 then
  125.             for i = redirect.buffer.sizeY, math.abs(n) + 1, -1 do
  126.                 if redirect.buffer.text[i + n] then
  127.                     redirect.buffer.text[i] = redirect.buffer.text[i + n]
  128.                     redirect.buffer.textColor[i] = redirect.buffer.textColor[i + n]
  129.                     redirect.buffer.backColor[i] = redirect.buffer.backColor[i + n]
  130.                 end
  131.             end
  132.             for i = 1, math.abs(n) do
  133.                 redirect.buffer.text[i] = string.rep(" ", redirect.buffer.sizeX)
  134.                 redirect.buffer.textColor[i] = string.rep(redirect.buffer.curTextColor, redirect.buffer.sizeX)
  135.                 redirect.buffer.backColor[i] = string.rep(redirect.buffer.curBackColor, redirect.buffer.sizeX)
  136.             end
  137.         end
  138.     end
  139.     redirect.getTextColor = function()
  140.         return 2 ^ tonumber(redirect.buffer.curTextColor, 16)
  141.     end
  142.     redirect.getTextColour = redirect.getTextColor
  143.     redirect.setTextColor = function(clr)
  144.         if clr and clr <= 32768 and clr >= 1 then
  145.             if redirect.buffer.color then
  146.                 redirect.buffer.curTextColor = string.format("%x", math.floor(math.log(clr) / math.log(2)))
  147.             elseif clr == 1 or clr == 32768 then
  148.                 redirect.buffer.curTextColor = string.format("%x", math.floor(math.log(clr) / math.log(2)))
  149.             else
  150.                 return nil, "Colour not supported"
  151.             end
  152.         end
  153.     end
  154.     redirect.setTextColour = redirect.setTextColor
  155.     redirect.getBackgroundColor = function()
  156.         return 2 ^ tonumber(redirect.buffer.curBackColor, 16)
  157.     end
  158.     redirect.getBackgroundColour = redirect.getBackgroundColor
  159.     redirect.setBackgroundColor = function(clr)
  160.         if clr and clr <= 32768 and clr >= 1 then
  161.             if redirect.buffer.color then
  162.                 redirect.buffer.curBackColor = string.format("%x", math.floor(math.log(clr) / math.log(2)))
  163.             elseif clr == 32768 or clr == 1 then
  164.                 redirect.buffer.curBackColor = string.format("%x", math.floor(math.log(clr) / math.log(2)))
  165.             else
  166.                 return nil, "Colour not supported"
  167.             end
  168.         end
  169.     end
  170.     redirect.setBackgroundColour = redirect.setBackgroundColor
  171.     redirect.isColor = function()
  172.         return redirect.buffer.color == true
  173.     end
  174.     redirect.isColour = redirect.isColor
  175.     redirect.render = function(inputBuffer)
  176.         for i = 1, redirect.buffer.sizeY do
  177.             redirect.buffer.text[i] = inputBuffer.text[i]
  178.             redirect.buffer.textColor[i] = inputBuffer.textColor[i]
  179.             redirect.buffer.backColor[i] = inputBuffer.backColor[i]
  180.         end
  181.     end
  182.     redirect.twrite = function(text)
  183.         local text = tostring(text)
  184.         doWrite(text, string.rep(redirect.buffer.curTextColor, #text), nil)
  185.     end
  186.     redirect.bwrite = function(text)
  187.         local text = tostring(text)
  188.         doWrite(text, nil, string.rep(redirect.buffer.curBackColor, #text))
  189.     end
  190.     redirect.getCharData = function(_x,_y)
  191.         local x,y = tonumber(_x) or redirect.cursorX, tonumber(_y) or redirect.cursorY
  192.         local output = {}
  193.         output.text = redirect.buffer.text[y]:sub(x,x)
  194.         output.textColor = redirect.buffer.text[y]:sub(x,x)
  195.         output.backColor = redirect.buffer.text[y]:sub(x,x)
  196.         return output
  197.     end
  198.     redirect.setBounds = function(x_min, y_min, x_max, y_max)
  199.         redirect.buffer.minX = x_min
  200.         redirect.buffer.maxX = x_max
  201.         redirect.buffer.minY = y_min
  202.         redirect.buffer.maxY = y_max
  203.     end
  204.     redirect.setBounds(1, 1, redirect.buffer.sizeX, redirect.buffer.sizeY)
  205.     redirect.clear()
  206.     return redirect
  207. end
  208.  
  209. function draw(buffer, current)
  210.     for i = buffer.minY, buffer.maxY do
  211.         term.setCursorPos(buffer.minX + buffer.xOffset, i + buffer.yOffset)
  212.         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
  213.             if term.blit then
  214.                 term.blit(buffer.text[i], buffer.textColor[i], buffer.backColor[i])
  215.             else
  216.                 local lineEnd = false
  217.                 local offset = buffer.minX
  218.                 while not lineEnd do
  219.                     local limit = buffer.maxX - offset + 1
  220.                     local textColorString = string.match(string.sub(buffer.textColor[i], offset), string.sub(buffer.textColor[i], offset, offset).."*")
  221.                     local backColorString = string.match(string.sub(buffer.backColor[i], offset), string.sub(buffer.backColor[i], offset, offset).."*")
  222.                     term.setTextColor(2 ^ tonumber(string.sub(textColorString, 1, 1), 16))
  223.                     term.setBackgroundColor(2 ^ tonumber(string.sub(backColorString, 1, 1), 16))
  224.                     term.write(string.sub(buffer.text[i], offset, offset + math.min(#textColorString, #backColorString, limit) - 1))
  225.                     offset = offset + math.min(#textColorString, #backColorString, limit)
  226.                     if offset > buffer.maxX then lineEnd = true end
  227.                 end
  228.             end
  229.             if current then
  230.                 current.text[i] = buffer.text[i]
  231.                 current.textColor[i] = buffer.textColor[i]
  232.                 current.backColor[i] = buffer.backColor[i]
  233.             end
  234.         end
  235.     end
  236.     term.setCursorPos(buffer.cursorX + buffer.xOffset, buffer.cursorY + buffer.yOffset)
  237.     term.setTextColor(2 ^ tonumber(buffer.curTextColor, 16))
  238.     term.setBackgroundColor(2 ^ tonumber(buffer.curBackColor, 16))
  239.     term.setCursorBlink(buffer.cursorBlink)
  240.     return current
  241. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement