mirror of
https://gitea.com/SweetSea-ButImNotSweet/tromi_mobile.git
synced 2025-01-08 17:33:09 +08:00
Update simple-button module
This commit is contained in:
@@ -17,26 +17,30 @@ local function checkColorTableValidation(C)
|
|||||||
end
|
end
|
||||||
|
|
||||||
---@class BUTTON.button
|
---@class BUTTON.button
|
||||||
---@field name string
|
---@field name? string # Name of the button, will be used to show
|
||||||
---@field x number
|
---@field x? number # Position of the button (x, y, w, h)
|
||||||
---@field y number
|
---@field y? number # Position of the button (x, y, w, h)
|
||||||
---@field w number
|
---@field w? number # Position of the button (x, y, w, h)
|
||||||
---@field h number
|
---@field h? number # Position of the button (x, y, w, h)
|
||||||
---@field r number # Round corner
|
---@field r? number # Radius corner, cannot larger than half of button's width and half of button's height
|
||||||
---
|
---
|
||||||
---@field borderWidth number
|
---@field borderWidth? number|1 # Line width will be used to draw button
|
||||||
---@field borderJoin "bevel"|"miter"|"none"
|
---@field borderJoin? "bevel"|"miter"|"none"
|
||||||
---@field borderStyle "rough"|"smooth"
|
---@field borderStyle? "rough"|"smooth"
|
||||||
---
|
---
|
||||||
---@field font love.Font
|
---@field font? love.Font
|
||||||
---
|
---
|
||||||
---@field codeWhenPressed function
|
---@field backgroundColor? integer[]
|
||||||
---@field codeWhenReleased function
|
---@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<br>You can override the default one if you feel the default text drawing function is not suitable for you
|
||||||
---
|
---
|
||||||
---@field draw function
|
---@field draw? function
|
||||||
---@field update function
|
---@field update? function
|
||||||
local button = {
|
local button = {
|
||||||
name = "NEW BUTTON",
|
name = "NEW BUTTON",
|
||||||
x = 0, y = 0, w = 1, h = 1, r = 0,
|
x = 0, y = 0, w = 1, h = 1, r = 0,
|
||||||
@@ -72,11 +76,12 @@ function button:draw()
|
|||||||
|
|
||||||
love.graphics.setColor(self.textColor)
|
love.graphics.setColor(self.textColor)
|
||||||
love.graphics.setFont(self.font)
|
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.setColor(self.borderColor)
|
||||||
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
|
||||||
|
---Check if current position is hovering the button, if yes, set ``self._hovering`` to true and return true
|
||||||
function button:isHovering(x,y)
|
function button:isHovering(x,y)
|
||||||
if
|
if
|
||||||
x >= self.x and
|
x >= self.x and
|
||||||
@@ -88,33 +93,35 @@ function button:isHovering(x,y)
|
|||||||
else
|
else
|
||||||
self._hovering = false
|
self._hovering = false
|
||||||
end
|
end
|
||||||
|
return self._hovering
|
||||||
end
|
end
|
||||||
---@param touch? boolean @ If the button is touched, remember to pass true into touch param
|
---Trigger press action, only when ``self._hovering`` is true
|
||||||
function button:press(touch)
|
function button:press()
|
||||||
if touch or self._hovering then
|
if self._hovering then self.codeWhenPressed() end
|
||||||
self.codeWhenPressed()
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
---@param touch? boolean @ If the button is touched, remember to pass true into touch param
|
---Trigger release action, don't need ``self._hovering`` to ``true``
|
||||||
function button:release(touch)
|
function button:release()
|
||||||
if touch or self._hovering then
|
self.codeWhenReleased()
|
||||||
self.codeWhenReleased()
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local BUTTON = {}
|
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``<br>
|
---Validate the provided data, will be called by ``BUTTON.new`` and ``BUTTON.setDefaultOption``<br>
|
||||||
---***WARNING! THIS FUNCTION WILL RAISE EXCEPTION IF DATA IS INVALID!***
|
---***WARNING! THIS FUNCTION WILL RAISE EXCEPTION IF DATA IS INVALID!***
|
||||||
function BUTTON.checkDataValidation(D)
|
function BUTTON.checkDataValidation(D, safe)
|
||||||
assert(type(D.name) == "string", "[name] is missing or not valid")
|
-- <SAFE TO IGNORE>
|
||||||
|
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.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((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")
|
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.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")
|
||||||
|
|
||||||
assert(type(D.codeWhenPressed) == "function" or D.codeWhenPressed == nil, "[codeWhenPressed] 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.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.drawingButtonFunc) == "function" or D.drawingButtonFunc == nil, "[drawingButtonFunc] must be a function or nil")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
---@class BUTTON.newData
|
---@class BUTTON.newData
|
||||||
---@field name? string # Name of the button, will be used to show
|
---@field name string # Name of the button, will be used to show
|
||||||
---@field x? number # Position of the button (x, y, w, h)
|
---@field x number # Position of the button (x, y, w, h)
|
||||||
---@field y? 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 w number # Position of the button (x, y, w, h)
|
||||||
---@field h? 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 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
|
---@field borderWidth? number|1 # Line width will be used to draw button
|
||||||
@@ -146,48 +152,30 @@ end
|
|||||||
---
|
---
|
||||||
---@field font? love.Font
|
---@field font? love.Font
|
||||||
---
|
---
|
||||||
---@field backgroundColor? table<[r,g,b,a]>
|
---@field backgroundColor? integer[]
|
||||||
---@field textColor? table<[r,g,b,a]>
|
---@field textColor? integer[]
|
||||||
---@field borderColor? table<[r,g,b,a]>
|
---@field borderColor? integer[]
|
||||||
---@field hoverColor? table<[r,g,b,a]>
|
---@field hoverColor? 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
|
||||||
---@field drawingButtonFunc? function| # The function is used to draw text<br>You can override the default one if you feel the default text drawing function is not suitable for you
|
---@field drawingButtonFunc? function| # The function is used to draw text<br>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
|
---@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<br>
|
---Create a new button, provide you a table with 4 functions inside: draw and update, press and release<br>
|
||||||
---You need to put them into intended callbacks :)
|
---You need to put them into intended callbacks :)
|
||||||
function BUTTON.new(D)
|
function BUTTON.new(D)
|
||||||
BUTTON.checkDataValidation(D)
|
BUTTON.checkDataValidation(D)
|
||||||
return setmetatable({
|
return setmetatable(D, button)
|
||||||
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)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param D BUTTON.newData
|
---@param D BUTTON.button
|
||||||
function BUTTON.setDefaultOption(D)
|
function BUTTON.setDefaultOption(D)
|
||||||
---@diagnostic disable-next-line: param-type-mismatch
|
BUTTON.checkDataValidation(setmetatable(D, button), true)
|
||||||
BUTTON.checkDataValidation(setmetatable(D, button))
|
|
||||||
for k, v in pairs(D) do
|
for k, v in pairs(D) do
|
||||||
if button[k] ~= nil then
|
if button[k] ~= nil then
|
||||||
button[k] = v
|
button[k] = v
|
||||||
|
|||||||
Reference in New Issue
Block a user