Found out that my original mouse wrapping code was not working:
func _unhandled_input (event: InputEvent ) -> void :
if event is InputEventMouseMotion :
handle_input_mouse_motion (event as InputEventMouseMotion )
func handle_input_mouse_motion (event: InputEventMouseMotion ) -> void :
var window := get_window ()
var xform := get_viewport (). get_final_transform ()
var new_position := get_viewport (). get_mouse_position () * xform + event.relative
const margin := 10
var warp := false
if new_position.x < margin:
warp = true
new_position.x += window.size.x - margin
if new_position.x > window.size.x - margin:
warp = true
new_position.x -= window.size.x - margin
if new_position.y < margin:
warp = true
new_position.y += window.size.y - margin
if new_position.y > window.size.y - margin:
warp = true
new_position.y -= window.size.y - margin
if warp:
Input . warp_mouse (new_position)
just_warped = true
However, if your Viewport
is letterboxed this would stop working because the coordinate spaces were not the same. Trying to fix the existing code wasn’t particularly desirable, as the coordinate spaces were relative to the Viewport
and I wanted to work in the space of the window. I was able to do this by using the DisplayServer
directly:
func handle_input_mouse_motion (event: InputEventMouseMotion ) -> void :
var window := Rect2i (
DisplayServer . window_get_position (),
DisplayServer . window_get_size ()
)
var new_position := DisplayServer . mouse_get_position ()
const margin := 10
var warp := false
if new_position.x < window.position.x + margin:
warp = true
new_position.x += window.size.x - (margin * 2 )
elif new_position.x > window.end.x - margin:
warp = true
new_position.x -= window.size.x - (margin * 2 )
elif new_position.y < window.position.y + margin:
warp = true
new_position.y += window.size.y - (margin * 2 )
elif new_position.y > window.end.y - margin:
warp = true
new_position.y -= window.size.y - (margin * 2 )
if warp:
DisplayServer . warp_mouse (new_position - window.position)
just_warped = true