Advertisement
LDDestroier

Draw Grid API (ComputerCraft)

Jul 20th, 2016
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.32 KB | None | 0 0
  1. local ro = function(input, max)
  2.     return math.floor(input % max)
  3. end
  4. local stringShift = function(str,amt)
  5.     return str:sub(ro(amt-1,#str)+1)..str:sub(1,ro(amt-1,#str))
  6. end
  7. --[[
  8. Example on how to use:
  9.  
  10. local grid = {   ---That's how the grid is formatted.
  11.     "+---",
  12.     "|   ",
  13.     "|   ",
  14. }
  15.  
  16. local x,y = term.getSize()
  17.  
  18. --If you want to fill the whole screen with that grid, then:
  19.  
  20. drawGrid(1, 1, x, y, 0, 0)
  21.  
  22. --and if you want a specific coloration, you can add bg color and txt color as two more arguments.
  23.  
  24. --]]
  25.  
  26. function drawGrid(_x1,_y1,_x2,_y2,grid,_xscroll,_yscroll,_bgcol,_txtcol)
  27.     local scr_x, scr_y = term.getSize()
  28.     local _txt, _bg = term.getTextColor(), term.getBackgroundColor()
  29.     local _x, _y = term.getCursorPos()
  30.     local xscroll, yscroll = _xscroll or 0, _yscroll or 0
  31.     local x1,x2,y1,y2 = _x1,_x2,_y1,_y2
  32.     if _x2 < _x1 then
  33.         x1,x2=_x2,_x1
  34.     end
  35.     if _y2 < _y1 then
  36.         y1,y2=_y2,_y1
  37.     end
  38.     local lenx, leny = (x2-x1)+1, y2-y1
  39.     if _bgcol then
  40.         term.setBackgroundColor(_bgcol)
  41.     end
  42.     if _txtcol then
  43.         term.setTextColor(_txtcol)
  44.     end
  45.     for y = y1, y2 do
  46.         term.setCursorPos(x1,y)
  47.         term.write(stringShift(grid[ro(y+(yscroll+2),#grid)+1],xscroll+1):rep(math.ceil(lenx/#grid[ro(y+(yscroll+2),#grid)+1])):sub(1,lenx))
  48.     end
  49.     term.setCursorPos(_x,_y)
  50.     term.setBackgroundColor(_bg)
  51.     term.setTextColor(_txt)
  52. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement