Skip to content

Commit

Permalink
Merge pull request #447 from akashic-games/add-button-to-point-event
Browse files Browse the repository at this point in the history
add button to PointEvent
  • Loading branch information
dera- authored Jun 21, 2023
2 parents 4fd5eb2 + 29e11f0 commit 2156ac3
Show file tree
Hide file tree
Showing 11 changed files with 173 additions and 71 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# ChangeLog

## 3.13.0
* どのボタンでマウスクリックが行われたか認識できるように
* 内部モジュールの更新

## 3.12.0
* @akashic/trigger@2.0.0 に更新

Expand Down
56 changes: 28 additions & 28 deletions package-lock.json

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

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "@akashic/akashic-engine",
"version": "3.12.0",
"version": "3.13.0",
"description": "The core library of Akashic Engine",
"main": "index.js",
"dependencies": {
"@akashic/game-configuration": "~1.10.0",
"@akashic/pdi-types": "~1.8.0",
"@akashic/playlog": "~3.1.0",
"@akashic/pdi-types": "~1.11.1",
"@akashic/playlog": "~3.2.0",
"@akashic/trigger": "~2.0.0"
},
"devDependencies": {
Expand Down
22 changes: 17 additions & 5 deletions src/Event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,25 @@ export class PointEventBase<T extends PointTarget> implements Event {
pointerId: number;
point: CommonOffset;
target: T | undefined;
button: number;

constructor(pointerId: number, target: T | undefined, point: CommonOffset, player?: Player, local?: boolean, eventFlags?: number) {
constructor(
pointerId: number,
target: T | undefined,
point: CommonOffset,
player?: Player,
local?: boolean,
eventFlags?: number,
button?: number
) {
// @ts-ignore TODO: eventFlags のデフォルト値の扱い
this.eventFlags = eventFlags;
this.local = !!local;
this.player = player;
this.pointerId = pointerId;
this.target = target;
this.point = point;
this.button = button ?? 0;
}
}

Expand Down Expand Up @@ -128,9 +138,10 @@ export class PointUpEventBase<T extends PointTarget> extends PointEventBase<T> {
startDelta: CommonOffset,
player?: Player,
local?: boolean,
eventFlags?: number
eventFlags?: number,
button?: number
) {
super(pointerId, target, point, player, local, eventFlags);
super(pointerId, target, point, player, local, eventFlags, button);
this.prevDelta = prevDelta;
this.startDelta = startDelta;
}
Expand Down Expand Up @@ -160,9 +171,10 @@ export class PointMoveEventBase<T extends PointTarget> extends PointEventBase<T>
startDelta: CommonOffset,
player?: Player,
local?: boolean,
eventFlags?: number
eventFlags?: number,
button?: number
) {
super(pointerId, target, point, player, local, eventFlags);
super(pointerId, target, point, player, local, eventFlags, button);
this.prevDelta = prevDelta;
this.startDelta = startDelta;
}
Expand Down
21 changes: 15 additions & 6 deletions src/EventConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ export class EventConverter {
_game: EventConverterParameterObjectGameLike;
_playerId: string | null;
_playerTable: { [key: string]: Player };
_pointDownButtonTable: { [key: number]: number };

constructor(param: EventConverterParameterObject) {
this._game = param.game;
this._playerId = param.playerId ?? null;
this._playerTable = {};
this._pointDownButtonTable = {};
}

/**
Expand All @@ -53,6 +55,7 @@ export class EventConverter {
let prevDelta: CommonOffset;
let local: boolean;
let timestamp: number;
let button: number;

const eventCode = pev[EventIndex.General.Code];
const prio = pev[EventIndex.General.EventFlags];
Expand Down Expand Up @@ -118,7 +121,9 @@ export class EventConverter {
x: pev[EventIndex.PointDown.X],
y: pev[EventIndex.PointDown.Y]
};
return new PointDownEvent(pointerId, target, point, player, local, prio);
button = pev[EventIndex.PointDown.Button];
this._pointDownButtonTable[pointerId] = button;
return new PointDownEvent(pointerId, target, point, player, local, prio, button);

case pl.EventCode.PointMove:
local = pev[EventIndex.PointMove.Local];
Expand All @@ -137,7 +142,8 @@ export class EventConverter {
x: pev[EventIndex.PointMove.PrevDeltaX],
y: pev[EventIndex.PointMove.PrevDeltaY]
};
return new PointMoveEvent(pointerId, target, point, prevDelta, startDelta, player, local, prio);
button = this._pointDownButtonTable[pointerId];
return new PointMoveEvent(pointerId, target, point, prevDelta, startDelta, player, local, prio, button);

case pl.EventCode.PointUp:
local = pev[EventIndex.PointUp.Local];
Expand All @@ -156,7 +162,9 @@ export class EventConverter {
x: pev[EventIndex.PointUp.PrevDeltaX],
y: pev[EventIndex.PointUp.PrevDeltaY]
};
return new PointUpEvent(pointerId, target, point, prevDelta, startDelta, player, local, prio);
button = this._pointDownButtonTable[pointerId];
delete this._pointDownButtonTable[pointerId];
return new PointUpEvent(pointerId, target, point, prevDelta, startDelta, player, local, prio, button);

case pl.EventCode.Operation:
local = pev[EventIndex.Operation.Local];
Expand Down Expand Up @@ -213,7 +221,8 @@ export class EventConverter {
pointDown.point.x, // 4: X座標
pointDown.point.y, // 5: Y座標
targetId, // 6?: エンティティID
!!pointDown.local // 7?: 直前のポイントムーブイベントからのY座標の差
pointDown.button, // 7?: ボタンの種類
!!pointDown.local // 8?: ローカルイベントかどうか
];
case "point-move":
const pointMove = e as PointMoveEvent;
Expand All @@ -231,7 +240,7 @@ export class EventConverter {
pointMove.prevDelta.x, // 8: 直前のポイントムーブイベントからのX座標の差
pointMove.prevDelta.y, // 9: 直前のポイントムーブイベントからのY座標の差
targetId, // 10?: エンティティID
!!pointMove.local // 11?: 直前のポイントムーブイベントからのY座標の差
!!pointMove.local // 11?: ローカルイベントかどうか
];
case "point-up":
const pointUp = e as PointUpEvent;
Expand All @@ -249,7 +258,7 @@ export class EventConverter {
pointUp.prevDelta.x, // 8: 直前のポイントムーブイベントからのX座標の差
pointUp.prevDelta.y, // 9: 直前のポイントムーブイベントからのY座標の差
targetId, // 10?: エンティティID
!!pointUp.local // 11?: 直前のポイントムーブイベントからのY座標の差
!!pointUp.local // 11?: ローカルイベントかどうか
];
case "message":
const message = e as MessageEvent;
Expand Down
5 changes: 3 additions & 2 deletions src/EventIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* インデックスのハードコーディングを避けるため、ここで const enum で名前を与えることにする。
*
* 本当はこのファイルの内容は playlog に移管すべきだが、
* playlog に存在しない `Local` のフィールドを使うため akashic-engine 側で扱う。
* playlog に存在しない `Local` や一部の `Button` のフィールドを使うため akashic-engine 側で扱う。
*
*/
export module EventIndex {
Expand Down Expand Up @@ -77,7 +77,8 @@ export module EventIndex {
X = 4,
Y = 5,
EntityId = 6,
Local = 7
Button = 7,
Local = 8
}

export const enum PointMove {
Expand Down
9 changes: 6 additions & 3 deletions src/PointEventResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ interface PointEventHolder {
point: CommonOffset;
start: CommonOffset;
prev: CommonOffset;
button: number;
// TODO: タイムスタンプのようなものを入れて一定時間後にクリアする仕組みが必要かもしれない。
// pointUpをトリガに解放するので、pointUpを取り逃すとリークする(mapに溜まったままになってしまう)
}
Expand Down Expand Up @@ -83,7 +84,8 @@ export class PointEventResolver {
local,
point,
start: { x: e.offset.x, y: e.offset.y },
prev: { x: e.offset.x, y: e.offset.y }
prev: { x: e.offset.x, y: e.offset.y },
button: e.button ?? 0
};
this._currentPoints++;

Expand All @@ -95,9 +97,10 @@ export class PointEventResolver {
e.identifier, // 3: ポインターID
point.x, // 4: X座標
point.y, // 5: Y座標
targetId // 6?: エンティティID
targetId, // 6?: エンティティID
e.button // 7?: ボタンの種類
];
if (source && source.local) ret.push(source.local); // 7?: ローカル
if (source && source.local) ret.push(source.local); // 8?: ローカル
return ret;
}

Expand Down
3 changes: 2 additions & 1 deletion src/__tests__/ESpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,8 @@ describe("test E", () => {
const event = runtime.game._pointEventResolver.pointDown({
type: PlatformPointType.Down,
identifier: 1,
offset: { x: 0, y: 0 }
offset: { x: 0, y: 0 },
button: 0
});
expectToBeDefined(event);
runtime.game.tick(true, 0, [event]);
Expand Down
20 changes: 20 additions & 0 deletions src/__tests__/EventConvertSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,25 @@ describe("EventConverter", () => {
expect(pointDown2.player!.id).toBe("dummyPlayerId");
expect(pointDown2.player!.name).toBe("dummyName");
expect(pointDown2.player!.userData).toEqual({ userDataProp: "dummy" });

// PointMoveEvent と PointUpEvent の button の値が PointerId が同一の PointDownEvent の button と同値であること
const pdown3: pl.PointDownEvent = [pl.EventCode.PointDown, EventPriority.System, "dummyPlayerId", 1, 10, 20, 0, 2];
const pointDown3 = self.toGameEvent(pdown3) as PointDownEvent;
expect(pointDown3.type).toBe("point-down");
expect(pointDown3.pointerId).toBe(1);
expect(pointDown3.button).toBe(2);
const pmove: pl.PointMoveEvent = [pl.EventCode.PointMove, EventPriority.System, "dummyPlayerId", 1, 12, 25, 2, 5, 0, 1, 0];
const pointMove = self.toGameEvent(pmove) as PointMoveEvent;
expect(pointMove.type).toBe("point-move");
expect(pointMove.pointerId).toBe(pointDown3.pointerId);
expect(pointMove.button).toBe(pointDown3.button);
const pup: pl.PointUpEvent = [pl.EventCode.PointUp, EventPriority.System, "dummyPlayerId", 1, 15, 25, 5, 5, 1, 0, 0];
const pointUp = self.toGameEvent(pup) as PointUpEvent;
expect(pointUp.type).toBe("point-up");
expect(pointUp.pointerId).toBe(pointDown3.pointerId);
expect(pointUp.button).toBe(pointDown3.button);
// 一度PointUpEventが発生すると、PointerId が同一でも PointDownEvent の button と異なる値になる(厳密にはデフォルト値の0になる)
const pointUp2 = self.toGameEvent(pup) as PointUpEvent;
expect(pointUp2.button).toBe(0);
});
});
8 changes: 5 additions & 3 deletions src/__tests__/GameSpec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type * as pl from "@akashic/playlog";
import type { Asset, AssetConfiguration, GameConfiguration, LoadingSceneParameterObject, LocalTickModeString } from "..";
import { Camera2D, E, LoadingScene, MessageEvent, PlatformPointType, Scene, XorshiftRandomGenerator } from "..";
import { Camera2D, E, LoadingScene, MessageEvent, PlatformPointType, Scene, XorshiftRandomGenerator, PlatformButtonType } from "..";
import { customMatchers, EntityStateFlags, Game, Renderer, ImageAsset, ScriptAsset, FilledRect } from "./helpers";

expect.extend(customMatchers);
Expand Down Expand Up @@ -1110,7 +1110,8 @@ describe("test Game", () => {
game._pointEventResolver.pointDown({
type: PlatformPointType.Down,
identifier: 0,
offset: { x: 0, y: 0 }
offset: { x: 0, y: 0 },
button: PlatformButtonType.Primary
});
const moduleManager = game._moduleManager;
game._reset({
Expand All @@ -1129,7 +1130,8 @@ describe("test Game", () => {
game._pointEventResolver.pointUp({
type: PlatformPointType.Up,
identifier: 0,
offset: { x: 0, y: 0 }
offset: { x: 0, y: 0 },
button: PlatformButtonType.Primary
})
).toBeNull();
// reset で Game#onSceneChange は removeAll() されるが、Game#_onSceneChange は removeAll() されないことを確認
Expand Down
Loading

0 comments on commit 2156ac3

Please sign in to comment.