DanisRace/Scripts/Mouse.py

80 lines
2.1 KiB
Python
Raw Permalink Normal View History

2024-07-13 16:15:50 +03:00
# Gpl3 or later
# (C) J.Y.Amihud 2024
# Stuff related to mouse control
import bge
2025-03-16 04:47:56 +02:00
from Scripts.Common import *
2024-07-13 16:15:50 +03:00
def MouseLook(cont):
MouseLookActual(cont)
def MouseLookActual(cont, workanyway=False):
if not bge.logic.globalDict.get("mouse-active", True):
return
2024-08-27 15:34:26 +03:00
2024-07-13 16:15:50 +03:00
# We need this to be able disable mouse look from the
# the code.
# Getting object that will rotate.
if type(cont) == bge.types.SCA_PythonController:
obj = cont.owner
else: obj = cont
scene = bge.logic.getCurrentScene()
dani = scene.objects["Dani_Box"]
# Disabling the camera rotation
2024-08-27 15:34:26 +03:00
if not workanyway and dani.get("driving"):
2024-07-13 16:15:50 +03:00
return
# Getting mouse position on the screen.
mouse = bge.logic.mouse
pos = list(mouse.position)
# Mouse positions are normalized to be 0.5 at the center.
# We need center to be 0.0.
pos[0] -= 0.5
pos[1] -= 0.5
2024-08-27 15:34:26 +03:00
if round(pos[0], 2) == 0:
pos[0] = 0
if round(pos[1], 2) == 0:
pos[1] = 0
2024-07-13 16:15:50 +03:00
# Getting factor information about how to move the mouse
# from the object.
my = -obj.get("mouse_Y", 1.0)
mx = -obj.get("mouse_X", 1.0)
mg = obj.get("mouse_global", True)
# Applying the rotation.
obj.applyRotation((pos[1]*my, 0, pos[0]*mx), mg)
# Centring the mouse.
CenterCursor()
def CenterCursor():
if not bge.logic.globalDict.get("mouse-active", True):
return
bge.render.setMousePosition(int(bge.render.getWindowWidth() / 2), int(bge.render.getWindowHeight() / 2))
2025-03-16 04:47:56 +02:00
def DeactivateToggler():
# Press Tab to release mouse cursor.
keys = bge.logic.globalDict["keys"]
if "mouse-active" not in bge.logic.globalDict:
bge.logic.globalDict["mouse-active"] = True
if keycodes["Tab"] in keys and not bge.logic.globalDict.get("mouse-active-timer"):
bge.logic.globalDict["mouse-active"] = not bge.logic.globalDict["mouse-active"]
bge.logic.globalDict["mouse-active-timer"] = 10
if bge.logic.globalDict.get("mouse-active-timer"):
bge.logic.globalDict["mouse-active-timer"] -= 1