Using event.Mouse with win.viewport shifts mouse position

Hi,

I was using the win.viewport for my application and noticed that the mouse position (setPos and getPos from event.Mouse) is offset by the viewport offset. This makes it impossible to use the mouse.isPressedIn(shape) functionality for example. Is this expected behaviour or a bug?

Here is an example: I set the position of a circle as the mouse position. When displayed, the circle follows the mouse as expected (first image). After 60 frames, I set the viewport to the right of the screen, and the circle shifts by the same amount while the cursor position does not change (second image).


I am on Windows 10 with psychpy 2022.1.4 and Python 3.8.13. Here is the code for the demo:

from psychopy import __version__, core, event, visual
from psychopy.visual import circle

print(__version__)

# Setup the Window
win = visual.Window(
    fullscr=False, size=[800,600], screen=0, color='black', units='height',
    useFBO=False, # this is important for correctly sized viewport
    )

mouse = event.Mouse(win=win)

stim = visual.ImageStim(win=win,image='sin',size=(200,200),units='pix')
stim.autoDraw=True
stimText = visual.TextStim(win=win,text='',pos=(.5,.8),height=0.05,units='norm',autoDraw=True)
stimText.autoDraw=True
c = circle.Circle(win=win,radius=.02,fillColor="red",anchor="center",pos=[0,0],autoDraw=True)


for ii in range(60):
    c.pos = mouse.getPos()
    win.flip()
    if  mouse.isPressedIn(stim):
        stimText.text = 'no viewport - pressed'
    else:
        stimText.text = f'no viewport mouse pos ({mouse.getPos()})'
    
win.scissor=win.viewport=[300,0,500,600]
while not event.getKeys():
    c.pos = mouse.getPos()
    win.flip()
    if  mouse.isPressedIn(stim):
        stimText.text = 'with viewport - pressed'
    else:
        stimText.text = f'with viewport - mouse pos ({mouse.getPos()})'
win.close()
core.quit()