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:
parent
c25759d147
commit
4d34f9fa78
|
|
@ -4,7 +4,7 @@ export type { IDisposable, DisposableItem } from "./utils";
|
|||
|
||||
// Drag & drop utilities
|
||||
export { dragDropEventEffect, DragDropEventType } from "./utils";
|
||||
export type { DragDropEvent } from "./utils";
|
||||
export type { DragDropEvent, DragDropCallback } from "./utils";
|
||||
|
||||
// Data-driven object spawning
|
||||
export { spawnEffect } from "./spawner";
|
||||
|
|
|
|||
|
|
@ -18,8 +18,11 @@ export type DragDropEvent = {
|
|||
deltaY: number;
|
||||
};
|
||||
|
||||
export type DragDropCallback = (event: DragDropEvent) => void;
|
||||
|
||||
export function dragDropEventEffect(
|
||||
gameObject: Phaser.GameObjects.GameObject,
|
||||
callback: DragDropCallback,
|
||||
disposables?: DisposableBag,
|
||||
): () => void {
|
||||
let isDragging = false;
|
||||
|
|
@ -35,8 +38,7 @@ export function dragDropEventEffect(
|
|||
deltaX: 0,
|
||||
deltaY: 0,
|
||||
};
|
||||
gameObject.emit("drag", event);
|
||||
gameObject.emit("dragstart", event);
|
||||
callback(event);
|
||||
}
|
||||
|
||||
function onPointerUp(pointer: Phaser.Input.Pointer) {
|
||||
|
|
@ -46,8 +48,7 @@ export function dragDropEventEffect(
|
|||
const deltaX = pointer.x - down.x;
|
||||
const deltaY = pointer.y - down.y;
|
||||
const event: DragDropEvent = { type: DragDropEventType.UP, deltaX, deltaY };
|
||||
gameObject.emit("drag", event);
|
||||
gameObject.emit("dragend", event);
|
||||
callback(event);
|
||||
down = null;
|
||||
}
|
||||
|
||||
|
|
@ -61,8 +62,7 @@ export function dragDropEventEffect(
|
|||
deltaX,
|
||||
deltaY,
|
||||
};
|
||||
gameObject.emit("drag", event);
|
||||
gameObject.emit("dragmove", event);
|
||||
callback(event);
|
||||
}
|
||||
|
||||
gameObject.on("pointerdown", onPointerDown);
|
||||
|
|
|
|||
|
|
@ -4,4 +4,5 @@ export {
|
|||
dragDropEventEffect,
|
||||
DragDropEventType,
|
||||
type DragDropEvent,
|
||||
type DragDropCallback,
|
||||
} from "./dnd";
|
||||
|
|
|
|||
Loading…
Reference in New Issue