Round the values of mouse io getScratchX/Y

This commit is contained in:
Paul Kaplan 2018-12-13 11:06:31 -05:00
parent ebe06a97d9
commit 1bede31ec1
2 changed files with 10 additions and 10 deletions
src/io
test/unit

View file

@ -59,19 +59,19 @@ class Mouse {
postData (data) { postData (data) {
if (data.x) { if (data.x) {
this._clientX = data.x; this._clientX = data.x;
this._scratchX = MathUtil.clamp( this._scratchX = Math.round(MathUtil.clamp(
480 * ((data.x / data.canvasWidth) - 0.5), 480 * ((data.x / data.canvasWidth) - 0.5),
-240, -240,
240 240
); ));
} }
if (data.y) { if (data.y) {
this._clientY = data.y; this._clientY = data.y;
this._scratchY = MathUtil.clamp( this._scratchY = Math.round(MathUtil.clamp(
-360 * ((data.y / data.canvasHeight) - 0.5), -360 * ((data.y / data.canvasHeight) - 0.5),
-180, -180,
180 180
); ));
} }
if (typeof data.isDown !== 'undefined') { if (typeof data.isDown !== 'undefined') {
const previousDownState = this._isDown; const previousDownState = this._isDown;
@ -119,7 +119,7 @@ class Mouse {
/** /**
* Get the X position of the mouse in scratch coordinates. * Get the X position of the mouse in scratch coordinates.
* @return {number} Clamped X position of the mouse cursor. * @return {number} Clamped and integer rounded X position of the mouse cursor.
*/ */
getScratchX () { getScratchX () {
return this._scratchX; return this._scratchX;
@ -127,7 +127,7 @@ class Mouse {
/** /**
* Get the Y position of the mouse in scratch coordinates. * Get the Y position of the mouse in scratch coordinates.
* @return {number} Clamped Y position of the mouse cursor. * @return {number} Clamped and integer rounded Y position of the mouse cursor.
*/ */
getScratchY () { getScratchY () {
return this._scratchY; return this._scratchY;

View file

@ -40,14 +40,14 @@ test('mouseDown', t => {
const m = new Mouse(rt); const m = new Mouse(rt);
m.postData({ m.postData({
x: 10, x: 9.9,
y: 400, y: 400.1,
isDown: true, isDown: true,
canvasWidth: 480, canvasWidth: 480,
canvasHeight: 360 canvasHeight: 360
}); });
t.strictEquals(m.getClientX(), 10); t.strictEquals(m.getClientX(), 9.9);
t.strictEquals(m.getClientY(), 400); t.strictEquals(m.getClientY(), 400.1);
t.strictEquals(m.getScratchX(), -230); t.strictEquals(m.getScratchX(), -230);
t.strictEquals(m.getScratchY(), -180); t.strictEquals(m.getScratchY(), -180);
t.strictEquals(m.getIsDown(), true); t.strictEquals(m.getIsDown(), true);