Advertisement
sci4me

CC Buffer

Dec 12th, 2014
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.77 KB | None | 0 0
  1. return {
  2.     new = function(w, h, color)
  3.         local t = {
  4.             x = 1,
  5.             y = 1,
  6.             w = w,
  7.             h = h,
  8.             color = color ~= nil,
  9.             fg = colors.white,
  10.             bg = colors.black,
  11.             buffer = {},
  12.             setPixel = function(self, x, y, char, bg, fg)
  13.                 local col = self.buffer[x]
  14.                 local _y = y * 3 - 2
  15.                 col[_y] = char
  16.                 col[_y + 1] = bg
  17.                 col[_y + 2] = fg
  18.                 return self
  19.             end,
  20.             getPixel = function(self, x, y)
  21.                 local col = self.buffer[x]
  22.                 local _y = y * 3 - 2
  23.                 return {
  24.                     char = col[_y],
  25.                     bg = col[_y + 1],
  26.                     fg = col[_y + 2]
  27.                 }
  28.             end,
  29.             write = function(self, str)
  30.                 for i = 1, string.len(str) do
  31.                     self:setPixel(self.x, self.y, string.sub(str, i, i), self.bg, self.fg)
  32.                     self.x = self.x + 1
  33.                 end
  34.                 return self
  35.             end,
  36.             scroll = function(self, diff)
  37.                 local oldLines = {}
  38.  
  39.                 for i = 1, self.w do
  40.                     if not oldLines[i] then oldLines[i] = {} end
  41.                     for j = 1, self.h do
  42.                         oldLines[i][j] = self:getPixel(i, j)
  43.                     end
  44.                 end
  45.  
  46.                 local newLines = {}
  47.  
  48.                 for i = 1, self.w do
  49.                     local oldY = i + diff
  50.                     if ((oldY >= 1) and (oldY < self.h)) then
  51.                         newLines[i] = oldLines[oldY]
  52.                     else
  53.                         newLines[i] = {}
  54.                         for j = 1, self.h do
  55.                             newLines[j] = { char = " ", bg = colors.black, fg = colors.white }
  56.                         end
  57.                     end
  58.                 end
  59.  
  60.                 for i = 1, self.w do
  61.                     local col = self.buffer[i]
  62.                     for j = 1, self.h do
  63.                         col[j] = newLines[i][j].char
  64.                         col[j + 1] = newLines[i][j].bg
  65.                         col[j + 2] = newLines[i][j].fg
  66.                     end
  67.                 end
  68.                 return self
  69.             end,
  70.             setCursorPos = function(self, x, y)
  71.                 self.x = x
  72.                 self.y = y
  73.                 return self
  74.             end,
  75.             getCursorPos = function(self)
  76.                 return self.x, self.y
  77.             end,
  78.             getSize = function(self)
  79.                 return self.w, self.h
  80.             end,
  81.             isColor = function(self)
  82.                 return self.isColor
  83.             end,
  84.             setTextColor = function(self, c)
  85.                 self.fg = c
  86.                 return self
  87.             end,
  88.             setBackgroundColor = function(self, c)
  89.                 self.bg = c
  90.                 return self
  91.             end,
  92.             getTextColor = function(self)
  93.                 return self.fg
  94.             end,
  95.             getBackgroundColor = function(self)
  96.                 return self.bg
  97.             end,
  98.             clear = function(self)
  99.                 for i = 1, self.w do
  100.                     for j = 1, self.h do
  101.                         self:setPixel(i, j, " ", self.bg, self.fg)
  102.                     end
  103.                 end
  104.                 return self
  105.             end,
  106.             clearLine = function(self)
  107.                 for i = 1, self.w do
  108.                     self:setPixel(i, self.y, " ", self.bg, self.fg)
  109.                 end
  110.             end,
  111.             draw = function(self, target)
  112.                 for i = 1, self.w do
  113.                     for j = 1, self.h do
  114.                         local pixel = self:getPixel(i, j)
  115.                         if type(pixel.fg) == "number" then target.setTextColor(pixel.fg) end
  116.                         if type(pixel.bg) == "number" then target.setBackgroundColor(pixel.bg) end
  117.                         target.setCursorPos(i, j)
  118.                         if pixel.char then target.write(pixel.char) else target.write(" ") end
  119.                     end
  120.                 end
  121.                 target.setTextColor(self.fg)
  122.                 target.setBackgroundColor(self.bg)
  123.                 target.setCursorPos(self.x, self.y)
  124.                 return self
  125.             end
  126.         }
  127.  
  128.         for i = 1, w do
  129.             if not t.buffer[i] then t.buffer[i] = {} end
  130.  
  131.             local col = t.buffer[i]
  132.             for j = 1, h do
  133.                 local y = j * 3 - 2
  134.                 col[y] = " "
  135.                 col[y + 1] = colors.black
  136.                 col[y + 2] = colors.white
  137.             end
  138.         end
  139.  
  140.         return t
  141.     end
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement