Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ButtonObj = {}
- function ButtonObj:new(screen, x, y, w, h, labelOff, labelOn, cLabelOff, cLabelOn, cBgOff, cBgOn)
- local obj = obj or {}
- setmetatable(obj, {__index = self})
- obj.screen = screen
- obj.monitor = screen:getMonitor()
- obj.x = x or 1
- obj.y = y or 1
- obj.w = w or 1
- obj.h = h or 1
- obj.xOffset = 0
- obj.yOffset = 0
- obj.labelOff = labelOff or "off"
- obj.labelOn = labelOn or obj.labelOff
- obj.cLabelOff = cLabelOff or colors.white
- obj.cLabelOn = cLabelOn or colors.white
- obj.cBgOff = cBgOff or colors.cyan
- obj.cBgOn = cBgOn or colors.green
- obj.func = nil
- obj.funcArgs = nil
- obj.isActive = false
- obj.isLocked = false
- obj.canToggle = true -- this is useful for one-shot buttons
- obj.parent = nil
- return obj
- end
- function ButtonObj:draw()
- local old = term.redirect(self.monitor)
- local oldTC = self.monitor.getTextColor()
- local oldBC = self.monitor.getBackgroundColor()
- local cBg = self.cBgOff
- local cLabel = self.cLabelOff
- local label = self.labelOff
- if self.isActive then
- cBg = self.cBgOn
- cLabel = self.cLabelOn
- label = self.labelOn
- else
- cBg = self.cBgOff
- cLabeL = self.cLabelOff
- label = self.labelOff
- end
- if self.isLocked then cBg = colors.gray end
- local newX, newY
- if self.parent then
- oX, oY = self.parent:getStartPos()
- newX = self.x + oX
- newY = self.y + oY
- else
- newX = self.x + self.xOffset
- newY = self.y + self.yOffset
- end
- paintutils.drawFilledBox(newX , newY, self.w + newX, self.h + newY, cBg)
- local txtOffset = (self.w/2) - (math.floor(string.len(label)/2))
- self.screen:write(label, newX + txtOffset, newY + 1, cBg, cLabel)
- -- return to previous colors etc. to not mess up anything after
- self.monitor.setTextColor(oldTC)
- self.monitor.setBackgroundColor(oldBC)
- term.redirect(old)
- end
- function ButtonObj:setLocked(lock)
- self.isLocked = lock or false
- end
- function ButtonObj:setFunction(func, args)
- self.func = func
- self.funcArgs = args
- end
- function ButtonObj:check(monX, monY)
- print("checked "..self.labelOff.." !")
- local newX, newY
- if self.parent then
- oX, oY = self.parent:getStartPos()
- newX = self.x + oX
- newY = self.y + oY
- else
- newX = self.x + self.xOffset
- newY = self.y + self.yOffset
- end
- if (monX >= newX and monX <= newX+self.w) and (monY >= newY and monY <= newY+self.h) and not self.isLocked then
- if self.canToggle then
- self.isActive = not self.isActive
- end
- self:execute()
- end
- end
- function ButtonObj:execute()
- local bool = nil
- if self.isActive or (not self.canToggle) then
- bool = true
- else
- bool = false
- end
- self.func(bool, self.args) --self.func is the assigned function. this confused the heck out of me when rewriting lol
- end
- function ButtonObj:setActive(active, exec) -- set the button active. this can also execute the function tied to the button
- self.isActive = active or false
- if exec then self:execute() end
- end
- function ButtonObj:setOffset(x, y)
- self.xOffset = x
- self.yOffset = y
- end
- function ButtonObj:setLabel(off, on)
- self.labelOff = off or self.labelOff
- self.labelOn = on or off
- end
- function ButtonObj:setLabelColor(off, on)
- self.cLabelOff = off or self.cLabelOff
- self.cLabelOn = on or off
- end
- function ButtonObj:setBackgroundColor(off, on)
- self.cBgOff = off or self.cBgOff
- self.cBgOn = on or off
- end
- function ButtonObj:setParent(obj)
- local add = function()
- self.parent:addObject(self)
- end
- self.parent = obj
- pcall(add)
- end
- function ButtonObj:setCanToggle(x)
- local bool = x
- self.canToggle = bool
- end
- function ButtonObj:getParent()
- return self.parent
- end
- function ButtonObj:setX(x)
- self.x = x
- end
- function ButtonObj:getX()
- return self.x
- end
- function ButtonObj:setY(y)
- self.y = y
- end
- function ButtonObj:getY()
- return self.y
- end
- function ButtonObj:setW(w)
- self.w = w
- end
- function ButtonObj:getW()
- return self.w
- end
- function ButtonObj:setH(h)
- self.h = h
- end
- function ButtonObj:getH()
- return self.h
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement