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 textColor? integer[]
---@field borderColor? integer[] ---@field borderColor? integer[]
---@field hoverColor? integer[] ---@field hoverColor? integer[]
---@field pressColor? integer[]
--- ---
---@field codeWhenPressed? function| # Code will be execute when pressed ---@field codeWhenPressed? function| # Code will be execute when pressed
---@field codeWhenReleased? function| # Code will be execute when released ---@field codeWhenReleased? function| # Code will be execute when released
@@ -49,6 +50,7 @@ local button = {
backgroundColor = {0,0,0,0}, backgroundColor = {0,0,0,0},
hoverColor = {1,1,1,0.5}, hoverColor = {1,1,1,0.5},
pressColor = {0, 1, 0.5, 0.5},
borderColor = {1,1,1}, borderColor = {1,1,1},
textColor = {1,1,1}, textColor = {1,1,1},
@@ -72,7 +74,10 @@ function button:draw()
love.graphics.setColor(self.backgroundColor) love.graphics.setColor(self.backgroundColor)
love.graphics.rectangle('fill', self.x, self.y, self.w, self.h, self.r) 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.setColor(self.hoverColor)
love.graphics.rectangle('fill', self.x, self.y, self.w, self.h, self.r) love.graphics.rectangle('fill', self.x, self.y, self.w, self.h, self.r)
end end
@@ -108,21 +113,23 @@ function button:isHovering(x,y)
return self._hovering return self._hovering
end end
---Trigger press action, only when ``self._hovering`` is true ---Trigger press action, only when ``self._hovering`` is true
function button:press(x, y) ---@param isTouch? boolean Button just released by mouse?
if self:isHovering(x, y) and not self._pressed then 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.codeWhenPressed()
self._pressed = true self._pressed = true
end end
end end
---Trigger release action, don't need ``self._hovering`` to ``true`` ---Trigger release action, don't need ``self._hovering`` to ``true``
---@param isMouse? boolean Button just released by mouse? ---@param isTouch? boolean Button just released by mouse?
function button:release(x, y, isMouse) function button:release(x, y, isTouch)
if self:isHovering(x, y) and self._pressed then local hovering = isTouch and self:isHovering(x, y) or self._hovering
if hovering and self._pressed then
self.codeWhenReleased() self.codeWhenReleased()
self._pressed = false self._pressed = false
if not love.mouse.isCursorSupported() then
self._hovering = false
end
end 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.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.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.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") 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 textColor? integer[]
---@field borderColor? integer[] ---@field borderColor? integer[]
---@field hoverColor? integer[] ---@field hoverColor? integer[]
---@field pressColor? integer[]
--- ---
---@field codeWhenPressed? function| # Code will be execute when pressed ---@field codeWhenPressed? function| # Code will be execute when pressed
---@field codeWhenReleased? function| # Code will be execute when released ---@field codeWhenReleased? function| # Code will be execute when released

View File

@@ -14,11 +14,14 @@ BUTTON.setDefaultOption{
draw = function(self) draw = function(self)
local need_big_font = (self.font == FONT_big) local need_big_font = (self.font == FONT_big)
love.graphics.setColor(0, 0, 0, 0.8) love.graphics.setColor(self.backgroundColor)
love.graphics.rectangle('fill', self.x, self.y, self.w, self.h, self.r) 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(0.4, 1, 1, 0.5) 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) love.graphics.rectangle('fill', self.x, self.y, self.w, self.h, self.r)
end end
@@ -48,6 +51,8 @@ BUTTON.setDefaultOption{
love.graphics.setLineWidth(1) love.graphics.setLineWidth(1)
love.graphics.rectangle('line', self.x, self.y, self.w, self.h, self.r) love.graphics.rectangle('line', self.x, self.y, self.w, self.h, self.r)
end, end,
backgroundColor = {0, 0, 0, 0.8},
pressColor = {0.4, 1, 1, 0.5}
} }
-- Graphics -- Graphics