Skip to content

Commit 0e0b26d

Browse files
committed
BF: handle multiple mouse objects in one Routine in JS
Previously we were defining the `xys` and `buttons` with local scope. Instead remove that local definition (const/let) and the script compiler will then automatically give it global scope (var at the top of the script) To avoid likely clashes we've given more obscure names: - `_buttonXYs` - `_mouseButtons` fixes psychopy/psychojs-88
1 parent f6efca8 commit 0e0b26d

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

psychopy/experiment/components/mouse/__init__.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -417,32 +417,32 @@ def writeFrameCodeJS(self, buff):
417417
# write param checking code
418418
if (self.params['saveMouseState'].val == 'on click'
419419
or forceEnd in ['any click', 'valid click']):
420-
code = ("let buttons = %(name)s.getPressed();\n")
420+
code = ("_mouseButtons = %(name)s.getPressed();\n")
421421
buff.writeIndentedLines(code % self.params)
422422
# buff.setIndentLevel(1, relative=True)
423423
# dedentAtEnd += 1
424-
code = "if (!buttons.every( (e,i,) => (e == prevButtonState[i]) )) { // button state changed?\n"
424+
code = "if (!_mouseButtons.every( (e,i,) => (e == prevButtonState[i]) )) { // button state changed?\n"
425425
buff.writeIndented(code)
426426
buff.setIndentLevel(1, relative=True)
427427
dedentAtEnd += 1
428428
buff.writeIndented("prevButtonState = buttons;\n")
429-
code = ("if (buttons.reduce( (e, acc) => (e+acc) ) > 0) { // state changed to a new click\n")
429+
code = ("if (_mouseButtons.reduce( (e, acc) => (e+acc) ) > 0) { // state changed to a new click\n")
430430
buff.writeIndentedLines(code % self.params)
431431
buff.setIndentLevel(1, relative=True)
432432
dedentAtEnd += 1
433433

434434
elif self.params['saveMouseState'].val == 'every frame':
435-
code = "let buttons = %(name)s.getPressed();\n" % self.params
435+
code = "_mouseButtons = %(name)s.getPressed();\n" % self.params
436436
buff.writeIndented(code)
437437

438438
# only do this if buttons were pressed
439439
if self.params['saveMouseState'].val in ['on click', 'every frame']:
440-
code = ("const xys = %(name)s.getPos();\n"
441-
"%(name)s.x.push(xys[0]);\n"
442-
"%(name)s.y.push(xys[1]);\n"
443-
"%(name)s.leftButton.push(buttons[0]);\n"
444-
"%(name)s.midButton.push(buttons[1]);\n"
445-
"%(name)s.rightButton.push(buttons[2]);\n" %
440+
code = ("_mouseXYs = %(name)s.getPos();\n"
441+
"%(name)s.x.push(_mouseXYs[0]);\n"
442+
"%(name)s.y.push(_mouseXYs[1]);\n"
443+
"%(name)s.leftButton.push(_mouseButtons[0]);\n"
444+
"%(name)s.midButton.push(_mouseButtons[1]);\n"
445+
"%(name)s.rightButton.push(_mouseButtons[2]);\n" %
446446
self.params)
447447
code += ("%s.time.push(%s.getTime());\n" % (self.params['name'], self.clockStr))
448448
buff.writeIndentedLines(code)

0 commit comments

Comments
 (0)