Advertisement
CaptainSpaceCat

Dial API

Jun 1st, 2015
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.35 KB | None | 0 0
  1. local dialimg = {}
  2. for i = 1, 4 do
  3.   dialimg[i] = {}
  4. end
  5. dialimg[1][1] = 0
  6. dialimg[4][1] = 0
  7. for i = 1, 4, 3 do
  8.   for n = 2, 4 do
  9.     dialimg[i][n] = 128
  10.   end
  11. end
  12. dialimg[1][1] = 0
  13. dialimg[1][2] = 128
  14. dialimg[1][3] = 128
  15. dialimg[1][4] = 128
  16. dialimg[1][5] = 0
  17. for i = 2, 3 do
  18.   for n = 1, 5 do
  19.     dialimg[i][n] = 128
  20.   end
  21. end
  22.  
  23. Dial = {
  24. name = "",
  25. xImg = 0,
  26. yImg = 0,
  27. x = 0,
  28. y = 0,
  29. state = 1,
  30. mark = ""
  31. }
  32.  
  33. function Dial:new(name, xImg, yImg)
  34.   local class = {name = name, xImg = xImg, yImg = yImg, x = xImg, y = yImg + 3, state = 1, mark = "/"}
  35.   setmetatable(class, self)
  36.   self.__index = self
  37.   return class
  38. end
  39.  
  40. function Dial:clear()
  41.   term.setBackgroundColor(colors.black)
  42.   term.setCursorPos(self.xImg - 1, self.yImg + 4)
  43.   term.write("1")
  44.   term.setCursorPos(self.xImg - 2, self.yImg + 1)
  45.   term.write("2")
  46.   term.setCursorPos(self.xImg - 1, self.yImg - 1)
  47.   term.write("3")
  48.   term.setCursorPos(self.xImg + 2, self.yImg - 2)
  49.   term.write("4")
  50.   term.setCursorPos(self.xImg + 5, self.yImg - 1)
  51.   term.write("5")
  52.   term.setCursorPos(self.xImg + 6, self.yImg + 1)
  53.   term.write("6")
  54.   term.setCursorPos(self.xImg + 5, self.yImg + 4)
  55.   term.write("7")
  56.   term.setCursorPos(self.xImg, self.yImg + 3)
  57.   term.write(" ")
  58.   term.setCursorPos(self.xImg - 1, self.yImg + 2)
  59.   term.write(" ")
  60.   term.setCursorPos(self.xImg - 1, self.yImg + 1)
  61.   term.write(" ")
  62.   term.setCursorPos(self.xImg, self.yImg)
  63.   term.write(" ")
  64.   term.setCursorPos(self.xImg + 1, self.yImg - 1)
  65.   term.write(" ")
  66.   term.setCursorPos(self.xImg + 2, self.yImg - 1)
  67.   term.write(" ")
  68.   term.setCursorPos(self.xImg + 3, self.yImg - 1)
  69.   term.write(" ")
  70.   term.setCursorPos(self.xImg + 4, self.yImg)
  71.   term.write(" ")
  72.   term.setCursorPos(self.xImg + 5, self.yImg + 1)
  73.   term.write(" ")
  74.   term.setCursorPos(self.xImg + 5, self.yImg + 2)
  75.   term.write(" ")
  76.   term.setCursorPos(self.xImg + 4, self.yImg + 3)
  77.   term.write(" ")
  78. end
  79.  
  80. function Dial:display()
  81.   paintutils.drawImage(dialimg, self.xImg, self.yImg)
  82.   self:clear()
  83.   term.setBackgroundColor(colors.lightGray)
  84.   term.setCursorPos(self.x, self.y)
  85.   term.write(self.mark)
  86.   term.setCursorPos(self.xImg + 2.5 - (#self.name / 2), self.yImg + 5)
  87.   term.setBackgroundColor(colors.black)
  88.   term.write(self.name)
  89. end
  90.  
  91. function getEvents()
  92.   events = {os.pullEventRaw()}
  93. end
  94.  
  95.  
  96. local clickX = nil
  97. local clickY = nil
  98. local begin = false
  99. function Dial:check()
  100.   if events[1] == "mouse_click" and events[2] == 1 then
  101.     clickX = events[3]
  102.     clickY = events[4]
  103.   end
  104.   if clickX >= self.x - 1 and clickX <= self.x + 1 and clickY >= self.y - 1 and clickY <= self.y + 1 then
  105.     begin = true
  106.     if events[1] == "mouse_drag" and events[2] == 1 then
  107.       clickX = events[3]
  108.       clickY = events[4]
  109.     end
  110.   else
  111.     begin = false
  112.   end
  113.   if begin then
  114.     if events[3] <= self.xImg and events[4] >= self.yImg + 3 then --1
  115.       self.x, self.y = self.xImg, self.yImg + 3
  116.       self.state = 1
  117.       self.mark = "/"
  118.       self:display()
  119.       return 1
  120.     elseif events[3] <= self.xImg - 1 and events[4] == self.yImg + 2 then --2
  121.       self.x, self.y = self.xImg - 1, self.yImg + 2
  122.       self.state = 2
  123.       self.mark = "-"
  124.     elseif events[3] <= self.xImg - 1 and events[4] >= self.yImg + 1 then --3
  125.       self.x, self.y = self.xImg - 1, self.yImg + 1
  126.       self.state = 3
  127.       self.mark = "-"
  128.       self:display()
  129.       return 2
  130.     elseif events[3] <= self.xImg and events[4] <= self.yImg then --4
  131.       self.x, self.y = self.xImg, self.yImg
  132.       self.state = 4
  133.       self.mark = "\\"
  134.       self:display()
  135.       return 3
  136.    elseif events[3] == self.xImg + 1 and events[4] <= self.yImg - 1 then --5
  137.       self.x, self.y = self.xImg + 1, self.yImg - 1
  138.       self.state = 5
  139.       self.mark = "|"
  140.     elseif events[3] == self.xImg + 2 and events[4] <= self.yImg - 1 then --6
  141.       self.x, self.y = self.xImg + 2, self.yImg - 1
  142.       self.state = 6
  143.       self.mark = "|"
  144.       self:display()
  145.       return 4
  146.     elseif events[3] == self.xImg + 3 and events[4] <= self.yImg - 1 then --7
  147.       self.x, self.y = self.xImg + 3, self.yImg - 1
  148.       self.state = 7
  149.       self.mark = "|"
  150.     elseif events[3] >= self.xImg + 4 and events[4] <= self.yImg then --8
  151.       self.x, self.y = self.xImg + 4, self.yImg
  152.       self.state = 8
  153.       self.mark = "/"
  154.       self:display()
  155.       return 5
  156.     elseif events[3] >= self.xImg + 5 and events[4] == self.yImg + 1 then --9
  157.       self.x, self.y = self.xImg + 5, self.yImg + 1
  158.       self.state = 9
  159.       self.mark = "-"
  160.       self:display()
  161.       return 6
  162.     elseif events[3] >= self.xImg + 5 and events[4] == self.yImg + 2 then --10
  163.       self.x, self.y = self.xImg + 5, self.yImg + 2
  164.       self.state = 10
  165.       self.mark = "-"
  166.     elseif events[3] >= self.xImg + 4 and events[4] >= self.yImg + 3 then --11
  167.       self.x, self.y = self.xImg + 4, self.yImg + 3
  168.       self.state = 11
  169.       self.mark = "\\"
  170.       self:display()
  171.       return 7
  172.     end
  173.   end
  174.   self:display()
  175. end
  176.  
  177. --optional display program--
  178. --[[term.setBackgroundColor(colors.black)
  179. term.clear()
  180. Start = Dial:new("Start", 10, 5)
  181. Octy = Dial:new("Octy", 23, 12)
  182. while true do
  183.   Start:display()
  184.   Octy:display()
  185.   getEvents()
  186.   if Start:check() == 7 then
  187.     break
  188.   end
  189.   if Octy:check() then
  190.     term.setCursorPos(1, 1)
  191.     term.clearLine()
  192.     term.write(Octy.state)
  193.   end
  194. end]]--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement