Advertisement
Corona

[Computercraft] Object Oriented Drawing API

Jun 24th, 2015
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.10 KB | None | 0 0
  1. out = term
  2. function setOutput( monitor )
  3.   out = monitor
  4. end
  5.  
  6. Rectangle = { x = 0, y = 0, xs = 10, ys = 10, filled = false, color = colors.white, fcolor = colors.white, visible = true }
  7.  
  8. function Rectangle:new ( o )
  9.   o = o or {}   -- create object if user does not provide one
  10.   setmetatable( o, self ) -- sets the metatable recursively
  11.   self.__index = self -- sets the index metamethod recursively
  12.   return o
  13. end
  14.  
  15. function Rectangle:setVisible( visible )
  16.   self.visible = visible
  17. end
  18.  
  19. function Rectangle:setColor( color )
  20.   self.color = color
  21. end
  22.  
  23. function Rectangle:setFillColor( color )
  24.   self.fcolor = color
  25. end
  26.  
  27. function Rectangle:setFilled( filled )
  28.   self.filled = filled
  29. end
  30.  
  31. function Rectangle:setPos( x, y )
  32.   self.x = x
  33.   self.y = y
  34. end
  35.  
  36. function Rectangle:setSize( xs, ys )
  37.   self.xs = xs
  38.   self.ys = ys
  39. end
  40.  
  41. function Rectangle:getPos()
  42.   return { x = self.x, y = self.y }
  43. end
  44.  
  45. function Rectangle:draw()
  46.  
  47.   if not self.visible then
  48.     return 0
  49.   end
  50.  
  51.   for j=0, self.ys-1 do
  52.     for i=0, self.xs-1 do
  53.       out.setCursorPos( self.x+i, self.y+j )
  54.       if not self.filled then
  55.         if ( j == 0 or j == self.ys-1 or i == 0 or i == self.xs-1 ) then
  56.           --sleep(0.02)
  57.           out.setBackgroundColor( self.color )
  58.           out.write( " " )
  59.         end
  60.       else
  61.         --sleep(0.02)
  62.         if ( j == 0 or j == self.ys-1 or i == 0 or i == self.xs-1 ) then
  63.           out.setBackgroundColor( self.color )
  64.         else
  65.           out.setBackgroundColor( self.fcolor )
  66.         end
  67.         out.write( " " )
  68.       end
  69.     end
  70.   end
  71.   out.setBackgroundColor( colors.black )
  72. end
  73.  
  74. --------------------------------CIRCLE-----------------------------
  75.  
  76. Circle = { x = 0, y = 0, r = 0, filled = false, color = colors.white, fcolor = colors.white, visible = true}
  77.  
  78. function Circle:new( o )
  79.   o = o or {}   -- create object if user does not provide one
  80.   setmetatable( o, self ) -- sets the metatable recursively
  81.   self.__index = self -- sets the index metamethod recursively
  82.   return o
  83. end
  84.  
  85. function Circle:setColor( color )
  86.   self.color = color
  87. end
  88.  
  89. function Circle:setFillColor( color )
  90.   self.fcolor = color
  91. end
  92.  
  93. function Circle:setFilled( filled )
  94.   self.filled = filled
  95. end
  96.  
  97. function Circle:setPos( x, y )
  98.   self.x = x
  99.   self.y = y
  100. end
  101.  
  102. function Circle:setRadius( r )
  103.   self.r = r
  104. end
  105.  
  106. function Circle:draw()
  107.  
  108.   if not self.visible then
  109.     return 0
  110.   end
  111.  
  112.   for i = 1, 360 do
  113.     if self.filled then -- IMPROVE!!!
  114.       for j = 0, self.r-1 do
  115.         local angle = i * math.pi / 180
  116.         out.setBackgroundColor( self.fcolor )
  117.         out.setCursorPos( math.floor( self.x + j * math.cos( angle ) + 0.5 ), math.floor( self.y + j * math.sin( angle ) + 0.5 ) )
  118.         out.write( " " )
  119.         --sleep(0.02)
  120.       end
  121.     end
  122.     local angle = i * math.pi / 180
  123.     out.setBackgroundColor( self.color )
  124.     out.setCursorPos( math.floor( self.x + self.r * math.cos( angle ) + 0.5 ), math.floor( self.y + self.r * math.sin( angle ) + 0.5 ) )
  125.     out.write( " " )
  126.     --sleep(0.02)
  127.   end
  128.   out.setBackgroundColor( colors.black )
  129. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement