Advertisement
Guest User

button.lua

a guest
Jun 25th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.58 KB | None | 0 0
  1. Button = {}
  2. Button.__index = Button
  3.  
  4. setmetatable(Button, {
  5.     __index = Object, -- this makes Button inherited from object
  6.     __call = function(cls,...)
  7.     local self = setmetatable({},cls)
  8.     self:_init(...)
  9.     return self
  10.     end,
  11. })
  12.  
  13. function Button:_init(x,y,w,h,color,activeColor)
  14.     Object._init(self,x,y,w,h,color)
  15.     Hooks[index] = self
  16.     self.activeColor = activeColor and activeColor or color
  17.     self.hook = "monitor_touch"
  18.     return self
  19. end
  20.  
  21. function Button:hooked(...) -- called when an element that registers itself in the hook table's event gets called
  22.     local x = arg[3]
  23.     local y = arg[4]
  24.     if x >= self.pos.x and x<= self.pos.x +self.size.x and
  25.             y >= self.pos.y and y<= self.pos.y +self.size.y then
  26.         self:onButtonPressed(x,y)
  27.         self.activated = true
  28.         return true
  29.     else
  30.         return false
  31.     end
  32. end  
  33.  
  34. function Button:onButtonPressed(x,y)
  35.     --I "think" the user should be able to do:
  36.     --function MyButton:onButtonPressed()
  37.     --  what ever they want to do
  38.     --end
  39.     --so that when the button is pressed i call this function, which should call their function
  40.     -- because they overrode it
  41. end
  42.  
  43.  
  44. function Button:render(Term)
  45.     local color = self.activated and self.activeColor or self.color
  46.     for Y=1,self.size.y do
  47.         for X=1,self.size.x do
  48.             Term.setCursorPos(self.pos.x+X-1,self.pos.y+Y-1)
  49.             Term.setTextColor(color)
  50.             Term.setBackgroundColor(color)
  51.             Term.write(" ")
  52.         end
  53.     end
  54.     self.activated = false
  55. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement