@@ -3,6 +3,7 @@ import nullthrows from "nullthrows";
33import { type PointerEvent , type ReactNode , useState } from "react" ;
44import { theme } from "../../../../common/theme" ;
55import { type Vector2 , vector2 } from "../../../../common/vector2" ;
6+ import { useDraggable } from "../../../../hooks/drag" ;
67import { CCConnectionStore } from "../../../../store/connection" ;
78import type { CCNodePinId } from "../../../../store/nodePin" ;
89import { useStore } from "../../../../store/react" ;
@@ -33,40 +34,6 @@ export default function CCComponentEditorRendererNodePin({
3334 cursorPosition : Vector2 ;
3435 nodePinPositionKDTree : KDTree < CCNodePinId > ;
3536 } | null > ( null ) ;
36- const onDrag = ( e : PointerEvent ) => {
37- let nodePinPositionKDTree = draggingState ?. nodePinPositionKDTree ;
38- if ( ! nodePinPositionKDTree ) {
39- const nodes = store . nodes . getManyByParentComponentId (
40- node . parentComponentId ,
41- ) ;
42- nodePinPositionKDTree = KDTree . from (
43- nodes
44- . filter ( ( yourNode ) => yourNode . id !== node . id )
45- . flatMap ( ( yourNode ) => [
46- ...getCCComponentEditorRendererNodeGeometry ( store , yourNode . id )
47- . nodePinPositionById ,
48- ] )
49- . flatMap ( ( [ yourNodePinId , yourNodePinPosition ] ) => {
50- const yourNodePin = nullthrows ( store . nodePins . get ( yourNodePinId ) ) ;
51- const yourComponentPin = nullthrows (
52- store . componentPins . get ( yourNodePin . componentPinId ) ,
53- ) ;
54- if ( yourComponentPin . type === componentPin . type ) return [ ] ;
55- return [
56- [ yourNodePinId , [ yourNodePinPosition . x , yourNodePinPosition . y ] ] ,
57- ] as const ;
58- } ) ,
59- 2 ,
60- ) ;
61- }
62- setDraggingState ( {
63- cursorPosition : componentEditorState . fromCanvasToStage (
64- vector2 . fromDomEvent ( e . nativeEvent ) ,
65- ) ,
66- nodePinPositionKDTree,
67- } ) ;
68- } ;
69-
7037 let draggingView : ReactNode = null ;
7138 let nodePinIdToConnect : CCNodePinId | null = null ;
7239 if ( draggingState ) {
@@ -100,6 +67,57 @@ export default function CCComponentEditorRendererNodePin({
10067 />
10168 ) ;
10269 }
70+ const draggableProps = useDraggable ( {
71+ onDrag : ( e : PointerEvent ) => {
72+ let nodePinPositionKDTree = draggingState ?. nodePinPositionKDTree ;
73+ if ( ! nodePinPositionKDTree ) {
74+ const nodes = store . nodes . getManyByParentComponentId (
75+ node . parentComponentId ,
76+ ) ;
77+ nodePinPositionKDTree = KDTree . from (
78+ nodes
79+ . filter ( ( yourNode ) => yourNode . id !== node . id )
80+ . flatMap ( ( yourNode ) => [
81+ ...getCCComponentEditorRendererNodeGeometry ( store , yourNode . id )
82+ . nodePinPositionById ,
83+ ] )
84+ . flatMap ( ( [ yourNodePinId , yourNodePinPosition ] ) => {
85+ const yourNodePin = nullthrows ( store . nodePins . get ( yourNodePinId ) ) ;
86+ const yourComponentPin = nullthrows (
87+ store . componentPins . get ( yourNodePin . componentPinId ) ,
88+ ) ;
89+ if ( yourComponentPin . type === componentPin . type ) return [ ] ;
90+ return [
91+ [ yourNodePinId , [ yourNodePinPosition . x , yourNodePinPosition . y ] ] ,
92+ ] as const ;
93+ } ) ,
94+ 2 ,
95+ ) ;
96+ }
97+ setDraggingState ( {
98+ cursorPosition : componentEditorState . fromCanvasToStage (
99+ vector2 . fromDomEvent ( e . nativeEvent ) ,
100+ ) ,
101+ nodePinPositionKDTree,
102+ } ) ;
103+ } ,
104+ onDragEnd : ( ) => {
105+ if ( nodePinIdToConnect ) {
106+ const route = {
107+ input : { from : nodePinIdToConnect , to : nodePin . id } ,
108+ output : { from : nodePin . id , to : nodePinIdToConnect } ,
109+ } [ componentPin . type ] ;
110+ store . connections . register (
111+ CCConnectionStore . create ( {
112+ parentComponentId : node . parentComponentId ,
113+ ...route ,
114+ bentPortion : 0.5 ,
115+ } ) ,
116+ ) ;
117+ }
118+ setDraggingState ( null ) ;
119+ } ,
120+ } ) ;
103121
104122 const isSimulationMode = useComponentEditorStore ( ) (
105123 ( s ) => s . editorMode === "play" ,
@@ -119,7 +137,10 @@ export default function CCComponentEditorRendererNodePin({
119137 let nodePinValue : SimulationValue ;
120138 let nodePinValueAsString : string | null = null ;
121139 if ( isSimulationMode && hasNoConnection ) {
122- if ( implementationComponentPin ) {
140+ if (
141+ implementationComponentPin &&
142+ implementationComponentPin . type === "input"
143+ ) {
123144 nodePinValue = nullthrows (
124145 componentEditorState . getInputValue ( implementationComponentPin . id ) ,
125146 ) ;
@@ -131,11 +152,15 @@ export default function CCComponentEditorRendererNodePin({
131152 nodePinValueAsString = simulationValueToString ( nodePinValue ) ;
132153 }
133154 const updateInputValue = ( ) => {
134- if ( ! implementationComponentPin ) return ;
155+ if (
156+ ! implementationComponentPin ||
157+ implementationComponentPin . type !== "input"
158+ )
159+ return ;
135160 const updatedPinValue = [ ...nodePinValue ] ;
136161 updatedPinValue [ 0 ] = ! updatedPinValue [ 0 ] ;
137162 componentEditorState . setInputValue (
138- implementationComponentPin . id ,
163+ implementationComponentPin ? .id ,
139164 updatedPinValue ,
140165 ) ;
141166 } ;
@@ -152,30 +177,7 @@ export default function CCComponentEditorRendererNodePin({
152177 { nodePinValueAsString }
153178 </ text >
154179 ) }
155- < g
156- onPointerDown = { ( e ) => {
157- e . currentTarget . setPointerCapture ( e . pointerId ) ;
158- onDrag ( e ) ;
159- } }
160- onPointerMove = { draggingState ? onDrag : undefined }
161- onPointerUp = { ( ) => {
162- if ( ! nodePinIdToConnect ) return ;
163- const route = {
164- input : { from : nodePinIdToConnect , to : nodePin . id } ,
165- output : { from : nodePin . id , to : nodePinIdToConnect } ,
166- } [ componentPin . type ] ;
167- store . connections . register (
168- CCConnectionStore . create ( {
169- parentComponentId : node . parentComponentId ,
170- ...route ,
171- bentPortion : 0.5 ,
172- } ) ,
173- ) ;
174- } }
175- onLostPointerCapture = { ( ) => {
176- setDraggingState ( null ) ;
177- } }
178- >
180+ < g { ...draggableProps } style = { { cursor : "pointer" } } >
179181 < rect
180182 x = { position . x - 5 }
181183 y = { position . y - 5 }
0 commit comments