Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---------------
- -- BY UPZONE --
- ---------------
- os.loadAPI("builder\\apis\\Paint")
- os.loadAPI("builder\\apis\\Colors")
- local Button = {
- }
- function Button.__init__(baseClass, button)
- self = button
- setmetatable (self, {__index=Button})
- return self
- end
- setmetatable(Button, {__call=Button.__init__})
- function Button:getText()
- return self.text
- end
- function Button:getPosition()
- return self.position
- end
- function Button:getPositionX()
- return tonumber(self:getPosition()["x"])
- end
- function Button:getPositionY()
- return tonumber(self:getPosition()["y"])
- end
- function Button:raiseOnClick()
- self:setHighlighted(true)
- self:onClick()
- sleep(.075)
- self:setHighlighted(false)
- end
- function Button:getSize()
- return self.size
- end
- function Button:getSizeWidth()
- return tonumber(self:getSize()["width"])
- end
- function Button:getSizeHeight()
- return tonumber(self:getSize()["height"])
- end
- function Button:intersects(posX, posY)
- x = self:getPositionX()
- y = self:getPositionY()
- width = self:getSizeWidth()
- height = self:getSizeHeight()
- endX = x + width - 1
- endY = y + height - 1
- intersectsX = ( posX >= x and posX <= endX )
- intersectsY = ( posY >= y and posY <= endY )
- return ( intersectsX and intersectsY )
- end
- function Button:getColors()
- return self.colors
- end
- function Button:getTextColor()
- return self:getColors()["textColor"]
- end
- function Button:getHighlightedTextColor()
- return self:getColors()["highlightedTextColor"]
- end
- function Button:getBackgroundColor()
- return self:getColors()["backgroundColor"]
- end
- function Button:getHighlightedBackgroundColor()
- return self:getColors()["highlightedBackgroundColor"]
- end
- function Button:setHighlighted(highlighted)
- bgColor = nil
- txtColor = nil
- if highlighted then
- bgColor = self:getHighlightedBackgroundColor()
- txtColor = self:getHighlightedTextColor()
- else
- bgColor = self:getBackgroundColor()
- txtColor = self:getTextColor()
- end
- -- background
- Paint.fill(self:getPositionX(), self:getPositionY(), ( self:getPositionX() + self:getSizeWidth() - 1), ( self:getPositionY() + self:getSizeHeight() - 1), bgColor)
- textLength = string.len(self:getText())
- offsetLeft = math.floor((self:getSizeWidth() - textLength) / 2)
- offsetTop = math.floor((self:getSizeHeight() - 1) / 2)
- -- text
- term.setCursorPos(self:getPositionX() + offsetLeft, self:getPositionY() + offsetTop)
- term.blit(self:getText(), string.rep(txtColor, textLength), string.rep(bgColor, textLength))
- end
- function Button:draw()
- self:setHighlighted(false)
- end
- -- Factory
- function create(text, x, y, width, height, onClick, textColor, backgroundColor, highlightedTextColor, highlightedBackgroundColor)
- return Button({
- text = text,
- position = {
- x = x,
- y = y
- },
- size = {
- width = width,
- height = height
- },
- onClick = onClick,
- colors = {
- textColor = textColor,
- backgroundColor = backgroundColor,
- highlightedTextColor = highlightedTextColor,
- highlightedBackgroundColor = highlightedBackgroundColor
- }})
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement