Advertisement
CaptainSpaceCat

Button API

May 31st, 2015
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.99 KB | None | 0 0
  1. Button = {
  2. name = "",
  3. x = 0,
  4. y = 0,
  5. x2 = 0,
  6. y2 = 0,
  7. txtcol = 0,
  8. bakcol = 0,
  9. txtcol2 = 0,
  10. bakcol2 = 0
  11. }
  12.  
  13. iColor = {}
  14. for i = 0, 15 do
  15.   iColor[i + 1] = 2 ^ i
  16. end
  17.  
  18. function Button:new(name, x, y, x2, y2, txtcol, bakcol, txtcol2, bakcol2)
  19.   class = {name = name, x = x, y = y, x2 = x2, y2 = y2, txtcol = txtcol, bakcol = bakcol, txtcol2 = txtcol2, bakcol2 = bakcol2}
  20.   setmetatable(class, self)
  21.   self.__index = self
  22.   return class
  23. end
  24.  
  25. function Button:display()
  26.   term.setTextColor(iColor[self.txtcol])
  27.   paintutils.drawFilledBox(self.x, self.y, self.x2, self.y2, iColor[self.bakcol])
  28.   term.setCursorPos(((self.x2 - self.x) / 2) - (#self.name / 2) + self.x + 1, ((self.y2 - self.y) / 2) + self.y)
  29.   term.write(self.name)
  30. end
  31.  
  32. function Button:activate()
  33.   term.setTextColor(iColor[self.txtcol2])
  34.   paintutils.drawFilledBox(self.x, self.y, self.x2, self.y2, iColor[self.bakcol2])
  35.   term.setCursorPos(((self.x2 - self.x) / 2) - (#self.name / 2) + self.x + 1, ((self.y2 - self.y) / 2) + self.y)
  36.   term.write(self.name)
  37. end
  38.  
  39. function getEvents()
  40.   events = {os.pullEvent()}
  41. end
  42.  
  43. function Button:check()
  44.   if events[1] == "mouse_click" and events[2] == 1 and events[3] >= self.x and events[3] <= self.x2 and events[4] >= self.y and events[4] <= self.y2 then
  45.     self:activate()
  46.     sleep(.2)
  47.     return true
  48.   else
  49.     return false
  50.   end
  51. end
  52.  
  53. --otional display program--
  54. --[[term.setBackgroundColor(colors.black)
  55. term.clear()
  56. Start = Button:new("Start", 10, 5, 17, 7, 8, 10, 2, 1)
  57. Quit = Button:new("Quit", 19, 10, 24, 11, 15, 2, 12, 5)
  58. Banana = Button:new("Banana", 30, 2, 39, 5, 13, 5, 2, 11)
  59. while true do
  60.   Start:display()
  61.   Quit:display()
  62.   Banana:display()
  63.   events = {os.pullEventRaw()}
  64.   if Start:check() then
  65.     term.setCursorPos(1, 1)
  66.     term.write("Started")
  67.   end
  68.   if Quit:check() then
  69.     break
  70.   end
  71.   if Banana:check() then
  72.     term.setCursorPos(1, 1)
  73.     term.write("yum")
  74.     term.setBackgroundColor(colors.black)
  75.     term.write("    ")
  76.   end
  77. end]]--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement