@@ -34,6 +34,8 @@ export class Device extends Sprite {
3434 rightbar : RightBar ;
3535 highlightMarker : Graphics | null = null ; // Marker to indicate selection
3636
37+ static dragTarget : Device | null = null ;
38+
3739 constructor (
3840 id : number ,
3941 svg : string ,
@@ -82,7 +84,7 @@ export class Device extends Sprite {
8284 fill : Colors . Black ,
8385 align : "center" ,
8486 } ) ;
85- const idText = new Text ( `ID: ${ this . id } ` , textStyle ) ;
87+ const idText = new Text ( { text : `ID: ${ this . id } ` , style : textStyle } ) ;
8688 idText . anchor . set ( 0.5 ) ;
8789 idText . y = this . height - 15 ;
8890 idText . zIndex = ZIndexLevels . Label ;
@@ -141,8 +143,11 @@ export class Device extends Sprite {
141143 }
142144
143145 onPointerDown ( event : FederatedPointerEvent ) : void {
144- // console.log("Entered onPointerDown");
145- this . dragging = true ;
146+ console . log ( "Entered onPointerDown" ) ;
147+ if ( ! selectedDeviceId ) {
148+ selectElement ( this ) ;
149+ }
150+ Device . dragTarget = this ;
146151 event . stopPropagation ( ) ;
147152
148153 // Get the pointer position in world (viewport) coordinates
@@ -155,37 +160,8 @@ export class Device extends Sprite {
155160 this . offsetY = worldPosition . y - this . y ;
156161
157162 // Listen to global pointermove and pointerup events
158- document . addEventListener ( "pointermove" , this . onPointerMove . bind ( this ) ) ;
159- document . addEventListener ( "pointerup" , this . onPointerUp . bind ( this ) ) ;
160- }
161-
162- onPointerMove ( event : FederatedPointerEvent ) : void {
163- // console.log("Entered onPointerMove");
164- if ( this . dragging ) {
165- // Get the new pointer position in world coordinates
166- const worldPosition = this . viewgraph
167- . getViewport ( )
168- . toWorld ( event . clientX , event . clientY ) ;
169-
170- // Calculate the new sprite position using the calculated offset
171- const newPositionX = worldPosition . x - this . offsetX ;
172- const newPositionY = worldPosition . y - this . offsetY ;
173-
174- // Update the sprite position
175- this . x = newPositionX ;
176- this . y = newPositionY ;
177-
178- // Notify view graph about its movement
179- this . viewgraph . deviceMoved ( this . id ) ;
180- }
181- }
182-
183- onPointerUp ( ) : void {
184- // console.log("Entered onPointerUp");
185- this . dragging = false ;
186- // Remove global pointermove and pointerup events
187- document . removeEventListener ( "pointermove" , this . onPointerMove ) ;
188- document . removeEventListener ( "pointerup" , this . onPointerUp ) ;
163+ this . parent . on ( "pointermove" , onPointerMove ) ;
164+ this . parent . on ( "pointerup" , onPointerUp ) ;
189165 }
190166
191167 connectTo ( adyacentId : number ) : boolean {
@@ -327,3 +303,27 @@ export class Device extends Sprite {
327303 setSelectedDeviceId ( null ) ;
328304 }
329305}
306+
307+ function onPointerMove ( event : FederatedPointerEvent ) : void {
308+ console . log ( "Entered onPointerMove" ) ;
309+ if ( Device . dragTarget ) {
310+ Device . dragTarget . parent . toLocal (
311+ event . global ,
312+ null ,
313+ Device . dragTarget . position ,
314+ ) ;
315+
316+ // Notify view graph about its movement
317+ Device . dragTarget . viewgraph . deviceMoved ( Device . dragTarget . id ) ;
318+ }
319+ }
320+
321+ function onPointerUp ( ) : void {
322+ console . log ( "Entered onPointerUp" ) ;
323+ if ( Device . dragTarget ) {
324+ // Remove global pointermove and pointerup events
325+ Device . dragTarget . parent . off ( "pointermove" , onPointerMove ) ;
326+ Device . dragTarget . parent . off ( "pointerup" , onPointerUp ) ;
327+ Device . dragTarget = null ;
328+ }
329+ }
0 commit comments