oblinger

OBB

Jun 7th, 2014
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.41 KB | None | 0 0
  1. buttonList = {}
  2. Buttons = {}
  3. Buttons.__index = Buttons
  4.  
  5. function createButton( name, func )
  6.     button = {}
  7.     setmetatable(button,Buttons)
  8.     button.name = name
  9.     button.action = func
  10.     return button
  11. end
  12.  
  13. function Buttons:toggle( newColor,sec )
  14.  
  15.     self:draw( self.x,self.y,self.width,newColor,self.tcolor)
  16.  
  17.     if sec ~= nil then
  18.         sleep(sec)
  19.         self:draw( self.x,self.y,self.width,self.color,self.tcolor )
  20.     end
  21. end
  22.  
  23.  
  24. function Buttons:draw( x,y,width,color,tcolor )
  25.  
  26.     table.insert(buttonList,{self.name,x,y,width,self.action})
  27.    
  28.     self.x = x
  29.     self.y = y
  30.     self.width = width
  31.     if self.tcolor == nil then
  32.         self.color = color
  33.     end
  34.     self.tcolor = tcolor
  35.  
  36.     for i = 1,width do
  37.         paintutils.drawLine(x, y + i, x + #self.name + 1, y + i, color)
  38.     end
  39.  
  40.     term.setCursorPos(x,y + math.ceil(width/2))
  41.     term.setTextColor(tcolor)
  42.     term.setBackgroundColor(color)
  43.     term.write(" "..self.name.." ")
  44. end
  45.  
  46. function Buttons:trigger()
  47.     buttonList[i][5]()
  48. end
  49.  
  50. function Buttons:remove()
  51.     for i = 1,#buttonList do
  52.         if self.name == buttonList[i][1] then
  53.             table.remove(buttonList,i)
  54.         end
  55.     end
  56.     self = nil
  57. end
  58.  
  59. function detect( x,y,trigger )
  60.     for i = 1,#buttonList do
  61.         if x >= buttonList[i][2] and x <= buttonList[i][2] + #buttonList[i][1] and y >= buttonList[i][3] and y <= buttonList[i][3] + buttonList[i][4] then
  62.             if trigger == true then
  63.                 buttonList[i][5]()
  64.             end
  65.             return buttonList[i][1]
  66.         end
  67.     end
  68. end
Advertisement
Add Comment
Please, Sign In to add comment