Advertisement
tobast

button api

Jun 26th, 2014
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.31 KB | None | 0 0
  1.  
  2. os.loadAPI('/lib/slot_sig')
  3.  
  4. function Button(side,x,y,text,cb,bg,fg)
  5.     if fg == nil then
  6.         fg = colors.black
  7.     end
  8.     if bg == nil then
  9.         fg = colors.white
  10.     end
  11.     obj = {
  12.         side=side,
  13.         x=x,
  14.         y=y,
  15.         text=text,
  16.         bgColor=bg,
  17.         fgColor=fg,
  18.         callback=cb
  19.     }
  20.    
  21.     if(side=='term') then
  22.         obj.device = term
  23.     else
  24.         obj.device = peripheral.wrap(side)
  25.     end
  26.  
  27.     function obj:draw()
  28.         self.device.setCursorPos(self.x, self.y)
  29.         self.device.setTextColor(self.fgColor)
  30.         self.device.setBackgroundColor(self.bgColor)
  31.  
  32.         self.device.write(self.text)
  33.  
  34.         self.device.setTextColor(colors.white)
  35.         self.device.setBackgroundColor(colors.black)
  36.     end
  37.  
  38.     function obj:setText(nText)
  39.         self.text = nText
  40.         self:draw()
  41.     end
  42.     function obj:setBgColor(color)
  43.         self.bgColor=color
  44.         self:draw()
  45.     end
  46.     function obj:setFgColor(color)
  47.         self.fgColor = color
  48.         self:draw()
  49.     end
  50.  
  51.     function obj:onTermClick(data)
  52.         --TODO
  53.     end
  54.     function obj:onClick(data)
  55.         if(data[1] == self.side) then
  56.             x = data[2]
  57.             y = data[3]
  58.             if (x >= self.x and y == self.y and x < (self.x + string.len(self.text))) then
  59.                 self.callback(data)
  60.             end
  61.         end
  62.     end
  63.    
  64.     if(device == term) then
  65.         slot_sig.connectSlot('mouse_click', obj.onTermClick, obj)
  66.     else
  67.         slot_sig.connectSlot('monitor_touch', obj.onClick, obj)
  68.     end
  69.  
  70.     return obj
  71. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement