Advertisement
Pinkishu

Untitled

Jun 19th, 2013
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.11 KB | None | 0 0
  1. document = {}
  2.  
  3. function document.new(w,h,data)
  4.     document.w,document.h = w,h
  5.     if data == nil then
  6.         document.data = {}
  7.         for i=1,h*w,1 do
  8.             document.data[i] = {col=colors.white,bgCol=colors.black,char=32}
  9.         end
  10.     else
  11.         document.data = data
  12.     end
  13.     document.cPos = {x=1,y=1}
  14.     document.lRender={x=1,y=1}
  15.     document.isDirty=true
  16.     document.active=false
  17.     document.col = colors.white
  18.     document.bgCol = colors.black
  19. end
  20.  
  21. function document.activate()
  22.     document.active=true
  23. end
  24.  
  25. function document.deactivate()
  26.     document.active=false
  27. end
  28.  
  29. function document.key(key)
  30.     if document.active then
  31.         if key == keys.left then
  32.             document.moveCursor(-1,nil)
  33.         elseif key == keys.right then
  34.             document.moveCursor(1,nil)
  35.         elseif key == keys.up then
  36.             document.moveCursor(0,-1)
  37.         elseif key == keys.down then
  38.             document.moveCursor(0,1)
  39.         elseif key == keys.backspace and (document.cPos.x+((document.cPos.y-1)*document.h))>1 then
  40.             print(document.cPos.x+((document.cPos.y-1)*document.h)) read()
  41.             print(textutils.serialize(table.remove(document.data,document.cPos.x+((document.cPos.y-1)*document.h)))) read()
  42.             print(textutils.serialize(document.data[1]))read()
  43.        --     table.insert(document.data,#document.data,{col=colors.white,bgCol=colors.black,char=32})
  44.             document.moveCursor(-1,nil)
  45.             document.isDirty=true
  46.         end
  47.     end
  48. end
  49.  
  50. function document.char(char)
  51.     if document.insertMode then
  52.         document.data[document.cPos.y][document.cPos.x].char = string.byte(char,1)
  53.         document.moveCursor(1,nil)
  54.         document.isDirty=true
  55.     else
  56.         table.insert(document.data,document.cPos.x+(document.cPos.y*document.h),{col=document.col,bgCol=document.bgCol,char=string.byte(char,1)})
  57.         table.remove(document.data,#document.data)
  58.         document.moveCursor(1,nil)
  59.         document.isDirty=true
  60.     end
  61. end
  62.  
  63. function document.moveCursor(x,y)
  64.     local nX,nY = document.cPos.x,document.cPos.y
  65.     if x ~= 0 and x ~= nil then
  66.         nX = nX+x
  67.         if nX < 1 and y == nil and nY > 1 then
  68.             nX = document.w
  69.             nY = math.max(1,nY - 1)
  70.         elseif nX > document.w and y == nil and nY < document.h then
  71.             nY = nY + 1
  72.             nX = 1
  73.         end
  74.     end
  75.     if y ~= 0 and y ~= nil then
  76.         nY = nY + y
  77.     end
  78.     document.setCursor(nX,nY)
  79. end
  80.  
  81. function document.setCursor(x,y)
  82.     document.cPos={x=math.max(1,math.min(document.w,x)),y=math.max(1,math.min(document.h,y))}
  83.     document.updateCursorPos()
  84. end
  85.  
  86. function document.updateCursorPos()
  87.     term.setCursorPos(document.cPos.x+document.lRender.x-1,document.cPos.y+document.lRender.y-1)
  88. end
  89.  
  90. function document.render(x,y)
  91.     if x == nil then x = document.lRender.x end
  92.     if y == nil then y = document.lRender.y end
  93.     document.lRender={x=x,y=y}
  94.     local l = 1
  95.     local s = ""
  96.     local cols = {bg=document.data[1].bgCol,fg=document.data[1].col}
  97.     term.setCursorPos(x,y)
  98.     for i=1,#document.data,1 do
  99.         local cL = math.ceil(i/document.w)
  100.        
  101.         if l ~= cL or cols.bg ~= document.data[i].bgCol or cols.fg ~= document.data[i].col then
  102.             term.setBackgroundColor(cols.bg)
  103.             term.setTextColor(cols.fg)
  104.             term.write(s)
  105.             if l ~= cL then
  106.                 term.setCursorPos(x,y+l-1)
  107.             end
  108.             l=cL
  109.  
  110.             s = ""
  111.             cols.bg = document.data[i].bgCol
  112.             cols.fg = document.data[i].col
  113.         end
  114.         s= s..string.char(document.data[i].char)
  115.     end
  116.     if s ~= "" then
  117.         term.setBackgroundColor(cols.bg)
  118.         term.setTextColor(cols.fg)
  119.         term.write(s)            
  120.     end
  121.  
  122.     term.setCursorPos(document.cPos.x-1+x,document.cPos.y-1+y)
  123.     term.setCursorBlink(true)
  124.     document.isDirty=false
  125.  
  126. end
  127.  
  128. function document.checkCoords(x,y)
  129.     return (x >= document.lRender.x and x < document.lRender.x+document.w and y >= document.lRender.y and y < document.lRender.y+document.h)
  130. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement