Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Slider = {
- name = "",
- x = 0,
- y = 0,
- dir = "",
- state = 0,
- length = 0
- }
- function Slider:new(name, x, y, dir, length)
- local class = {name = name, x = x, y = y, dir = dir, state = 0, length = length}
- setmetatable(class, self)
- self.__index = self
- return class
- end
- function Slider:clear()
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.setCursorPos(self.x, self.y)
- if self.dir == "horizontal" then
- for i = 1, self.length * 2 + 1 do
- term.write("-")
- end
- term.setCursorPos(self.x, self.y - 1)
- for i = 0, self.length do
- term.write(tostring(i) .. " ")
- end
- term.setCursorPos(self.x - 1, self.y)
- term.write(" ")
- term.setCursorPos(self.x + self.length * 2 + 1, self.y)
- term.write(" ")
- term.setCursorPos(self.x + self.length - #self.name / 2 + 1, self.y + 1)
- term.write(self.name)
- elseif self.dir == "vertical" then
- term.setCursorPos(self.x, self.y - 1)
- term.write(" ")
- term.setCursorPos(self.x, self.y + self.length * 2 + 1)
- term.write(" ")
- for i = 0, self.length * 2 do
- term.setCursorPos(self.x, self.y + i)
- term.write("|")
- end
- for i = 0, self.length do
- term.setCursorPos(self.x - 1, self.length * 2 + self.y - i * 2)
- term.write(tostring(i))
- end
- term.setCursorPos(self.x - #self.name / 2 + 1, self.y + self.length * 2 + 2)
- term.write(self.name)
- end
- end
- function Slider:display()
- self:clear()
- if self.dir == "horizontal" then
- term.setBackgroundColor(colors.lightGray)
- term.setCursorPos(self.x + self.state - 1, self.y)
- term.write(" ")
- term.setBackgroundColor(colors.gray)
- term.setCursorPos(self.x + self.state, self.y)
- term.write(" ")
- elseif self.dir == "vertical" then
- term.setBackgroundColor(colors.lightGray)
- for i = -1, 1 do
- term.setCursorPos(self.x, self.y + self.length * 2 - self.state + i)
- term.write(" ")
- end
- term.setBackgroundColor(colors.gray)
- term.setCursorPos(self.x, self.y + self.length * 2 - self.state)
- term.write(" ")
- end
- end
- function getEvents()
- events = {os.pullEventRaw()}
- end
- local sliderClickX = nil
- local sliderClickY = nil
- function Slider:check()
- if events[1] == "mouse_click" and events[2] == 1 then
- sliderClickX = events[3]
- sliderClickY = events[4]
- end
- if self.dir == "horizontal" then
- if sliderClickX >= self.x + self.state - 1 and sliderClickX <= self.x + self.state + 1 and sliderClickY == self.y then
- if events[1] == "mouse_drag" and events[2] == 1 then
- sliderClickX = events[3]
- sliderClickY = events[4]
- if sliderClickX >= self.x and sliderClickX <= self.x + self.length * 2 then
- self.state = sliderClickX - self.x
- end
- end
- self:display()
- return self.state / 2
- end
- elseif self.dir == "vertical" then
- 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
- if events[1] == "mouse_drag" and events[2] == 1 then
- sliderClickX = events[3]
- sliderClickY = events[4]
- if sliderClickY >= self.y and sliderClickY <= self.y + self.length * 2 then
- self.state = self.y + self.length * 2 - sliderClickY
- end
- end
- self:display()
- return self.state / 2
- end
- end
- end
- --optional display program--
- --[[term.setBackgroundColor(colors.black)
- term.clear()
- Start = Slider:new("Start", 3, 5, "horizontal", 3)
- Slide = Slider:new("Slide", 24, 2, "vertical", 5)
- while true do
- Start:display()
- Slide:display()
- getEvents()
- if Start:check() == 3 then
- break
- end
- if Slide:check() then
- term.setCursorPos(1, 1)
- term.setBackgroundColor(colors.black)
- term.clearLine()
- term.write(Slide.state / 2)
- end
- end]]--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement