mirror of
https://gitea.com/SweetSea-ButImNotSweet/tromi_mobile.git
synced 2025-01-08 17:33:09 +08:00
Update button module
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user