diff --git a/libs/simple-button.lua b/libs/simple-button.lua index 8482c6d..a26c1a2 100644 --- a/libs/simple-button.lua +++ b/libs/simple-button.lua @@ -17,7 +17,9 @@ local function checkColorTableValidation(C) end ---@class BUTTON.button ----@field name? string # Name of the button, will be used to show +---@field text? string # 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) ---@field y? number # Position of the button (x, y, w, h) ---@field w? number # Position of the button (x, y, w, h) @@ -42,8 +44,8 @@ end ---@field draw? function ---@field update? function local button = { - name = "NEW BUTTON", - x = 0, y = 0, w = 1, h = 1, r = 0, + textOrientation = "center", + r = 0, backgroundColor = {0,0,0,0}, hoverColor = {1,1,1,0.5}, @@ -76,7 +78,15 @@ 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.5) - (self.font:getHeight() * 0.5), self.w, "center") + + local lineAmount + do + local _, t = self.font:getWrap(self.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.setColor(self.borderColor) love.graphics.rectangle('line', self.x, self.y, self.w, self.h, self.r) @@ -112,15 +122,16 @@ local BUTTON = {} ---@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, safe) +function BUTTON.checkDataValidation(D) -- - 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.text) == "string" == "string","[text] 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(table.contains({"center","justify","left","right"}, D.textOrientation) or D.textOrientation == nil, "[borderJoin] must be 'bevel', 'miter' 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") @@ -139,7 +150,9 @@ function BUTTON.checkDataValidation(D, safe) end ---@class BUTTON.newData ----@field name string # Name of the button, will be used to show +---@field text string # 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) ---@field y number # Position of the button (x, y, w, h) ---@field w number # Position of the button (x, y, w, h) @@ -169,8 +182,9 @@ end ---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(D, button) + local B = setmetatable(D, button) + BUTTON.checkDataValidation(B) + return B end ---@param D BUTTON.button @@ -180,7 +194,7 @@ function BUTTON.setDefaultOption(D) if button[k] ~= nil then button[k] = v else - error("There is no "..k.." parameter in BUTTON!") + error("Parameter named ["..k.."] is not existed or cannot be set default value, in BUTTON!") end end end