diff --git a/libs/simple-button.lua b/libs/simple-button.lua index a13eb8e..8482c6d 100644 --- a/libs/simple-button.lua +++ b/libs/simple-button.lua @@ -17,26 +17,30 @@ local function checkColorTableValidation(C) end ---@class BUTTON.button ----@field name string ----@field x number ----@field y number ----@field w number ----@field h number ----@field r number # Round corner +---@field name? string # Name of the button, will be used to show +---@field x? number # Position of the button (x, y, w, h) +---@field y? number # Position of the button (x, y, w, h) +---@field w? number # Position of the button (x, y, w, h) +---@field h? number # Position of the button (x, y, w, h) +---@field r? number # Radius corner, cannot larger than half of button's width and half of button's height --- ----@field borderWidth number ----@field borderJoin "bevel"|"miter"|"none" ----@field borderStyle "rough"|"smooth" +---@field borderWidth? number|1 # Line width will be used to draw button +---@field borderJoin? "bevel"|"miter"|"none" +---@field borderStyle? "rough"|"smooth" --- ----@field font love.Font +---@field font? love.Font --- ----@field codeWhenPressed function ----@field codeWhenReleased function +---@field backgroundColor? integer[] +---@field textColor? integer[] +---@field borderColor? integer[] +---@field hoverColor? integer[] --- ----@field _hovering? boolean +---@field codeWhenPressed? function| # Code will be execute when pressed +---@field codeWhenReleased? function| # Code will be execute when released +---@field drawingButtonFunc? function| # The function is used to draw text
You can override the default one if you feel the default text drawing function is not suitable for you --- ----@field draw function ----@field update function +---@field draw? function +---@field update? function local button = { name = "NEW BUTTON", x = 0, y = 0, w = 1, h = 1, r = 0, @@ -72,11 +76,12 @@ function button:draw() love.graphics.setColor(self.textColor) love.graphics.setFont(self.font) - love.graphics.printf(self.name, self.x, self.y + (self.h * 0.25), self.w, "center") + love.graphics.printf(self.name, self.x, self.y + (self.h * 0.5) - (self.font:getHeight() * 0.5), self.w, "center") love.graphics.setColor(self.borderColor) love.graphics.rectangle('line', self.x, self.y, self.w, self.h, self.r) end +---Check if current position is hovering the button, if yes, set ``self._hovering`` to true and return true function button:isHovering(x,y) if x >= self.x and @@ -88,33 +93,35 @@ function button:isHovering(x,y) else self._hovering = false end + return self._hovering end ----@param touch? boolean @ If the button is touched, remember to pass true into touch param -function button:press(touch) - if touch or self._hovering then - self.codeWhenPressed() - end +---Trigger press action, only when ``self._hovering`` is true +function button:press() + if self._hovering then self.codeWhenPressed() end end ----@param touch? boolean @ If the button is touched, remember to pass true into touch param -function button:release(touch) - if touch or self._hovering then - self.codeWhenReleased() - end +---Trigger release action, don't need ``self._hovering`` to ``true`` +function button:release() + self.codeWhenReleased() end local BUTTON = {} ----@param D BUTTON.newData +---@param D BUTTON.button|BUTTON.newData +---@param safe? boolean @ Creating widget? If not then ignore accept missing important parameters +---@return nil ---Validate the provided data, will be called by ``BUTTON.new`` and ``BUTTON.setDefaultOption``
---***WARNING! THIS FUNCTION WILL RAISE EXCEPTION IF DATA IS INVALID!*** -function BUTTON.checkDataValidation(D) - assert(type(D.name) == "string", "[name] is missing or not valid") +function BUTTON.checkDataValidation(D, safe) + -- + assert(type(D.name) == "string" or (safe and D.name == nil) == "string","[name] is missing, or you just passed an empty data?") + + assert( type(D.x) == "number" or (safe and D.x == nil), "[x] must be a integer") + assert( type(D.y) == "number" or (safe and D.y == nil), "[y] must be a integer") + assert((type(D.w) == "number" and D.w > 0) or (safe and D.w == nil), "[w] must be a positive integer") + assert((type(D.h) == "number" and D.h > 0) or (safe and D.h == nil), "[h] must be a positive integer") + -- - assert(type(D.x) == "number", "[x] must be a integer") - assert(type(D.y) == "number", "[y] must be a integer") - assert(type(D.w) == "number" and D.w > 0, "[w] must be a positive integer") - assert(type(D.h) == "number" and D.h > 0, "[h] must be a positive integer") assert((type(D.r) == "number" and D.r >= 0 and D.r <= D.w * 0.5 and D.r <= D.h * 0.5) or D.r == nil, "[r] must be a positive integer and cannot larger than half of button's width and half of button's height") assert((type(D.borderWidth) == "number" and D.borderWidth > 0) or D.borderWidth == nil, "[borderWidth] must be a postive integer") assert(table.contains({"bevel", "miter", "none"}, D.borderJoin) or D.borderJoin == nil, "[borderJoin] must be 'bevel', 'miter' or 'none") @@ -126,18 +133,17 @@ function BUTTON.checkDataValidation(D) 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(type(D.codeWhenPressed) == "function" or D.codeWhenPressed == nil, "[codeWhenPressed] must be a function or nil") - assert(type(D.codeWhenReleased) == "function" or D.codeWhenReleased == nil, "[codeWhenReleased] must be a function or nil") - assert(type(D.drawingButtonFunc) == "function" or D.drawingButtonFunc == nil, "[drawingButtonFunc] must be a function or nil") + assert(type(D.codeWhenPressed) == "function" or D.codeWhenPressed == nil, "[codeWhenPressed] must be a function or nil") + assert(type(D.codeWhenReleased) == "function" or D.codeWhenReleased == nil, "[codeWhenReleased] must be a function or nil") + assert(type(D.drawingButtonFunc) == "function" or D.drawingButtonFunc == nil, "[drawingButtonFunc] must be a function or nil") end - ---@class BUTTON.newData ----@field name? string # Name of the button, will be used to show ----@field x? number # Position of the button (x, y, w, h) ----@field y? number # Position of the button (x, y, w, h) ----@field w? number # Position of the button (x, y, w, h) ----@field h? number # Position of the button (x, y, w, h) +---@field name string # Name of the button, will be used to show +---@field x number # Position of the button (x, y, w, h) +---@field y number # Position of the button (x, y, w, h) +---@field w number # Position of the button (x, y, w, h) +---@field h number # Position of the button (x, y, w, h) ---@field r? number # Radius corner, cannot larger than half of button's width and half of button's height --- ---@field borderWidth? number|1 # Line width will be used to draw button @@ -146,48 +152,30 @@ end --- ---@field font? love.Font --- ----@field backgroundColor? table<[r,g,b,a]> ----@field textColor? table<[r,g,b,a]> ----@field borderColor? table<[r,g,b,a]> ----@field hoverColor? table<[r,g,b,a]> +---@field backgroundColor? integer[] +---@field textColor? integer[] +---@field borderColor? integer[] +---@field hoverColor? integer[] --- ---@field codeWhenPressed? function| # Code will be execute when pressed ---@field codeWhenReleased? function| # Code will be execute when released ---@field drawingButtonFunc? function| # The function is used to draw text
You can override the default one if you feel the default text drawing function is not suitable for you +--- +---@field draw? function +---@field update? function ---@param D BUTTON.newData ----@return BUTTON.button +---@nodiscard ---Create a new button, provide you a table with 4 functions inside: draw and update, press and release
---You need to put them into intended callbacks :) function BUTTON.new(D) BUTTON.checkDataValidation(D) - return setmetatable({ - name = D.name, - x = D.x, - y = D.y, - w = D.w, - h = D.h, - r = D.r, - - borderWidth = D.borderWidth, - borderJoin = D.borderJoin, - borderStyle = D.borderStyle, - - backgroundColor = D.backgroundColor, - hoverColor = D.hoverColor, - textColor = D.textColor, - borderColor = D.borderColor, - - codeWhenPressed = D.codeWhenPressed, - codeWhenReleased = D.codeWhenReleased, - drawingButtonFunc = D.drawingButtonFunc, - }, button) + return setmetatable(D, button) end ----@param D BUTTON.newData +---@param D BUTTON.button function BUTTON.setDefaultOption(D) - ---@diagnostic disable-next-line: param-type-mismatch - BUTTON.checkDataValidation(setmetatable(D, button)) + BUTTON.checkDataValidation(setmetatable(D, button), true) for k, v in pairs(D) do if button[k] ~= nil then button[k] = v