From 7cf9d0cadcdddf8d4fe65ed0cb312e943fdc104a Mon Sep 17 00:00:00 2001 From: "Squishy (C6H12O6+NaCl+H2O)" <106439598+SweetSea-ButImNotSweet@users.noreply.github.com> Date: Tue, 21 May 2024 22:34:45 +0700 Subject: [PATCH] Update button module --- libs/simple-button.lua | 27 ++++++++++++++++++--------- load.lua | 11 ++++++++--- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/libs/simple-button.lua b/libs/simple-button.lua index 9f3063a..c7aa617 100644 --- a/libs/simple-button.lua +++ b/libs/simple-button.lua @@ -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 diff --git a/load.lua b/load.lua index edc65f0..3f40538 100644 --- a/load.lua +++ b/load.lua @@ -14,11 +14,14 @@ BUTTON.setDefaultOption{ draw = function(self) 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) - if self._hovering then - love.graphics.setColor(0.4, 1, 1, 0.5) + 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 @@ -48,6 +51,8 @@ BUTTON.setDefaultOption{ love.graphics.setLineWidth(1) love.graphics.rectangle('line', self.x, self.y, self.w, self.h, self.r) end, + backgroundColor = {0, 0, 0, 0.8}, + pressColor = {0.4, 1, 1, 0.5} } -- Graphics