-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodePin.tsx
More file actions
182 lines (175 loc) · 5.9 KB
/
NodePin.tsx
File metadata and controls
182 lines (175 loc) · 5.9 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { useState, type PointerEvent, type ReactNode } from "react";
import { KDTree } from "mnemonist";
import nullthrows from "nullthrows";
import type { CCNodePinId } from "../../../../store/nodePin";
import { CCComponentEditorRendererConnectionCore } from "./Connection";
import { useComponentEditorStore } from "../store";
import { useStore } from "../../../../store/react";
import getCCComponentEditorRendererNodeGeometry from "./Node.geometry";
import { CCConnectionStore } from "../../../../store/connection";
import type { SimulationValue } from "../store/slices/core";
import { vector2, type Vector2 } from "../../../../common/vector2";
const NODE_PIN_POSITION_SENSITIVITY = 10;
export type CCComponentEditorRendererNodeProps = {
nodePinId: CCNodePinId;
position: Vector2;
};
export default function CCComponentEditorRendererNodePin({
nodePinId,
position,
}: CCComponentEditorRendererNodeProps) {
const { store } = useStore();
const componentEditorState = useComponentEditorStore()();
const nodePin = nullthrows(store.nodePins.get(nodePinId));
const node = nullthrows(store.nodes.get(nodePin.nodeId));
const componentPin = nullthrows(
store.componentPins.get(nodePin.componentPinId)
);
const [draggingState, setDraggingState] = useState<{
cursorPosition: Vector2;
nodePinPositionKDTree: KDTree<CCNodePinId>;
} | null>(null);
const onDrag = (e: PointerEvent) => {
let nodePinPositionKDTree = draggingState?.nodePinPositionKDTree;
if (!nodePinPositionKDTree) {
const nodes = store.nodes.getManyByParentComponentId(
node.parentComponentId
);
nodePinPositionKDTree = KDTree.from(
nodes
.filter((yourNode) => yourNode.id !== node.id)
.flatMap((yourNode) => [
...getCCComponentEditorRendererNodeGeometry(store, yourNode.id)
.nodePinPositionById,
])
.flatMap(([yourNodePinId, yourNodePinPosition]) => {
const yourNodePin = nullthrows(store.nodePins.get(yourNodePinId));
const yourComponentPin = nullthrows(
store.componentPins.get(yourNodePin.componentPinId)
);
if (yourComponentPin.type === componentPin.type) return [];
return [
[yourNodePinId, [yourNodePinPosition.x, yourNodePinPosition.y]],
] as const;
}),
2
);
}
setDraggingState({
cursorPosition: componentEditorState.fromCanvasToStage(
vector2.fromDomEvent(e.nativeEvent)
),
nodePinPositionKDTree,
});
};
let draggingView: ReactNode = null;
let nodePinIdToConnect: CCNodePinId | null = null;
if (draggingState) {
const nearestNodePinId =
draggingState.nodePinPositionKDTree.nearestNeighbor([
draggingState.cursorPosition.x,
draggingState.cursorPosition.y,
]);
const nearestNodePin = nullthrows(store.nodePins.get(nearestNodePinId));
const nearestNodePinPosition = nullthrows(
getCCComponentEditorRendererNodeGeometry(
store,
nearestNodePin.nodeId
).nodePinPositionById.get(nearestNodePinId)
);
const distance = Math.hypot(
nearestNodePinPosition.x - draggingState.cursorPosition.x,
nearestNodePinPosition.y - draggingState.cursorPosition.y
);
if (distance < NODE_PIN_POSITION_SENSITIVITY) {
nodePinIdToConnect = nearestNodePinId;
}
draggingView = (
<CCComponentEditorRendererConnectionCore
from={position}
to={
nodePinIdToConnect
? nearestNodePinPosition
: draggingState.cursorPosition
}
/>
);
}
const isSimulationMode = useComponentEditorStore()(
(s) => s.editorMode === "play"
);
const hasNoConnection =
store.connections.getConnectionsByNodePinId(nodePinId).length === 0;
const pinType = componentPin.type;
const simulationValueToString = (simulationValue: SimulationValue) => {
return simulationValue.reduce(
(acm, currentValue) => acm + (currentValue === true ? "1" : "0"),
""
);
};
const implementedComponentPin =
store.componentPins.getByImplementation(nodePinId);
let nodePinValueInit = null;
if (isSimulationMode && hasNoConnection) {
if (pinType === "input") {
nodePinValueInit = componentEditorState.getInputValue(
implementedComponentPin!.id
)!;
} else {
nodePinValueInit = componentEditorState.getNodePinValue(nodePinId)!;
}
}
const nodePinValue = nodePinValueInit;
const updateInputValue = () => {
const updatedPinValue = [...nodePinValue!];
updatedPinValue[0] = !updatedPinValue[0];
componentEditorState.setInputValue(
implementedComponentPin!.id,
updatedPinValue
);
};
return (
<>
{isSimulationMode && hasNoConnection && (
<text
x={position.x - (pinType === "input" ? 15 : -8)}
y={position.y}
onClick={pinType === "input" ? updateInputValue : undefined}
>
{simulationValueToString(nodePinValue!)}
</text>
)}
<circle
cx={position.x}
cy={position.y}
r={5}
fill="white"
stroke="black"
strokeWidth={2}
onPointerDown={(e) => {
e.currentTarget.setPointerCapture(e.pointerId);
onDrag(e);
}}
onPointerMove={draggingState ? onDrag : undefined}
onPointerUp={() => {
if (!nodePinIdToConnect) return;
const route = {
input: { from: nodePinIdToConnect, to: nodePin.id },
output: { from: nodePin.id, to: nodePinIdToConnect },
}[componentPin.type];
store.connections.register(
CCConnectionStore.create({
parentComponentId: node.parentComponentId,
...route,
bentPortion: 0.5,
})
);
}}
onLostPointerCapture={() => {
setDraggingState(null);
}}
/>
{draggingView}
</>
);
}