11import { useState , type PointerEvent , type ReactNode } from "react" ;
2- import * as matrix from "transformation-matrix" ;
32import { KDTree } from "mnemonist" ;
43import nullthrows from "nullthrows" ;
5- import type { Point } from "../../../../common/types" ;
64import type { CCNodePinId } from "../../../../store/nodePin" ;
75import { CCComponentEditorRendererConnectionCore } from "./Connection" ;
86import { useComponentEditorStore } from "../store" ;
97import { useStore } from "../../../../store/react" ;
108import getCCComponentEditorRendererNodeGeometry from "./Node.geometry" ;
119import { CCConnectionStore } from "../../../../store/connection" ;
1210import type { SimulationValue } from "../store/slices/core" ;
11+ import { vector2 , type Vector2 } from "../../../../common/vector2" ;
1312
1413const NODE_PIN_POSITION_SENSITIVITY = 10 ;
1514
1615export type CCComponentEditorRendererNodeProps = {
1716 nodePinId : CCNodePinId ;
18- position : Point ;
17+ position : Vector2 ;
1918} ;
2019export default function CCComponentEditorRendererNodePin ( {
2120 nodePinId,
2221 position,
2322} : CCComponentEditorRendererNodeProps ) {
2423 const { store } = useStore ( ) ;
24+ const componentEditorState = useComponentEditorStore ( ) ( ) ;
2525 const nodePin = nullthrows ( store . nodePins . get ( nodePinId ) ) ;
2626 const node = nullthrows ( store . nodes . get ( nodePin . nodeId ) ) ;
2727 const componentPin = nullthrows (
2828 store . componentPins . get ( nodePin . componentPinId )
2929 ) ;
3030
31- const inverseViewTransformation = useComponentEditorStore ( ) ( ( s ) =>
32- s . getInverseViewTransformation ( )
33- ) ;
3431 const [ draggingState , setDraggingState ] = useState < {
35- cursorPosition : Point ;
32+ cursorPosition : Vector2 ;
3633 nodePinPositionKDTree : KDTree < CCNodePinId > ;
3734 } | null > ( null ) ;
3835 const onDrag = ( e : PointerEvent ) => {
@@ -62,10 +59,9 @@ export default function CCComponentEditorRendererNodePin({
6259 ) ;
6360 }
6461 setDraggingState ( {
65- cursorPosition : matrix . applyToPoint ( inverseViewTransformation , {
66- x : e . nativeEvent . offsetX ,
67- y : e . nativeEvent . offsetY ,
68- } ) ,
62+ cursorPosition : componentEditorState . fromCanvasToStage (
63+ vector2 . fromDomEvent ( e . nativeEvent )
64+ ) ,
6965 nodePinPositionKDTree,
7066 } ) ;
7167 } ;
@@ -110,7 +106,6 @@ export default function CCComponentEditorRendererNodePin({
110106 const hasNoConnection =
111107 store . connections . getConnectionsByNodePinId ( nodePinId ) . length === 0 ;
112108
113- const componentEditorStore = useComponentEditorStore ( ) ( ) ;
114109 const pinType = componentPin . type ;
115110 const simulationValueToString = ( simulationValue : SimulationValue ) => {
116111 return simulationValue . reduce (
@@ -123,18 +118,18 @@ export default function CCComponentEditorRendererNodePin({
123118 let nodePinValueInit = null ;
124119 if ( isSimulationMode && hasNoConnection ) {
125120 if ( pinType === "input" ) {
126- nodePinValueInit = componentEditorStore . getInputValue (
121+ nodePinValueInit = componentEditorState . getInputValue (
127122 implementedComponentPin ! . id
128123 ) ! ;
129124 } else {
130- nodePinValueInit = componentEditorStore . getNodePinValue ( nodePinId ) ! ;
125+ nodePinValueInit = componentEditorState . getNodePinValue ( nodePinId ) ! ;
131126 }
132127 }
133128 const nodePinValue = nodePinValueInit ;
134129 const updateInputValue = ( ) => {
135130 const updatedPinValue = [ ...nodePinValue ! ] ;
136131 updatedPinValue [ 0 ] = ! updatedPinValue [ 0 ] ;
137- componentEditorStore . setInputValue (
132+ componentEditorState . setInputValue (
138133 implementedComponentPin ! . id ,
139134 updatedPinValue
140135 ) ;
0 commit comments