Advertisement
CaptainSpaceCat

Slider API

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