Update modules

This commit is contained in:
Squishy (C6H12O6+NaCl+H2O)
2024-05-23 10:58:57 +07:00
parent 16e472f7ef
commit ffe1bf5049
5 changed files with 44 additions and 28 deletions

View File

@@ -17,7 +17,7 @@ local function checkColorTableValidation(C)
end
---@class BUTTON.button
---@field text? string # Name of the button, will be used to show
---@field text? string|function # Name of the button, will be used to show
---@field textOrientation? "center"|"justify"|"left"|"right"
---
---@field x? number # Position of the button (x, y, w, h)
@@ -86,14 +86,16 @@ function button:draw()
love.graphics.setColor(self.textColor)
love.graphics.setFont(self.font)
local text = type(self.text) == 'function' and self.text() or self.text
local lineAmount
do
local _, t = self.font:getWrap(self.text, self.w)
local _, t = self.font:getWrap(text, self.w)
lineAmount = #t
end
local textHeight = self.font:getHeight() * (lineAmount * 0.5)
local textPos = self.y + (self.h * 0.5) - textHeight
love.graphics.printf(self.text, self.x, textPos, self.w, self.textOrientation)
love.graphics.printf(text, self.x, textPos, self.w, self.textOrientation)
love.graphics.setColor(self.borderColor)
love.graphics.rectangle('line', self.x, self.y, self.w, self.h, self.r)
@@ -150,7 +152,11 @@ local BUTTON = {}
---***WARNING! THIS FUNCTION WILL RAISE EXCEPTION IF DATA IS INVALID!***
function BUTTON.checkDataValidation(D, safe)
if not safe then
assert(type(D.text) == "string","[text] is missing, or you just passed an empty data?")
if type(D.text) == 'function' then
assert(type(D.text()) == 'string', "[text] is a function but it doesn't return any string?!")
elseif type(D.text) ~= 'string' then
error("[text] must be a string or a function returns string, got "..type(D.text))
end
assert(type(D.x) == "number" , "[x] must be a integer")
assert(type(D.y) == "number" , "[y] must be a integer")
@@ -178,7 +184,7 @@ function BUTTON.checkDataValidation(D, safe)
end
---@class BUTTON.newData
---@field text string # Name of the button, will be used to show
---@field text string|function # Name of the button, will be used to show. If function provided, it should return the string!
---@field textOrientation? "center"|"justify"|"left"|"right"
---
---@field x number # Position of the button (x, y, w, h)
@@ -210,6 +216,8 @@ end
---@nodiscard
---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 :)
---
---Remember to fill 5 necessary parameters: name, x, y, w and h
function BUTTON.new(D)
local B = setmetatable(D, button)
BUTTON.checkDataValidation(B)
@@ -231,7 +239,7 @@ end
-- < EXTRA GENERAL OPTIONS >
---Draw all buttons in provided list
---@param list table<BUTTON.button>
---@param list table<any,BUTTON.button>
function BUTTON.draw(list)
for _, v in pairs(list) do v:draw() end
end