feat(framework): add hoverEffect utility
Add a `hoverEffect` utility to `boardgame-phaser` to simplify handling pointerover and pointerout events. Refactor `CardContainer` in `sts-like-viewer` to use this new utility and centralize card dimensions in `CARD_CONFIG`.
This commit is contained in:
@@ -5,6 +5,7 @@ export type { IDisposable, DisposableItem } from "./utils";
|
||||
// Drag & drop utilities
|
||||
export { dragDropEventEffect, DragDropEventType } from "./utils";
|
||||
export type { DragDropEvent, DragDropCallback } from "./utils";
|
||||
export { hoverEffect } from "./utils";
|
||||
|
||||
// Data-driven object spawning
|
||||
export { spawnEffect } from "./spawner";
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
type HoverCallback = (hovering: boolean) => void;
|
||||
export function hoverEffect(
|
||||
gameObject: Phaser.GameObjects.GameObject,
|
||||
callback: HoverCallback,
|
||||
) {
|
||||
let isHovering = false;
|
||||
const onPointerOver = () => {
|
||||
if (isHovering) return;
|
||||
isHovering = true;
|
||||
callback(true);
|
||||
};
|
||||
const onPointerOut = () => {
|
||||
if (!isHovering) return;
|
||||
isHovering = false;
|
||||
callback(false);
|
||||
};
|
||||
|
||||
gameObject.on("pointerover", onPointerOver);
|
||||
gameObject.on("pointerout", onPointerOut);
|
||||
const cleanup = () => {
|
||||
gameObject.off("pointerover", onPointerOver);
|
||||
gameObject.off("pointerout", onPointerOut);
|
||||
gameObject.off("destroy", cleanup);
|
||||
};
|
||||
|
||||
gameObject.once("destroy", cleanup);
|
||||
return cleanup;
|
||||
}
|
||||
@@ -6,3 +6,4 @@ export {
|
||||
type DragDropEvent,
|
||||
type DragDropCallback,
|
||||
} from "./dnd";
|
||||
export * from "./hover";
|
||||
|
||||
Reference in New Issue
Block a user