Advertisement
HPWebcamAble

[CC][0.1][BETA] Buffer API

Nov 4th, 2015
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.83 KB | None | 0 0
  1. --[[
  2. HPWebcamAble Presents...
  3. Buffer
  4.  
  5. --------------------------------
  6. /!\ Warning /!\
  7. The program is in BETA
  8. It is not officially stable yet!
  9. --------------------------------
  10.  
  11. === Description ====
  12. This is an API that lets you efficiently draw to the screen
  13.  
  14.  
  15. ==== Documentation ====
  16. Coming soon
  17.  
  18.  
  19. ==== Installation and Use ====
  20. Pastebin Code: LVmgReu6
  21.  
  22. pastebin get <code> buffer
  23.  
  24. Then run 'buffer' (Or what you called it)
  25.  
  26.  
  27. ==== Update History ====
  28. Pastebin will always have the most recent version
  29.  
  30. |0.1| <- This program
  31.   First BETA release
  32. ]]
  33.  
  34. local toHex = {
  35.     [ colors.white ] = "0",
  36.     [ colors.orange ] = "1",
  37.     [ colors.magenta ] = "2",
  38.     [ colors.lightBlue ] = "3",
  39.     [ colors.yellow ] = "4",
  40.     [ colors.lime ] = "5",
  41.     [ colors.pink ] = "6",
  42.     [ colors.gray ] = "7",
  43.     [ colors.lightGray ] = "8",
  44.     [ colors.cyan ] = "9",
  45.     [ colors.purple ] = "a",
  46.     [ colors.blue ] = "b",
  47.     [ colors.brown ] = "c",
  48.     [ colors.green ] = "d",
  49.     [ colors.red ] = "e",
  50.     [ colors.black ] = "f",
  51. }
  52.  
  53. local function checkArgs(tCheckArgs,tExpectedArgs) -- This function isn't used. I know ;)
  54.   local err = "expected "..tExpectedArgs[1]
  55.   local got = ", got "..type(tCheckArgs[1])
  56.   local shouldErr = type(tCheckArgs[1]) ~= tExpectedArgs[1]
  57.   for i = 2, #tExpectedArgs do
  58.     err = err..","..tExpectedArgs[i]
  59.     got = got..","..type(tCheckArgs[i])
  60.     shouldErr = (shouldErr or type(tCheckArgs[i]) ~= tExpectedArgs[i])
  61.   end
  62.   if shouldErr then
  63.     error( err..got , 3 )
  64.   end
  65.   return true
  66. end
  67.  
  68.  
  69. function create()
  70.  
  71.   local string_rep = string.rep
  72.   local string_sub = string.sub
  73.  
  74.   local function createLine(backgroundColor)
  75.     local w,h = term.getSize()
  76.     local line = {
  77.       text = string_rep(" " , w),
  78.       backColor = string_rep( toHex[backgroundColor] , w ),
  79.       textColor = string_rep( toHex[colors.white] , w )
  80.     }
  81.    
  82.     return line
  83.   end
  84.  
  85.   local screen = {} -- Stores each line of the computer's screen
  86.   local w,h = term.getSize()
  87.   for i = 1, h do
  88.     screen[i] = createLine(colors.black)
  89.   end
  90.   local buffer -- Stores which lines have been updated and need redrawing
  91.  
  92.   local api = {} -- Returned for the program implementing this API to use
  93.  
  94.   function api.buffer(text)
  95.     if not buffer then buffer = {} end
  96.     local curX,curY = term.getCursorPos()
  97.     local maxX = curX + #text
  98.     local minX = curX - 1
  99.    
  100.     screen[curY].text = screen[curY].text:sub(1,minX)..text..screen[curY].text:sub(maxX)
  101.     screen[curY].textColor = screen[curY].textColor:sub(1,minX)..string_rep( toHex[term.getTextColor()] ,#text)..screen[curY].textColor:sub(maxX)
  102.     screen[curY].backColor = screen[curY].backColor:sub(1,minX)..string_rep( toHex[term.getBackgroundColor()] ,#text)..screen[curY].backColor:sub(maxX)
  103.    
  104.     buffer[curY] = true
  105.     term.setCursorPos(maxX-1,curY)
  106.   end
  107.  
  108.   function api.writeToScreen()
  109.     local startX,startY = term.getCursorPos()
  110.     local w,h = term.getSize()
  111.     if not buffer then return end
  112.     for i = 1, h do
  113.       if buffer[i] then
  114.         term.setCursorPos(1,i)
  115.         local line = screen[i]
  116.         term.blit(line.text,line.textColor,line.backColor)
  117.       end
  118.     end
  119.     buffer = nil
  120.     term.setCursorPos(startX,startY)
  121.   end
  122.  
  123.   function api.clearScreen()
  124.     local w,h = term.getSize()
  125.     local curBack = term.getBackgroundColor()
  126.     if not buffer then buffer = {} end
  127.     for i = 1, h do
  128.       screen[i] = createLine(curBack)
  129.       buffer[i] = true
  130.     end
  131.   end
  132.  
  133.   function api.focreRedraw()
  134.     local startX,startY = term.getCursorPos()
  135.     local w,h = term.getSize()
  136.     for i = 1, h do
  137.       term.setCursorPos(1,i)
  138.       local line = screen[i]
  139.       term.blit(line.text,line.textColor,line.backColor)
  140.     end
  141.     term.setCursorPos(startX,startY)
  142.   end
  143.  
  144.   return api
  145.  
  146. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement