Compare commits
No commits in common. "0eb2138a75f6ee39186ef22303af2471cd5107a7" and "d7dcda1adaf004a25a0037433f1737d27c376b41" have entirely different histories.
0eb2138a75
...
d7dcda1ada
4 changed files with 32 additions and 99 deletions
18
button.lua
18
button.lua
|
|
@ -1,5 +1,4 @@
|
||||||
require 'point'
|
require 'point'
|
||||||
require 'text'
|
|
||||||
|
|
||||||
-- Clickable UI button
|
-- Clickable UI button
|
||||||
---@class Button
|
---@class Button
|
||||||
|
|
@ -7,35 +6,29 @@ require 'text'
|
||||||
---@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, text, pressed, held, released )
|
function Button:new( position, size, pressed, held, released )
|
||||||
local pos = position or Button.position
|
|
||||||
local siz = size or Button.size
|
|
||||||
local button = {
|
local button = {
|
||||||
position = pos,
|
position = position or Button.position,
|
||||||
size = siz,
|
size = size or Button.size,
|
||||||
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 } )
|
||||||
|
|
@ -56,7 +49,6 @@ 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
|
||||||
|
|
|
||||||
21
main.lua
21
main.lua
|
|
@ -27,8 +27,11 @@ function love.update( dt )
|
||||||
if InMenu then
|
if InMenu then
|
||||||
Menu:update( dt, Point:new( Mouse.x, Mouse.y ), Mouse.pressed )
|
Menu:update( dt, Point:new( Mouse.x, Mouse.y ), Mouse.pressed )
|
||||||
else
|
else
|
||||||
if GameGrid:isCompleted() and not Mouse.pressed then
|
if Input:actionReleased( 'nextlevel' ) then
|
||||||
Menu.current_state = MenuStateIndex.completed
|
if GameGrid:isCompleted() then
|
||||||
|
GameGrid = LevelHandler:next()
|
||||||
|
Menu.updateCellSize()
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
-- Switch menu state or exit
|
-- Switch menu state or exit
|
||||||
|
|
@ -52,5 +55,19 @@ function love.draw()
|
||||||
else
|
else
|
||||||
love.graphics.setLineWidth( Config.cellSize * Config.lineWidth )
|
love.graphics.setLineWidth( Config.cellSize * Config.lineWidth )
|
||||||
GameGrid:draw()
|
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
|
||||||
end
|
end
|
||||||
|
|
|
||||||
52
menu.lua
52
menu.lua
|
|
@ -43,18 +43,12 @@ MenuStateIndex = {
|
||||||
start = 'start',
|
start = 'start',
|
||||||
pause = 'pause',
|
pause = 'pause',
|
||||||
levels = 'levels',
|
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(
|
local startButton = Button:new(
|
||||||
buttonTopPos,
|
Point:new( 240, 120 ),
|
||||||
buttonSize,
|
Point:new( 320, 80 ),
|
||||||
'Start',
|
|
||||||
nil,
|
nil,
|
||||||
nil,
|
nil,
|
||||||
function()
|
function()
|
||||||
|
|
@ -65,9 +59,8 @@ local startButton = Button:new(
|
||||||
)
|
)
|
||||||
|
|
||||||
local exitToMenuButton = Button:new(
|
local exitToMenuButton = Button:new(
|
||||||
buttonBotPos,
|
Point:new( 240, 200 ),
|
||||||
buttonSize,
|
Point:new( 320, 80 ),
|
||||||
'Return to menu',
|
|
||||||
nil,
|
nil,
|
||||||
nil,
|
nil,
|
||||||
function()
|
function()
|
||||||
|
|
@ -76,9 +69,8 @@ local exitToMenuButton = Button:new(
|
||||||
)
|
)
|
||||||
|
|
||||||
local exitGameButton = Button:new(
|
local exitGameButton = Button:new(
|
||||||
buttonBotPos,
|
Point:new( 240, 240 ),
|
||||||
buttonSize,
|
Point:new( 320, 80 ),
|
||||||
'Exit',
|
|
||||||
nil,
|
nil,
|
||||||
nil,
|
nil,
|
||||||
function()
|
function()
|
||||||
|
|
@ -86,30 +78,6 @@ local exitGameButton = Button:new(
|
||||||
end
|
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
|
-- Menu handler
|
||||||
---@class Menu
|
---@class Menu
|
||||||
---@field states MenuState[]
|
---@field states MenuState[]
|
||||||
|
|
@ -122,15 +90,11 @@ Menu = {
|
||||||
}),
|
}),
|
||||||
-- Pause menu
|
-- Pause menu
|
||||||
[MenuStateIndex.pause] = MenuState:new({
|
[MenuStateIndex.pause] = MenuState:new({
|
||||||
backToGameButton, exitToMenuButton
|
exitToMenuButton
|
||||||
}),
|
}),
|
||||||
-- Level selector
|
-- Level selector
|
||||||
[MenuStateIndex.levels] = MenuState:new({
|
[MenuStateIndex.levels] = MenuState:new({
|
||||||
}),
|
}),
|
||||||
-- Level completed menu
|
|
||||||
[MenuStateIndex.completed] = MenuState:new({
|
|
||||||
nextLevelButton, exitToMenuButton
|
|
||||||
}),
|
|
||||||
-- Empty state
|
-- Empty state
|
||||||
[MenuStateIndex.hidden] = MenuState:new()
|
[MenuStateIndex.hidden] = MenuState:new()
|
||||||
},
|
},
|
||||||
|
|
|
||||||
40
text.lua
40
text.lua
|
|
@ -1,40 +0,0 @@
|
||||||
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