diff --git a/libs/simple-button.lua b/libs/simple-button.lua new file mode 100644 index 0000000..a5766e6 --- /dev/null +++ b/libs/simple-button.lua @@ -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 \ No newline at end of file diff --git a/load.lua b/load.lua index e71a625..f56ec0e 100644 --- a/load.lua +++ b/load.lua @@ -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) diff --git a/scene/touch_config.lua b/scene/touch_config.lua index 45fe6f4..681d377 100644 --- a/scene/touch_config.lua +++ b/scene/touch_config.lua @@ -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)