Wrapping mouse cursor?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
replicate
Prole
Posts: 3
Joined: Tue Apr 13, 2010 11:18 pm

Wrapping mouse cursor?

Post by replicate »

I've searched high and low, and haven't been able to find anything relevant, so here goes: Could anyone tell me a simple way to make the mouse cursor wrap around the screen edges? ie. if you're moving your mouse all the way to the right and hit the screen edge, I want the cursor to jump back to the left side of the screen, and continue moving right until you quit moving in that direction. Vice versa in the other direction. Any help would be greatly appreciated, and thanks in advance.
User avatar
nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

Re: Wrapping mouse cursor?

Post by nevon »

I suppose you could use love.mouse.setPosition to move it to the opposite side when you reach the edge of the window.
User avatar
kalle2990
Party member
Posts: 245
Joined: Sat Sep 12, 2009 1:17 pm
Location: Sweden

Re: Wrapping mouse cursor?

Post by kalle2990 »

This should do that :)

Code: Select all

function love.load()
	w = love.graphics.getWidth()
	h = love.graphics.getHeight()
	margin = 80 --This is a safe value, if you're running fullscreen, set this to something smaller. Like 5
end

function love.update(dt)
	local x, y = love.mouse.getPosition()
	local newX, newY = x, y
	if x < margin then
		newX = w - margin
	end
	if x > w - margin then
		newX = margin
	end
	if y < margin then
		newY = h - margin
	end
	if y > h - margin then
		newY = margin
	end
	if newX ~= x or newY ~= y then
		love.mouse.setGrab(true)
		love.mouse.setPosition(newX, newY)
		love.mouse.setGrab(false)
	end
end
replicate
Prole
Posts: 3
Joined: Tue Apr 13, 2010 11:18 pm

Re: Wrapping mouse cursor?

Post by replicate »

Thank you both kindly! I <3 this community.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 0 guests