Advertisement
CaptainSpaceCat

Clickables API

Jun 1st, 2015
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.25 KB | None | 0 0
  1. ------------============COMMON FUNCS============------------
  2. events = {}
  3. function getEvents(table)
  4.   if table then
  5.     events = table
  6.   else
  7.     events = {os.pullEvent()}
  8.   end
  9. end
  10.  
  11. iColor = {}
  12. for i = 0, 15 do
  13.   iColor[i + 1] = 2 ^ i
  14. end
  15. ------------============BUTTON API============------------
  16. Button = {
  17. name = "",
  18. x = 0,
  19. y = 0,
  20. x2 = 0,
  21. y2 = 0,
  22. txtcol = 0,
  23. bakcol = 0,
  24. txtcol2 = 0,
  25. bakcol2 = 0
  26. }
  27.  
  28. function Button:new(name, x, y, x2, y2, txtcol, bakcol, txtcol2, bakcol2)
  29.   class = {name = name, x = x, y = y, x2 = x2, y2 = y2, txtcol = txtcol, bakcol = bakcol, txtcol2 = txtcol2, bakcol2 = bakcol2}
  30.   setmetatable(class, self)
  31.   self.__index = self
  32.   return class
  33. end
  34.  
  35. function Button:display()
  36.   term.setTextColor(iColor[self.txtcol])
  37.   paintutils.drawFilledBox(self.x, self.y, self.x2, self.y2, iColor[self.bakcol])
  38.   term.setCursorPos(((self.x2 - self.x) / 2) - (#self.name / 2) + self.x + 1, ((self.y2 - self.y) / 2) + self.y)
  39.   term.write(self.name)
  40. end
  41.  
  42. function Button:activate()
  43.   term.setTextColor(iColor[self.txtcol2])
  44.   paintutils.drawFilledBox(self.x, self.y, self.x2, self.y2, iColor[self.bakcol2])
  45.   term.setCursorPos(((self.x2 - self.x) / 2) - (#self.name / 2) + self.x + 1, ((self.y2 - self.y) / 2) + self.y)
  46.   term.write(self.name)
  47. end
  48.  
  49. function Button:check()
  50.   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
  51.     self:activate()
  52.     sleep(.2)
  53.     return true
  54.   else
  55.     return false
  56.   end
  57. end
  58.  
  59. ------------============TOGGLE API============------------
  60. Toggle = {
  61. name = "",
  62. x = 0,
  63. y = 0,
  64. x2 = 0,
  65. y2 = 0,
  66. txtcol = 0,
  67. bakcol = 0,
  68. txtcol2 = 0,
  69. bakcol2 = 0,
  70. state = false
  71. }
  72.  
  73. function Toggle:new(name, x, y, x2, y2, txtcol, bakcol, txtcol2, bakcol2, state)
  74.   class = {name = name, x = x, y = y, x2 = x2, y2 = y2, txtcol = txtcol, bakcol = bakcol, txtcol2 = txtcol2, bakcol2 = bakcol2, state = state}
  75.   setmetatable(class, self)
  76.   self.__index = self
  77.   return class
  78. end
  79.  
  80. function Toggle:display()
  81.   if self.state == true then
  82.     term.setTextColor(iColor[self.txtcol2])
  83.     paintutils.drawFilledBox(self.x, self.y, self.x2, self.y2, iColor[self.bakcol2])
  84.   else
  85.     term.setTextColor(iColor[self.txtcol])
  86.     paintutils.drawFilledBox(self.x, self.y, self.x2, self.y2, iColor[self.bakcol])
  87.   end
  88.   term.setCursorPos(((self.x2 - self.x) / 2) - (#self.name / 2) + self.x + 1, ((self.y2 - self.y) / 2) + self.y)
  89.   term.write(self.name)
  90. end
  91.  
  92. function Toggle:activate()
  93.   if self.state == true then
  94.     self.state = false
  95.   else
  96.     self.state = true
  97.   end
  98. end
  99.  
  100. function Toggle:check()
  101.   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
  102.     self:activate()
  103.     return self.state
  104.   else
  105.     return self.state
  106.   end
  107. end
  108.  
  109. ------------============SWITCH API============------------
  110. Switch = {
  111. name = "",
  112. x = 0,
  113. y = 0,
  114. dir = "",
  115. state = false
  116. }
  117.  
  118. function Switch:new(name, x, y, dir)
  119.   local class = {name = name, x = x, y = y, dir = dir, state = false}
  120.   setmetatable(class, self)
  121.   self.__index = self
  122.   return class
  123. end
  124.  
  125. function Switch:clear()
  126.   term.setBackgroundColor(colors.black)
  127.   term.setTextColor(colors.white)
  128.   if self.dir == "horizontal" then
  129.     term.setCursorPos(self.x - 4, self.y)
  130.     term.write("X       O")
  131.   elseif self.dir == "vertical" then
  132.     term.setCursorPos(self.x, self.y + 4)
  133.     term.write("X")
  134.     term.setCursorPos(self.x, self.y - 4)
  135.     term.write("O")
  136.     for i = -3, 3 do
  137.       term.setCursorPos(self.x, self.y - i)
  138.       term.write(" ")
  139.     end
  140.   end
  141.   term.setBackgroundColor(colors.gray)
  142.   term.setCursorPos(self.x, self.y)
  143.   term.write(" ")
  144. end
  145.  
  146. function Switch:display()
  147.   self:clear()
  148.   term.setBackgroundColor(colors.lightGray)
  149.   if self.dir == "horizontal" then
  150.     if self.state then
  151.       term.setCursorPos(self.x + 1, self.y)
  152.       term.write("   ")
  153.     else
  154.       term.setCursorPos(self.x - 3, self.y)
  155.       term.write("   ")
  156.     end
  157.     term.setBackgroundColor(colors.black)
  158.     term.setTextColor(colors.white)
  159.     term.setCursorPos(self.x - #self.name / 2 + 1, self.y + 1)
  160.     term.write(self.name)
  161.   elseif self.dir == "vertical" then
  162.     if self.state then
  163.       for i = 3, 1, -1 do
  164.         term.setCursorPos(self.x, self.y - i)
  165.         term.write(" ")
  166.       end
  167.     else
  168.       for i = 1, 3 do
  169.         term.setCursorPos(self.x, self.y + i)
  170.         term.write(" ")
  171.       end
  172.     end
  173.     term.setBackgroundColor(colors.black)
  174.     term.setTextColor(colors.white)
  175.     term.setCursorPos(self.x - #self.name / 2 + 1, self.y + 5)
  176.     term.write(self.name)
  177.   end
  178. end
  179.  
  180. local switchClickX = nil
  181. local switchClickY = nil
  182. function Switch:check()
  183.   if events[1] == "mouse_click" and events[2] == 1 then
  184.     switchClickX = events[3]
  185.     switchClickY = events[4]
  186.   end
  187.   if events[1] == "mouse_drag" and events[2] == 1 then
  188.     if self.dir == "horizontal" then
  189.       if self.state then
  190.         if switchClickX > self.x and switchClickX <= self.x + 3 and switchClickY == self.y then
  191.           self.state = false
  192.           self:display()
  193.           return false
  194.         end
  195.       else
  196.         if switchClickX >= self.x - 3 and switchClickX < self.x and switchClickY == self.y then
  197.           self.state = true
  198.           self:display()
  199.           return true
  200.         end
  201.       end
  202.     elseif self.dir == "vertical" then
  203.       if self.state then
  204.         if switchClickX == self.x and switchClickY >= self.y - 3 and switchClickY < self.y then
  205.           self.state = false
  206.           self:display()
  207.           return false
  208.         end
  209.       else
  210.         if switchClickX == self.x and switchClickY > self.y and switchClickY <= self.y + 3 then
  211.           self.state = true
  212.           self:display()
  213.           return true
  214.         end
  215.       end
  216.     end
  217.   end
  218. end
  219.  
  220. ------------============SLIDER API============------------
  221. Slider = {
  222. name = "",
  223. x = 0,
  224. y = 0,
  225. dir = "",
  226. state = 0,
  227. length = 0
  228. }
  229.  
  230. function Slider:new(name, x, y, dir, length)
  231.   local class = {name = name, x = x, y = y, dir = dir, state = 0, length = length}
  232.   setmetatable(class, self)
  233.   self.__index = self
  234.   return class
  235. end
  236.  
  237. function Slider:clear()
  238.   term.setBackgroundColor(colors.black)
  239.   term.setTextColor(colors.white)
  240.   term.setCursorPos(self.x, self.y)
  241.   if self.dir == "horizontal" then
  242.     for i = 1, self.length * 2 + 1 do
  243.       term.write("-")
  244.     end
  245.     term.setCursorPos(self.x, self.y - 1)
  246.     for i = 0, self.length do
  247.       term.write(tostring(i) .. " ")
  248.     end
  249.     term.setCursorPos(self.x - 1, self.y)
  250.     term.write(" ")
  251.     term.setCursorPos(self.x + self.length * 2 + 1, self.y)
  252.     term.write(" ")
  253.     term.setCursorPos(self.x + self.length - #self.name / 2 + 1, self.y + 1)
  254.     term.write(self.name)
  255.   elseif self.dir == "vertical" then
  256.     term.setCursorPos(self.x, self.y - 1)
  257.     term.write(" ")
  258.     term.setCursorPos(self.x, self.y + self.length * 2 + 1)
  259.     term.write(" ")
  260.     for i = 0, self.length * 2 do
  261.       term.setCursorPos(self.x, self.y + i)
  262.       term.write("|")
  263.     end
  264.     for i = 0, self.length do
  265.       term.setCursorPos(self.x - 1, self.length * 2 + self.y - i * 2)
  266.       term.write(tostring(i))
  267.     end
  268.     term.setCursorPos(self.x - #self.name / 2 + 1, self.y + self.length * 2 + 2)
  269.     term.write(self.name)
  270.   end
  271. end
  272.  
  273. function Slider:display()
  274.   self:clear()
  275.   if self.dir == "horizontal" then
  276.     term.setBackgroundColor(colors.lightGray)
  277.     term.setCursorPos(self.x + self.state - 1, self.y)
  278.     term.write("   ")
  279.     term.setBackgroundColor(colors.gray)
  280.     term.setCursorPos(self.x + self.state, self.y)
  281.     term.write(" ")
  282.   elseif self.dir == "vertical" then
  283.     term.setBackgroundColor(colors.lightGray)
  284.     for i = -1, 1 do
  285.       term.setCursorPos(self.x, self.y + self.length * 2 - self.state + i)
  286.       term.write(" ")
  287.     end
  288.     term.setBackgroundColor(colors.gray)
  289.     term.setCursorPos(self.x, self.y + self.length * 2 - self.state)
  290.     term.write(" ")
  291.   end
  292. end
  293.  
  294. local sliderClickX = nil
  295. local sliderClickY = nil
  296. function Slider:check()
  297.   if events[1] == "mouse_click" and events[2] == 1 then
  298.     sliderClickX = tonumber(events[3])
  299.     sliderClickY = tonumber(events[4])
  300.   end
  301.   if self.dir == "horizontal" and sliderClickX and sliderClickY then
  302.     if sliderClickX >= self.x + self.state - 1 and sliderClickX <= self.x + self.state + 1 and sliderClickY == self.y then
  303.       if events[1] == "mouse_drag" and events[2] == 1 then
  304.         sliderClickX = events[3]
  305.         sliderClickY = events[4]
  306.         if sliderClickX >= self.x and sliderClickX <= self.x + self.length * 2 then
  307.           self.state = sliderClickX - self.x
  308.         end
  309.       end
  310.       self:display()
  311.       return self.state / 2
  312.     end
  313.   elseif self.dir == "vertical" and sliderClickX and sliderClickY then
  314.     if sliderClickY >= self.y + self.length * 2 - self.state - 1 and sliderClickY <= self.y + self.length * 2 - self.state + 1 and sliderClickX == self.x then
  315.       if events[1] == "mouse_drag" and events[2] == 1 then
  316.         sliderClickX = events[3]
  317.         sliderClickY = events[4]
  318.         if sliderClickY >= self.y and sliderClickY <= self.y + self.length * 2 then
  319.           self.state = self.y + self.length * 2 - sliderClickY
  320.         end
  321.       end
  322.       self:display()
  323.       return self.state / 2
  324.     end
  325.   end
  326. end
  327.  
  328. ------------============DIAL API============------------
  329. local dialimg = {}
  330. for i = 1, 4 do
  331.   dialimg[i] = {}
  332. end
  333. dialimg[1][1] = 0
  334. dialimg[4][1] = 0
  335. for i = 1, 4, 3 do
  336.   for n = 2, 4 do
  337.     dialimg[i][n] = 128
  338.   end
  339. end
  340. dialimg[1][1] = 0
  341. dialimg[1][2] = 128
  342. dialimg[1][3] = 128
  343. dialimg[1][4] = 128
  344. dialimg[1][5] = 0
  345. for i = 2, 3 do
  346.   for n = 1, 5 do
  347.     dialimg[i][n] = 128
  348.   end
  349. end
  350.  
  351. Dial = {
  352. name = "",
  353. xImg = 0,
  354. yImg = 0,
  355. x = 0,
  356. y = 0,
  357. state = 1,
  358. mark = ""
  359. }
  360.  
  361. function Dial:new(name, xImg, yImg)
  362.   local class = {name = name, xImg = xImg, yImg = yImg, x = xImg, y = yImg + 3, state = 1, mark = "/"}
  363.   setmetatable(class, self)
  364.   self.__index = self
  365.   return class
  366. end
  367.  
  368. function Dial:clear()
  369.   term.setBackgroundColor(colors.black)
  370.   term.setTextColor(colors.white)
  371.   term.setCursorPos(self.xImg - 1, self.yImg + 4)
  372.   term.write("1")
  373.   term.setCursorPos(self.xImg - 2, self.yImg + 1)
  374.   term.write("2")
  375.   term.setCursorPos(self.xImg - 1, self.yImg - 1)
  376.   term.write("3")
  377.   term.setCursorPos(self.xImg + 2, self.yImg - 2)
  378.   term.write("4")
  379.   term.setCursorPos(self.xImg + 5, self.yImg - 1)
  380.   term.write("5")
  381.   term.setCursorPos(self.xImg + 6, self.yImg + 1)
  382.   term.write("6")
  383.   term.setCursorPos(self.xImg + 5, self.yImg + 4)
  384.   term.write("7")
  385.   term.setCursorPos(self.xImg, self.yImg + 3)
  386.   term.write(" ")
  387.   term.setCursorPos(self.xImg - 1, self.yImg + 2)
  388.   term.write(" ")
  389.   term.setCursorPos(self.xImg - 1, self.yImg + 1)
  390.   term.write(" ")
  391.   term.setCursorPos(self.xImg, self.yImg)
  392.   term.write(" ")
  393.   term.setCursorPos(self.xImg + 1, self.yImg - 1)
  394.   term.write(" ")
  395.   term.setCursorPos(self.xImg + 2, self.yImg - 1)
  396.   term.write(" ")
  397.   term.setCursorPos(self.xImg + 3, self.yImg - 1)
  398.   term.write(" ")
  399.   term.setCursorPos(self.xImg + 4, self.yImg)
  400.   term.write(" ")
  401.   term.setCursorPos(self.xImg + 5, self.yImg + 1)
  402.   term.write(" ")
  403.   term.setCursorPos(self.xImg + 5, self.yImg + 2)
  404.   term.write(" ")
  405.   term.setCursorPos(self.xImg + 4, self.yImg + 3)
  406.   term.write(" ")
  407. end
  408.  
  409. function Dial:display()
  410.   paintutils.drawImage(dialimg, self.xImg, self.yImg)
  411.   self:clear()
  412.   term.setBackgroundColor(colors.lightGray)
  413.   term.setCursorPos(self.x, self.y)
  414.   term.write(self.mark)
  415.   term.setCursorPos(self.xImg + 2.5 - (#self.name / 2), self.yImg + 5)
  416.   term.setBackgroundColor(colors.black)
  417.   term.write(self.name)
  418. end
  419.  
  420. local clickDialX = nil
  421. local clickDialY = nil
  422. local begin = false
  423. function Dial:check()
  424.   if events[1] == "mouse_click" and events[2] == 1 then
  425.     clickDialX = tonumber(events[3])
  426.     clickDialY = tonumber(events[4])
  427.   end
  428.   if clickDialX and clickDialY and clickDialX >= self.x - 1 and clickDialX <= self.x + 1 and clickDialY >= self.y - 1 and clickDialY <= self.y + 1 then
  429.     begin = true
  430.     if events[1] == "mouse_drag" and events[2] == 1 then
  431.       clickDialX = events[3]
  432.       clickDialY = events[4]
  433.     end
  434.   else
  435.     begin = false
  436.   end
  437.   if begin and clickDialX and clickDialY then
  438.     if clickDialX <= self.xImg and clickDialY >= self.yImg + 3 then --1
  439.       self.x, self.y = self.xImg, self.yImg + 3
  440.       self.state = 1
  441.       self.mark = "/"
  442.       self:display()
  443.       return 1
  444.     elseif clickDialX <= self.xImg - 1 and clickDialY == self.yImg + 2 then --2
  445.       self.x, self.y = self.xImg - 1, self.yImg + 2
  446.       self.state = 2
  447.       self.mark = "-"
  448.     elseif clickDialX <= self.xImg - 1 and clickDialY >= self.yImg + 1 then --3
  449.       self.x, self.y = self.xImg - 1, self.yImg + 1
  450.       self.state = 3
  451.       self.mark = "-"
  452.       self:display()
  453.       return 2
  454.     elseif clickDialX <= self.xImg and clickDialY <= self.yImg then --4
  455.       self.x, self.y = self.xImg, self.yImg
  456.       self.state = 4
  457.       self.mark = "\\"
  458.       self:display()
  459.       return 3
  460.    elseif clickDialX == self.xImg + 1 and clickDialY <= self.yImg - 1 then --5
  461.       self.x, self.y = self.xImg + 1, self.yImg - 1
  462.       self.state = 5
  463.       self.mark = "|"
  464.     elseif clickDialX == self.xImg + 2 and clickDialY <= self.yImg - 1 then --6
  465.       self.x, self.y = self.xImg + 2, self.yImg - 1
  466.       self.state = 6
  467.       self.mark = "|"
  468.       self:display()
  469.       return 4
  470.     elseif clickDialX == self.xImg + 3 and clickDialY <= self.yImg - 1 then --7
  471.       self.x, self.y = self.xImg + 3, self.yImg - 1
  472.       self.state = 7
  473.       self.mark = "|"
  474.     elseif clickDialX >= self.xImg + 4 and clickDialY <= self.yImg then --8
  475.       self.x, self.y = self.xImg + 4, self.yImg
  476.       self.state = 8
  477.       self.mark = "/"
  478.       self:display()
  479.       return 5
  480.     elseif clickDialX >= self.xImg + 5 and clickDialY == self.yImg + 1 then --9
  481.       self.x, self.y = self.xImg + 5, self.yImg + 1
  482.       self.state = 9
  483.       self.mark = "-"
  484.       self:display()
  485.       return 6
  486.     elseif clickDialX >= self.xImg + 5 and clickDialY == self.yImg + 2 then --10
  487.       self.x, self.y = self.xImg + 5, self.yImg + 2
  488.       self.state = 10
  489.       self.mark = "-"
  490.     elseif clickDialX >= self.xImg + 4 and clickDialY >= self.yImg + 3 then --11
  491.       self.x, self.y = self.xImg + 4, self.yImg + 3
  492.       self.state = 11
  493.       self.mark = "\\"
  494.       self:display()
  495.       return 7
  496.     end
  497.   end
  498.   self:display()
  499. end
  500.  
  501. ------------optional display program------------
  502. --[[term.setBackgroundColor(colors.black)
  503. term.clear()
  504. Spin = Dial:new("Spin", 3, 3)
  505. SlideX = Slider:new("SlideX", 2, 11, "horizontal", 4)
  506. SlideY = Slider:new("SlideY", 16, 2, "vertical", 6)
  507. Flip = Switch:new("Flip", 6, 16, "horizontal")
  508. Flap = Switch:new("Flap", 22, 7, "vertical")
  509. OnOff = Toggle:new("Onoff", 30, 2, 34, 5, 2, 15, 5, 4, false)
  510. Push = Button:new("Push", 27, 12, 33, 14, 1, 6, 16, 15)
  511. while true do
  512.   Spin:display()
  513.   SlideX:display()
  514.   SlideY:display()
  515.   Flip:display()
  516.   Flap:display()
  517.   OnOff:display()
  518.   Push:display()
  519.   getEvents()
  520.   Spin:check()
  521.   SlideX:check()
  522.   SlideY:check()
  523.   Flip:check()
  524.   Flap:check()
  525.   OnOff:check()
  526.   Push:check()
  527. end]]--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement