MattiasBuelens

CCGUI paint

Jan 2nd, 2013
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.00 KB | None | 0 0
  1. --[[
  2.  
  3.     ComputerCraft GUI
  4.     Painting
  5.  
  6. --]]
  7.  
  8. ccgui = ccgui or {}
  9.  
  10. local PaintPixel = common.newClass({
  11.     x           = 0,
  12.     y           = 0,
  13.     content     = " ",
  14.     foreground  = colours.white,
  15.     background  = 0
  16. })
  17.  
  18. -- Compare pixels
  19. function PaintPixel:__eq(o)
  20.     return self.x == o.x and self.y == o.y
  21.         and self.content == o.content
  22.         and self.foreground == o.foreground and self.background == o.background
  23. end
  24.  
  25. -- Merge with pixel below
  26. function PaintPixel:merge(pixel)
  27.     -- Inherit colors from other pixel when transparent (zero)
  28.     if self.foreground == 0 then
  29.         if pixel ~= nil and pixel.foreground ~= 0 then
  30.             self.foreground = pixel.foreground
  31.         else
  32.             self.foreground = colours.white
  33.         end
  34.     end
  35.     if self.background == 0 then
  36.         if pixel ~= nil and pixel.background ~= 0 then
  37.             self.background = pixel.background
  38.         else
  39.             self.background = colours.black
  40.         end
  41.     end
  42. end
  43.  
  44. local PaintLayer = common.newClass({
  45.     -- Painted pixels
  46.     buffer = nil,
  47.     -- Pixels to be painted
  48.     update = nil,
  49.     -- Output device
  50.     output = term
  51. })
  52. ccgui.PaintLayer = PaintLayer
  53.  
  54. function PaintLayer:init(o)
  55.     self.buffer = {}
  56.     self.update = {}
  57.  
  58.     self:updateBounds()
  59. end
  60.  
  61. -- Get bounding rectangle
  62. function PaintLayer:getBounds()
  63.     return ccgui.newRectangle(1, 1, self.width, self.height)
  64. end
  65.  
  66. function PaintLayer:updateBounds()
  67.     self.width, self.height = self.output.getSize()
  68.     -- Clear buffers, redraw needed
  69.     self.buffer = {}
  70.     self.update = {}
  71. end
  72.  
  73. function PaintLayer:getIndex(x, y)
  74.     return (y-1) * self.width + x
  75. end
  76.  
  77. -- Get painted pixel
  78. function PaintLayer:getBufferPixel(x, y)
  79.     return self.buffer[self:getIndex(x, y)]
  80. end
  81.  
  82. -- Get pixel to be paint
  83. function PaintLayer:getPixel(x, y)
  84.     local i = self:getIndex(x, y)
  85.     return self.update[i] or self.buffer[i]
  86. end
  87.  
  88. function PaintLayer:setBufferPixel(pixel)
  89.     self.buffer[self:getIndex(pixel.x, pixel.y)] = pixel
  90. end
  91.  
  92. function PaintLayer:setPixel(pixel)
  93.     -- Merge with current pixel
  94.     local current = self:getPixel(pixel.x, pixel.y)
  95.     pixel:merge(current)
  96.  
  97.     self.update[self:getIndex(pixel.x, pixel.y)] = pixel
  98. end
  99.  
  100. function PaintLayer:commitPixel(pixel, force)
  101.     if pixel == nil then
  102.         return false
  103.     end
  104.  
  105.     if not force then
  106.         -- Compare to current pixel
  107.         local current = self:getBufferPixel(pixel.x, pixel.y)
  108.         if current ~= nil and current == pixel then
  109.             return false
  110.         end
  111.         self.changed = (self.changed or 0) + 1
  112.     end
  113.  
  114.     -- Set as buffer pixel
  115.     self:setBufferPixel(pixel)
  116.     -- Draw pixel
  117.     self:drawPixel(pixel)
  118.     return true
  119. end
  120.  
  121. -- Draw on output
  122. function PaintLayer:drawPixel(pixel)
  123.     self.output.setCursorPos(pixel.x, pixel.y)
  124.     self.output.setTextColor(self.output.isColor() and pixel.foreground or colours.white)
  125.     self.output.setBackgroundColor(self.output.isColor() and pixel.background or colours.black)
  126.     self.output.write(pixel.content)
  127. end
  128.  
  129. -- Paint updated pixels
  130. function PaintLayer:paint()
  131.     -- Loop over update queue
  132.     for i,pixel in pairs(self.update) do
  133.         -- Commit pixel
  134.         self:commitPixel(pixel)
  135.     end
  136.     --[[for y,row in pairs(self.update) do
  137.         for x,pixel in pairs(row) do
  138.             self:commitPixel(pixel)
  139.         end
  140.     end]]--
  141.     -- Clear update queue
  142.     self.update = {}
  143. end
  144.  
  145. -- Redraw all pixels
  146. function PaintLayer:repaint()
  147.     -- Loop over whole screen
  148.     for y=1,self.height do
  149.         for x=1,self.width do
  150.             -- Recommit pixel
  151.             local pixel = self:getPixel(x, y)
  152.             self:commitPixel(pixel, true)
  153.         end
  154.     end
  155.     -- Clear update queue
  156.     self.update = {}
  157. end
  158.  
  159. -- Clear whole screen
  160. function PaintLayer:clear()
  161.     -- Reset output
  162.     self.output.setTextColor(colours.white)
  163.     self.output.setBackgroundColor(colours.black)
  164.     self.output.clear()
  165.     self.output.setCursorPos(1, 1)
  166.     -- Clear buffers
  167.     self.buffer = {}
  168.     self.update = {}
  169. end
  170.  
  171. -- Write single text line
  172. function PaintLayer:write(x, y, text, foreground, background)
  173.     local n = string.len(text)
  174.     for i=1,n do
  175.         self:setPixel(PaintPixel:new({
  176.             x           = x + i - 1,
  177.             y           = y,
  178.             content     = string.sub(text, i, i),
  179.             foreground  = foreground,
  180.             background  = background
  181.         }))
  182.     end
  183. end
Advertisement
Add Comment
Please, Sign In to add comment