Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

touching mouse-pointer returns false when outside stage #2309

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions src/io/mouse.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,13 @@ class Mouse {
postData (data) {
if (data.x) {
this._clientX = data.x;
this._scratchX = Math.round(MathUtil.clamp(
480 * ((data.x / data.canvasWidth) - 0.5),
-240,
240
));
this._rawScratchX = 480 * ((data.x / data.canvasWidth) - 0.5);
this._scratchX = Math.round(MathUtil.clamp(this._rawScratchX, -240, 240));
}
if (data.y) {
this._clientY = data.y;
this._scratchY = Math.round(MathUtil.clamp(
-360 * ((data.y / data.canvasHeight) - 0.5),
-180,
180
));
this._rawScratchY = -360 * ((data.y / data.canvasHeight) - 0.5);
this._scratchY = Math.round(MathUtil.clamp(this._rawScratchY, -180, 180));
}
if (typeof data.isDown !== 'undefined') {
const previousDownState = this._isDown;
Expand Down Expand Up @@ -133,6 +127,19 @@ class Mouse {
return this._scratchY;
}

/**
* Check if the mouse is outside the stage.
* @return {boolean} Is the mouse outside the stage?
*/
getIsOutside () {
return !(
this._rawScratchX >= -240 &&
this._rawScratchX <= 240 &&
this._rawScratchY >= -180 &&
this._rawScratchY <= 180
);
}

/**
* Get the down state of the mouse.
* @return {boolean} Is the mouse down?
Expand Down
1 change: 1 addition & 0 deletions src/sprites/rendered-target.js
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,7 @@ class RenderedTarget extends Target {
isTouchingObject (requestedObject) {
if (requestedObject === '_mouse_') {
if (!this.runtime.ioDevices.mouse) return false;
if (this.runtime.ioDevices.mouse.getIsOutside()) return false;
const mouseX = this.runtime.ioDevices.mouse.getClientX();
const mouseY = this.runtime.ioDevices.mouse.getClientY();
return this.isTouchingPoint(mouseX, mouseY);
Expand Down
16 changes: 16 additions & 0 deletions test/unit/sprites_rendered-target.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,22 @@ test('isTouchingPoint', t => {
t.end();
});

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this test. Could you also please add a test where the sprite is touching the mouse?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how to add that test?

Copy link
Contributor

@adroitwhiz adroitwhiz May 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test('isTouchingMouse', t => {
    const r = new Runtime();
    const s = new Sprite(null, r);
    const renderer = new FakeRenderer();
    r.attachRenderer(renderer);
    const a = new RenderedTarget(s, r);
    a.renderer = renderer;
    r.ioDevices.mouse.postData({
        x: 0,
        y: 0
    });
    t.equals(a.isTouchingObject('__mouse__'), true);
    t.end();
});

I think this will work because FakeRenderer.drawableTouching always returns true (haven't tried it though)

test('isTouchingObjectOutsideStage', t => {
const r = new Runtime();
const s = new Sprite(null, r);
const renderer = new FakeRenderer();
r.attachRenderer(renderer);
const a = new RenderedTarget(s, r);
a.renderer = renderer;
r.ioDevices.mouse.postData({
x: 1000,
y: -300
});
t.equals(a.isTouchingObject('__mouse__'), false);
t.end();
});


test('isTouchingEdge', t => {
const r = new Runtime();
const s = new Sprite(null, r);
Expand Down