Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local w,h = term.getSize()
- local Pos = {1,1} --postion of cursor
- local Bc = 32768 --background color
- local Tc = 1 --text Color
- local buffer = {}
- local moniters = {}
- local lodingBar = {}
- local main = term
- ---------------------------------
- --buffer is the screen code
- ------------------------------------
- --buffer = buffer[n][1][2]==X=1,Y=2
- --n=nomber you get when you create a buffer
- --buffer[n][1][2]([1]<-- 1 2 3)
- --1=bc background color
- --2=tc text colorq
- --3=words/chars stuff you write to the screen
- ---------------------------------
- --Clear Buffer-------------------
- function clearBuffer()
- buffer = {}
- end
- --Setup Buffer-------------------
- local function setupBuffer(xo,yo)
- local xo = xo or w
- local yo = yo or h
- table.insert(buffer,{})
- local n = #buffer
- for y=1, yo do
- table.insert(buffer[n],{})
- for x=1, xo do
- local i = {1,32768," "}
- table.insert(buffer[n][y],i)
- end
- end
- buffer[n].screen = main
- return n
- end
- local function setupBufferMoniter(wrapMon, xo,yo)
- local wM,hM = wrapMon.getSize()
- local xo = xo or wM
- local yo = yo or hM
- table.insert(buffer,{})
- local n = #buffer
- for y=1, yo do
- table.insert(buffer[n],{})
- for x=1, xo do
- local i = {1,32768," "}
- table.insert(buffer[n][y],i)
- end
- end
- buffer[n].screen = wrapMon
- return n
- end
- ---------------------------------
- --Draws Buffer On Screen----------
- function drawBuffer(n,x2,y2)
- local h = #buffer[n]
- local w = #buffer[n][1]
- local x = 1
- local y = 1
- local screen = buffer[n].screen
- if x2 then
- ox2 = x2
- else
- ox2 = 1
- end
- x2 = x2 or 1
- y2 = y2 or 1
- while true do
- if y<=h then
- if x<=w then
- screen.setBackgroundColor(buffer[n][y][x][1])
- screen.setTextColor(buffer[n][y][x][2])
- screen.setCursorPos(x2,y2)
- screen.write(buffer[n][y][x][3])
- x=x+1
- x2=x2+1
- else
- x2 = ox2
- x=1
- y=y+1
- y2 = y2+1
- end
- else
- screen.setCursorPos(2,4)
- break
- end
- end
- end
- function getSizeBuff(n)
- local r = {#buffer[n],#buffer[n][1]}
- return r
- end
- function copyBufferNew(n1) --n1 = orriginal; newbuf is the reffrence to the new buffer
- table.insert(buffer,{})
- local newbuf = #buffer
- buffer[newbuf] = buffer[n1]
- return newbuf
- end
- function copyBuffer(n1,n2) --n1 = orriginal; n2 is the other
- buffer[n2] = buffer[n1]
- end
- function resizeBuffer(n,w,h)
- buffer[n] = {}
- local xo = xo or w
- local yo = yo or h
- for y=1, yo do
- table.insert(buffer[n],{})
- for x=1, xo do
- local i = {1,32768," "}
- table.insert(buffer[n][y],i)
- end
- end
- end
- ---------------------------------
- --Loads One Pixel----------------
- local function ldrawPixel(n,x,y,cbc,ctc,s)
- local ctc = ctc or Tc
- local s = s or " "
- local cbc = cbc or Bc
- buffer[n][y][x][1]=cbc
- buffer[n][y][x][2]=ctc
- buffer[n][y][x][3]=s
- end
- ----------------------------------
- --User Commands-------------------
- --[[ More Detailed Information located by function
- pos(x,y) -sets the postion on the screen
- getPos() -returns curent posistion
- bc(colors) -sets the background color
- tc(colors) -sets the text color
- cls() -clears the screen
- clsB() -clears the buffer
- clsLx() -clears the X line/row
- clsLy() -clears the Y line/colaum
- bWrite("text") -writes text to the screen in string
- ]]
- -----------------------------------
- --[[ Example Program #1: Simple Header and Footer Program
- local w,h = term.getSize()
- buf1 = buffer.createBuffer() -- creates the buffer
- buffer.bc(colors.blue) -- sets the background color
- buffer.tc(colors.white) --sets the text color
- buffer.pos(2,1) --sets the posistion
- buffer.clsLx(buf1) --clears the x row in this case line 1
- buffer.bWriteL(buf1,"Welcome!") -- writes welcome! starting at x=1 y=2
- buffer.pos(2,h) --sets the posistion to x=2, y=19 or h
- buffer.clsLx(buf1) --clears that line
- buffer.wCentered(buf1,"This is in the center") --prints the string in the senter
- buffer.drawBuffer(buf1) -- befor this nothing has been drawn
- sleep(5)
- ]]
- --[[ Example Program #2: has borders around screen with a black dot that can move with keys w a s d
- function main()
- local w,h = term.getSize()
- buf1 = buffer.createBuffer() -- creates the buffer
- buffer.clsB(buf1,colors.red) -- this makes all the pixels red
- --makes a red border
- buffer.wCentered(buf1,"Dot Example",1,colors.white,colors.red)
- buffer.drawBuffer(buf1)
- --beging code for Examples
- buf2 = buffer.createBuffer(50,17)
- buffer.clsB(buf2,colors.blue)
- buffer.drawPixel(buf2,math.floor(w/2)-1,math.floor(h/2)-1,colors.white) -- first pixels
- buffer.drawBuffer(buf2,2,2)
- buffer.pos(math.floor(w/2)-1,math.floor(h/2)-1)
- while true do
- local lastpos = buffer.getPos()
- local newpos = lastpos
- local arg = { os.pullEvent("key") }
- if arg[2]==keys.w then
- if lastpos[2]~=1 then
- newpos = {lastpos[1],lastpos[2]-1}
- end
- elseif arg[2]==keys.s then
- if lastpos[2]~=h-2 then
- newpos = {lastpos[1],lastpos[2]+1}
- end
- elseif arg[2]==keys.a then
- if lastpos[1]~=1 then
- newpos = {lastpos[1]-1,lastpos[2]}
- end
- elseif arg[2]==keys.d then
- if lastpos[1]~=w-2 then
- newpos = {lastpos[1]+1,lastpos[2]}
- end
- elseif arg[2]==keys.q then
- break
- end
- if lastpos~=newpos then
- buffer.drawPixel(buf2,lastpos[1],lastpos[2],colors.blue)
- buffer.pos(newpos[1],newpos[2])
- buffer.drawPixel(buf2,newpos[1],newpos[2],colors.white)
- buffer.drawBuffer(buf2,2,2)
- end
- end
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.setCursorPos(1,1)
- term.clear() -- runs term.clear()
- end
- main() -- this allows for error checking if you wish
- ]]
- function createBuffer(xo,yo,monWrap) -- monWrap if it exists it will use the wraped peripheral insted of term
- if monWrap then
- local n = setupBufferMoniter(monWrap, xo,yo)
- return n
- else
- local n = setupBuffer(xo,yo)
- return n
- end
- end
- function screenPos(x,y,n)--n is optional it allows for moniter support. linked to buffer number
- local screen = buffer[n].screen or main
- --[[
- this function alows for you to set the position on the screen so you can use functions like read()
- ]]
- screen.setCursorPos(x,y)
- end
- function pos(x,y)
- --[[
- this function sets the Postion on the screen like the term.setCursorPos() function
- This Affects the following functions:
- -bWrite
- -clsLx: clears the x line
- -clsLy: clears the y line
- ]]
- Pos = {x,y}
- end
- function posCenter(l,y)
- --[[
- this function is for centering the text for functions like slowWrite()
- this alows for you to get the text in the center of the screen but still beable to slowWrite with out writing it in
- ]]
- if y then
- Pos = {math.floor((w/2)-(#l/2)),y}
- elseif l then
- Pos = {math.floor((w/2)-(#l/2)),Pos[2]}
- else
- error('Usage: posCenter("string",<yLine>)',2)
- end
- end
- function posRight(l,y)
- if y then
- Pos = {math.floor(w-#l+1),y}
- elseif l then
- Pos = {math.floor(w-#l+1),Pos[2]}
- else
- error('Usage: posRight("string",<yLine>)',2)
- end
- end
- function movePos(mx,my)--mx = Move over x, my = Move over y
- if mx and my then
- if Pos[1]+mx<0 or Pos[2]+my<0 or Pos[1]+mx>w+1 or Pos[2]+my>h+1 then
- error("Can not extend bounders of screen",2)
- else
- Pos = {Pos[1]+mx,Pos[2]+my}
- end
- else
- return false
- end
- end
- function getPos()
- --[[
- this function just returns the curent posistion of your buffer
- this pirnts out the last pos() function called
- ]]
- return Pos
- end
- function bc(c)
- --[[
- this Function Sets the background color like the term.setBackgroundColor() function
- This Affects the following functions:
- -cls(),clsB(),clsLx(),clsLy()
- -bWrite()
- ]]
- Bc = c
- end
- function tc(c)
- --[[
- this Function Sets the text color like the term.setTextColor() function
- This Affects the following functions:
- -bWrite()
- ]]
- Tc = c
- end
- function cls(n)
- local screen = buffer[n].screen or main
- --[[
- this function only clears the screen it is exactly like term.celar()
- do not use this unless you need it for specal uses
- ]]
- screen.clear()
- end
- function clsB(n,cBc,cTc,Cw)
- --[[
- this function clears the buffer. everything is set to deffalt and bc color is apllied to all pixels
- ]]
- local w = #buffer[n][1]
- local h = #buffer[n]
- local x =0
- local y =0
- local cBc = cBc or Bc
- local cTc = cTc or 1
- local Cw = Cw or " "
- for x=1,w do
- for y=1,h do
- buffer[n][y][x][1]= cBc
- buffer[n][y][x][2]= cTc
- buffer[n][y][x][3]= Cw
- end
- end
- end
- function clsLx(n,y,c,ctc,s) -- y,c,tc,s is optional
- --[[
- this function is like the term.clearLine() function it sets everthing in the
- buffer on the curent x line to be defalt and have the bc color
- ]]
- local w = #buffer[n][1]
- local oldPos = {Pos[1],Pos[2]}
- local c = c or Bc
- if y~=nil then
- Pos = {1,y}
- else
- Pos = {1,Pos[2]}
- end
- local Ctc = ctc or Tc
- local cs = s or " "
- while Pos[1]<=w do
- drawPixel(n,Pos[1],Pos[2],c,Ctc,cs)
- Pos = {Pos[1]+1,Pos[2]}
- end
- Pos = oldPos
- end
- function clsLy(n,x,c,ctc,s) -- x,c,tc,s is optional
- --[[
- this function acks like the clsLx() function but in steed of clearing
- a line/row it clears a colaum it setes everthing on the y colaum to defalt and the bc color
- ]]
- local h = #buffer[n]
- local oldPos = {Pos[1],Pos[2]}
- local cColor = c or Bc
- if x~=nil then
- Pos = {x,1}
- else
- Pos = {Pos[1],1}
- end
- local Ctc = ctc or Tc
- local cs = s or " "
- while Pos[2]<=h do
- drawPixel(n,Pos[1],Pos[2],cColor,Ctc,cs)
- Pos = {Pos[1],Pos[2]+1}
- end
- Pos = oldPos
- end
- function bWrite(n,s,ctc,cbc) --tc and bc are optional
- --[[
- this adds text to the buffer /n does not work in this verstion
- bc and tc both affect this function
- ]]
- local cBc = cbc or Bc
- local Ctc = ctc or Tc
- ldrawPixel(n,Pos[1],Pos[2],cBc,Ctc,s)
- end
- --needs Comments V and addet to list of functions
- function bWriteL(n,l,ctc,cbc)
- local oldPos = Pos
- local cBc = cbc or Bc
- local Ctc = ctc or Tc
- if #l<2 then
- bWrite(n,l,Ctc,cBc)
- end
- local cpos = Pos
- for i=1,#l do
- Pos = {cpos[1],cpos[2]}
- local s = string.sub(l,i,i)
- bWrite(n,s,Ctc,cBc)
- cpos = {cpos[1]+1,cpos[2]}
- end
- Pos = oldPos
- end
- function bWriteLP(n,l,ctc,cbc) --leaves pos where it left of like write()
- local cBc = cbc or Bc
- local Ctc = ctc or Tc
- if #l<2 then
- bWrite(n,l,Ctc,cBc)
- end
- local cpos = Pos
- for i=1,#l do
- Pos = {cpos[1],cpos[2]}
- local s = string.sub(l,i,i)
- bWrite(n,s,Ctc,cBc)
- cpos = {cpos[1]+1,cpos[2]}
- end
- Pos = {Pos[1]+1,Pos[2]}
- end
- function wCentered(n,s,y,ctc,cbc) -- y is optional
- local w = #buffer[n][1]
- local cbc = cbc or Bc
- local ctc = ctc or Tc
- y = y or Pos[2]
- Pos = {(math.floor(w/2)-math.floor(#s/2)),y}
- bWriteLP(n,s,ctc,cbc)
- end
- function wLeft(n,s,y,ctc,cbc) -- y is optional
- local oldPos = Pos
- local cpos = Pos
- if y then
- cpos[2] = y
- end
- Pos = {1,cpos[2]}
- if cbc~=nil then
- bWriteL(n,s,ctc,cbc)
- elseif ctc~=nil then
- bWriteL(n,s,ctc)
- else
- bWriteL(n,s)
- end
- Pos = oldPos
- end
- function wRight(n,s,y,ctc,cbc) -- y is optional
- local w = #buffer[n][1]
- local oldPos = Pos
- local cpos = Pos
- if y then
- cpos[2] = y
- end
- Pos = {w-#s+1,cpos[2]}
- if cbc~=nil then
- bWriteL(n,s,ctc,cbc)
- elseif ctc~=nil then
- bWriteL(n,s,ctc)
- else
- bWriteL(n,s)
- end
- Pos = oldPos
- end
- function slowWrite(n,l,Time,ctc,cbc)
- local oldPos = Pos
- local cBc = cbc or Bc
- local Ctc = ctc or Tc
- if #l<2 then
- bWrite(n,l,Ctc,cBc)
- end
- if Time==nil then
- Time = 1/20
- else
- if Time>0 then
- Time = 1/Time
- else
- error("Rate must be positive",2)
- end
- end
- local cpos = Pos
- for i=1,#l do
- Pos = {cpos[1],cpos[2]}
- local s = string.sub(l,i,i)
- bWrite(n,tostring(s),Ctc,cBc)
- cpos = {cpos[1]+1,cpos[2]}
- drawBuffer(n)
- sleep(Time)
- end
- Pos = oldPos
- end
- function drawPixel(n,x,y,c,ctc,s)
- local x = x or Pos[1]
- local y = y or Pos[2]
- local c = c or Bc
- local Ctc = ctc or Tc
- local cs = s or " "
- ldrawPixel(n,x,y,c,Ctc,cs)
- end
- function drawBox(n,x1,y1,x2,y2,c,tc,s)
- local y = 0
- local x = 0
- local c = c or Bc
- local tc = tc or Tc
- local s = s or " "
- for y=y1,y2 do
- for x=x1,x2 do
- if x==x1 or y==y1 or x==x2 or y==y2 then
- drawPixel(n,x,y,c,tc,s)
- end
- end
- end
- end
- function drawFilledBox(n,x1,y1,x2,y2,c,tc,s)
- local y = 0
- local x = 0
- local c = c or Bc
- local tc = tc or Tc
- local s = s or " "
- for y=y1,y2 do
- for x=x1,x2 do
- drawPixel(n,x,y,c,tc,s)
- end
- end
- end
- local tColorLookup = {}
- for n=1,16 do
- tColorLookup[ string.byte("0123456789abcdef",n,n)] = 2^(n-1)
- end
- function loadImage(path)
- local tImage = {}
- if pcall(function ()
- if fs.exists( path ) then
- local file = io.open(path,"r")
- local sLine = file:read()
- while sLine do
- local tLine = {}
- for x=1,sLine:len() do
- tLine[x] = tColorLookup[string.byte(sLine,x,x)] or 0
- end
- table.insert(tImage,tLine)
- sLine = file:read()
- end
- file:close()
- return true
- end
- end) then
- return tImage
- else
- error("Attempt to load image failed: Buf:477-499, Image_Path:"..path,2)
- end
- end
- function drawImage(n,xPos,yPos,tImage)
- if pcall(function ()
- if type(tImage)~="table" then
- tImage = loadImage(tImage)
- end
- for y=1,#tImage do
- local tLine = tImage[y]
- for x=1,#tLine do
- if tLine[x] > 0 then
- ldrawPixel(n,x+xPos-1,y+yPos-1,tLine[x],Tc," ")
- end
- end
- end
- end) then
- else
- error("Attempt to load image failed: Buf:477-498, Image_Path:"..tostring(path),2)
- end
- end
- function loadImageASCII(path)
- local tImage = {}
- if fs.exists( path ) then
- local file = io.open(path,"r")
- local sLine = file:read()
- while sLine do
- local tLine = {}
- for x=1,sLine:len() do
- tLine[x] = string.sub(sLine,x,x)
- end
- table.insert(tImage,tLine)
- sLine = file:read()
- end
- file:close()
- return tImage
- end
- end
- function drawImageASCII(n,xPos,yPos,tImage,cbc,ctc)
- Bc = cbc or Bc
- Tc = ctc or Tc
- local screen = buffer[n].screen
- if type(tImage)~="table" then
- tImage = loadImageASCII(tImage)
- end
- screen.setCursorPos(xPos,yPos)
- for y=1,#tImage do
- local tLine = tImage[y]
- for x=1,#tLine do
- ldrawPixel(n,x+xPos-1,y+yPos-1,Bc,Tc,tLine[x])
- end
- end
- end
- function drawLine(n,x1,y1,x2,y2,c,tc,s)
- local x1 = math.floor(x1)
- local x2 = math.floor(x2)
- local y1 = math.floor(y1)
- local y2 = math.floor(y2)
- local tc = tc or Tc
- local s = s or " "
- local c = c or Bc
- if x1==x2 and y1==y2 then
- drawPixel(n,x1,y1,tc,s)
- return
- end
- local minX = math.min(x1,x2)
- if minX==x1 then
- minY = y1
- maxX = x2
- maxY = y2
- else
- minY = y2
- maxX = x1
- maxY = y1
- end
- local xDiff = maxX - minX
- local yDiff = maxY - minY
- if xDiff > math.abs(yDiff) then
- local y = minY
- local dy = yDiff/xDiff
- for x=minX,maxX do
- drawPixel(n,x,math.floor(y+0.5),c,tc,s)
- y = y+dy
- end
- else
- local x = minX
- local dx = xDiff/yDiff
- if maxY>=minY then
- for y=minY,maxY do
- drawPixel(n,math.floor(x+0.5),y,c,tc,s)
- x = x+dx
- end
- else
- for y=minY,maxY,-1 do
- drawPixel(n,math.floor(x+0.5),y,c,tc,s)
- x = x-dx
- end
- end
- end
- end
- function drawCircle(n,xc,yc,r,c) -- need to fix gaps
- local x = 0
- local y = r
- local delta = 2-2*r
- local Err = 0
- local c = c or Bc
- while y>=0 do
- drawPixel(n,xc + x, yc + y, c) --1
- drawPixel(n,xc - x, yc + y, c) --2
- drawPixel(n,xc + x, yc - y, c) --3
- drawPixel(n,xc - x, yc - y, c) --4
- Err = 2 * (delta + y) - 1
- if delta < 0 and Err <= 0 then
- x = x+1
- delta = delta + 2 * x + 1
- end
- Err = 2 * (delta - x) - 1
- if delta > 0 and Err > 0 then
- y = y-1
- delta = delta + 1 - 2 * y
- end
- x = x+1
- delta = delta + 2 * (x - y)
- y = y-1
- end
- end
- function drawEllipse(n,xc,yc,w,h,c) --does not work
- local w2 = w * w
- local h2 = h * h
- local fw2 = 4 * w2
- local fh2 = 4 * h2
- local xc = xc + w
- local yc = yc + h
- local x = 0
- local y = h
- local s = 2 * h2 + w2 * (1 - h)
- while h2 * x <= w2 * y do
- drawPixel(n,xc + x,yc + y,c)
- drawPixel(n,xc - x, yc + y,c)
- drawPixel(n,xc + x, yc - y,c)
- drawPixel(n,xc - x, yc - y,c)
- if s >= 0 then
- s = s + fw2 * (1 - y)
- y = y - 1
- end
- s = s + h2 * ((4 * x) + 6)
- x = x + 1
- end
- local x = w
- local y = 0
- local s = 2 * w2 + h2 * (1 - w)
- while w2 * y <= h2 * x do
- drawPixel(n,xc + x, yc + y,c)
- drawPixel(n,xc - x, yc + y,c)
- drawPixel(n,xc + x, yc - y,c)
- drawPixel(n,xc - x, yc - y,c)
- if s >= 0 then
- s = s + fh2 * (1 - x)
- x = x - 1
- end
- s = s + w2 * ((4 * y) + 6)
- y = y + 1
- end
- end
- function drawTriagle(n,x1,y1,x2,y2,x3,y3,c)
- local c = c or Bc
- drawLine(n,x1,y1,x2,y2,c)
- drawLine(n,x2,y2,x3,y3,c)
- drawLine(n,x3,y3,x1,y1,c)
- end
- function drawPolygon(n,c, ...)
- local arg = {...}
- local xy = {}
- if c==nil then
- c=Bc
- end
- if #arg%2 ~= 0 then
- return false
- end
- local pra = 1
- for i=2,#arg+1,2 do
- local l = {arg[pra],arg[pra+1]}
- table.insert(xy,l)
- pra = pra +2
- end
- if #xy==1 then
- drawPixel(n,xy[1][1],xy[1][2],c)
- elseif #xy==2 then
- drawLine(n,xy[1][1],xy[1][2],xy[2][1],xy[2][2],c)
- else
- for i=1, #xy do
- if i==#xy then
- drawLine(n,xy[i][1],xy[i][2],xy[1][1],xy[1][2],c)
- else
- drawLine(n,xy[i][1],xy[i][2],xy[i+1][1],xy[i+1][2],c)
- end
- end
- end
- end
- function drawBotton(n,x1,y1,sx,sy,c1,c2,s,c3) --(x,y,How dig x,how big y,color rim,color inside,w,color words)
- drawFilledBox(n,x1,y1,x1+sx-1,y1+sy-1,c2)
- drawBox(n,x1,y1,x1+sx-1,y1+sy-1,c1)
- if s then
- local oldPos = Pos
- pos(math.floor(x1+(sx/2))-math.floor(#s/2),math.floor((y1+sy)/y1))
- bWriteL(n,s,c3,c2)
- pos(oldPos)
- end
- end
- function createLoadbar(n2,x1,y1,x2,y2,bbc,fbc,p,ctc1,ctc2,t1,t2)
- p = p or 0
- ctc1 = ctc1 or Tc
- ctc2 = ctc2 or Tc
- t1 = t1 or " "
- t2 = t2 or " "
- local n = #lodingBar+1
- lodingBar[n] = {x1,y1,x2,y2,bbc,fbc,p,ctc1,ctc2,t1,t2,n2}
- return n
- end
- function drawLoadbar(n)
- local bar = lodingBar[n]
- if bar[2] == bar[4] then
- drawLine(bar[12],bar[1],bar[2],bar[3],bar[4],bar[5],bar[8],bar[10])
- else
- drawFilledBox(bar[12],bar[1],bar[2],bar[3],bar[4],bar[5],bar[8],bar[10])
- end
- if bar[7]~=0 then
- local lenght = bar[3]-bar[1]
- local px = bar[1]+math.ceil((bar[7]*lenght)/100)
- if bar[2] == bar[4] then
- drawLine(bar[12],bar[1],bar[2],px,bar[4],bar[6],bar[9],bar[11])
- else
- drawFilledBox(bar[12],bar[1],bar[2],px,bar[4],bar[6],bar[9],bar[11])
- end
- end
- end
- function increnetLoadbar(n,p,draw,x2,y2) --draw if true will draw if false or nill will not
- lodingBar[n][7] = tonumber(p)
- if draw then
- local x2 = x2 or 1
- local y2 = y2 or 1
- drawLoadbar(n)
- drawBuffer(lodingBar[n][12],x2,y2)
- else
- drawLoadbar(n)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement