-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathutil.js
78 lines (72 loc) · 2.34 KB
/
util.js
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
import Config from './config';
export function parseBounds(bounds) {
bounds.x = bounds.x ? bounds.x : 0;
bounds.y = bounds.y ? bounds.y : 0;
if (bounds.width <= 0) {
console.warn('PhaserListView: bounds.width <= 0');
} else if (bounds.height <= 0) {
console.warn('PhaserListView: bounds.height <= 0');
}
return bounds;
}
// prefer nominalWidth and nominalHeight
export function getWidthOrHeight(displayObject, widthOrHeight) {
return (
displayObject[`nominal${capitalizeFirstLetter(widthOrHeight)}`] ||
displayObject[widthOrHeight]
);
}
export function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
export function findChild(children, predicate, scope = null) {
if (!children) return false;
for (let i = 0; i < children.length; i++) {
const child = children[i];
if (!child) continue;
if (predicate.call(scope, child)) {
return child;
}
const found = findChild(child.children, predicate, scope);
if (found) {
return found;
}
}
return false;
}
export function detectDrag(pointer) {
const distanceX = Math.abs(pointer.positionDown.x - pointer.positionUp.x);
const distanceY = Math.abs(pointer.positionDown.y - pointer.positionUp.y);
const time = pointer.timeUp - pointer.timeDown;
return (
distanceX > Config.AUTO_DETECT_THRESHOLD ||
distanceY > Config.AUTO_DETECT_THRESHOLD
);
}
export function dispatchClicks(pointer, clickables, type) {
if (type == 'onInputUp' && detectDrag(pointer)) return;
// SEARCH OBJECT UNDER POINT AS THERE IS NO CLICK PROPAGATION SUPPORT IN PHASER
const found = findChild(clickables, clickable => {
const pt = clickable.worldPosition;
const { anchor, pivot, width, height, scale } = clickable;
const x = pt.x - (anchor ? anchor.x * width : 0) - pivot.x * scale.x;
const y = pt.y - (anchor ? anchor.y * height : 0) - pivot.y * scale.y;
// console.log('does ', x, y, clickable.width, clickable.height, ' intersect ', pointer.x, pointer.y)
return (
clickable.inputEnabled &&
new Phaser.Rectangle(x, y, clickable.width, clickable.height).contains(
pointer.x,
pointer.y
)
);
});
if (
found &&
found.events &&
found.events[type] &&
found.events[type].dispatch
) {
found.events[type].dispatch(found, pointer, true);
}
return found;
}