Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- document = {}
- function document.new(w,h,data)
- document.w,document.h = w,h
- if data == nil then
- document.data = {}
- for i=1,h*w,1 do
- document.data[i] = {col=colors.white,bgCol=colors.black,char=32}
- end
- else
- document.data = data
- end
- document.cPos = {x=1,y=1}
- document.lRender={x=1,y=1}
- document.isDirty=true
- document.active=false
- document.col = colors.white
- document.bgCol = colors.black
- end
- function document.activate()
- document.active=true
- end
- function document.deactivate()
- document.active=false
- end
- function document.key(key)
- if document.active then
- if key == keys.left then
- document.moveCursor(-1,nil)
- elseif key == keys.right then
- document.moveCursor(1,nil)
- elseif key == keys.up then
- document.moveCursor(0,-1)
- elseif key == keys.down then
- document.moveCursor(0,1)
- elseif key == keys.backspace and (document.cPos.x+((document.cPos.y-1)*document.h))>1 then
- print(document.cPos.x+((document.cPos.y-1)*document.h)) read()
- print(textutils.serialize(table.remove(document.data,document.cPos.x+((document.cPos.y-1)*document.h)))) read()
- print(textutils.serialize(document.data[1]))read()
- -- table.insert(document.data,#document.data,{col=colors.white,bgCol=colors.black,char=32})
- document.moveCursor(-1,nil)
- document.isDirty=true
- end
- end
- end
- function document.char(char)
- if document.insertMode then
- document.data[document.cPos.y][document.cPos.x].char = string.byte(char,1)
- document.moveCursor(1,nil)
- document.isDirty=true
- else
- table.insert(document.data,document.cPos.x+(document.cPos.y*document.h),{col=document.col,bgCol=document.bgCol,char=string.byte(char,1)})
- table.remove(document.data,#document.data)
- document.moveCursor(1,nil)
- document.isDirty=true
- end
- end
- function document.moveCursor(x,y)
- local nX,nY = document.cPos.x,document.cPos.y
- if x ~= 0 and x ~= nil then
- nX = nX+x
- if nX < 1 and y == nil and nY > 1 then
- nX = document.w
- nY = math.max(1,nY - 1)
- elseif nX > document.w and y == nil and nY < document.h then
- nY = nY + 1
- nX = 1
- end
- end
- if y ~= 0 and y ~= nil then
- nY = nY + y
- end
- document.setCursor(nX,nY)
- end
- function document.setCursor(x,y)
- document.cPos={x=math.max(1,math.min(document.w,x)),y=math.max(1,math.min(document.h,y))}
- document.updateCursorPos()
- end
- function document.updateCursorPos()
- term.setCursorPos(document.cPos.x+document.lRender.x-1,document.cPos.y+document.lRender.y-1)
- end
- function document.render(x,y)
- if x == nil then x = document.lRender.x end
- if y == nil then y = document.lRender.y end
- document.lRender={x=x,y=y}
- local l = 1
- local s = ""
- local cols = {bg=document.data[1].bgCol,fg=document.data[1].col}
- term.setCursorPos(x,y)
- for i=1,#document.data,1 do
- local cL = math.ceil(i/document.w)
- if l ~= cL or cols.bg ~= document.data[i].bgCol or cols.fg ~= document.data[i].col then
- term.setBackgroundColor(cols.bg)
- term.setTextColor(cols.fg)
- term.write(s)
- if l ~= cL then
- term.setCursorPos(x,y+l-1)
- end
- l=cL
- s = ""
- cols.bg = document.data[i].bgCol
- cols.fg = document.data[i].col
- end
- s= s..string.char(document.data[i].char)
- end
- if s ~= "" then
- term.setBackgroundColor(cols.bg)
- term.setTextColor(cols.fg)
- term.write(s)
- end
- term.setCursorPos(document.cPos.x-1+x,document.cPos.y-1+y)
- term.setCursorBlink(true)
- document.isDirty=false
- end
- function document.checkCoords(x,y)
- return (x >= document.lRender.x and x < document.lRender.x+document.w and y >= document.lRender.y and y < document.lRender.y+document.h)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement