Skip to content

Commit

Permalink
Merge pull request #306 from akashic-games/fix-pointevent-outside-view
Browse files Browse the repository at this point in the history
Fix to exclude pointDown event outside view
  • Loading branch information
ShinobuTakahashi authored Apr 10, 2024
2 parents 282637a + 2181ce1 commit c5cdbb9
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# CHANGELOG

## 2.9.1
* View の外をクリック時に `pointDown` イベントが発生しないよう修正

## 2.9.0
* @akashic/pdi-types@1.13.0 に追従
* サポートする `CompisiteOperation``"difference"``"saturation"` を追加(ただし Canvas 描画時のみ)
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@akashic/pdi-browser",
"version": "2.9.0",
"version": "2.9.1",
"description": "An akashic-pdi implementation for Web browsers",
"main": "index.js",
"typings": "lib/full/index.d.ts",
Expand Down
7 changes: 7 additions & 0 deletions src/handler/InputEventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ export abstract class InputEventHandler {
}

pointDown(identifier: number, pagePosition: OffsetPosition, button: PlatformButtonType): void {
// chrome で view の境界部分をクリックした際にポイント座標が view の外の座標となることがあるため、view 外の座標の場合は除外する
if ( pagePosition.offsetX < 0
|| pagePosition.offsetY < 0
|| pagePosition.offsetX > this.inputView.offsetWidth
|| pagePosition.offsetY > this.inputView.offsetHeight
) return;

this.pointTrigger.fire({
type: PlatformPointType.Down,
identifier: identifier,
Expand Down
22 changes: 22 additions & 0 deletions src/handler/__tests__/InputEventHandler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ class TestInputEventHandler extends InputEventHandler {
}

describe("InputEventHandler", () => {
// レンダリングしていないので offsetWidth,offsetHeight が取得時に 0 となるのでモック化
const spyWidth = jest.spyOn(HTMLElement.prototype, "offsetWidth", "get").mockReturnValue(640);
const spyHeight = jest.spyOn(HTMLElement.prototype, "offsetHeight", "get").mockReturnValue(320);
afterAll(() => {
spyWidth.mockClear();
spyHeight.mockClear();
});
describe("DownのDOMイベントが発生済みの時", () => {
let handler: InputEventHandler;
let identifier: number;
Expand Down Expand Up @@ -91,5 +98,20 @@ describe("InputEventHandler", () => {
expect(true).toBeTruthy();
done();
});
it("view 外の座標の場合 pointDown は呼ばれない", (done) => {
const handler = new TestInputEventHandler(document.createElement("div"));
let callCount = 0;
handler.pointTrigger.add((_object) => {
callCount++;
done.fail();
});

handler.pointDown(1, {offsetX: -0.1, offsetY: 10}, PlatformButtonType.Primary);
handler.pointDown(1, {offsetX: 10, offsetY: -0.01}, PlatformButtonType.Primary);
handler.pointDown(1, {offsetX: 640.1, offsetY: 10}, PlatformButtonType.Primary);
handler.pointDown(1, {offsetX: 10, offsetY: 320.01}, PlatformButtonType.Primary);
expect(callCount).toBe(0);
done();
});
});
});

0 comments on commit c5cdbb9

Please sign in to comment.