Advertisement
CaptainSpaceCat

Switch API

Jun 1st, 2015
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.42 KB | None | 0 0
  1. Switch = {
  2. name = "",
  3. x = 0,
  4. y = 0,
  5. dir = "",
  6. state = false
  7. }
  8.  
  9. function Switch:new(name, x, y, dir)
  10.   local class = {name = name, x = x, y = y, dir = dir, state = false}
  11.   setmetatable(class, self)
  12.   self.__index = self
  13.   return class
  14. end
  15.  
  16. function Switch:clear()
  17.   term.setBackgroundColor(colors.black)
  18.   term.setTextColor(colors.white)
  19.   if self.dir == "horizontal" then
  20.     term.setCursorPos(self.x - 4, self.y)
  21.     term.write("X       O")
  22.   elseif self.dir == "vertical" then
  23.     term.setCursorPos(self.x, self.y + 4)
  24.     term.write("X")
  25.     term.setCursorPos(self.x, self.y - 4)
  26.     term.write("O")
  27.     for i = -3, 3 do
  28.       term.setCursorPos(self.x, self.y - i)
  29.       term.write(" ")
  30.     end
  31.   end
  32.   term.setBackgroundColor(colors.gray)
  33.   term.setCursorPos(self.x, self.y)
  34.   term.write(" ")
  35. end
  36.  
  37. function Switch:display()
  38.   self:clear()
  39.   term.setBackgroundColor(colors.lightGray)
  40.   if self.dir == "horizontal" then
  41.     if self.state then
  42.       term.setCursorPos(self.x + 1, self.y)
  43.       term.write("   ")
  44.     else
  45.       term.setCursorPos(self.x - 3, self.y)
  46.       term.write("   ")
  47.     end
  48.     term.setBackgroundColor(colors.black)
  49.     term.setTextColor(colors.white)
  50.     term.setCursorPos(self.x - #self.name / 2 + 1, self.y + 1)
  51.     term.write(self.name)
  52.   elseif self.dir == "vertical" then
  53.     if self.state then
  54.       for i = 3, 1, -1 do
  55.         term.setCursorPos(self.x, self.y - i)
  56.         term.write(" ")
  57.       end
  58.     else
  59.       for i = 1, 3 do
  60.         term.setCursorPos(self.x, self.y + i)
  61.         term.write(" ")
  62.       end
  63.     end
  64.     term.setBackgroundColor(colors.black)
  65.     term.setTextColor(colors.white)
  66.     term.setCursorPos(self.x - #self.name / 2 + 1, self.y + 5)
  67.     term.write(self.name)
  68.   end
  69. end
  70.  
  71. function getEvents()
  72.   events = {os.pullEventRaw()}
  73. end
  74.  
  75. local switchClickX = nil
  76. local switchClickY = nil
  77. function Switch:check()
  78.   if events[1] == "mouse_click" and events[2] == 1 then
  79.     switchClickX = events[3]
  80.     switchClickY = events[4]
  81.   end
  82.   if events[1] == "mouse_drag" and events[2] == 1 then
  83.     if self.dir == "horizontal" then
  84.       if self.state then
  85.         if switchClickX > self.x and switchClickX <= self.x + 3 and switchClickY == self.y then
  86.           self.state = false
  87.           self:display()
  88.           return false
  89.         end
  90.       else
  91.         if switchClickX >= self.x - 3 and switchClickX < self.x and switchClickY == self.y then
  92.           self.state = true
  93.           self:display()
  94.           return true
  95.         end
  96.       end
  97.     elseif self.dir == "vertical" then
  98.       if self.state then
  99.         if switchClickX == self.x and switchClickY >= self.y - 3 and switchClickY < self.y then
  100.           self.state = false
  101.           self:display()
  102.           return false
  103.         end
  104.       else
  105.         if switchClickX == self.x and switchClickY > self.y and switchClickY <= self.y + 3 then
  106.           self.state = true
  107.           self:display()
  108.           return true
  109.         end
  110.       end
  111.     end
  112.   end
  113. end
  114.  
  115. --optional display program--
  116. --[[term.setBackgroundColor(colors.black)
  117. term.clear()
  118. Start = Switch:new("Start", 7, 5, "horizontal")
  119. Flappy = Switch:new("Flappy", 17, 12, "vertical")
  120. while true do
  121.   Start:display()
  122.   Flappy:display()
  123.   getEvents()
  124.   if Start:check() then
  125.     break
  126.   end
  127.   if Flappy:check() then
  128.     term.setCursorPos(1, 1)
  129.     term.write("Flip, flap")
  130.     sleep(.2)
  131.     term.clearLine()
  132.   end
  133. end]]--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement