Skip to content

Commit

Permalink
Add/implement pc.EVENT_MOUSEOUT
Browse files Browse the repository at this point in the history
  • Loading branch information
kungfooman committed Dec 10, 2022
1 parent b07a438 commit 7c582ab
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/platform/input/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ export const EVENT_MOUSEUP = 'mouseup';
*/
export const EVENT_MOUSEWHEEL = 'mousewheel';

/**
* Name of event fired when the mouse moves out or enters another element.
*
* @type {string}
*/
export const EVENT_MOUSEOUT = 'mouseout';

/**
* Name of event fired when a new touch occurs. For example, a finger is placed on the device.
*
Expand Down
2 changes: 1 addition & 1 deletion src/platform/input/mouse-event.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class MouseEvent {
* @type {number}
*/
this.y = coords.y;
} else if (isMousePointerLocked()) {
} else if (isMousePointerLocked() || event.type === 'mouseout') {
this.x = 0;
this.y = 0;
} else {
Expand Down
12 changes: 11 additions & 1 deletion src/platform/input/mouse.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { platform } from '../../core/platform.js';
import { EventHandler } from '../../core/event-handler.js';

import { EVENT_MOUSEDOWN, EVENT_MOUSEMOVE, EVENT_MOUSEUP, EVENT_MOUSEWHEEL } from './constants.js';
import { EVENT_MOUSEDOWN, EVENT_MOUSEMOVE, EVENT_MOUSEOUT, EVENT_MOUSEUP, EVENT_MOUSEWHEEL } from './constants.js';
import { isMousePointerLocked, MouseEvent } from './mouse-event.js';

/**
Expand Down Expand Up @@ -36,6 +36,7 @@ class Mouse extends EventHandler {
this._downHandler = this._handleDown.bind(this);
this._moveHandler = this._handleMove.bind(this);
this._wheelHandler = this._handleWheel.bind(this);
this._outHandler = this._handleOut.bind(this);
this._contextMenuHandler = (event) => {
event.preventDefault();
};
Expand Down Expand Up @@ -98,6 +99,7 @@ class Mouse extends EventHandler {
window.addEventListener('mouseup', this._upHandler, opts);
window.addEventListener('mousedown', this._downHandler, opts);
window.addEventListener('mousemove', this._moveHandler, opts);
window.addEventListener('mouseout', this._outHandler, opts);
window.addEventListener('wheel', this._wheelHandler, opts);
}

Expand All @@ -113,6 +115,7 @@ class Mouse extends EventHandler {
window.removeEventListener('mouseup', this._upHandler, opts);
window.removeEventListener('mousedown', this._downHandler, opts);
window.removeEventListener('mousemove', this._moveHandler, opts);
window.removeEventListener('mouseout', this._outHandler, opts);
window.removeEventListener('wheel', this._wheelHandler, opts);
}

Expand Down Expand Up @@ -289,6 +292,13 @@ class Mouse extends EventHandler {
this.fire(EVENT_MOUSEWHEEL, e);
}

_handleOut(event) {
const e = new MouseEvent(this, event);
if (!e.event) return;

this.fire(EVENT_MOUSEOUT, e);
}

_getTargetCoords(event) {
const rect = this._target.getBoundingClientRect();
const left = Math.floor(rect.left);
Expand Down

0 comments on commit 7c582ab

Please sign in to comment.