Advertisement
Guest User

paintutils.lua

a guest
Jun 26th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.19 KB | None | 0 0
  1. local com = require"component"
  2. local e = require"event"
  3. local term = require"term"
  4. local sbc = com.gpu.setBackground
  5. local stc = com.gpu.setForeground
  6. local scp = term.setCursor
  7. local write = term.write
  8.  
  9. local ui = {}
  10.  
  11. function ui.gwrite(t)
  12.     local x,y = term.getCursor()
  13.     com.gpu.set(x,y,t)
  14. end
  15.  
  16. function ui.drawPixel(x,y)
  17.    scp(x,y)
  18.    ui.gwrite(" ") -- add ui. when you call your own functions!
  19. end
  20.  
  21. function ui.drawLine( startX, startY, endX, endY, nColour )
  22.     startX = math.floor(startX)
  23.     startY = math.floor(startY)
  24.     endX = math.floor(endX)
  25.     endY = math.floor(endY)
  26.     sbc(nColour)
  27.     if startX == endX and startY == endY then
  28.        ui.drawPixel( startX, startY )
  29.         return
  30.     end
  31.    
  32.     local minX = math.min( startX, endX )
  33.     if minX == startX then
  34.         minY = startY
  35.         maxX = endX
  36.         maxY = endY
  37.     else
  38.         minY = endY
  39.         maxX = startX
  40.         maxY = startY
  41.     end
  42.  
  43.     -- TODO: clip to screen rectangle?
  44.        
  45.     local xDiff = maxX - minX
  46.     local yDiff = maxY - minY
  47.            
  48.     if xDiff > math.abs(yDiff) then
  49.         local y = minY
  50.         local dy = yDiff / xDiff
  51.         for x=minX,maxX do
  52.             ui.drawPixel( x, math.floor( y + 0.5 ) )
  53.             y = y + dy
  54.         end
  55.     else
  56.         local x = minX
  57.         local dx = xDiff / yDiff
  58.         if maxY >= minY then
  59.             for y=minY,maxY do
  60.                 ui.drawPixel( math.floor( x + 0.5 ), y )
  61.                 x = x + dx
  62.             end
  63.         else
  64.             for y=minY,maxY,-1 do
  65.                 ui.drawPixel( math.floor( x + 0.5 ), y )
  66.                 x = x - dx
  67.             end
  68.         end
  69.     end
  70.   sbc(0x000000)
  71. end
  72.  
  73. function ui.drawBox(sx,sy,ex,ey,col)
  74.    sbc(col)
  75.    scp(sx,sy)
  76.    ui.gwrite(string.rep(" ",(ex-sx)+1))
  77.    for i = 0, ey-sy do
  78.        scp(sx,sy+i)
  79.        ui.gwrite(" ")
  80.        scp(ex,sy+i)
  81.        ui.gwrite(" ")
  82.    end
  83.    scp(sx,ey)
  84.    ui.gwrite(string.rep(" ",ex-sx))
  85.   --sbc(0x000000)
  86. end
  87.  
  88. function ui.drawFilledBox(sx,sy,ex,ey,col)
  89.    sbc(col)
  90.    for i = 0, ey-sy do
  91.       scp(sx,sy+i)
  92.       ui.gwrite(string.rep(" ",(ex-sx)+1))
  93.    end
  94.   --sbc(0x000000)
  95. end
  96.  
  97. return ui
  98. --drawBox(1,1,10,10,0x00FF00)
  99. --drawFilledBox(11,11,21,21,0x0000FF)
  100. --drawLine(10,10,40,20,0xFFFFFF)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement