Advertisement
guitarplayer616

Updated WebApi with Graphics Buffer

Jun 28th, 2015
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.07 KB | None | 0 0
  1. local page = {}
  2.  
  3. function page.getPos(self)
  4.     for y=1,self.h do
  5.         for x=1,self.w do
  6.             if self[x][y][1] == nil then
  7.                 return x,y
  8.             end
  9.         end
  10.     end
  11. end
  12.  
  13. function page.draw(self,words,x,y,txt,back)
  14.     txt = txt or self.deftext
  15.     back = back or self.defback
  16.     for i = 0,#words-1 do
  17.         if not (x + i > self.w) then
  18.             self[x + i][y] = {words:sub(i+1,i+1),txt,back}
  19.         end
  20.     end
  21.     if x + #words > self.w then
  22.         self.x = 1
  23.         self.y = y + 1
  24.     else
  25.         self.x = x + #words
  26.         self.y = y
  27.     end
  28. end
  29.  
  30. function page.write(self,words,txt,back)
  31.     local x,y = self.x,self.y
  32.     txt = txt or self.deftxt
  33.     back = back or self.defback
  34.     for i = 0,#words-1 do
  35.         if not (x + i > self.w) then
  36.             self[x + i][y] = {words:sub(i+1,i+1),txt,back}
  37.         end
  38.     end
  39.     if x + #words > self.w then
  40.         self.x = 1
  41.         self.y = y + 1
  42.     else
  43.         self.x = x + #words
  44.         self.y = y
  45.     end
  46. end
  47.  
  48. function page.print(self,words,txt,back)
  49.     local x,y = self:getPos()
  50.     txt = txt or self.deftxt
  51.     back = back or self.defback
  52.     for i = 0,#words-1 do
  53.         if not (x + i > self.w) then
  54.             self[x + i][y] = {words:sub(i+1,i+1),txt,back}
  55.         end
  56.     end
  57.     self.x = 1
  58.     self.y = y + 1
  59. end
  60.  
  61.  
  62. function page.render(self,w,h)
  63.     term.setBackgroundColor(self.defback)
  64.     term.clear()
  65.     local txtcol = self.deftxt
  66.     local backcol = self.defback
  67.     if not (w and h) then
  68.         w,h = term.getSize()
  69.     end
  70.     for y = 1,h do
  71.         for x = 1,w do
  72.             if self[x][y][1] then
  73.                 term.setCursorPos(x,y)
  74.                 if self[x][y][2] ~= txtcol then
  75.                     term.setTextColor(self[x][y][2])
  76.                     txtcol = self[x][y][2]
  77.                 end
  78.                 if self[x][y][3] ~= backcol then
  79.                     term.setBackgroundColor(self[x][y][3])
  80.                     txtcol = self[x][y][3]
  81.                 end
  82.                 write(self[x][y][1])
  83.             end
  84.         end
  85.     end
  86. end
  87.  
  88. function createPage(w,h)
  89.     if not (w and h) then
  90.         w,h = term.getSize()
  91.     end
  92.     local buffer = {}
  93.     for x = 1,w do
  94.         buffer[x] = {}
  95.         for y = 1,h do
  96.             buffer[x][y] = {}
  97.         end
  98.     end
  99.     buffer.x = 1
  100.     buffer.y = 1
  101.     buffer.w = w
  102.     buffer.h = h
  103.     buffer.deftxt = colors.white
  104.     buffer.defback = colors.black
  105.     for i,v in pairs(page) do
  106.         buffer[i] = v
  107.     end
  108.     return buffer
  109. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement