Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Switch = {
- name = "",
- x = 0,
- y = 0,
- dir = "",
- state = false
- }
- function Switch:new(name, x, y, dir)
- local class = {name = name, x = x, y = y, dir = dir, state = false}
- setmetatable(class, self)
- self.__index = self
- return class
- end
- function Switch:clear()
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- if self.dir == "horizontal" then
- term.setCursorPos(self.x - 4, self.y)
- term.write("X O")
- elseif self.dir == "vertical" then
- term.setCursorPos(self.x, self.y + 4)
- term.write("X")
- term.setCursorPos(self.x, self.y - 4)
- term.write("O")
- for i = -3, 3 do
- term.setCursorPos(self.x, self.y - i)
- term.write(" ")
- end
- end
- term.setBackgroundColor(colors.gray)
- term.setCursorPos(self.x, self.y)
- term.write(" ")
- end
- function Switch:display()
- self:clear()
- term.setBackgroundColor(colors.lightGray)
- if self.dir == "horizontal" then
- if self.state then
- term.setCursorPos(self.x + 1, self.y)
- term.write(" ")
- else
- term.setCursorPos(self.x - 3, self.y)
- term.write(" ")
- end
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.setCursorPos(self.x - #self.name / 2 + 1, self.y + 1)
- term.write(self.name)
- elseif self.dir == "vertical" then
- if self.state then
- for i = 3, 1, -1 do
- term.setCursorPos(self.x, self.y - i)
- term.write(" ")
- end
- else
- for i = 1, 3 do
- term.setCursorPos(self.x, self.y + i)
- term.write(" ")
- end
- end
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.setCursorPos(self.x - #self.name / 2 + 1, self.y + 5)
- term.write(self.name)
- end
- end
- function getEvents()
- events = {os.pullEventRaw()}
- end
- local switchClickX = nil
- local switchClickY = nil
- function Switch:check()
- if events[1] == "mouse_click" and events[2] == 1 then
- switchClickX = events[3]
- switchClickY = events[4]
- end
- if events[1] == "mouse_drag" and events[2] == 1 then
- if self.dir == "horizontal" then
- if self.state then
- if switchClickX > self.x and switchClickX <= self.x + 3 and switchClickY == self.y then
- self.state = false
- self:display()
- return false
- end
- else
- if switchClickX >= self.x - 3 and switchClickX < self.x and switchClickY == self.y then
- self.state = true
- self:display()
- return true
- end
- end
- elseif self.dir == "vertical" then
- if self.state then
- if switchClickX == self.x and switchClickY >= self.y - 3 and switchClickY < self.y then
- self.state = false
- self:display()
- return false
- end
- else
- if switchClickX == self.x and switchClickY > self.y and switchClickY <= self.y + 3 then
- self.state = true
- self:display()
- return true
- end
- end
- end
- end
- end
- --optional display program--
- --[[term.setBackgroundColor(colors.black)
- term.clear()
- Start = Switch:new("Start", 7, 5, "horizontal")
- Flappy = Switch:new("Flappy", 17, 12, "vertical")
- while true do
- Start:display()
- Flappy:display()
- getEvents()
- if Start:check() then
- break
- end
- if Flappy:check() then
- term.setCursorPos(1, 1)
- term.write("Flip, flap")
- sleep(.2)
- term.clearLine()
- end
- end]]--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement