Corona

[Computercraft] Object Oriented Window API

Jun 24th, 2015
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.98 KB | None | 0 0
  1. os.loadAPI( "./lib/draw" )
  2. os.loadAPI( "./lib/util" )
  3.  
  4. out = term
  5. function setOutput( monitor )
  6.   out = monitor
  7. end
  8.  
  9.  
  10. Window = { x = 1, y = 1, xs = 15, ys = 10, color = colors.blue, fcolor = colors.black, visible = true, uuid = 0, text = {"",1,1} }
  11. windowTable = {}
  12.  
  13. function drawWindows( tWindows )
  14.   tWindows = tWindows or windowTable
  15.   for i=#tWindows, 1, -1 do
  16.     tWindows[i]:draw()
  17.   end
  18. end
  19.  
  20. function Window:new ( o )
  21.   o = o or {}   -- create object if user does not provide one
  22.   setmetatable( o, self )
  23.   self.__index = self
  24.   o.uuid = math.random(1,10000)
  25.   o:setTitle( o.uuid )
  26.   return o
  27. end
  28.  
  29. function Window:setVisible( visible )
  30.   self.visible = visible
  31. end
  32.  
  33. function Window:setColor( color )
  34.   self.color = color
  35. end
  36.  
  37. function Window:setTitle( title )
  38.   if string.len(title) > 6 then title = string.sub( title, 1, 6 ) end
  39.   self.title = title
  40. end
  41.  
  42. function Window:register( tablePosition )
  43.   for i = 1, #windowTable do
  44.     if( windowTable[i].uuid == self.uuid ) then
  45.       table.remove( windowTable, i )
  46.       break
  47.     end
  48.   end
  49.   if tablePosition < #windowTable then
  50.     for i = #windowTable, tablePosition ,-1 do
  51.       windowTable[i+1] = windowTable[i]
  52.     end
  53.   end
  54.     windowTable[tablePosition] = self
  55. end
  56.  
  57. function Window:vanish()
  58.   self:setVisible( false )
  59.   for j = 0, self.ys - 1 do
  60.     for i = 0, self.xs - 1 do
  61.       out.setCursorPos( self.x + i, self.y + j )
  62.       out.setBackgroundColor( colors.black )
  63.       out.write( " " )
  64.     end
  65.   end
  66. end
  67.  
  68. function Window:setPos( x,y )
  69.   self.x = x
  70.   self.y = y
  71. end
  72.  
  73. function Window:getPos()
  74.   return self.x, self.y
  75. end
  76.  
  77. function Window:setSize( xs, ys )
  78.   if xs < 15 then xs = 15 end
  79.   if ys < 5 then ys = 5 end
  80.   self.xs = xs
  81.   self.ys = ys
  82. end
  83.  
  84. function Window:isClicked( xCoord, yCoord )
  85.   if( self.x <= xCoord and xCoord < (self.x + self.xs) ) and ( self.y <= yCoord and yCoord < (self.y + self.ys) ) then
  86.     return true
  87.   else
  88.     return false
  89.   end
  90. end
  91.  
  92. function Window:getClick( xCoord, yCoord )
  93.   return self:isClicked( xCoord, yCoord ), xCoord - self.x + 1, yCoord - self.y +1
  94. end
  95.  
  96. function Window:setText( text, xPos, yPos )
  97.   xPos = xPos or 1
  98.   yPos = yPos or 1
  99.   self.text = {text, xPos, yPos}
  100. end
  101.  
  102. function Window:getText()
  103.   return self.text
  104. end
  105.  
  106. function Window:draw()
  107.  
  108.   if not self.visible then
  109.     return 0
  110.   end
  111.  
  112.   for j = 0, self.ys - 1 do
  113.     for i = 0, self.xs - 1 do
  114.       out.setCursorPos( self.x + i, self.y + j )
  115.       if ( j == 0 or j == self.ys - 1 or i == 0 or i == self.xs - 1 ) then
  116.         out.setBackgroundColor( self.color )
  117.         if( j == 0 and i == 0 ) then
  118.           out.write( "[" )
  119.         elseif( j == 0 and i == 1 ) then
  120.           out.write( "X" )
  121.         elseif( j == 0 and i == 2 ) then
  122.           out.write( "-" )
  123.         elseif( j == 0 and i == 3 ) or ( j == self.ys - 1 and i == 0 ) or ( j == 0 and i == self.xs - 1) then
  124.           out.write( "+" )
  125.         elseif( j == self.ys - 1 and i == self.xs - 1) then
  126.           out.write( "v" )
  127.         elseif( j == 0 and i == 4 ) then
  128.           out.write( "]" )
  129.         elseif( j == self.ys - 1 ) then
  130.           out.write( "-" )
  131.         elseif( j == 0 ) then
  132.           out.write( "-" )
  133.         else
  134.           out.write( "|" )
  135.         end
  136.       else
  137.         out.setBackgroundColor( self.fcolor )
  138.         out.write( " " )
  139.       end
  140.  
  141.     end
  142.   end
  143.   --DEBUG
  144.   out.setCursorPos( self.x + 5, self.y )
  145.   out.write( "["..self.title.."]" )
  146.  
  147.   out.setBackgroundColor( colors.black )
  148.  
  149.   if #self.text[1] > self.xs - 2 then
  150.     local line = 0
  151.     for i=1, #self.text[1], self.xs - 2 do
  152.       if line > self.ys - 3 then
  153.         break
  154.       end
  155.       out.setCursorPos(self.x+self.text[2],self.y+self.text[3]+line)
  156.       out.write(util.trim(string.sub(self.text[1], i, i+self.xs - 3)))
  157.       line = line + 1
  158.     end
  159.   else
  160.  
  161.   out.setCursorPos( self.x + self.text[2], self.y + self.text[3])
  162.   out.write( self.text[1] )
  163.  
  164. end
  165.  
  166.   out.setBackgroundColor( colors.black )
  167. end
Advertisement
Add Comment
Please, Sign In to add comment