mirror of
https://gitea.com/SweetSea-ButImNotSweet/tromi_mobile.git
synced 2025-01-08 17:33:09 +08:00
Make a simple BUTTON module
This commit is contained in:
51
libs/simple-button.lua
Normal file
51
libs/simple-button.lua
Normal 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
|
||||
10
load.lua
10
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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user