-
Notifications
You must be signed in to change notification settings - Fork 1
/
addSelectBox.js
75 lines (66 loc) · 1.73 KB
/
addSelectBox.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
import { createListener } from "./utils.js";
export function addSelectBox(el, state) {
const listen = createListener(el);
let start = null;
let end = null;
const endSelect = () => {
start = null;
end = null;
state.selectBox = { start, end };
state.lockInteraction = false;
};
listen("pointercancel", "", (e) => {
endSelect();
state.selection.clear();
});
listen("pointerdown", "#svg-layer", (e) => {
if (!e.shiftKey) return;
state.lockInteraction = true;
start = state.mouse;
});
listen("pointermove", "", (e) => {
if (!e.shiftKey) {
endSelect();
return;
}
if (start === null) return;
end = state.mouse;
state.selectBox = { start, end };
state.selection.clear();
if (start && end) {
const selectBox = { start, end };
Object.entries(state.toolchain.tools).forEach(([toolID, tool]) => {
const bounds = {
xMin: tool.pos.x,
xMax: tool.pos.x + (tool.ui.width ?? 0),
yMin: tool.pos.y,
yMax: tool.pos.y + (tool.ui.height ?? 0),
};
if (isSelected(bounds, selectBox)) {
state.selection.add(toolID);
}
});
}
});
function isSelected(t, selectBox) {
let { start, end } = selectBox;
return (
(t.xMin > start.x &&
t.xMax < end.x &&
t.yMin > start.y &&
t.yMax < end.y) ||
(t.xMin > start.x &&
t.xMax < end.x &&
t.yMin < start.y &&
t.yMax > end.y) ||
(t.xMin < start.x &&
t.xMax > end.x &&
t.yMin > start.y &&
t.yMax < end.y) ||
(t.xMin < start.x && t.xMax > end.x && t.yMin < start.y && t.yMax > end.y)
);
}
listen("pointerup", "", (e) => {
endSelect();
});
}