5bitesofcookies

askapro

Mar 21st, 2015
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.21 KB | None | 0 0
  1.  
  2.  
  3. --CONFIG:
  4. local lazy = false
  5. --[[?:
  6. lazy is weather or not write will precalculate string replacement. If you're blitting a lot, turn this off. If you're using mostly
  7. active buffers, turn this on. This will:
  8. Speed up writing
  9. Slows down bliting(only happens when pulling up a previously off-screen application)
  10. Eat up more memory(a decent bit--this will keep lazilly adding stuff to a table rather than calculating it.)
  11. If you're bliting a lot, you probably want to set it to false
  12.  
  13. Default value: false
  14.  
  15.  ]]
  16. --[[A NOTE ON EXESSIVE OP COUNT INCRECIMENT:
  17. Due to the way Lua works, if you get a rediculously high number(like, if you left something running without ever de-foucusing and re-foucusing it for several months), this script WILL break.
  18. That said, if you ever get a legitiment case of this happening, let me know, so I can congratulate you on your lack of a life.
  19.  ]]
  20. local function yield()
  21.     os.queueEvent"bufferYield"
  22.     os.pullEvent"bufferYield"
  23. end
  24. local function moveUp(self)
  25.  
  26.  
  27.  
  28. end
  29. local b = {}
  30. function b.getSize(self)
  31.     return self.sizex,self.sizey
  32. end
  33. function b.resize(self,x,y)
  34.     if type(x)~="number" or type(y)~="number"then
  35.         return error"[buffer][resize] - Expected number, number"
  36.     end
  37.     self.sizex,self.sizey = x,y
  38.     self.blit()
  39. end
  40. function b.setCursorPos(self,x,y)
  41.     if type(x)~="number" or type(y)~="number"then
  42.         return error"[buffer][setCursorPos] - Expected number, number"
  43.     end
  44.     self.x,self.y=x,y
  45.     if self.isActive then
  46.         self.t.setCursorPos(x+self.pX,y+self.pY)
  47.     end
  48. end
  49. function b.getCursorPos(self)
  50.     return self.x,self.y
  51. end
  52. function b.setBackgroundColor(self,color)
  53.     self.bg = color
  54.     if self.isActive then
  55.         self.t.setBackgroundColor(color)
  56.     end
  57. end
  58. function b.setTextColor(self,color)
  59.     self.tc = color
  60.     if self.isActive then
  61.         self.t.setTextColor(color)
  62.     end
  63. end
  64. function b.setCursorBlink(self,a)
  65.     self.cursorBlink = a
  66.     if self.isActive then
  67.         self.t.setCursorBlink(a)
  68.     end
  69. end
  70. function b.scroll(self,ammount)
  71.     for l=1,ammount do
  72.         for i=2,#self do
  73.             self[i-1] = self[i]
  74.         end
  75.     end
  76.     if self.isActive then
  77.         self.t.scroll(ammount)
  78.     end
  79. end
  80. function b.current(self)
  81.     return self
  82. end
  83. function b.makeActive(self,pX,pY)
  84.     self.isActive,self.pX,self.pY = true,pX-1,pY-1
  85. end
  86. b.setTextColour = b.setTextColor
  87. b.setBackgroundColour = b.setBackgroundColor
  88. function b.isColor(self,color)
  89.     return self.t.isColor(color)
  90. end
  91.  
  92.  
  93. b.isColour = b.isColor
  94. function b.write(self,txt)
  95.     if type(txt)~="string" then
  96.         txt = tostring(txt)
  97.     end
  98.     --game.l"write"
  99.     --yield()-------------------------------------------------------------------
  100.     --self.t.write"hi"-------------------------------------------------------------------
  101.     if self.lazy then
  102.         self[self.y] = self[self.y] or {}
  103.         self[self.y][self.x] = self[self.y][self.x] or {}
  104.         self[self.y][self.x][self.opCount] = {}
  105.         --self[self.y][self.x][self.opCount].t = p
  106.         self[self.y][self.x][self.opCount].bg = self.bg
  107.         self[self.y][self.x][self.opCount].tc = self.tc
  108.         self[self.y][self.x][self.opCount].txt = txt
  109.         self.opCount = self.opCount+1
  110.     else
  111.         --self.t.write"l"-------------------------------------------------------------------
  112.         local oldTxt,oldbg,oldtc
  113.         if (self[self.y] or {})[self.x] then
  114.             oldTxt,oldbg,oldtc=self[self.y][self.x].txt,self[self.y][self.x].bg,self[self.y][self.x].tc
  115.         end
  116.         oldTxt = nil
  117.         --game.l"old"-------------------------------------------------------------------
  118.         --self.t.write(self)
  119.         self[self.y] = self[self.y] or {}
  120.         --game.l"1"--------------------------------------------------------------------------------------------------------------------------------------
  121.         self[self.y][self.x] = self[self.y][self.x] or {}
  122.         --self.t.write"2"---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  123.         self[self.y][self.x].txt = txt
  124.         --self.t.write"3"----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  125.         self[self.y][self.x].bg = self.bg
  126.         --game.l"4"-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  127.         self[self.y][self.x].tc = self.tc
  128.        
  129.         --game.l"str"------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  130.         local left = string.sub(oldTxt or "",#txt,#(oldTxt or ""))
  131.         --game.l"man"-------------------------------------------------------------------
  132.         if #left>1 then
  133.             --game.l"left"
  134.             --self.t.write("     "..self.y)-------------------------------------------------------------------
  135.             --self.t.write("    "..self.x+#txt)-------------------------------------------------------------------
  136.             --self.t.write("    "..left)-------------------------------------------------------------------
  137.             self[self.y] = self[self.y] or {}
  138.             self[self.y][self.x+#txt] = self[self.y][self.x+#txt] or {}
  139.  
  140.             self[self.y][self.x+#txt].txt = left
  141.             --game.l"left1"
  142.             self[self.y][self.x+#txt].bg = oldbg
  143.             self[self.y][self.x+#txt].tc = oldtc
  144.         end
  145.         --clear anything in our path:
  146.         for i=self.x+1,self.x+#txt do
  147.             self[self.y][i] = nil
  148.         end
  149.         self.x = self.x+#txt--increase x count
  150.     end
  151.     --game.l"bye"-------------------------------------------------------------------
  152.     if self.isActive then
  153.         --game.l"a"-------------------------------------------------------------------
  154.         --self.t.setCursorPos(self.x+self.pX,self.y+self.pY) if the buffer is active, this is not necisary
  155.         --game.l"m"-------------------------------------------------------------------
  156.         --self.t.setBackgroundColor(((self[y] or {})[x] or {}).bg) -- The buffer is active, meaning it should already be set to its own colors
  157.         --self.t.setTextColor((self[y] or {})[x] or {}).tc)
  158.         self.t.write(txt)
  159.     end
  160.     --[[game.l"{"
  161.     for i=1,#self do
  162.         if self[i] then
  163.             game.l(textutils.serialize(self[i]))
  164.         end
  165.     end
  166.     game.l"}"]]-----------------------------------------
  167. end
  168. function b.blit(self,x,y)  -- NOTE: Code normal buffer system first
  169.    
  170.     game.l"blit"
  171.     x,y=x or self.lastX, y or self.lastY
  172.     self.lastX,self.lastY = x,y
  173.     x,y=x-1,y-1
  174.     if self.lazy then
  175.         --group operations by opnum
  176.         local ops = {}
  177.         local where = {}
  178.         for i=1,table.maxn(self) do
  179.             ops[i] = op[i] or {}
  180.             where[i]=where[i] or {}
  181.             if self[i] then
  182.                 for o=1,table.maxn(self[i]) do
  183.                     if #ops[i] == 1 then
  184.                         ops[i][1] = self[i][o].opN
  185.                         where[i][1] = o
  186.                     else
  187.                         for p=1,#ops[i] do
  188.                             if ops[i][p] > self[i][o].opN then
  189.                                 table.insert(ops[i],p,self[i][o].opN)
  190.                                 table.insert(where[i],p,o)
  191.                             end
  192.                         end
  193.                     end
  194.                 end
  195.             end
  196.         end
  197.         for i=1,#ops do
  198.             for o=1,#ops[i] do
  199.                 term.setBackgroundColor(self[i][where.o].bg)
  200.                 term.setTextColor(self[i][where.o].tc)
  201.                 self.t.write(self[i][where.o].txt)
  202.             end
  203.         end
  204.     else
  205.         for i=1+y,table.maxn(self) do
  206.             if self[i] then
  207.                 for o=1+x,table.maxn(self[i]) do
  208.                     game.l("m Y "..i.." x "..o)
  209.                     if self[i][o] then
  210.                         term.setCursorPos(o,i)
  211.                         term.setBackgroundColor(self[i][o].bg)
  212.                         term.setTextColor(self[i][o].tc)
  213.                         self.t.write(self[i][o].txt)
  214.                         game.l("ye Y "..i.." x "..o)
  215.                     end
  216.                 end
  217.             end
  218.         end
  219.     end
  220. end
  221. function b.clear(self)
  222.     for i=1,table.maxn(self) do
  223.         self[i]={}
  224.     end
  225.     if self.isActive then
  226.         self.t.clear()
  227.     end
  228. end
  229. function b.clearLine(self,l)
  230.     self[(l or 1)]={}
  231.     if self.isActive then
  232.         self.t.clearLine(l)
  233.     end
  234. end
  235.  
  236. --[[ idk wtf this is, but I'm afriad to delete it ._.
  237. if ((b[y] or {})[x] or {}).bg then
  238.         term.setBackgroundColor(b[y][x].bg)
  239.     end
  240.     if ((b[y] or {})[x] or {}).tx then
  241.         term.setTextColor(b[y][x].tx)
  242.     end ]]
  243. function newBuf(w,h)
  244.   --I ripped redirect's meta functions :3 3lazy1make my own
  245.     local meta={}
  246.     local buffer = {}
  247.     local function wrap(f,o)
  248.         return function(...)
  249.             return f(o,...)
  250.         end
  251.     end
  252.     for k,v in pairs(b) do
  253.         if type(v)=="function" then
  254.             meta[k]=wrap(v,buffer)
  255.         else
  256.             meta[k]=v
  257.         end
  258.    end
  259.    setmetatable(buffer,{__index=meta})
  260.    --buffer vars
  261.    buffer.opCount = 1
  262.    buffer.cursorBlink = true
  263.    --set bg colors
  264.    buffer.bg = colors.black
  265.    buffer.tc = colors.white
  266.    buffer.t = term.current()
  267.    buffer.y = 1
  268.    buffer.x = 1
  269.    buffer.lastX = 1
  270.    buffer.lastY= 1
  271.    buffer.lazy = lazy
  272.    buffer.isntNative = true
  273.    buffer.resize(w,h)
  274.    return buffer--,"Thank you, come again!"
  275. end
Advertisement
Add Comment
Please, Sign In to add comment