diff --git a/game/main.lua b/game/main.lua index fc4d77d..1e38d95 100644 --- a/game/main.lua +++ b/game/main.lua @@ -47,6 +47,48 @@ function love.load() 16 * 5 ) tilemap:random(10, 10) + +end + +-- 0: nothing +-- 1: holding LMB +-- 2: dragging with LMB held +local mouseState = 0 +local mouseStartX = 0 +local mouseStartY = 0 +local mouseEndX = 0 +local mouseEndY = 0 + +function love.mousepressed(x, y, button) + if button == 1 then + if mouseState == 0 then + mouseState = 1 + mouseStartX = x + mouseStartY = y + end + end +end + +function love.mousemoved(x, y, dx, dy) + if mouseState == 1 then + mouseState = 2 + end + -- fallthrough + if mouseState == 2 then + mouseEndX = x + mouseEndY = y + end +end + +function love.mousereleased(x, y, button) + if button == 1 then + if mouseState == 1 then + -- TODO: callback + elseif mouseState == 2 then + -- TODO: callback + end + mouseState = 0 + end end function love.update(dt) @@ -84,6 +126,12 @@ function love.draw() drawText('Hellorld!', 160, 100) + if mouseState == 2 then + love.graphics.setColor(0, 255, 255) + love.graphics.rectangle("line", mouseStartX, mouseStartY, mouseEndX - mouseStartX, mouseEndY - mouseStartY) + love.graphics.setColor(255, 255, 255) + end + love.graphics.draw(hamsterMouse, love.mouse.getX(), love.mouse.getY()) platform.drawEnd() diff --git a/game/platform/native.lua b/game/platform/native.lua index be35531..df99698 100644 --- a/game/platform/native.lua +++ b/game/platform/native.lua @@ -11,6 +11,16 @@ function platform.init() love.graphics.setCanvas() -- Reset the canvas to avoid any bugs love.window.setMode(screenWidth * screenScale, screenHeight * screenScale, { resizable = false, vsync = true }) canvas = love.graphics.newCanvas(screenWidth, screenHeight) + + love.handlers.mousemoved = function(x, y, dx, dy, t) + if love.mousemoved then return love.mousemoved(x / screenScale, y / screenScale, dx / screenScale, dy / screenScale, t) end + end + love.handlers.mousepressed = function(x, y, b, t, c) + if love.mousepressed then return love.mousepressed(x / screenScale, y / screenScale, b, t, c) end + end + love.handlers.mousereleased = function(x, y, b, t, c) + if love.mousereleased then return love.mousereleased(x / screenScale, y / screenScale, b, t, c) end + end end function platform.drawStart()