This commit is contained in:
SweetSea-ButImNotSweet
2024-05-21 20:07:53 +07:00
parent 7c39c1ada8
commit 5cf9deb9eb
7 changed files with 149 additions and 61 deletions

View File

@@ -61,7 +61,8 @@ local button = {
codeWhenPressed = NULL,
codeWhenReleased = NULL,
_hovering = false
_hovering = false,
_pressed = false,
}; button.__index = button
function button:draw()
love.graphics.setLineWidth(self.borderWidth)
@@ -107,14 +108,23 @@ function button:isHovering(x,y)
return self._hovering
end
---Trigger press action, only when ``self._hovering`` is true
function button:press()
if self._hovering then self.codeWhenPressed() end
function button:press(x, y)
if self:isHovering(x, y) and not self._pressed then
self.codeWhenPressed()
self._pressed = true
end
end
---Trigger release action, don't need ``self._hovering`` to ``true``
function button:release()
if self._hovering then
---@param isMouse? boolean Button just released by mouse?
function button:release(x, y, isTouch)
if self:isHovering(x, y) and self._pressed then
self.codeWhenReleased()
self._hovering = false
self._pressed = false
if isTouch then
self._hovering = false
else
self:isHovering(x, y)
end
end
end
@@ -204,4 +214,38 @@ function BUTTON.setDefaultOption(D)
end
end
-- < EXTRA GENERAL OPTIONS >
---Draw all buttons in provided list
---@param list table<BUTTON.button>
function BUTTON.draw(list)
for _, v in pairs(list) do v:draw() end
end
---Check if current mouse position is inside button<br>
---Calling BUTTON.press will trigger this, but you can call it when moving mouse so button can be highlighted when being hovered
---@param list table<BUTTON.button>
---@param x number # Mouse position
---@param y nunber # Mouse position
function BUTTON.checkHovering(list, x, y)
for _, v in pairs(list) do v:isHovering(x, y) end
end
--- Trigger the press action, only if ``button._hovering == true``
---@param list table<BUTTON.button>
---@param x number # Mouse position
---@param y nunber # Mouse position
function BUTTON.press(list, x, y)
for _, v in pairs(list) do v:press(x, y) end
end
---Trigger the release action
---@param list table<BUTTON.button>
---@param x number # Mouse position
---@param y nunber # Mouse position
---@param isMouse? boolean # Is mouse just released a button?
function BUTTON.release(list, x, y, isTouch)
for _, v in pairs(list) do v:release(x, y, isTouch) end
end
return BUTTON