Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --CONFIG:
- local lazy = false
- --[[?:
- lazy is weather or not write will precalculate string replacement. If you're blitting a lot, turn this off. If you're using mostly
- active buffers, turn this on. This will:
- Speed up writing
- Slows down bliting(only happens when pulling up a previously off-screen application)
- Eat up more memory(a decent bit--this will keep lazilly adding stuff to a table rather than calculating it.)
- If you're bliting a lot, you probably want to set it to false
- Default value: false
- ]]
- --[[A NOTE ON EXESSIVE OP COUNT INCRECIMENT:
- 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.
- 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.
- ]]
- local function yield()
- os.queueEvent"bufferYield"
- os.pullEvent"bufferYield"
- end
- local function moveUp(self)
- end
- local b = {}
- function b.getSize(self)
- return self.sizex,self.sizey
- end
- function b.resize(self,x,y)
- if type(x)~="number" or type(y)~="number"then
- return error"[buffer][resize] - Expected number, number"
- end
- self.sizex,self.sizey = x,y
- self.blit()
- end
- function b.setCursorPos(self,x,y)
- if type(x)~="number" or type(y)~="number"then
- return error"[buffer][setCursorPos] - Expected number, number"
- end
- self.x,self.y=x,y
- if self.isActive then
- self.t.setCursorPos(x+self.pX,y+self.pY)
- end
- end
- function b.getCursorPos(self)
- return self.x,self.y
- end
- function b.setBackgroundColor(self,color)
- self.bg = color
- if self.isActive then
- self.t.setBackgroundColor(color)
- end
- end
- function b.setTextColor(self,color)
- self.tc = color
- if self.isActive then
- self.t.setTextColor(color)
- end
- end
- function b.setCursorBlink(self,a)
- self.cursorBlink = a
- if self.isActive then
- self.t.setCursorBlink(a)
- end
- end
- function b.scroll(self,ammount)
- for l=1,ammount do
- for i=2,#self do
- self[i-1] = self[i]
- end
- end
- if self.isActive then
- self.t.scroll(ammount)
- end
- end
- function b.current(self)
- return self
- end
- function b.makeActive(self,pX,pY)
- self.isActive,self.pX,self.pY = true,pX-1,pY-1
- end
- b.setTextColour = b.setTextColor
- b.setBackgroundColour = b.setBackgroundColor
- function b.isColor(self,color)
- return self.t.isColor(color)
- end
- b.isColour = b.isColor
- function b.write(self,txt)
- if type(txt)~="string" then
- txt = tostring(txt)
- end
- --game.l"write"
- --yield()-------------------------------------------------------------------
- --self.t.write"hi"-------------------------------------------------------------------
- if self.lazy then
- self[self.y] = self[self.y] or {}
- self[self.y][self.x] = self[self.y][self.x] or {}
- self[self.y][self.x][self.opCount] = {}
- --self[self.y][self.x][self.opCount].t = p
- self[self.y][self.x][self.opCount].bg = self.bg
- self[self.y][self.x][self.opCount].tc = self.tc
- self[self.y][self.x][self.opCount].txt = txt
- self.opCount = self.opCount+1
- else
- --self.t.write"l"-------------------------------------------------------------------
- local oldTxt,oldbg,oldtc
- if (self[self.y] or {})[self.x] then
- oldTxt,oldbg,oldtc=self[self.y][self.x].txt,self[self.y][self.x].bg,self[self.y][self.x].tc
- end
- oldTxt = nil
- --game.l"old"-------------------------------------------------------------------
- --self.t.write(self)
- self[self.y] = self[self.y] or {}
- --game.l"1"--------------------------------------------------------------------------------------------------------------------------------------
- self[self.y][self.x] = self[self.y][self.x] or {}
- --self.t.write"2"---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- self[self.y][self.x].txt = txt
- --self.t.write"3"----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- self[self.y][self.x].bg = self.bg
- --game.l"4"-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- self[self.y][self.x].tc = self.tc
- --game.l"str"------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- local left = string.sub(oldTxt or "",#txt,#(oldTxt or ""))
- --game.l"man"-------------------------------------------------------------------
- if #left>1 then
- --game.l"left"
- --self.t.write(" "..self.y)-------------------------------------------------------------------
- --self.t.write(" "..self.x+#txt)-------------------------------------------------------------------
- --self.t.write(" "..left)-------------------------------------------------------------------
- self[self.y] = self[self.y] or {}
- self[self.y][self.x+#txt] = self[self.y][self.x+#txt] or {}
- self[self.y][self.x+#txt].txt = left
- --game.l"left1"
- self[self.y][self.x+#txt].bg = oldbg
- self[self.y][self.x+#txt].tc = oldtc
- end
- --clear anything in our path:
- for i=self.x+1,self.x+#txt do
- self[self.y][i] = nil
- end
- self.x = self.x+#txt--increase x count
- end
- --game.l"bye"-------------------------------------------------------------------
- if self.isActive then
- --game.l"a"-------------------------------------------------------------------
- --self.t.setCursorPos(self.x+self.pX,self.y+self.pY) if the buffer is active, this is not necisary
- --game.l"m"-------------------------------------------------------------------
- --self.t.setBackgroundColor(((self[y] or {})[x] or {}).bg) -- The buffer is active, meaning it should already be set to its own colors
- --self.t.setTextColor((self[y] or {})[x] or {}).tc)
- self.t.write(txt)
- end
- --[[game.l"{"
- for i=1,#self do
- if self[i] then
- game.l(textutils.serialize(self[i]))
- end
- end
- game.l"}"]]-----------------------------------------
- end
- function b.blit(self,x,y) -- NOTE: Code normal buffer system first
- game.l"blit"
- x,y=x or self.lastX, y or self.lastY
- self.lastX,self.lastY = x,y
- x,y=x-1,y-1
- if self.lazy then
- --group operations by opnum
- local ops = {}
- local where = {}
- for i=1,table.maxn(self) do
- ops[i] = op[i] or {}
- where[i]=where[i] or {}
- if self[i] then
- for o=1,table.maxn(self[i]) do
- if #ops[i] == 1 then
- ops[i][1] = self[i][o].opN
- where[i][1] = o
- else
- for p=1,#ops[i] do
- if ops[i][p] > self[i][o].opN then
- table.insert(ops[i],p,self[i][o].opN)
- table.insert(where[i],p,o)
- end
- end
- end
- end
- end
- end
- for i=1,#ops do
- for o=1,#ops[i] do
- term.setBackgroundColor(self[i][where.o].bg)
- term.setTextColor(self[i][where.o].tc)
- self.t.write(self[i][where.o].txt)
- end
- end
- else
- for i=1+y,table.maxn(self) do
- if self[i] then
- for o=1+x,table.maxn(self[i]) do
- game.l("m Y "..i.." x "..o)
- if self[i][o] then
- term.setCursorPos(o,i)
- term.setBackgroundColor(self[i][o].bg)
- term.setTextColor(self[i][o].tc)
- self.t.write(self[i][o].txt)
- game.l("ye Y "..i.." x "..o)
- end
- end
- end
- end
- end
- end
- function b.clear(self)
- for i=1,table.maxn(self) do
- self[i]={}
- end
- if self.isActive then
- self.t.clear()
- end
- end
- function b.clearLine(self,l)
- self[(l or 1)]={}
- if self.isActive then
- self.t.clearLine(l)
- end
- end
- --[[ idk wtf this is, but I'm afriad to delete it ._.
- if ((b[y] or {})[x] or {}).bg then
- term.setBackgroundColor(b[y][x].bg)
- end
- if ((b[y] or {})[x] or {}).tx then
- term.setTextColor(b[y][x].tx)
- end ]]
- function newBuf(w,h)
- --I ripped redirect's meta functions :3 3lazy1make my own
- local meta={}
- local buffer = {}
- local function wrap(f,o)
- return function(...)
- return f(o,...)
- end
- end
- for k,v in pairs(b) do
- if type(v)=="function" then
- meta[k]=wrap(v,buffer)
- else
- meta[k]=v
- end
- end
- setmetatable(buffer,{__index=meta})
- --buffer vars
- buffer.opCount = 1
- buffer.cursorBlink = true
- --set bg colors
- buffer.bg = colors.black
- buffer.tc = colors.white
- buffer.t = term.current()
- buffer.y = 1
- buffer.x = 1
- buffer.lastX = 1
- buffer.lastY= 1
- buffer.lazy = lazy
- buffer.isntNative = true
- buffer.resize(w,h)
- return buffer--,"Thank you, come again!"
- end
Advertisement
Add Comment
Please, Sign In to add comment