Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --By Willa_/WillaRL_
- local event = require("event")
- local process = require("process")
- local component = require("component")
- local gpu = component.gpu
- local buttonAPI = {}
- buttonAPI.__index = buttonAPI
- setmetatable(buttonAPI, {__call = function(cls,...) return cls.new(...) end,})
- local buttonAPIarr = {}
- buttonAPIarr.__index = buttonAPIarr
- setmetatable(buttonAPIarr, {__call = function(cls,...) return cls.new(...) end,})
- --------------------------
- --START CLASS buttonAPIarr
- --------------------------
- function buttonAPIarr.new()
- local self = setmetatable({}, buttonAPIarr)
- self.buttons = {}
- return self
- end
- function buttonAPIarr:add(label, behaviour, x2, x1, y2, y1, colourInit, colourClked, length, func)
- local newButton = buttonAPI(label, behaviour, x2, x1, y2, y1, colourInit, colourClked, length, func)
- table.insert(self.buttons, newButton)
- end
- function buttonAPIarr:click(x,y)
- for i, button in ipairs(self.buttons) do
- button:click(x,y)
- end
- end
- ------------------------
- --END CLASS buttonAPIarr
- ------------------------
- -----------------------
- --START CLASS buttonAPI
- -----------------------
- function buttonAPI.new(label, behaviour, x2, x1, y2, y1, colourInit, colourClked, length, func)
- local self = setmetatable({}, buttonAPI)
- self.xMin = x1
- self.yMin = y1
- self.xMax = x2
- self.yMax = y2
- self.colourInit = colourInit
- self.colourClked = colourClked
- self.label = label
- self.behaviour = behaviour
- self.length = length
- self.func = func
- self.clicked = false
- self:draw()
- return self
- end
- function buttonAPI:draw()
- if(self.clicked) then
- self:__draw(self.colourClked)
- else
- self:__draw(self.colourInit)
- end
- end
- function buttonAPI:__draw(colour)
- local oldColour = gpu.setBackground(colour, false)
- gpu.fill(self.xMin,self.yMin,self.xMax-self.xMin,self.yMax-self.yMin, " ")
- local midX = math.ceil( (self.xMax - self.xMin-1)/2) + self.xMin
- local midY = math.floor((self.yMax - self.yMin-1)/2) + self.yMin
- local labelLen = string.len(self.label)
- local labelMid = math.ceil(labelLen/2)
- local startPoint = midX-labelLen+labelMid
- gpu.set(startPoint,midY,self.label)
- gpu.setBackground(oldColour)
- end
- function buttonAPI:check(x,y)
- if(x >= self.xMin and x <= (self.xMax - 1)) and (y >= self.yMin and y <= (self.yMax - 1)) then
- return true
- else
- return false
- end
- end
- function buttonAPI:clickToggle()
- if (self.clicked) then
- self.clicked = false
- self:draw()
- return true
- else
- self.clicked = true
- self:draw()
- return true
- end
- end
- function buttonAPI:clickPulse()
- self.clicked = true
- self:draw()
- os.sleep(self.length)
- self.clicked = false
- self:draw()
- end
- function buttonAPI:click(x,y)
- if(self:check(x,y)) then
- self.func()
- if(self.behaviour == 0) then
- self:clickPulse()
- return true
- elseif(self.behaviour == 1) then
- self:clickToggle()
- return true
- end
- end
- return false
- end
- ---------------------
- --END CLASS buttonAPI
- ---------------------
- return buttonAPIarr
Advertisement
Add Comment
Please, Sign In to add comment