hitTest.ts
2.46 KB
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
import { isClusterDrawn, type SpotlightOpen, useHeatmapStore } from '@/store/useHeatmapStore';
import type { BucketHit } from './ClusterLayer';
/**
* 地图上这一点能点开什么。
*
* 「哪个图层先接住点击」是这一座地图上最容易漂移的口径:黑点环盖在聚合数字上、
* 聚焦形状又盖在黑点上、定位卡片的小叉压在最上面。原来这段优先级写在 MapStage 的
* click handler 里,后来要给 hover 光标也用同一套判据 —— 两处各写一遍的话,
* 总有一天会出现「光标说能点、点下去却是另一个东西」或反过来。
*
* 所以这里只回答「这一点归谁」,不执行任何动作:click 拿到结果去开抽屉 / 清选择,
* hover 拿到结果去决定要不要给小手光标。顺序与理由都留在下面,与原来的 click handler 逐字一致。
*/
export type MapHit =
| { kind: 'pinClose' }
| { kind: 'spot'; index: number; unit: SpotlightOpen['units'][number] }
| { kind: 'hotspot'; index: number }
| { kind: 'bucket'; hit: BucketHit };
/** 可点图层集合。session 把它们都挂在实例上,测试里不必建一整张地图 */
export interface HitLayers {
pin: { hitClose(x: number, y: number): boolean } | null;
spot: { hitAt(x: number, y: number): number } | null;
hotspot: { hitAt(x: number, y: number): number } | null;
cluster: { hitAt(x: number, y: number): BucketHit | null } | null;
}
type HitState = ReturnType<typeof useHeatmapStore.getState>;
/**
* 按「最上面的先答」问四层:卡片关闭叉 → 聚焦形状 → 黑点 → 聚合桶。
* 都不命中返回 null,调用方据此清选择。
*
* 聚合桶额外过 isClusterDrawn:降噪 / 聚焦 / 定位期间数字层收起了,
* 屏上看不见的桶不该还能点开(光标也不该说它能点)。
*/
export function hitTest(
layers: HitLayers,
st: HitState,
px: { x: number; y: number },
): MapHit | null {
if (layers.pin?.hitClose(px.x, px.y)) return { kind: 'pinClose' };
const spot = st.spotlight;
if (spot) {
const i = layers.spot?.hitAt(px.x, px.y) ?? -1;
const unit = i >= 0 ? spot.units[i] : undefined;
if (unit) return { kind: 'spot', index: i, unit };
}
const idx = layers.hotspot?.hitAt(px.x, px.y) ?? -1;
if (idx >= 0) return { kind: 'hotspot', index: idx };
if (isClusterDrawn(st)) {
const hit = layers.cluster?.hitAt(px.x, px.y) ?? null;
if (hit) return { kind: 'bucket', hit };
}
return null;
}