mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-06-08 03:24:56 -04:00
Merge f532499ba3
into f207757ef0
This commit is contained in:
commit
ff42b414c1
3 changed files with 51 additions and 10 deletions
|
@ -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;
|
||||
|
@ -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?
|
||||
|
|
|
@ -743,6 +743,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);
|
||||
|
|
|
@ -269,6 +269,39 @@ test('isTouchingPoint', t => {
|
|||
t.end();
|
||||
});
|
||||
|
||||
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('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;
|
||||
// (0, 0) is the top left, and (canvasWidth, canvasHeight) is the bottom right
|
||||
r.ioDevices.mouse.postData({
|
||||
canvasHeight: 360,
|
||||
canvasWidth: 480,
|
||||
x: 240,
|
||||
y: 180
|
||||
});
|
||||
t.equals(a.isTouchingObject('_mouse_'), true);
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('isTouchingEdge', t => {
|
||||
const r = new Runtime();
|
||||
const s = new Sprite(null, r);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue