Advertisement
Redxone

RedButton - ButtonAPI

Sep 6th, 2015
986
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.45 KB | None | 0 0
  1. -- Easy term access --
  2. easyterms = {
  3.   sbc = term.setBackgroundColor,
  4.   stc = term.setTextColor,
  5.   scp = term.setCursorPos,
  6. }
  7.  
  8. -- Create button method --
  9.  
  10. function addButton(x,y,width,height,
  11.                    textcolor, backcolor, text)
  12.   local button = {
  13.  
  14.     x = x,
  15.     y = y,
  16.     w = width,
  17.     h = height,
  18.     text = text,
  19.     tcol = textcolor,
  20.     bcol = backcolor,
  21.     sbc = easyterms.sbc,
  22.     scp = easyterms.scp,
  23.     stc = easyterms.stc,
  24.     s_width, s_height = term.getSize(),
  25.    
  26.     -- Draw Button
  27.       draw  = function(self)
  28.        -- loop through height and draw by width
  29.        
  30.         for h = 0, self.h do
  31.             self.sbc(self.bcol)
  32.             self.scp(self.x,self.y+h)
  33.             write(string.rep(" ",self.w))
  34.         end
  35.         -- draw the text centered
  36.           self.scp( ((self.w/2 + self.x))
  37.                       - #text/2,
  38.                     ((self.h/2) + (self.y))
  39.                      
  40.           )
  41.             self.stc(self.tcol)
  42.             write(self.text)
  43.        
  44.       end,
  45.  
  46.       rename = function(self,newname)
  47.           self.text = newname
  48.           self:draw()
  49.       end,    
  50.  
  51.       clear = function(self,color)
  52.         for nh = 0, self.h do
  53.           self.sbc(color)
  54.           self.scp(self.x,self.y+nh)
  55.           write(string.rep(" ",self.w))
  56.         end
  57.       end,
  58.      
  59.       setPos = function(self,newx, newy,clearcolor)
  60.         self:clear(clearcolor)
  61.         self.x = newx
  62.         self.y = newy
  63.         self:draw()
  64.       end,
  65.      
  66.       colorize = function(self,tcolor,bcolor)
  67.         self.tcol = tcolor
  68.         self.bcol = bcolor
  69.         self:draw()
  70.       end,
  71.      
  72.       -- check if button was clicked --
  73.      
  74.       pressed = function(self,event)
  75.         if(event[1] == "mouse_click")then
  76.        
  77.        
  78.         -- store event information
  79.             local mx = event[3]
  80.             local my = event[4]
  81.             local mb = event[2]    
  82.        
  83.         -- check if the button as clicked
  84.             if(mb == 1 and mx >= self.x
  85.                and mx <= (self.w + self.x)
  86.                and my >= self.y
  87.                and my <= (self.y + self.h))then              
  88.                return true
  89.             end
  90.        
  91.         else
  92.         -- if the event wasnt mouse related
  93.           return false
  94.         end
  95.       end
  96.      
  97.      
  98.   }
  99.  
  100.     -- add ofcourse what would be the point
  101.     -- if we didnt return the sucker?
  102.    
  103.     return button
  104.  
  105. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement