Update button module

This commit is contained in:
Squishy (C6H12O6+NaCl+H2O)
2024-05-18 16:49:12 +07:00
parent dddedd357e
commit e10cc30820

View File

@@ -1,4 +1,20 @@
local NULL = function () end
local NULL = function() end
local function checkColorTableValidation(C)
if C then
if type(C) == "table" then
if #C == 3 or #C == 4 then
for _, v in pairs(C) do
if type(v) ~= "number" or v<0 or v>1 then return false end
end
else
return false
end
else
return false
end
end
return true
end
---@class BUTTON.button
---@field name string
@@ -6,24 +22,24 @@ local NULL = function () end
---@field y number
---@field w number
---@field h number
---@field r number # Round corner
---
---@field borderWidth? number
---@field borderJoin? "bevel"|"milter"|"none"
---@field borderStyle? "rough"|"smooth"
---@field borderWidth number
---@field borderJoin "bevel"|"miter"|"none"
---@field borderStyle "rough"|"smooth"
---
---@field font? love.Font
---@field font love.Font
---
---@field codeWhenPressed? function
---@field codeWhenReleased? function
---@field drawingTextFunc? function
---@field codeWhenPressed function
---@field codeWhenReleased function
---
---@field _hovering? boolean
---
---@field draw? function
---@field update? function
---@field draw function
---@field update function
local button = {
name = "NEW BUTTON",
x = 0, y = 0, w = 0, h = 0,
x = 0, y = 0, w = 1, h = 1, r = 0,
backgroundColor = {0,0,0,0},
hoverColor = {1,1,1,0.5},
@@ -36,34 +52,30 @@ local button = {
borderJoin = "none",
borderStyle = "smooth",
drawingBackgroundFunc = function(self)
love.graphics.setLineWidth(self.borderWidth)
love.graphics.setLineStyle(self.borderStyle)
love.graphics.setLineJoin(self.borderJoin)
love.graphics.setColor(unpack(self.backgroundColor))
love.graphics.rectangle('fill', self.x, self.y, self.w, self.h)
if self._hovering then
love.graphics.setColor(unpack(self.hoverColor))
love.graphics.rectangle('fill', self.x, self.y, self.w, self.h)
end
love.graphics.setColor(unpack(self.borderColor))
love.graphics.rectangle('line', self.x, self.y, self.w, self.h)
end,
drawingTextFunc = function(self)
love.graphics.setFont(self.font)
love.graphics.printf(self.name, self.x, self.y + (self.h * 0.25), self.w, "center")
end,
codeWhenPressed = NULL,
codeWhenReleased = NULL,
_hovering = false
}; button.__index = button
function button:draw()
self:drawingBackgroundFunc()
self:drawingTextFunc()
love.graphics.setLineWidth(self.borderWidth)
love.graphics.setLineStyle(self.borderStyle)
love.graphics.setLineJoin(self.borderJoin)
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(self.hoverColor)
love.graphics.rectangle('fill', self.x, self.y, self.w, self.h, self.r)
end
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.setColor(self.borderColor)
love.graphics.rectangle('line', self.x, self.y, self.w, self.h, self.r)
end
function button:isHovering(x,y)
if
@@ -99,107 +111,53 @@ local BUTTON = {}
function BUTTON.checkDataValidation(D)
assert(type(D.name) == "string", "[name] is missing or not valid")
assert(type(D.x) == "number" and D.x > 0, "[x] must be a positive integer")
assert(type(D.y) == "number" and D.y > 0, "[y] 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.borderWidth) == "number" and D.borderWidth > 0) or D.drawingTextFunc == nil, "[borderWidth] must be a postive integer")
assert(table.contains({"bevel", "milter", "none"}, D.borderJoin) or D.borderJoin == nil, "[borderJoin] must be 'bevel', 'milter' or 'none")
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")
assert(table.contains({"rough", "smooth"}, D.borderStyle) or D.borderStyle == nil, "[borderStyle] must be 'rough' or 'smooth'")
assert((D.font and D.font.typeOf and D.font:typeOf("Font")) or D.font == nil, "[font] must be love.Font")
local red_flag = false
if D.backgroundColor then
if type(D.backgroundColor) == "table" then
if #D.backgroundColor == 3 or #D.backgroundColor == 4 then
for _, v in pairs(D.backgroundColor) do
if type(v) ~= "number" or v<0 or v>1 then red_flag = true end
end
else
red_flag = true
end
else
red_flag = true
end
end
if red_flag then error("[backgroundColor] must be a table with r, g, b (, a) values, all of them must be integers between 0 and 1") end
if D.hoverColor then
if type(D.hoverColor) == "table" then
if #D.hoverColor == 3 or #D.hoverColor == 4 then
for _, v in pairs(D.hoverColor) do
if type(v) ~= "number" or v<0 or v>1 then red_flag = true end
end
else
red_flag = true
end
else
red_flag = true
end
end
if red_flag then error("[hoverColor] must be a table with r, g, b (, a) values, all of them must be integers between 0 and 1") end
if D.borderColor then
if type(D.borderColor) == "table" then
if #D.borderColor == 3 or #D.borderColor == 4 then
for _, v in pairs(D.borderColor) do
if type(v) ~= "number" or v<0 or v>1 then red_flag = true end
end
else
red_flag = true
end
else
red_flag = true
end
end
if red_flag then error("[borderColor] must be a table with r, g, b (, a) values, all of them must be integers between 0 and 1") end
if D.textColor then
if type(D.textColor) == "table" then
if #D.textColor == 3 or #D.textColor == 4 then
for _, v in pairs(D.textColor) do
if type(v) ~= "number" or v<0 or v>1 then red_flag = true end
end
else
red_flag = true
end
else
red_flag = true
end
end
if red_flag then error("[textColor] must be a table with r, g, b (, a) values, all of them must be integers between 0 and 1") end
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.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.drawingTextFunc) == "function" or D.drawingTextFunc == nil, "[drawingTextFunc] 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 shown
---@field x number
---@field y number
---@field w number
---@field h number
---@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
---@field borderJoin? "bevel"|"milter"|"none"
---@field borderJoin? "bevel"|"miter"|"none"
---@field borderStyle? "rough"|"smooth"
---
---@field font? love.Font
---
---@field backgroundColor? table<[r,g,b,a]>
---@field hoverColor? table<[r,g,b,a]>
---@field borderColor? 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 codeWhenPressed? function|nil # Code will be execute when pressed
---@field codeWhenReleased? function|nil # Code will be execute when released
---@field drawingTextFunc? function|defaultDrawingTextFunc
---@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
---@param D BUTTON.newData
---@return BUTTON.button
---Create a new button, provide you a table with 2 functions inside: draw and update<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 :)
function BUTTON.new(D)
BUTTON.checkDataValidation(D)
@@ -209,6 +167,7 @@ function BUTTON.new(D)
y = D.y,
w = D.w,
h = D.h,
r = D.r,
borderWidth = D.borderWidth,
borderJoin = D.borderJoin,
@@ -216,24 +175,19 @@ function BUTTON.new(D)
backgroundColor = D.backgroundColor,
hoverColor = D.hoverColor,
borderColor = D.borderColor,
textColor = D.textColor,
borderColor = D.borderColor,
codeWhenPressed = D.codeWhenPressed,
codeWhenReleased = D.codeWhenReleased,
drawingTextFunc = D.drawingTextFunc,
codeWhenPressed = D.codeWhenPressed,
codeWhenReleased = D.codeWhenReleased,
drawingButtonFunc = D.drawingButtonFunc,
}, button)
end
--- Set the default funciton used to drawing text, will be passed name, x, y, w and h
function BUTTON.setDefaultDrawingTextFunction(f)
assert(type(f) == "function", "[f] must be a function!")
button.drawingTextFunc = f
end
---@param D BUTTON.newData
function BUTTON.setDefaultOption(D)
BUTTON.checkDataValidation(D)
---@diagnostic disable-next-line: param-type-mismatch
BUTTON.checkDataValidation(setmetatable(D, button))
for k, v in pairs(D) do
if button[k] ~= nil then
button[k] = v