/** * Manager responsible for the interactions between the experiment's stimuli and the mouse. * * @author Alain Pitiot * @version 2020.5 * @copyright (c) 2020 Ilixa Ltd. ({@link http://ilixa.com}) * @license Distributed under the terms of the MIT License */ import {PsychoJS} from './PsychoJS'; import {PsychObject} from '../util/PsychObject'; import * as util from '../util/Util'; /** *
This manager handles the interactions between the experiment's stimuli and the mouse.
*Note: the unit of Mouse is that of its associated Window.
* * @name module:core.Mouse * @class * @extends PsychObject * @param {Object} options * @param {String} options.name - the name used when logging messages from this stimulus * @param {Window} options.win - the associated Window * @param {boolean} [options.autoLog= true] - whether or not to log * * @todo visible is not handled at the moment (mouse is always visible) */ export class Mouse extends PsychObject { constructor({ name, win, autoLog = true } = {}) { super(win._psychoJS, name); // note: those are in window units: this._lastPos = undefined; this._prevPos = undefined; // used for motion detection and timing this._movedistance = 0.0; const units = win.units; const visible = 1; this._addAttributes(Mouse, win, units, visible, autoLog); this.status = PsychoJS.Status.NOT_STARTED; } /** * Get the current position of the mouse in mouse/Window units. * * @name module:core.Mouse#getPos * @function * @public * @return {Array.number} the position of the mouse in mouse/Window units */ getPos() { // get mouse position in the canvas: const mouseInfo = this.psychoJS.eventManager.getMouseInfo(); let pos_px = mouseInfo.pos.slice(); // convert to the associated window's reference frame with (0,0) as the centre of the window: pos_px[0] = pos_px[0] - this.win.size[0] / 2; pos_px[1] = this.win.size[1] / 2 - pos_px[1]; // convert to window units: this._lastPos = util.to_win(pos_px, 'pix', this._win); return this._lastPos; } /** * Get the position of the mouse relative to that at the last call to getRel * or getPos, in mouse/Window units. * * @name module:core.Mouse#getRel * @function * @public * @return {Array.number} the relation position of the mouse in mouse/Window units. */ getRel() { if (typeof this._lastPos === 'undefined') { return this.getPos(); } else { // note: (this.getPos()-lastPos) would not work here since getPos changes this._lastPos const lastPos = this._lastPos; const pos = this.getPos(); return [-lastPos[0] + pos[0], -lastPos[1] + pos[1]]; } } /** * Get the travel of the mouse scroll wheel since the last call to getWheelRel. * *Note: Even though this method returns a [x, y] array, for most wheels/systems y is the only * value that varies.
* * @name module:core.Mouse#getWheelRel * @function * @public * @return {Array.number} the mouse scroll wheel travel */ getWheelRel() { const mouseInfo = this.psychoJS.eventManager.getMouseInfo(); const wheelRel_px = mouseInfo.wheelRel.slice(); // convert to window units: const wheelRel = util.to_win(wheelRel_px, 'pix', this._win); mouseInfo.wheelRel = [0, 0]; return wheelRel; } /** * Get the status of each button (pressed or released) and, optionally, the time elapsed between the last call to [clickReset]{@link module:core.Mouse#clickReset} and the pressing or releasing of the buttons. * *Note: clickReset is typically called at stimulus onset. When the participant presses a button, the time elapsed since the clickReset is stored internally and can be accessed any time afterwards with getPressed.
* * @name module:core.Mouse#getPressed * @function * @public * @param {boolean} [getTime= false] whether or not to also return timestamps * @return {Array.number | Array.Note: Since there can only be one button pressed at a time, there is no point passing in a "buttons" array like PsychoPy. Pass in a "wanted" array index for the target button instead.
* * @name module:core.Mouse#isPressedIn * @function * @public * @param {object|module:visual.VisualStim} [stimulus= undefined] A type of stimulus implementing a `contains()` method. * @param {number} [wanted= undefined] The target button index. * @return {boolean} Whether mouse with button(s) pressed is contained within stimulus. */ isPressedIn(stimulus, wanted) { // Will throw if stimulus is falsy or non-object like if (typeof stimulus.contains === 'function') { const { buttons } = this.psychoJS.eventManager.getMouseInfo(); // If no specific button wanted, any pressed will do const pressed = Number.isInteger(wanted) ? buttons.pressed[wanted] > 0 : buttons.pressed.some(v => v > 0); return pressed && stimulus.contains(this); } return false; } /** * Determine whether the mouse has moved beyond a certain distance. * *distance *
reset *