@@ -33,6 +33,8 @@ import {
3333 showTooltip ,
3434} from "../../graphics/renderables/canvas_tooltip_manager" ;
3535
36+ const CIRCLE_RADIUS = 6 ; // Radius of the circle for drag and drop
37+
3638export enum DeviceType {
3739 Host = 0 ,
3840 Router = 1 ,
@@ -60,6 +62,10 @@ export function layerFromType(type: DeviceType) {
6062export abstract class ViewDevice extends Container {
6163 private sprite : Sprite ;
6264 private tooltip : Text | null = null ; // Tooltip como un Text de PIXI.js
65+ private isDragCircle = false ;
66+ private circleGraphic ?: Graphics ;
67+ private idLabel ?: Text ;
68+ private isVisibleFlag = true ; // Flag to track visibility
6369
6470 readonly id : DeviceId ;
6571 readonly viewgraph : ViewGraph ;
@@ -124,10 +130,10 @@ export abstract class ViewDevice extends Container {
124130 this . interactive = true ;
125131 this . cursor = "pointer" ;
126132 this . zIndex = ZIndexLevels . Device ;
127- this . updateVisibility ( ) ;
128133
129134 // Add device ID label using the helper function
130135 this . addDeviceIdLabel ( ) ;
136+ this . updateVisibility ( ) ;
131137
132138 // Set up tooltip behavior
133139 this . setupHoverTooltip ( ) ;
@@ -146,6 +152,7 @@ export abstract class ViewDevice extends Container {
146152
147153 private setupHoverTooltip ( ) {
148154 this . on ( "mouseover" , ( ) => {
155+ if ( this . isDragCircle ) return ;
149156 const currentLayer = this . ctx . getCurrentLayer ( ) ;
150157 const tooltipMessage = this . getTooltipDetails ( currentLayer ) ;
151158 this . tooltip = showTooltip (
@@ -162,18 +169,81 @@ export abstract class ViewDevice extends Container {
162169 } ) ;
163170 }
164171
172+ setCircleColor ( color : number ) {
173+ if ( ! this . isDragCircle ) return ;
174+ if ( this . circleGraphic ) {
175+ this . circleGraphic . clear ( ) ;
176+ this . circleGraphic . circle ( 0 , 0 , CIRCLE_RADIUS ) ;
177+ this . circleGraphic . fill ( { color } ) ;
178+ }
179+ }
180+
165181 /**
166182 * Abstract method to get tooltip details based on the layer.
167183 * Must be implemented by derived classes.
168184 */
169185 abstract getTooltipDetails ( layer : Layer ) : string ;
170186
187+ updateDevicesAspect ( ) {
188+ if ( ! this . isVisibleFlag ) {
189+ const edges = this . viewgraph
190+ . getConnections ( this . id )
191+ . filter ( ( e ) => e . isVisible ( ) ) ;
192+ // if it doesn't have visible edges, hide it completely
193+ if ( ! edges || edges . length === 0 ) {
194+ this . visible = false ;
195+ return ;
196+ }
197+ // if it has visible edges, show it as a drag circle
198+ this . visible = true ;
199+ this . setAsDragCircle ( ) ;
200+ } else {
201+ // if it is in the current layer, show it as a normal device
202+ this . visible = true ;
203+ this . setAsNormalDevice ( ) ;
204+ }
205+ }
206+
171207 updateVisibility ( ) {
172- this . visible = layerIncluded ( this . getLayer ( ) , this . viewgraph . getLayer ( ) ) ;
208+ this . isVisibleFlag = layerIncluded (
209+ this . getLayer ( ) ,
210+ this . viewgraph . getLayer ( ) ,
211+ ) ;
212+ }
213+
214+ private setAsDragCircle ( ) {
215+ if ( this . isDragCircle ) return ;
216+ this . isDragCircle = true ;
217+
218+ if ( this . sprite ) this . sprite . visible = false ;
219+ if ( this . idLabel ) this . idLabel . visible = false ;
220+ if ( ! this . circleGraphic ) {
221+ this . circleGraphic = new Graphics ( ) ;
222+ this . circleGraphic . circle ( 0 , 0 , CIRCLE_RADIUS ) ;
223+ this . circleGraphic . fill ( { color : Colors . Lightblue } ) ;
224+ this . addChild ( this . circleGraphic ) ;
225+ }
226+ this . eventMode = "static" ;
227+ this . interactive = true ;
228+ this . cursor = "grab" ;
229+ }
230+
231+ private setAsNormalDevice ( ) {
232+ if ( ! this . isDragCircle ) return ;
233+ this . isDragCircle = false ;
234+
235+ if ( this . sprite ) this . sprite . visible = true ;
236+ if ( this . idLabel ) this . idLabel . visible = true ;
237+ if ( this . circleGraphic ) {
238+ this . removeChild ( this . circleGraphic ) ;
239+ this . circleGraphic . destroy ( ) ;
240+ this . circleGraphic = undefined ;
241+ }
242+ this . cursor = "pointer" ;
173243 }
174244
175245 isVisible ( ) : boolean {
176- return this . visible ;
246+ return this . isVisibleFlag ;
177247 }
178248
179249 // Function to add the ID label to the device
@@ -188,6 +258,7 @@ export abstract class ViewDevice extends Container {
188258 idText . anchor . set ( 0.5 ) ;
189259 idText . y = this . height * 0.8 ;
190260 idText . zIndex = ZIndexLevels . Label ;
261+ this . idLabel = idText ;
191262 this . addChild ( idText ) ; // Add the ID text as a child of the device
192263 }
193264
@@ -208,7 +279,6 @@ export abstract class ViewDevice extends Container {
208279 }
209280 ViewDevice . dragTarget = this ;
210281
211- // Guardar posición inicial
212282 ViewDevice . startPosition = { x : this . x , y : this . y } ;
213283 event . stopPropagation ( ) ;
214284
@@ -229,6 +299,13 @@ export abstract class ViewDevice extends Container {
229299 ViewDevice . connectionTarget = null ;
230300 return ;
231301 }
302+
303+ // if the device is not visible, ignore
304+ if ( ! this . isVisibleFlag || ! ViewDevice . connectionTarget . isVisibleFlag ) {
305+ ViewDevice . connectionTarget = null ;
306+ return ;
307+ }
308+
232309 // Connect both devices
233310 const n1 = ViewDevice . connectionTarget . id ;
234311 const n2 = this . id ;
@@ -240,6 +317,10 @@ export abstract class ViewDevice extends Container {
240317 }
241318
242319 selectToConnect ( ) {
320+ // if the device is not visible, do nothing
321+ if ( ! this . isVisibleFlag ) {
322+ return ;
323+ }
243324 if ( ViewDevice . connectionTarget ) {
244325 ViewDevice . connectionTarget = null ;
245326 return ;
@@ -287,6 +368,7 @@ export abstract class ViewDevice extends Container {
287368 }
288369
289370 select ( ) {
371+ if ( this . isDragCircle ) return ;
290372 this . highlight ( ) ; // Calls highlight on select
291373 this . showInfo ( ) ;
292374 }
0 commit comments