Update button module

This commit is contained in:
Squishy (C6H12O6+NaCl+H2O)
2024-05-21 22:34:45 +07:00
parent cdcd71a256
commit 7cf9d0cadc
2 changed files with 26 additions and 12 deletions

View File

@@ -36,6 +36,7 @@ end
---@field textColor? integer[]
---@field borderColor? integer[]
---@field hoverColor? integer[]
---@field pressColor? integer[]
---
---@field codeWhenPressed? function| # Code will be execute when pressed
---@field codeWhenReleased? function| # Code will be execute when released
@@ -49,6 +50,7 @@ local button = {
backgroundColor = {0,0,0,0},
hoverColor = {1,1,1,0.5},
pressColor = {0, 1, 0.5, 0.5},
borderColor = {1,1,1},
textColor = {1,1,1},
@@ -72,7 +74,10 @@ function button:draw()
love.graphics.setColor(self.backgroundColor)
love.graphics.rectangle('fill', self.x, self.y, self.w, self.h, self.r)
if self._hovering then
if self._pressed then
love.graphics.setColor(self.pressColor)
love.graphics.rectangle('fill', self.x, self.y, self.w, self.h, self.r)
elseif self._hovering then
love.graphics.setColor(self.hoverColor)
love.graphics.rectangle('fill', self.x, self.y, self.w, self.h, self.r)
end
@@ -108,21 +113,23 @@ function button:isHovering(x,y)
return self._hovering
end
---Trigger press action, only when ``self._hovering`` is true
function button:press(x, y)
if self:isHovering(x, y) and not self._pressed then
---@param isTouch? boolean Button just released by mouse?
function button:press(x, y, isTouch)
local hovering = isTouch and self:isHovering(x, y) or self._hovering
if hovering and not self._pressed then
self.codeWhenPressed()
self._pressed = true
end
end
---Trigger release action, don't need ``self._hovering`` to ``true``
---@param isMouse? boolean Button just released by mouse?
function button:release(x, y, isMouse)
if self:isHovering(x, y) and self._pressed then
---@param isTouch? boolean Button just released by mouse?
function button:release(x, y, isTouch)
local hovering = isTouch and self:isHovering(x, y) or self._hovering
if hovering and self._pressed then
self.codeWhenReleased()
self._pressed = false
if not love.mouse.isCursorSupported() then
self._hovering = false
end
end
end
@@ -154,6 +161,7 @@ function BUTTON.checkDataValidation(D, safe)
assert(checkColorTableValidation(D.backgroundColor), "[backgroundColor] must be a table with r, g, b (, a) values, all of them must be integers between 0 and 1")
assert(checkColorTableValidation(D.hoverColor), "[hoverColor] must be a table with r, g, b (, a) values, all of them must be integers between 0 and 1")
assert(checkColorTableValidation(D.pressColor), "[hoverColor] must be a table with r, g, b (, a) values, all of them must be integers between 0 and 1")
assert(checkColorTableValidation(D.borderColor), "[borderColor] must be a table with r, g, b (, a) values, all of them must be integers between 0 and 1")
assert(checkColorTableValidation(D.textColor), "[textColor] must be a table with r, g, b (, a) values, all of them must be integers between 0 and 1")
@@ -182,6 +190,7 @@ end
---@field textColor? integer[]
---@field borderColor? integer[]
---@field hoverColor? integer[]
---@field pressColor? integer[]
---
---@field codeWhenPressed? function| # Code will be execute when pressed
---@field codeWhenReleased? function| # Code will be execute when released