#
luxiaotao1123
2024-03-05 37921583eafff4a9cbad44c096dba516a1229d1f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
 
 
 
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;
        }
    }
 
}
 
export const isSpriteInSelectionBox = (sprite, selectionBox) => {
    const spriteBounds = sprite.getBounds();
    const boxBounds = selectionBox.getBounds();
 
    return spriteBounds.x + spriteBounds.width > boxBounds.x
        && spriteBounds.x < boxBounds.x + boxBounds.width
        && spriteBounds.y + spriteBounds.height > boxBounds.y
        && spriteBounds.y < boxBounds.y + boxBounds.height;
}
 
export const markSprite = (sprite) => {
    sprite.alpha = 0.5;
}
 
export const unMarkSprite = (sprite) => {
    sprite.alpha = 1;
}