Skip to content

Commit 325a6b5

Browse files
authored
feat: keep edges and hide devices from sub-layers (#181)
This PR makes viewgraph store all devices, but makes invisible devices not of the current layer (or above).
1 parent 3bbab32 commit 325a6b5

File tree

5 files changed

+39
-83
lines changed

5 files changed

+39
-83
lines changed

src/graphics/renderables/program_info.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ export class ProgramInfo implements Renderable {
2020
this.inputs.push(container);
2121
this.inputsValues.push(getValue);
2222
}
23-
getInputValues() {
24-
console.log("getInputValues", this.inputsValues);
2523

24+
getInputValues() {
2625
return this.inputsValues.map((getValue) => getValue());
2726
}
2827

@@ -33,7 +32,7 @@ export class ProgramInfo implements Renderable {
3332

3433
function otherDevices(viewgraph: ViewGraph, srcId: DeviceId) {
3534
return viewgraph
36-
.getDeviceIds()
35+
.getLayerDeviceIds()
3736
.filter((id) => id !== srcId)
3837
.map((id) => ({ value: id.toString(), text: `Device ${id}` }));
3938
}

src/graphics/right_bar.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,6 @@ export function createDropdown(
642642
const option = document.createElement("div");
643643
option.classList.add("dropdown-option");
644644
option.textContent = optionData.text;
645-
console.log("Attaching tooltip to", optionData.text);
646645
TooltipManager.getInstance().attachTooltip(option, optionData.text); // Attach tooltip to the option
647646

648647
// Set up click event for option selection

src/types/edge.ts

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -80,22 +80,15 @@ export class Edge extends Graphics {
8080
}
8181

8282
deselect() {
83-
// TODO
84-
console.log("deselected");
8583
this.removeHighlight();
8684
}
8785

8886
highlight() {
8987
this.drawEdge(this.startPos, this.endPos, Colors.Violet);
90-
this.viewgraph.transparentEdgesForEdge(
91-
this.connectedNodes.n1,
92-
this.connectedNodes.n2,
93-
);
9488
}
9589

9690
removeHighlight() {
9791
this.drawEdge(this.startPos, this.endPos, Colors.Lightblue);
98-
this.viewgraph.untransparentEdges();
9992
}
10093

10194
// Method to show the Edge information
@@ -156,23 +149,26 @@ export class Edge extends Graphics {
156149
super.destroy();
157150
}
158151

159-
becomeTransparent() {
160-
this.alpha = 0.2;
161-
}
162-
163-
becomeOpaque() {
164-
this.alpha = 1;
165-
}
166-
167152
public updatePosition(device1: ViewDevice, device2: ViewDevice) {
168153
const dx = device2.x - device1.x;
169154
const dy = device2.y - device1.y;
170155
const angle = Math.atan2(dy, dx);
171156

172-
const offsetX1 = ((device1.width + 5) / 2) * Math.cos(angle);
173-
const offsetY1 = ((device1.height + 5) / 2) * Math.sin(angle);
174-
const offsetX2 = ((device2.width + 5) / 2) * Math.cos(angle);
175-
const offsetY2 = ((device2.height + 5) / 2) * Math.sin(angle);
157+
const n1IsVisible = device1.visible;
158+
const n2IsVisible = device2.visible;
159+
160+
const offsetX1 = n1IsVisible
161+
? ((device1.width + 5) / 2) * Math.cos(angle)
162+
: 0;
163+
const offsetY1 = n1IsVisible
164+
? ((device1.height + 5) / 2) * Math.sin(angle)
165+
: 0;
166+
const offsetX2 = n2IsVisible
167+
? ((device2.width + 5) / 2) * Math.cos(angle)
168+
: 0;
169+
const offsetY2 = n2IsVisible
170+
? ((device2.height + 5) / 2) * Math.sin(angle)
171+
: 0;
176172

177173
const newStartPos: Point = new Point(
178174
device1.x + offsetX1,

src/types/graphs/viewgraph.ts

Lines changed: 20 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ import { ViewDevice } from "../view-devices";
22
import { Edge, EdgeEdges } from "./../edge";
33
import { DataGraph, DeviceId, DataNode, RemovedNodeData } from "./datagraph";
44
import { Viewport } from "../../graphics/viewport";
5-
import { Layer, layerIncluded } from "../layer";
5+
import { Layer } from "../layer";
66
import { createViewDevice } from "../view-devices/utils";
7-
import { layerFromType } from "../view-devices/vDevice";
87
import { IpAddress } from "../../packets/ip";
98
import { GlobalContext } from "../../context";
109
import { Graph } from "./graph";
@@ -36,17 +35,14 @@ export class ViewGraph {
3635
const allConnections = new Map<string, EdgePair>();
3736

3837
for (const [deviceId, device] of this.datagraph.getDevices()) {
39-
if (layerIncluded(layerFromType(device.getType()), this.layer)) {
40-
this.addDevice(deviceId, device.getDataNode());
41-
layerDFS(
42-
this.datagraph,
43-
this.layer,
44-
deviceId,
45-
deviceId,
46-
new Set([deviceId]),
47-
allConnections,
48-
);
49-
}
38+
this.addDevice(deviceId, device.getDataNode());
39+
layerDFS(
40+
this.datagraph,
41+
deviceId,
42+
deviceId,
43+
new Set([deviceId]),
44+
allConnections,
45+
);
5046
}
5147
console.debug(allConnections);
5248
this.addConnections(allConnections);
@@ -60,7 +56,6 @@ export class ViewGraph {
6056
const connections = new Map<string, EdgePair>();
6157
layerDFS(
6258
this.datagraph,
63-
this.layer,
6459
deviceId,
6560
deviceId,
6661
new Set([deviceId]),
@@ -220,9 +215,13 @@ export class ViewGraph {
220215
return Array.from(this.graph.getAllVertices()).map(([, device]) => device);
221216
}
222217

223-
// Returns an array of devices’ ids
224-
getDeviceIds(): DeviceId[] {
225-
return Array.from(this.graph.getAllVertices()).map(([id]) => id);
218+
/**
219+
* Returns all devices in the layer of the viewgraph
220+
*/
221+
getLayerDeviceIds(): DeviceId[] {
222+
return Array.from(this.graph.getAllVertices())
223+
.filter(([, { visible }]) => visible)
224+
.map(([id]) => id);
226225
}
227226

228227
// Get the number of devices in the graph
@@ -396,38 +395,10 @@ export class ViewGraph {
396395
}
397396
this.graph.clear();
398397
}
399-
400-
// Make all edges transparent except for the ones connected to the device
401-
transparentEdgesForDevice(id: DeviceId) {
402-
for (const [, , edge] of this.graph.getAllEdges()) {
403-
if (edge.connectedNodes.n1 !== id && edge.connectedNodes.n2 !== id) {
404-
edge.becomeTransparent();
405-
}
406-
}
407-
}
408-
409-
// Make all edges transparent except for the edge between the two devices
410-
transparentEdgesForEdge(n1: DeviceId, n2: DeviceId) {
411-
for (const [, , edge] of this.graph.getAllEdges()) {
412-
if (
413-
(edge.connectedNodes.n1 !== n1 || edge.connectedNodes.n2 !== n2) &&
414-
(edge.connectedNodes.n1 !== n2 || edge.connectedNodes.n2 !== n1)
415-
) {
416-
edge.becomeTransparent();
417-
}
418-
}
419-
}
420-
421-
untransparentEdges() {
422-
for (const [, , edge] of this.graph.getAllEdges()) {
423-
edge.becomeOpaque();
424-
}
425-
}
426398
}
427399

428400
function layerDFS(
429401
datagraph: DataGraph,
430-
layer: Layer,
431402
s: DeviceId, // source node
432403
v: DeviceId,
433404
visited: Set<DeviceId>,
@@ -437,18 +408,12 @@ function layerDFS(
437408
if (visited.has(w)) {
438409
return;
439410
}
440-
const adjacent = datagraph.getDevice(w);
441411
// mark node as visited
442412
visited.add(w);
443413

444-
if (layerIncluded(layerFromType(adjacent.getType()), layer)) {
445-
// NOTE: we use strings because according to JavaScript, [1, 2] !== [1, 2]
446-
const edgePair: EdgePair = [w, s];
447-
edgePair.sort();
448-
connections.set(edgePair.toString(), edgePair);
449-
} else {
450-
// continue with recursive search
451-
layerDFS(datagraph, layer, s, w, visited, connections);
452-
}
414+
// NOTE: we use strings because according to JavaScript, [1, 2] !== [1, 2]
415+
const edgePair: EdgePair = [w, s];
416+
edgePair.sort();
417+
connections.set(edgePair.toString(), edgePair);
453418
});
454419
}

src/types/view-devices/vDevice.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { DeviceInfo } from "../../graphics/renderables/device_info";
2121
import { IpAddress } from "../../packets/ip";
2222
import { DeviceId, RemovedNodeData } from "../graphs/datagraph";
2323
import { DragDeviceMove, AddEdgeMove } from "../undo-redo";
24-
import { Layer } from "../layer";
24+
import { Layer, layerIncluded } from "../layer";
2525
import { EthernetFrame, MacAddress } from "../../packets/ethernet";
2626
import { GlobalContext } from "../../context";
2727

@@ -101,6 +101,7 @@ export abstract class ViewDevice extends Container {
101101
this.interactive = true;
102102
this.cursor = "pointer";
103103
this.zIndex = ZIndexLevels.Device;
104+
this.visible = layerIncluded(this.getLayer(), this.viewgraph.getLayer());
104105

105106
// Add device ID label using the helper function
106107
this.addDeviceIdLabel();
@@ -203,9 +204,6 @@ export abstract class ViewDevice extends Container {
203204
});
204205
this.highlightMarker.zIndex = ZIndexLevels.Device;
205206

206-
// Make the unselected edges transparent to improve visibility
207-
this.viewgraph.transparentEdgesForDevice(this.id);
208-
209207
// Ensure the marker is in the same container as the viewport
210208
this.addChild(this.highlightMarker);
211209
}
@@ -215,7 +213,6 @@ export abstract class ViewDevice extends Container {
215213
this.highlightMarker.clear(); // Clear the graphic
216214
this.removeChild(this.highlightMarker); // Remove the marker from the viewport
217215
this.highlightMarker.destroy(); // Destroy the graphic object to free memory
218-
this.viewgraph.untransparentEdges(); // Make the edges opaque again
219216
this.highlightMarker = null;
220217
}
221218
}

0 commit comments

Comments
 (0)