Mouse look limited by screen size

Good day community!

I would link to know if there is a way remove the limitation of the screen for the mouse movement, because the script I am using simulates the camera rotation by comparing the mouse position over the screen, but when the mouse hits the border of the screen the camera can’t be moved to that direction any more.

This script is attached to a Empty Plane Axis object and a camera is also parented to it, the script is triggered by an always pulse on sensor.

import bge, math
from bge import logic

cont = logic.getCurrentController()
own = cont.owner
scene = logic.getCurrentScene()
obj = scene.objects
gd = logic.globalDict

theta = gd['theta']
phi = gd['phi']
radius = gd['radius']
pivot = gd['mouse_pivot']

player = obj['player1']

mouse = logic.mouse
mouse.visible = True

v1 = own.getVectTo(player)[1]

own.alignAxisToVect([0.0, 0.0, 1.0], 2, 1)
own.alignAxisToVect(v1, 1, 1)

#if mouse.position[0] < 0 or mouse.position[0] > 1:
#    mouse.position = 0.5, mouse.position[1]
#    gd['allowCameraControl'] = False
#if mouse.position[1] < 0 or mouse.position[1] > 1:
#    mouse.position = mouse.position[0], 0.5
#    gd['allowCameraControl'] = False

#if mouse.position[0] == 0.5 or if mouse.position[1] == 0.5:
    #gd['allowCameraControl'] = True

if gd['allowCameraControl'] == True:
    if logic.mouse.events[196] == 2:
        if mouse.position[0] < pivot[0] - 0.0001:
            theta += 2 * abs(pivot[0] - mouse.position[0])
        elif mouse.position[0] > pivot[0] + 0.0001:
            theta -= 2 * abs(pivot[0] - mouse.position[0])
    if logic.mouse.events[197] == 2:
        if mouse.position[1] > pivot[1] + 0.0001:
            phi -= 2 * abs(pivot[1] - mouse.position[1])
        if mouse.position[1] < pivot[1] - 0.0001:
            phi += 2 * abs(pivot[1] - mouse.position[1])

if phi < (1 / 8) * math.pi:
    phi = (1 / 8) * math.pi
if phi > math.pi -(1 / 8) * math.pi:
    phi = math.pi - (1 / 8) * math.pi
pivot = mouse.position

if logic.mouse.events[194] == 2 and radius > 5:
    radius -= 1
elif logic.mouse.events[195] == 2 and radius < 50:
    radius += 1

x = player.position[0] + radius * math.cos(theta) * math.sin(phi)
y = player.position[1] + radius * math.sin(theta) * math.sin(phi)
z = player.position[2] + radius * math.cos(phi)

own.position = own.position.lerp([x, y, z], 0.1)

gd['theta'] = theta
gd['phi'] = phi
gd['radius'] = radius
gd['mouse_pivot'] = pivot

I also have this code to initialize the global variables, this script is attached to the same object as the other one and triggered once by an always sensor without pulse mode, the script is marked to be executed before anything else:

import bge, math, Rasterizer
from bge import logic

scene = logic.getCurrentScene()
gd = logic.globalDict

def initiate_vars():
    # Camera
    gd['radius'] = 10.0
    gd['theta'] = 0.0
    gd['phi'] = math.pi / 3
    gd['mouse_pivot'] = logic.mouse.position
    gd['allowCameraControl'] = True

initiate_vars()

Blend file: