Files
tromi_mobile/libs/simple-button.lua
Squishy (C6H12O6+NaCl+H2O) cdcd71a256 TEST
2024-05-21 22:20:09 +07:00

249 lines
9.9 KiB
Lua

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 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)
---@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"|"miter"|"none"
---@field borderStyle? "rough"|"smooth"
---
---@field font? love.Font
---
---@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<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
local button = {
textOrientation = "center",
r = 0,
backgroundColor = {0,0,0,0},
hoverColor = {1,1,1,0.5},
borderColor = {1,1,1},
textColor = {1,1,1},
font = love.graphics.newFont(15),
borderWidth = 1,
borderJoin = "none",
borderStyle = "smooth",
codeWhenPressed = NULL,
codeWhenReleased = NULL,
_hovering = false,
_pressed = false,
}; button.__index = button
function button:draw()
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)
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)
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 not y then self._hovering = false; return false end
if
x >= self.x and
y >= self.y and
x <= self.x + self.w and
y <= self.y + self.h
then
self._hovering = true
else
self._hovering = false
end
return self._hovering
end
---Trigger press action, only when ``self._hovering`` is true
function button:press(x, y)
if self:isHovering(x, y) and not self._pressed then
self.codeWhenPressed()
self._pressed = true
end
end
---Trigger release action, don't need ``self._hovering`` to ``true``
---@param isMouse? boolean Button just released by mouse?
function button:release(x, y, isMouse)
if self:isHovering(x, y) and self._pressed then
self.codeWhenReleased()
self._pressed = false
if not love.mouse.isCursorSupported() then
self._hovering = false
end
end
end
local BUTTON = {}
---@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>
---***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?")
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")
else
assert(type(D.r) == "number" and D.r >= 0 or D.r == nil, "[r] must be a positive integer (CAUTION: a extra condition is temproraily ignored because you are setting default option)")
end
assert(table.contains({"center","justify","left","right"}, D.textOrientation) or D.textOrientation == nil, "[borderJoin] must be 'bevel', 'miter' or 'none")
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")
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.drawingButtonFunc) == "function" or D.drawingButtonFunc == nil, "[drawingButtonFunc] must be a function or nil")
end
---@class BUTTON.newData
---@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)
---@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"|"miter"|"none"
---@field borderStyle? "rough"|"smooth"
---
---@field font? love.Font
---
---@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<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
---@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 :)
function BUTTON.new(D)
local B = setmetatable(D, button)
BUTTON.checkDataValidation(B)
return B
end
---@param D BUTTON.button
function BUTTON.setDefaultOption(D)
BUTTON.checkDataValidation(setmetatable(D, button), true)
for k, v in pairs(D) do
if button[k] ~= nil then
button[k] = v
else
error("Parameter named ["..k.."] is not existed or cannot be set default value, in BUTTON!")
end
end
end
-- < EXTRA GENERAL OPTIONS >
---Draw all buttons in provided list
---@param list table<BUTTON.button>
function BUTTON.draw(list)
for _, v in pairs(list) do v:draw() end
end
---Check if current mouse position is inside button<br>
---Calling BUTTON.press will trigger this, but you can call it when moving mouse so button can be highlighted when being hovered
---@param list table<BUTTON.button>
---@param x number # Mouse position
---@param y number # Mouse position
function BUTTON.checkHovering(list, x, y)
for _, v in pairs(list) do v:isHovering(x, y) end
end
--- Trigger the press action, only if ``button._hovering == true``
---@param list table<BUTTON.button>
---@param x number # Mouse position
---@param y number # Mouse position
function BUTTON.press(list, x, y)
for _, v in pairs(list) do v:press(x, y) end
end
---Trigger the release action
---@param list table<BUTTON.button>
---@param x number # Mouse position
---@param y number # Mouse position
---@param isMouse? boolean # Is mouse just released a button?
function BUTTON.release(list, x, y, isMouse)
for _, v in pairs(list) do v:release(x, y, isMouse) end
end
return BUTTON