bezier-string-art/GUI/Button.lua
2023-05-11 21:28:37 +03:00

96 lines
2.3 KiB
Lua

local GUI = require("GUI-Framework")
local Vector2 = require("Vector2")
local utils = require("utils")
local Button = GUI.newTemplate("Button")
Button.isHover = false
Button.isDown = false
Button.text = ""
Button.font = love.graphics.getFont()
function Button:load()
local minWidth = self.font:getWidth(self.text)
local minHeight = self.font:getHeight(self.text) * ((select(2, self.text:gsub('\n', '\n')) or 0) + 1)
if self.size.x < minWidth then
self.size.x = minWidth + 10
end
if self.size.y < minHeight then
self.size.y = minHeight + 10
end
end
function Button:draw()
if self.isDown then
self:drawDown()
elseif self.isHover then
self:drawHover()
else
self:drawDefault()
end
end
function Button:drawHover()
local x, y, width, height = self:getBounds()
local cx, cy = x + width/2, y + height/2
-- Shadow
utils.smoothRectangle(x, y, width, height, 2, utils.rgba(0, 0, 0, 0.62))
-- The actual button
utils.smoothRectangle(x, y-1, width, height, 2, utils.rgb(42, 147, 247))
-- Text
love.graphics.setColor(1, 1, 1)
utils.alignedPrint(self.font, self.text, cx, cy-1)
end
function Button:drawDefault()
local x, y, width, height = self:getBounds()
local cx, cy = x + width/2, y + height/2
-- Shadow
utils.smoothRectangle(x, y, width, height, 2, utils.rgba(0, 0, 0, 0.62))
-- The actual button
utils.smoothRectangle(x, y-1, width, height, 2, utils.rgb(56, 158, 255))
-- Text
love.graphics.setColor(1, 1, 1)
utils.alignedPrint(self.font, self.text, cx, cy-1)
end
function Button:mousemoved(x, y)
if self:inBounds(x, y) then
self.isHover = true
return true
else
self.isHover = false
end
end
function Button:mousemovedFallback()
self.isHover = false
end
function Button:mousepressed()
if self.isHover then
self.isDown = true
return true
end
end
function Button:mousereleased(x, y, ...)
if self.isDown and self:inBounds(x, y) then
if self.mouseClicked then self:mouseClicked(x, y, ...) end
end
self.isDown = false
end
function Button:drawDown()
local x, y, width, height = self:getBounds()
local cx, cy = x + width/2, y + height/2
-- the actual button
utils.smoothRectangle(x, y, width, height, 2, utils.rgb(0, 116, 225))
-- Text
love.graphics.setColor(1, 1, 1)
utils.alignedPrint(self.font, self.text, cx, cy)
end