Advertisement
CaptainSpaceCat

ToggleButton API

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