Make a simple BUTTON module

This commit is contained in:
Squishy (C6H12O6+NaCl+H2O)
2024-05-17 23:46:17 +07:00
parent 8f559ff88e
commit f26e4c75bd
3 changed files with 73 additions and 1 deletions

51
libs/simple-button.lua Normal file
View File

@@ -0,0 +1,51 @@
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

View File

@@ -2,6 +2,16 @@
bigint = require "libs.bigint.bigint"
number_names = require "libs.bigint.named-powers-of-ten"
BUTTON = require "libs.simple-button"
BUTTON.setDefaultDrawingTextFunction(
function(name, x, y, w, h)
love.graphics.setLineWidth(1)
love.graphics.line(0, y + (h * 0.25), 640, y + (h * 0.25))
love.graphics.line(0, y + (h * 0.75), 640, y + (h * 0.75))
drawText(name, x, y + (h * 0.25), w, 'center')
end
)
-- Fonts
FONT_tromi = love.graphics.newFont('res/fonts/monofonto rg.otf', 28)
FONT_big = love.graphics.newFont('res/fonts/monofonto rg.otf', 56)

View File

@@ -8,9 +8,20 @@ function TouchConfigScene:update()
-- TODO
end
local test_button = BUTTON.new('PHÍM THỬ - Chọn widget', 50, 50, 200, 30)
local function drawButtons()
-- drawText('Select button', 10, 10, 100, 'center')
-- love.graphics.setColor(1,1,1)
-- love.graphics.setLineWidth(3)
-- love.graphics.rectangle('line',0,0,120,40)
test_button:draw()
end
function TouchConfigScene:render()
MainBackground()
drawText('Select button', 10, 10, 100, 'center') -- 0,10 120,30
drawButtons()
end
function TouchConfigScene:onInputPress(e)