From 8f9f2dd8f498572afebe4093bce6bd51d3040b1b Mon Sep 17 00:00:00 2001 From: 2ndbeam <2ndbeam@disroot.org> Date: Wed, 22 Oct 2025 15:21:56 +0300 Subject: [PATCH 1/3] Added text table --- button.lua | 18 +++++++++++++----- menu.lua | 3 +++ text.lua | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 text.lua diff --git a/button.lua b/button.lua index 8b56a21..8745b3e 100644 --- a/button.lua +++ b/button.lua @@ -1,4 +1,5 @@ require 'point' +require 'text' -- Clickable UI button ---@class Button @@ -6,29 +7,35 @@ require 'point' ---@field size Point ---@field prevPressed boolean ---@field lastPressed boolean +---@field text Text Button = { position = Point:new( 1, 1 ), size = Point:new( 1, 1 ), prevPressed = false, - lastPressed = false + lastPressed = false, + text = Text:new() } -- Factory function ---@param position Point | nil ---@param size Point | nil +---@param text string | nil ---@param pressed function | nil ---@param held function | nil ---@param released function | nil ---@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 = { - position = position or Button.position, - size = size or Button.size, + position = pos, + size = siz, prevPressed = false, lastPressed = false, pressed = pressed or Button.pressed, 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 } ) @@ -49,6 +56,7 @@ function Button:draw() local x, y = self.position:coords() local w, h = self.size:coords() love.graphics.rectangle( 'line', x, y, w, h ) + self.text:draw() end -- Check if given point is in bounds of button diff --git a/menu.lua b/menu.lua index db77c1a..020322d 100644 --- a/menu.lua +++ b/menu.lua @@ -49,6 +49,7 @@ MenuStateIndex = { local startButton = Button:new( Point:new( 240, 120 ), Point:new( 320, 80 ), + 'Start', nil, nil, function() @@ -61,6 +62,7 @@ local startButton = Button:new( local exitToMenuButton = Button:new( Point:new( 240, 200 ), Point:new( 320, 80 ), + 'Return to menu', nil, nil, function() @@ -71,6 +73,7 @@ local exitToMenuButton = Button:new( local exitGameButton = Button:new( Point:new( 240, 240 ), Point:new( 320, 80 ), + 'Exit', nil, nil, function() diff --git a/text.lua b/text.lua new file mode 100644 index 0000000..13b7d7b --- /dev/null +++ b/text.lua @@ -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 From 13c2ca15582c99010039ca3278c39ae79b5220e0 Mon Sep 17 00:00:00 2001 From: 2ndbeam <2ndbeam@disroot.org> Date: Wed, 22 Oct 2025 15:22:37 +0300 Subject: [PATCH 2/3] fixed sign in text offset --- text.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/text.lua b/text.lua index 13b7d7b..e6c6249 100644 --- a/text.lua +++ b/text.lua @@ -34,7 +34,7 @@ function Text:draw() local w, h = self.size:coords() local oy = 0 if self.align == "center" then - oy = -h / 2 - fontsize / 2 + oy = -h / 2 + fontsize / 2 end love.graphics.printf(self.text, x, y, w, self.align, 0, 1, 1, 0, oy ) end From 0eb2138a75f6ee39186ef22303af2471cd5107a7 Mon Sep 17 00:00:00 2001 From: 2ndbeam <2ndbeam@disroot.org> Date: Wed, 22 Oct 2025 15:39:03 +0300 Subject: [PATCH 3/3] Added level completed menu state and removed debug prints --- main.lua | 21 ++------------------- menu.lua | 49 +++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 43 insertions(+), 27 deletions(-) diff --git a/main.lua b/main.lua index 2c0533b..a1ca148 100644 --- a/main.lua +++ b/main.lua @@ -27,11 +27,8 @@ function love.update( dt ) if InMenu then Menu:update( dt, Point:new( Mouse.x, Mouse.y ), Mouse.pressed ) else - if Input:actionReleased( 'nextlevel' ) then - if GameGrid:isCompleted() then - GameGrid = LevelHandler:next() - Menu.updateCellSize() - end + if GameGrid:isCompleted() and not Mouse.pressed then + Menu.current_state = MenuStateIndex.completed end end -- Switch menu state or exit @@ -55,19 +52,5 @@ function love.draw() else love.graphics.setLineWidth( Config.cellSize * Config.lineWidth ) GameGrid:draw() - - local text = string.format( "%d:%d global\n%d:%d local\n%d:%d from start", Mouse.x, Mouse.y, Mouse.point.x, Mouse.point.y, Mouse.startX - Mouse.x, Mouse.startY - Mouse.y ) - love.graphics.print( text, 64, 256 ) - - if Mouse.dragged then - love.graphics.print( "drag", 64, 300 ) - end - if Mouse.lastLine ~= nil then - love.graphics.print( tostring( Mouse.lastLine ), 128, 300 ) - end - - if GameGrid:isCompleted() then - love.graphics.print( "Grid completed. Press space to proceed to next", 64, 320 ) - end end end diff --git a/menu.lua b/menu.lua index 020322d..9a855d0 100644 --- a/menu.lua +++ b/menu.lua @@ -43,12 +43,17 @@ MenuStateIndex = { start = 'start', pause = 'pause', levels = 'levels', - hidden = 'hidden' + hidden = 'hidden', + completed = 'completed' } +local buttonTopPos = Point:new( 240, 120 ) +local buttonBotPos = Point:new( 240, 240 ) +local buttonSize = Point:new( 320, 80 ) + local startButton = Button:new( - Point:new( 240, 120 ), - Point:new( 320, 80 ), + buttonTopPos, + buttonSize, 'Start', nil, nil, @@ -60,8 +65,8 @@ local startButton = Button:new( ) local exitToMenuButton = Button:new( - Point:new( 240, 200 ), - Point:new( 320, 80 ), + buttonBotPos, + buttonSize, 'Return to menu', nil, nil, @@ -71,8 +76,8 @@ local exitToMenuButton = Button:new( ) local exitGameButton = Button:new( - Point:new( 240, 240 ), - Point:new( 320, 80 ), + buttonBotPos, + buttonSize, 'Exit', nil, nil, @@ -81,6 +86,30 @@ local exitGameButton = Button:new( end ) +local nextLevelButton = Button:new( + buttonTopPos, + buttonSize, + 'Next level', + nil, + nil, + function() + GameGrid = LevelHandler:next() + Menu.updateCellSize() + Menu.current_state = MenuStateIndex.hidden + end +) + +local backToGameButton = Button:new( + buttonTopPos, + buttonSize, + 'Continue', + nil, + nil, + function() + Menu.current_state = MenuStateIndex.hidden + end +) + -- Menu handler ---@class Menu ---@field states MenuState[] @@ -93,11 +122,15 @@ Menu = { }), -- Pause menu [MenuStateIndex.pause] = MenuState:new({ - exitToMenuButton + backToGameButton, exitToMenuButton }), -- Level selector [MenuStateIndex.levels] = MenuState:new({ }), + -- Level completed menu + [MenuStateIndex.completed] = MenuState:new({ + nextLevelButton, exitToMenuButton + }), -- Empty state [MenuStateIndex.hidden] = MenuState:new() },