let app = null; let mapContainer = null; export function syncApp(param) { app = param; } export function syncMapContainer(param) { mapContainer = param; } export const MapEvent = Object.freeze({ SELECTION_BOX: Symbol.for(0), PAN: Symbol.for(1), }) export const getRealPosition = (x, y, mapContainer) => { const rect = app.view.getBoundingClientRect(); return { mapX: (x - rect.left) / mapContainer.scale.x - mapContainer.x / mapContainer.scale.x, mapY: (y - rect.top) / mapContainer.scale.y - mapContainer.y / mapContainer.scale.y } } export const initSprite = (sprite) => { sprite.anchor.set(0.5); sprite.cursor = 'pointer'; sprite.eventMode = 'static'; } export const beMovable = (sprite, setDidClickSprite) => { sprite.off('pointerup'); sprite.off('pointerdown'); sprite.off('click'); sprite.on("pointerdown", onDragStart); let dragTarget; function onDragStart(event) { setDidClickSprite(true); dragTarget = event.currentTarget; mapContainer.parent.off('pointermove'); mapContainer.parent.on('pointermove', onDragMove, dragTarget); mapContainer.parent.off('pointerup'); mapContainer.parent.on('pointerup', onDragEnd.bind(mapContainer)); } function onDragMove(event) { if (this) { this.parent.toLocal(event.global, null, this.position); } } function onDragEnd() { if (dragTarget) { setDidClickSprite(false); this.parent.off('pointermove'); dragTarget.alpha = 1; dragTarget = null; } } }