refactor(framework): use callback instead of events in

dragDropEventEffect

Replace Phaser event emissions with a `DragDropCallback` in
`dragDropEventEffect` to provide a more explicit and type-safe API
for handling drag-and-drop interactions.
This commit is contained in:
hypercross 2026-04-19 12:50:50 +08:00
parent c25759d147
commit 4d34f9fa78
3 changed files with 8 additions and 7 deletions

View File

@ -4,7 +4,7 @@ export type { IDisposable, DisposableItem } from "./utils";
// Drag & drop utilities // Drag & drop utilities
export { dragDropEventEffect, DragDropEventType } from "./utils"; export { dragDropEventEffect, DragDropEventType } from "./utils";
export type { DragDropEvent } from "./utils"; export type { DragDropEvent, DragDropCallback } from "./utils";
// Data-driven object spawning // Data-driven object spawning
export { spawnEffect } from "./spawner"; export { spawnEffect } from "./spawner";

View File

@ -18,8 +18,11 @@ export type DragDropEvent = {
deltaY: number; deltaY: number;
}; };
export type DragDropCallback = (event: DragDropEvent) => void;
export function dragDropEventEffect( export function dragDropEventEffect(
gameObject: Phaser.GameObjects.GameObject, gameObject: Phaser.GameObjects.GameObject,
callback: DragDropCallback,
disposables?: DisposableBag, disposables?: DisposableBag,
): () => void { ): () => void {
let isDragging = false; let isDragging = false;
@ -35,8 +38,7 @@ export function dragDropEventEffect(
deltaX: 0, deltaX: 0,
deltaY: 0, deltaY: 0,
}; };
gameObject.emit("drag", event); callback(event);
gameObject.emit("dragstart", event);
} }
function onPointerUp(pointer: Phaser.Input.Pointer) { function onPointerUp(pointer: Phaser.Input.Pointer) {
@ -46,8 +48,7 @@ export function dragDropEventEffect(
const deltaX = pointer.x - down.x; const deltaX = pointer.x - down.x;
const deltaY = pointer.y - down.y; const deltaY = pointer.y - down.y;
const event: DragDropEvent = { type: DragDropEventType.UP, deltaX, deltaY }; const event: DragDropEvent = { type: DragDropEventType.UP, deltaX, deltaY };
gameObject.emit("drag", event); callback(event);
gameObject.emit("dragend", event);
down = null; down = null;
} }
@ -61,8 +62,7 @@ export function dragDropEventEffect(
deltaX, deltaX,
deltaY, deltaY,
}; };
gameObject.emit("drag", event); callback(event);
gameObject.emit("dragmove", event);
} }
gameObject.on("pointerdown", onPointerDown); gameObject.on("pointerdown", onPointerDown);

View File

@ -4,4 +4,5 @@ export {
dragDropEventEffect, dragDropEventEffect,
DragDropEventType, DragDropEventType,
type DragDropEvent, type DragDropEvent,
type DragDropCallback,
} from "./dnd"; } from "./dnd";