This commit is contained in:
apple502j 2025-05-04 02:06:30 +00:00 committed by GitHub
commit ff42b414c1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 51 additions and 10 deletions

View file

@ -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?

View file

@ -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);

View file

@ -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);