mirror of
https://gitea.com/SweetSea-ButImNotSweet/tromi_mobile.git
synced 2025-01-08 17:33:09 +08:00
51 lines
1.9 KiB
Lua
51 lines
1.9 KiB
Lua
local defaultFont = love.graphics.newFont(20)
|
|
local function defaultDrawingTextFunc(name, x, y, w, h)
|
|
love.graphics.setFont(defaultFont)
|
|
love.graphics.printf(name, x, y, w, "center")
|
|
end
|
|
|
|
local button = {}; button.__index = button
|
|
function button:draw()
|
|
love.graphics.setColor(1,1,1)
|
|
love.graphics.setLineWidth(self.lineWidth)
|
|
love.graphics.rectangle('line', self.x, self.y, self.w, self.h)
|
|
self.drawingTextFunc(self.name, self.x, self.y, self.w, self.h)
|
|
end
|
|
|
|
|
|
local BUTTON = {}
|
|
|
|
---@param name string @ Name of the button, will be shown
|
|
---@param x number
|
|
---@param y number
|
|
---@param w number
|
|
---@param h number
|
|
---@param drawingTextFunc? function
|
|
---@param lineWidth? number|1 @ Line width will be used to draw button
|
|
function BUTTON.new(name, x, y, w, h, lineWidth, drawingTextFunc)
|
|
assert(type(name) == "string", "[name] is missing or not valid")
|
|
assert(type(x) == "number" and x > 0, "[x] must be a positive integer")
|
|
assert(type(y) == "number" and y > 0, "[y] must be a positive integer")
|
|
assert(type(w) == "number" and w > 0, "[w] must be a positive integer")
|
|
assert(type(h) == "number" and h > 0, "[h] must be a positive integer")
|
|
assert(type(drawingTextFunc) == "function" or type(drawingTextFunc) == "nil", "[drawingTextFunc] must be a function or nil")
|
|
assert((type(lineWidth) == "number" and lineWidth > 0) or type(drawingTextFunc) == "nil", "[lineWidth] must be a postive integer")
|
|
|
|
return setmetatable({
|
|
name = name,
|
|
x = x,
|
|
y = y,
|
|
w = w,
|
|
h = h,
|
|
drawingTextFunc = drawingTextFunc or defaultDrawingTextFunc,
|
|
lineWidth = lineWidth or 1
|
|
}, 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!")
|
|
defaultDrawingTextFunc = f
|
|
end
|
|
|
|
return BUTTON |