Skip to content

Commit

Permalink
Merge pull request #889 from paulkaplan/fix-mouse-positions
Browse files Browse the repository at this point in the history
Fix mouse positions for different stage zoom levels
  • Loading branch information
paulkaplan committed Jan 17, 2018
2 parents c2ff39e + 327d117 commit 90e9e1c
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 29 deletions.
10 changes: 5 additions & 5 deletions src/blocks/scratch3_motion.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ class Scratch3MotionBlocks {
let targetX = 0;
let targetY = 0;
if (targetName === '_mouse_') {
targetX = util.ioQuery('mouse', 'getX');
targetY = util.ioQuery('mouse', 'getY');
targetX = util.ioQuery('mouse', 'getScratchX');
targetY = util.ioQuery('mouse', 'getScratchY');
} else if (targetName === '_random_') {
const stageWidth = this.runtime.constructor.STAGE_WIDTH;
const stageHeight = this.runtime.constructor.STAGE_HEIGHT;
Expand Down Expand Up @@ -106,8 +106,8 @@ class Scratch3MotionBlocks {
let targetX = 0;
let targetY = 0;
if (args.TOWARDS === '_mouse_') {
targetX = util.ioQuery('mouse', 'getX');
targetY = util.ioQuery('mouse', 'getY');
targetX = util.ioQuery('mouse', 'getScratchX');
targetY = util.ioQuery('mouse', 'getScratchY');
} else {
const pointTarget = this.runtime.getSpriteTargetByName(args.TOWARDS);
if (!pointTarget) return;
Expand Down Expand Up @@ -155,7 +155,7 @@ class Scratch3MotionBlocks {
util.yield();
}
}

glideTo (args, util) {
const targetXY = this.getTargetXY(args.TO, util);
if (targetXY) {
Expand Down
12 changes: 6 additions & 6 deletions src/blocks/scratch3_sensing.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ class Scratch3SensingBlocks {
touchingObject (args, util) {
const requestedObject = args.TOUCHINGOBJECTMENU;
if (requestedObject === '_mouse_') {
const mouseX = util.ioQuery('mouse', 'getX');
const mouseY = util.ioQuery('mouse', 'getY');
const mouseX = util.ioQuery('mouse', 'getClientX');
const mouseY = util.ioQuery('mouse', 'getClientY');
return util.target.isTouchingPoint(mouseX, mouseY);
} else if (requestedObject === '_edge_') {
return util.target.isTouchingEdge();
Expand All @@ -146,8 +146,8 @@ class Scratch3SensingBlocks {
let targetX = 0;
let targetY = 0;
if (args.DISTANCETOMENU === '_mouse_') {
targetX = util.ioQuery('mouse', 'getX');
targetY = util.ioQuery('mouse', 'getY');
targetX = util.ioQuery('mouse', 'getScratchX');
targetY = util.ioQuery('mouse', 'getScratchY');
} else {
const distTarget = this.runtime.getSpriteTargetByName(
args.DISTANCETOMENU
Expand Down Expand Up @@ -175,11 +175,11 @@ class Scratch3SensingBlocks {
}

getMouseX (args, util) {
return util.ioQuery('mouse', 'getX');
return util.ioQuery('mouse', 'getScratchX');
}

getMouseY (args, util) {
return util.ioQuery('mouse', 'getY');
return util.ioQuery('mouse', 'getScratchY');
}

getMouseDown (args, util) {
Expand Down
42 changes: 34 additions & 8 deletions src/io/mouse.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,20 @@ class Mouse {
*/
postData (data) {
if (data.x) {
this._x = data.x - (data.canvasWidth / 2);
this._clientX = data.x;
this._scratchX = MathUtil.clamp(
480 * ((data.x / data.canvasWidth) - 0.5),
-240,
240
);
}
if (data.y) {
this._y = data.y - (data.canvasHeight / 2);
this._clientY = data.y;
this._scratchY = MathUtil.clamp(
-360 * ((data.y / data.canvasHeight) - 0.5),
-180,
180
);
}
if (typeof data.isDown !== 'undefined') {
this._isDown = data.isDown;
Expand All @@ -54,19 +64,35 @@ class Mouse {
}

/**
* Get the X position of the mouse.
* Get the X position of the mouse in client coordinates.
* @return {number} Non-clamped X position of the mouse cursor.
*/
getClientX () {
return this._clientX;
}

/**
* Get the Y position of the mouse in client coordinates.
* @return {number} Non-clamped Y position of the mouse cursor.
*/
getClientY () {
return this._clientY;
}

/**
* Get the X position of the mouse in scratch coordinates.
* @return {number} Clamped X position of the mouse cursor.
*/
getX () {
return MathUtil.clamp(this._x, -240, 240);
getScratchX () {
return this._scratchX;
}

/**
* Get the Y position of the mouse.
* Get the Y position of the mouse in scratch coordinates.
* @return {number} Clamped Y position of the mouse cursor.
*/
getY () {
return MathUtil.clamp(-this._y, -180, 180);
getScratchY () {
return this._scratchY;
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/sprites/rendered-target.js
Original file line number Diff line number Diff line change
Expand Up @@ -616,8 +616,7 @@ class RenderedTarget extends Target {
// Limits test to this Drawable, so this will return true
// even if the clone is obscured by another Drawable.
const pickResult = this.runtime.renderer.pick(
x + (this.runtime.constructor.STAGE_WIDTH / 2),
-y + (this.runtime.constructor.STAGE_HEIGHT / 2),
x, y,
null, null,
[this.drawableID]
);
Expand Down
39 changes: 31 additions & 8 deletions test/unit/io_mouse.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ test('spec', t => {

t.type(m, 'object');
t.type(m.postData, 'function');
t.type(m.getX, 'function');
t.type(m.getY, 'function');
t.type(m.getClientX, 'function');
t.type(m.getClientY, 'function');
t.type(m.getScratchX, 'function');
t.type(m.getScratchY, 'function');
t.type(m.getIsDown, 'function');
t.end();
});
Expand All @@ -19,14 +21,16 @@ test('mouseUp', t => {
const m = new Mouse(rt);

m.postData({
x: 1,
x: -20,
y: 10,
isDown: false,
canvasWidth: 480,
canvasHeight: 360
});
t.strictEquals(m.getX(), -239);
t.strictEquals(m.getY(), 170);
t.strictEquals(m.getClientX(), -20);
t.strictEquals(m.getClientY(), 10);
t.strictEquals(m.getScratchX(), -240);
t.strictEquals(m.getScratchY(), 170);
t.strictEquals(m.getIsDown(), false);
t.end();
});
Expand All @@ -37,13 +41,32 @@ test('mouseDown', t => {

m.postData({
x: 10,
y: 100,
y: 400,
isDown: true,
canvasWidth: 480,
canvasHeight: 360
});
t.strictEquals(m.getX(), -230);
t.strictEquals(m.getY(), 80);
t.strictEquals(m.getClientX(), 10);
t.strictEquals(m.getClientY(), 400);
t.strictEquals(m.getScratchX(), -230);
t.strictEquals(m.getScratchY(), -180);
t.strictEquals(m.getIsDown(), true);
t.end();
});

test('at zoomed scale', t => {
const rt = new Runtime();
const m = new Mouse(rt);

m.postData({
x: 240,
y: 540,
canvasWidth: 960,
canvasHeight: 720
});
t.strictEquals(m.getClientX(), 240);
t.strictEquals(m.getClientY(), 540);
t.strictEquals(m.getScratchX(), -120);
t.strictEquals(m.getScratchY(), -90);
t.end();
});

0 comments on commit 90e9e1c

Please sign in to comment.