UI button and main menu beginning
This commit is contained in:
parent
293f529613
commit
319a642688
4 changed files with 128 additions and 22 deletions
83
button.lua
Normal file
83
button.lua
Normal file
|
@ -0,0 +1,83 @@
|
||||||
|
require 'point'
|
||||||
|
|
||||||
|
-- Clickable UI button
|
||||||
|
---@class Button
|
||||||
|
---@field position Point
|
||||||
|
---@field size Point
|
||||||
|
---@field prevPressed boolean
|
||||||
|
---@field lastPressed boolean
|
||||||
|
Button = {
|
||||||
|
position = Point:new( 1, 1 ),
|
||||||
|
size = Point:new( 1, 1 ),
|
||||||
|
prevPressed = false,
|
||||||
|
lastPressed = false
|
||||||
|
}
|
||||||
|
|
||||||
|
-- Factory function
|
||||||
|
---@param position Point | nil
|
||||||
|
---@param size Point | nil
|
||||||
|
---@param pressed function | nil
|
||||||
|
---@param held function | nil
|
||||||
|
---@param released function | nil
|
||||||
|
---@return Button
|
||||||
|
function Button:new( position, size, pressed, held, released )
|
||||||
|
local button = {
|
||||||
|
position = position or Button.position,
|
||||||
|
size = size or Button.size,
|
||||||
|
prevPressed = false,
|
||||||
|
lastPressed = false,
|
||||||
|
pressed = pressed or Button.pressed,
|
||||||
|
held = held or Button.held,
|
||||||
|
released = released or Button.released
|
||||||
|
}
|
||||||
|
|
||||||
|
setmetatable( button, { __index = self } )
|
||||||
|
|
||||||
|
return button
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Called when button just clicked
|
||||||
|
function Button:pressed() end
|
||||||
|
-- Called when button is held
|
||||||
|
---@param dt number
|
||||||
|
function Button:held( dt ) dt = dt end
|
||||||
|
-- Called when button is just unclicked
|
||||||
|
function Button:released() end
|
||||||
|
|
||||||
|
-- Drawing function
|
||||||
|
function Button:draw()
|
||||||
|
local x, y = self.position:coords()
|
||||||
|
local w, h = self.size:coords()
|
||||||
|
love.graphics.rectangle( 'line', x, y, w, h )
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Check if given point is in bounds of button
|
||||||
|
---@param point Point
|
||||||
|
---@return boolean
|
||||||
|
function Button:checkPoint( point )
|
||||||
|
local px, py = point:coords()
|
||||||
|
local sx, sy = self.position:coords()
|
||||||
|
local ex, ey = self.position:coords()
|
||||||
|
ex = ex + sx
|
||||||
|
ey = ey + sy
|
||||||
|
return px >= sx and px <= ex and py >= sy and py <= ey
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Changing states and calls appropriate functions
|
||||||
|
---@param dt number
|
||||||
|
---@param point Point
|
||||||
|
function Button:update( dt, point, pressed )
|
||||||
|
self.prevPressed = self.lastPressed
|
||||||
|
self.lastPressed = pressed
|
||||||
|
|
||||||
|
if self.lastPressed and not self.prevPressed then
|
||||||
|
local inBounds = self:checkPoint( point )
|
||||||
|
if inBounds then
|
||||||
|
self:pressed()
|
||||||
|
end
|
||||||
|
elseif self.lastPressed and self.prevPressed then
|
||||||
|
self:held( dt )
|
||||||
|
elseif not self.lastPressed and self.prevPressed then
|
||||||
|
self:released()
|
||||||
|
end
|
||||||
|
end
|
2
grid.lua
2
grid.lua
|
@ -12,7 +12,7 @@ Grid = {
|
||||||
|
|
||||||
-- Factory function
|
-- Factory function
|
||||||
---@param size Point
|
---@param size Point
|
||||||
---@returns Grid
|
---@return Grid
|
||||||
function Grid:new( size )
|
function Grid:new( size )
|
||||||
local grid = {
|
local grid = {
|
||||||
lines = {},
|
lines = {},
|
||||||
|
|
60
main.lua
60
main.lua
|
@ -3,8 +3,11 @@ require 'point'
|
||||||
require 'line'
|
require 'line'
|
||||||
require 'mouse'
|
require 'mouse'
|
||||||
require 'levelhandler'
|
require 'levelhandler'
|
||||||
|
require 'button'
|
||||||
Input = require 'input'
|
Input = require 'input'
|
||||||
|
|
||||||
|
InMenu = true
|
||||||
|
|
||||||
local function updateCellSize()
|
local function updateCellSize()
|
||||||
local width, height = love.graphics.getDimensions()
|
local width, height = love.graphics.getDimensions()
|
||||||
local gridX, gridY = GameGrid.size:coords();
|
local gridX, gridY = GameGrid.size:coords();
|
||||||
|
@ -14,8 +17,16 @@ local function updateCellSize()
|
||||||
end
|
end
|
||||||
|
|
||||||
function love.load()
|
function love.load()
|
||||||
GameGrid = LevelHandler:first()
|
love.window.setMode( 800, 480 )
|
||||||
updateCellSize()
|
|
||||||
|
local menuButtonReleased = function()
|
||||||
|
InMenu = false
|
||||||
|
GameGrid = LevelHandler:first()
|
||||||
|
updateCellSize()
|
||||||
|
end
|
||||||
|
|
||||||
|
MenuStartButton = Button:new( Point:new( 240, 120 ), Point:new( 320, 80 ), nil, nil, menuButtonReleased )
|
||||||
|
|
||||||
|
|
||||||
love.graphics.setLineStyle( Config.lineStyle )
|
love.graphics.setLineStyle( Config.lineStyle )
|
||||||
end
|
end
|
||||||
|
@ -23,33 +34,40 @@ end
|
||||||
function love.update( dt )
|
function love.update( dt )
|
||||||
Mouse:update()
|
Mouse:update()
|
||||||
Input:update()
|
Input:update()
|
||||||
|
if InMenu then
|
||||||
|
MenuStartButton:update( dt, Point:new( Mouse.x, Mouse.y ), Mouse.pressed )
|
||||||
|
else
|
||||||
|
if Input:actionReleased( 'exit' ) then
|
||||||
|
love.event.quit()
|
||||||
|
end
|
||||||
|
|
||||||
if Input:actionReleased( 'exit' ) then
|
if Input:actionReleased( 'nextlevel' ) then
|
||||||
love.event.quit()
|
if GameGrid:isCompleted() then
|
||||||
end
|
GameGrid = LevelHandler:next()
|
||||||
|
updateCellSize()
|
||||||
if Input:actionReleased( 'nextlevel' ) then
|
end
|
||||||
if GameGrid:isCompleted() then
|
|
||||||
GameGrid = LevelHandler:next()
|
|
||||||
updateCellSize()
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function love.draw()
|
function love.draw()
|
||||||
GameGrid:draw()
|
if InMenu then
|
||||||
|
MenuStartButton:draw()
|
||||||
|
else
|
||||||
|
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 )
|
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 )
|
love.graphics.print( text, 64, 256 )
|
||||||
|
|
||||||
if Mouse.dragged then
|
if Mouse.dragged then
|
||||||
love.graphics.print( "drag", 64, 300 )
|
love.graphics.print( "drag", 64, 300 )
|
||||||
end
|
end
|
||||||
if Mouse.lastLine ~= nil then
|
if Mouse.lastLine ~= nil then
|
||||||
love.graphics.print( tostring( Mouse.lastLine ), 128, 300 )
|
love.graphics.print( tostring( Mouse.lastLine ), 128, 300 )
|
||||||
end
|
end
|
||||||
|
|
||||||
if GameGrid:isCompleted() then
|
if GameGrid:isCompleted() then
|
||||||
love.graphics.print( "Grid completed. Press space to proceed to next", 64, 320 )
|
love.graphics.print( "Grid completed. Press space to proceed to next", 64, 320 )
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -31,6 +31,11 @@ function Mouse:update()
|
||||||
Mouse.lastPoint = Mouse.point
|
Mouse.lastPoint = Mouse.point
|
||||||
Mouse.lastPressed = Mouse.pressed
|
Mouse.lastPressed = Mouse.pressed
|
||||||
Mouse.pressed = love.mouse.isDown( 1 )
|
Mouse.pressed = love.mouse.isDown( 1 )
|
||||||
|
|
||||||
|
if InMenu then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
Mouse.point = GridPoint.snapCoords( Point:new( Mouse.x, Mouse.y ) )
|
Mouse.point = GridPoint.snapCoords( Point:new( Mouse.x, Mouse.y ) )
|
||||||
if Mouse.lastLine ~= nil then
|
if Mouse.lastLine ~= nil then
|
||||||
local pointsLen = #Mouse.lastLine.points
|
local pointsLen = #Mouse.lastLine.points
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue