@@ -10,6 +10,7 @@ import { useComponentEditorStore } from "../store";
1010import type { SimulationValue } from "../store/slices/core" ;
1111import { CCComponentEditorRendererConnectionCore } from "./Connection" ;
1212import getCCComponentEditorRendererNodeGeometry from "./Node.geometry" ;
13+ import { useDraggable } from "../../../../hooks/drag" ;
1314
1415const NODE_PIN_POSITION_SENSITIVITY = 10 ;
1516
@@ -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,7 @@ export default function CCComponentEditorRendererNodePin({
119137 let nodePinValue : SimulationValue ;
120138 let nodePinValueAsString : string | null = null ;
121139 if ( isSimulationMode && hasNoConnection ) {
122- if ( implementationComponentPin ) {
140+ if ( implementationComponentPin && implementationComponentPin . type === "input" ) {
123141 nodePinValue = nullthrows (
124142 componentEditorState . getInputValue ( implementationComponentPin . id ) ,
125143 ) ;
@@ -131,11 +149,11 @@ export default function CCComponentEditorRendererNodePin({
131149 nodePinValueAsString = simulationValueToString ( nodePinValue ) ;
132150 }
133151 const updateInputValue = ( ) => {
134- if ( ! implementationComponentPin ) return ;
152+ if ( ! implementationComponentPin || implementationComponentPin . type !== "input" ) return ;
135153 const updatedPinValue = [ ...nodePinValue ] ;
136154 updatedPinValue [ 0 ] = ! updatedPinValue [ 0 ] ;
137155 componentEditorState . setInputValue (
138- implementationComponentPin . id ,
156+ implementationComponentPin ? .id ,
139157 updatedPinValue ,
140158 ) ;
141159 } ;
@@ -152,30 +170,7 @@ export default function CCComponentEditorRendererNodePin({
152170 { nodePinValueAsString }
153171 </ text >
154172 ) }
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- >
173+ < g { ...draggableProps } style = { { cursor : "pointer" } } >
179174 < rect
180175 x = { position . x - 5 }
181176 y = { position . y - 5 }
0 commit comments