Advertisement
Siasur

Computercraft / CC: Tweaked Drawing Tools

May 27th, 2020
939
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.90 KB | None | 0 0
  1. --[[ DrawingTools v0.2  ]]
  2. --[[ by Siasur | Mischa ]]
  3.  
  4. local version = 0.2
  5.  
  6. local _mt = { }
  7. _mt.__index = _mt
  8.  
  9. -- [[  HELPERS  ]]
  10.  
  11. -- Clamps a number between a min and a max value
  12. local function --[[ Number ]] clamp(--[[ Number ]] value, --[[ Number ]] min, --[[ Number ]] max )
  13.   if value < min then return min end
  14.   if value > max then return max end
  15.   return value
  16. end
  17.  
  18. local function --[[ String ]] getPaintColor( --[[ Color ]] color )
  19.   local shiftCount = 0
  20.   while bit.band(0x1, color) ~= 0x1 do
  21.     color = bit.brshift(color, 1)
  22.     shiftCount = shiftCount +1
  23.   end
  24.   return string.format("%x", shiftCount)
  25. end
  26.  
  27. local lightColorMask = 0x7e
  28. local darkColorMask = 0x7e00
  29.  
  30. local function --[[ Color ]] resolveColor( --[[ Color ]] color , --[[ bool ]] enableColor )
  31.  
  32.   if not color then
  33.     error("Can't resolve nil to color", 2)
  34.   end
  35.  
  36.   if 1 > color or color > 0x8000 then
  37.     error("Color is out of bounds", 3)
  38.   end
  39.  
  40.   if not enableColor then
  41.     if bit.band(lightColorMask, color) ~= 0 then return 0x100 end
  42.     if bit.band(darkColorMask, color) ~= 0 then return 0x80 end
  43.   end
  44.  
  45.   return color
  46. end
  47.  
  48. -- [[ /HELPERS  ]]
  49.  
  50. -- Clears the Buffer
  51. function --[[ void ]] _mt.clear( --[[ Canvas ]] self )
  52.   for x = 1, self.width, 1 do
  53.     self.buffer[x] = self.buffer[x] or { }
  54.     for y = 1, self.height, 1 do
  55.       self.buffer[x][y] = self.buffer[x][y] or { }
  56.       self.buffer[x][y].background = self.bgColor
  57.       self.buffer[x][y].foreground = self.fgColor
  58.       self.buffer[x][y].char = " "
  59.     end
  60.   end
  61. end
  62.  
  63. -- Sets a new default background color
  64. function --[[ void ]] _mt.setBackgroundColor( --[[ Canvas ]] self, --[[ Color ]] color )
  65.   self.bgColor = resolveColor(color, self.isColor)
  66. end
  67.  
  68. -- Sets a new default foreground color
  69. function --[[ void ]] _mt.setForegroundColor( --[[ Canvas ]] self, --[[ Color ]] color )
  70.   self.fgColor = resolveColor(color, self.isColor)
  71. end
  72.  
  73. -- Clears the text in the given area
  74. function --[[ void ]] _mt.eraseText( --[[ Canvas ]] self, --[[ Number ]] posX, --[[ Number ]] posY, --[[ Number ]] width, --[[ Number ]] height )
  75.   for x = clamp(posX, 1, self.width), clamp(width + posX - 1, 1, self.width), 1 do
  76.     for y = clamp(posY, 1, self.height), clamp(height + posY - 1, 1, self.height), 1 do
  77.       self.buffer[x][y].char = " "
  78.     end
  79.   end
  80. end
  81.  
  82. -- Draws a box in the given size at the given location
  83. function --[[ void ]] _mt.drawBox( --[[ Canvas ]] self, --[[ Number ]] posX, --[[ Number ]] posY, --[[ Number ]] width, --[[ Number ]] height, --[[ Color ]] color)
  84.   for x = clamp(posX, 1, self.width), clamp(width + posX - 1, 1, self.width), 1 do
  85.     for y = clamp(posY, 1, self.height), clamp(height + posY - 1, 1, self.height), 1 do
  86.       self.buffer[x][y].background = resolveColor(color, self.isColor)
  87.     end
  88.   end
  89. end
  90.  
  91. -- Writes some text starting at the specific location
  92. -- Returns true and and empty string if successful, false and the remaining string if it was too long
  93. function --[[ bool, String ]] _mt.write( --[[ Canvas ]] self, --[[ Number ]] posX, --[[ Number ]] posY, --[[ String ]] text, --[[ Color? ]] color )
  94.  
  95.   if 1 > posY or posY > self.height then
  96.     error("Can't write out of bounds! (X: " .. posX .. " Y: " .. posY .. ")", 2)
  97.     return false, text
  98.   end
  99.  
  100.   local remaining = string.format("%s", text)
  101.   local step = posX
  102.   local color = color and resolveColor(color, self.isColor) or self.fgColor
  103.   while #remaining > 0 do
  104.     if step > self.width then
  105.       return false, remaining
  106.     end
  107.     local current = remaining:sub(1,1)
  108.     remaining = remaining:sub(2)
  109.  
  110.     if step >= 1 then
  111.       self.buffer[step][posY].char = current
  112.       self.buffer[step][posY].foreground = color
  113.     end
  114.     step = step+1
  115.   end
  116.   return true, ""
  117. end
  118.  
  119. -- Renders the Canvas to the screen
  120. function --[[ void ]] _mt.render( --[[ Canvas ]] self )
  121.   for y = 1, self.height, 1 do
  122.     local lineText = ""
  123.     local lineColorFG = ""
  124.     local lineColorBG = ""
  125.     for x = 1, self.width, 1 do
  126.       lineColorBG = lineColorBG .. getPaintColor(self.buffer[x][y].background)
  127.       lineColorFG = lineColorFG .. getPaintColor(self.buffer[x][y].foreground)
  128.       lineText = lineText .. self.buffer[x][y].char or " "
  129.     end
  130.  
  131.     self.screen.setCursorPos(1, y)
  132.     self.screen.blit(lineText, lineColorFG, lineColorBG)
  133.   end
  134. end
  135.  
  136. -- Creates a new canvas
  137. function --[[ Canvas ]] createCanvas( --[[ display ]] display )
  138.   local isMonitor = display.seTextScale and true or false
  139.   local isColor = display.isColor()
  140.   local sizeX, sizeY = display.getSize()
  141.   display.setCursorBlink(false)
  142.  
  143.   local canvas = {
  144.     screen = display,
  145.     height = sizeY,
  146.     width = sizeX,
  147.     isColor = isColor,
  148.     bgColor = colors.black,
  149.     fgColor = colors.white,
  150.     buffer = {}
  151.   }
  152.  
  153.   _mt.clear(canvas)
  154.  
  155.   return setmetatable(canvas, _mt)
  156. end
  157.  
  158. -- Returns the current version number
  159. function --[[ Number ]] getVersion()
  160.   return version
  161. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement