Добавил простую обработку мыши с учётом масштаба
This commit is contained in:
parent
6e63814184
commit
e629d0ffa4
2 changed files with 58 additions and 0 deletions
|
|
@ -47,6 +47,48 @@ function love.load()
|
||||||
16 * 5
|
16 * 5
|
||||||
)
|
)
|
||||||
tilemap:random(10, 10)
|
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
|
end
|
||||||
|
|
||||||
function love.update(dt)
|
function love.update(dt)
|
||||||
|
|
@ -84,6 +126,12 @@ function love.draw()
|
||||||
|
|
||||||
drawText('Hellorld!', 160, 100)
|
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())
|
love.graphics.draw(hamsterMouse, love.mouse.getX(), love.mouse.getY())
|
||||||
|
|
||||||
platform.drawEnd()
|
platform.drawEnd()
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,16 @@ function platform.init()
|
||||||
love.graphics.setCanvas() -- Reset the canvas to avoid any bugs
|
love.graphics.setCanvas() -- Reset the canvas to avoid any bugs
|
||||||
love.window.setMode(screenWidth * screenScale, screenHeight * screenScale, { resizable = false, vsync = true })
|
love.window.setMode(screenWidth * screenScale, screenHeight * screenScale, { resizable = false, vsync = true })
|
||||||
canvas = love.graphics.newCanvas(screenWidth, screenHeight)
|
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
|
end
|
||||||
|
|
||||||
function platform.drawStart()
|
function platform.drawStart()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue