Added text table
This commit is contained in:
parent
d7dcda1ada
commit
8f9f2dd8f4
3 changed files with 56 additions and 5 deletions
18
button.lua
18
button.lua
|
|
@ -1,4 +1,5 @@
|
||||||
require 'point'
|
require 'point'
|
||||||
|
require 'text'
|
||||||
|
|
||||||
-- Clickable UI button
|
-- Clickable UI button
|
||||||
---@class Button
|
---@class Button
|
||||||
|
|
@ -6,29 +7,35 @@ require 'point'
|
||||||
---@field size Point
|
---@field size Point
|
||||||
---@field prevPressed boolean
|
---@field prevPressed boolean
|
||||||
---@field lastPressed boolean
|
---@field lastPressed boolean
|
||||||
|
---@field text Text
|
||||||
Button = {
|
Button = {
|
||||||
position = Point:new( 1, 1 ),
|
position = Point:new( 1, 1 ),
|
||||||
size = Point:new( 1, 1 ),
|
size = Point:new( 1, 1 ),
|
||||||
prevPressed = false,
|
prevPressed = false,
|
||||||
lastPressed = false
|
lastPressed = false,
|
||||||
|
text = Text:new()
|
||||||
}
|
}
|
||||||
|
|
||||||
-- Factory function
|
-- Factory function
|
||||||
---@param position Point | nil
|
---@param position Point | nil
|
||||||
---@param size Point | nil
|
---@param size Point | nil
|
||||||
|
---@param text string | nil
|
||||||
---@param pressed function | nil
|
---@param pressed function | nil
|
||||||
---@param held function | nil
|
---@param held function | nil
|
||||||
---@param released function | nil
|
---@param released function | nil
|
||||||
---@return Button
|
---@return Button
|
||||||
function Button:new( position, size, pressed, held, released )
|
function Button:new( position, size, text, pressed, held, released )
|
||||||
|
local pos = position or Button.position
|
||||||
|
local siz = size or Button.size
|
||||||
local button = {
|
local button = {
|
||||||
position = position or Button.position,
|
position = pos,
|
||||||
size = size or Button.size,
|
size = siz,
|
||||||
prevPressed = false,
|
prevPressed = false,
|
||||||
lastPressed = false,
|
lastPressed = false,
|
||||||
pressed = pressed or Button.pressed,
|
pressed = pressed or Button.pressed,
|
||||||
held = held or Button.held,
|
held = held or Button.held,
|
||||||
released = released or Button.released
|
released = released or Button.released,
|
||||||
|
text = Text:new( pos, siz, text)
|
||||||
}
|
}
|
||||||
|
|
||||||
setmetatable( button, { __index = self } )
|
setmetatable( button, { __index = self } )
|
||||||
|
|
@ -49,6 +56,7 @@ function Button:draw()
|
||||||
local x, y = self.position:coords()
|
local x, y = self.position:coords()
|
||||||
local w, h = self.size:coords()
|
local w, h = self.size:coords()
|
||||||
love.graphics.rectangle( 'line', x, y, w, h )
|
love.graphics.rectangle( 'line', x, y, w, h )
|
||||||
|
self.text:draw()
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Check if given point is in bounds of button
|
-- Check if given point is in bounds of button
|
||||||
|
|
|
||||||
3
menu.lua
3
menu.lua
|
|
@ -49,6 +49,7 @@ MenuStateIndex = {
|
||||||
local startButton = Button:new(
|
local startButton = Button:new(
|
||||||
Point:new( 240, 120 ),
|
Point:new( 240, 120 ),
|
||||||
Point:new( 320, 80 ),
|
Point:new( 320, 80 ),
|
||||||
|
'Start',
|
||||||
nil,
|
nil,
|
||||||
nil,
|
nil,
|
||||||
function()
|
function()
|
||||||
|
|
@ -61,6 +62,7 @@ local startButton = Button:new(
|
||||||
local exitToMenuButton = Button:new(
|
local exitToMenuButton = Button:new(
|
||||||
Point:new( 240, 200 ),
|
Point:new( 240, 200 ),
|
||||||
Point:new( 320, 80 ),
|
Point:new( 320, 80 ),
|
||||||
|
'Return to menu',
|
||||||
nil,
|
nil,
|
||||||
nil,
|
nil,
|
||||||
function()
|
function()
|
||||||
|
|
@ -71,6 +73,7 @@ local exitToMenuButton = Button:new(
|
||||||
local exitGameButton = Button:new(
|
local exitGameButton = Button:new(
|
||||||
Point:new( 240, 240 ),
|
Point:new( 240, 240 ),
|
||||||
Point:new( 320, 80 ),
|
Point:new( 320, 80 ),
|
||||||
|
'Exit',
|
||||||
nil,
|
nil,
|
||||||
nil,
|
nil,
|
||||||
function()
|
function()
|
||||||
|
|
|
||||||
40
text.lua
Normal file
40
text.lua
Normal file
|
|
@ -0,0 +1,40 @@
|
||||||
|
require 'point'
|
||||||
|
|
||||||
|
-- default font size
|
||||||
|
local fontsize = 12
|
||||||
|
|
||||||
|
-- UI Text
|
||||||
|
---@class Text
|
||||||
|
---@field position Point
|
||||||
|
---@field size Point
|
||||||
|
---@field text string
|
||||||
|
---@field align love.AlignMode
|
||||||
|
Text = {
|
||||||
|
position = Point:new( 1, 1 ),
|
||||||
|
size = Point:new( 1, 1 ),
|
||||||
|
text = "Sample text",
|
||||||
|
align = "center"
|
||||||
|
}
|
||||||
|
|
||||||
|
function Text:new( position, size, text, align )
|
||||||
|
local txt = {
|
||||||
|
position = position or Text.position,
|
||||||
|
size = size or Text.size,
|
||||||
|
text = text or Text.text,
|
||||||
|
align = align or Text.align
|
||||||
|
}
|
||||||
|
|
||||||
|
setmetatable( txt, { __index = self } )
|
||||||
|
|
||||||
|
return txt
|
||||||
|
end
|
||||||
|
|
||||||
|
function Text:draw()
|
||||||
|
local x, y = self.position:coords()
|
||||||
|
local w, h = self.size:coords()
|
||||||
|
local oy = 0
|
||||||
|
if self.align == "center" then
|
||||||
|
oy = -h / 2 - fontsize / 2
|
||||||
|
end
|
||||||
|
love.graphics.printf(self.text, x, y, w, self.align, 0, 1, 1, 0, oy )
|
||||||
|
end
|
||||||
Loading…
Add table
Add a link
Reference in a new issue