jseely

Button

Mar 16th, 2013
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.34 KB | None | 0 0
  1. Point = {}
  2.  
  3. Point.new = function(x, y)
  4.     -- Private
  5.     local self = {}
  6.     local X = x or 0
  7.     local Y = y or 0
  8.    
  9.     -- Get/Set
  10.     self.x = function(x)
  11.         X = x or X
  12.         return X
  13.     end
  14.     self.y = function(y)
  15.         Y = y or Y
  16.         return Y
  17.     end
  18.    
  19.     return self
  20. end
  21.  
  22. Button = {}
  23.  
  24. Button.new = function(pos, size, text, color, callback)
  25.     -- Private Variables
  26.     local self = {}
  27.     local Pos = pos or Point.new(0,0)
  28.     local Size = size or Point.new(0,0)
  29.     local Text = text or ""
  30.     local Color = color or colors.gray
  31.     local callback = callback or function() end
  32.    
  33.     -- Get/Set
  34.     self.pos = function(pos)
  35.         Pos = pos or Pos
  36.         return Pos
  37.     end
  38.     self.size = function(size)
  39.         Size = size or Size
  40.         return Size
  41.     end
  42.     self.text = function(text)
  43.         Text = text or Text
  44.         return Text
  45.     end
  46.     self.color = function(color)
  47.         Color = color or Color
  48.         return Color
  49.     end
  50.     self.callback = function(callback)
  51.         Callback = callback or Callback
  52.         return Callback
  53.     end
  54.    
  55.     -- Methods
  56.     self.render = function(monitor)
  57.         local lineCount = 0
  58.         local lines = {}
  59.         for line in string.gmatch(Text, "[^%|]+") do
  60.            lineCount = lineCount + 1
  61.            lines[lineCount] = line
  62.         end
  63.        
  64.         local textPos = {}
  65.         textPos.empty = true
  66.         for i = 1, lineCount do
  67.             textPos.empty = false
  68.             local textX = Pos.x() + math.floor((Size.x() - string.len(lines[i]))/2)
  69.             local textY = Pos.y() + math.floor((Size.y() - lineCount)/2) + i - 1
  70.             if textX < Pos.x() then
  71.                 textX = Pos.x()
  72.             end
  73.             textPos[i] = Point.new(textX, textY)
  74.         end
  75.    
  76.         monitor.setBackgroundColor(Color)
  77.        
  78.         local curLine = 1
  79.         monitor.setCursorPos(Pos.x(), Pos.y())
  80.         local x, y = monitor.getCursorPos()
  81.         while not (x == Pos.x() and y == Pos.y() + Size.y()) do
  82.             if textPos.empty or curLine > lineCount then
  83.                 monitor.write(" ")
  84.             elseif textPos[curLine].y() == y and textPos[curLine].x() ==  x then
  85.                 monitor.write(lines[curLine])
  86.                 curLine = curLine + 1
  87.             else
  88.                 monitor.write(" ")
  89.             end
  90.            
  91.             x, y = monitor.getCursorPos()
  92.             if x > Pos.x() + Size.x() - 1 then
  93.                 x = Pos.x()
  94.                 y = y + 1
  95.                 monitor.setCursorPos(x, y)
  96.             end
  97.         end
  98.     end
  99.     self.isClicked = function(click)
  100.         if Pos.x() <= click.x() and Pos.x() + Size.x() >= click.x() then
  101.             if Pos.y() <= click.y() and Pos.y() + Size.y() >= click.y() then
  102.                 return true
  103.             end
  104.         end
  105.         return false
  106.     end
  107.    
  108.     return self
  109. end
Advertisement
Add Comment
Please, Sign In to add comment