-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.geometry.ts
More file actions
62 lines (55 loc) · 1.68 KB
/
Node.geometry.ts
File metadata and controls
62 lines (55 loc) · 1.68 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
61
62
import nullthrows from "nullthrows";
import type CCStore from "../../../../store";
import type { CCNodeId } from "../../../../store/node";
import type { CCNodePinId } from "../../../../store/nodePin";
export default function getCCComponentEditorRendererNodeGeometry(
store: CCStore,
nodeId: CCNodeId,
) {
const width = 100;
const gapY = 20;
const paddingY = 15;
const node = nullthrows(store.nodes.get(nodeId));
const nodePins = store.nodePins.getManyByNodeId(nodeId);
const x = node.position.x - width / 2;
let inputPinCount = 0;
let outputPinCount = 0;
const nodePinPositionById = new Map<CCNodePinId, { x: number; y: number }>();
for (const nodePin of nodePins) {
const position = { x: 0, y: 0 };
const componentPin = nullthrows(
store.componentPins.get(nodePin.componentPinId),
);
if (componentPin.type === "input") {
position.x = node.position.x - width / 2;
position.y = node.position.y + inputPinCount * gapY;
inputPinCount += 1;
} else {
position.x = node.position.x + width / 2;
position.y = node.position.y + outputPinCount * gapY;
outputPinCount += 1;
}
nodePinPositionById.set(nodePin.id, position);
}
const height =
(Math.max(inputPinCount, outputPinCount) - 1) * gapY + paddingY * 2;
const y = node.position.y - height / 2;
for (const nodePin of nodePins) {
const componentPin = nullthrows(
store.componentPins.get(nodePin.componentPinId),
);
const position = nullthrows(nodePinPositionById.get(nodePin.id));
if (componentPin.type === "input") {
position.y -= ((inputPinCount - 1) * gapY) / 2;
} else {
position.y -= ((outputPinCount - 1) * gapY) / 2;
}
}
return {
x,
y,
width,
height,
nodePinPositionById,
};
}