CaptainPi

Button API

Jan 22nd, 2017 (edited)
1,771
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.40 KB | None | 0 0
  1. --[[
  2. Used to easily create button terminals with functions. Check https://pastebin.com/iNT0XGqc for an example use
  3.  
  4. Every time I tried to make this an object things got annoying. Still working on improving this.
  5. --]]
  6.  
  7. function new(xPos, yPos, length, height, text, action)
  8.   button = {
  9.     xStart = 1,
  10.     yStart = 1,
  11.     xEnd = 1,
  12.     yEnd = 1,
  13.     text = "",
  14.     action = nil
  15.   }
  16.  
  17.   if not length then
  18.     length = 1
  19.   end
  20.   if not height then
  21.     height = 1
  22.   end
  23.  
  24.   button.xStart = xPos
  25.   button.yStart = yPos
  26.   button.xEnd = xPos + length - 1
  27.   button.yEnd = yPos + height - 1
  28.  
  29.   if text then
  30.     button.text = text
  31.   end
  32.   if action then
  33.     button.action = action
  34.   end
  35.   return button
  36. end
  37.  
  38. function setText(self, text)
  39.   if text then
  40.     self.text = text
  41.   end
  42.   return self
  43. end
  44.  
  45. function draw(self, colour)
  46.   if colour then
  47.     oldColour = term.getBackgroundColour()
  48.     term.setBackgroundColour(colour)
  49.   end
  50.   paintutils.drawFilledBox(self.xStart,self.yStart,self.xEnd,self.yEnd)
  51.   term.setCursorPos(math.ceil(self.xStart + (self.xEnd-self.xStart)/2 - string.len(self.text)/2),   math.floor(   self.yStart+(self.yEnd-self.yStart)/2))
  52.   print(self.text)
  53.   if colour then
  54.     term.setBackgroundColour(oldColour)
  55.   end
  56. end
  57.  
  58. function inBounds(self, xPos, yPos)
  59.   return (xPos >= self.xStart and yPos >= self.yStart and xPos <= self.xEnd and yPos <= self.yEnd)
  60. end
Advertisement
Add Comment
Please, Sign In to add comment