Skip to content

Commit 1f9ce70

Browse files
authored
feat: add ID labels to devices (#38)
1 parent 11ddf40 commit 1f9ce70

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

src/types/devices/device.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { Texture, Sprite, FederatedPointerEvent, Graphics } from "pixi.js";
1+
import {
2+
Texture,
3+
Sprite,
4+
FederatedPointerEvent,
5+
Graphics,
6+
TextStyle,
7+
Text,
8+
} from "pixi.js";
29
import { Packet } from "./../packet";
310
import { ViewGraph } from "./../graphs/viewgraph";
411
import {
@@ -7,7 +14,7 @@ import {
714
selectElement,
815
} from "./../viewportManager";
916
import { RightBar } from "../../index";
10-
import { Colors } from "../../utils";
17+
import { Colors, ZIndexLevels } from "../../utils";
1118

1219
export const DEVICE_SIZE = 20;
1320

@@ -59,11 +66,29 @@ export class Device extends Sprite {
5966
this.eventMode = "static";
6067
this.interactive = true;
6168
this.cursor = "pointer";
69+
this.zIndex = ZIndexLevels.Device;
70+
71+
// Add device ID label using the helper function
72+
this.addDeviceIdLabel();
6273

6374
this.on("pointerdown", this.onPointerDown, this);
6475
this.on("click", this.onClick, this);
6576
}
6677

78+
// Function to add the ID label to the device
79+
addDeviceIdLabel() {
80+
const textStyle = new TextStyle({
81+
fontSize: 12,
82+
fill: Colors.Black,
83+
align: "center",
84+
});
85+
const idText = new Text(`ID: ${this.id}`, textStyle);
86+
idText.anchor.set(0.5);
87+
idText.y = this.height - 15;
88+
idText.zIndex = ZIndexLevels.Label;
89+
this.addChild(idText); // Add the ID text as a child of the device
90+
}
91+
6792
getConnections(): { edgeId: number; adyacentId: number }[] {
6893
return Array.from(this.connections.entries()).map(
6994
([edgeId, adyacentId]) => {
@@ -222,6 +247,8 @@ export class Device extends Sprite {
222247
// Change color to red and increase line thickness
223248
this.highlightMarker.stroke({ width: 3, color: Colors.Violet }); // Red and thicker
224249

250+
this.highlightMarker.zIndex = ZIndexLevels.Device;
251+
225252
// Ensure the marker is in the same container as the viewport
226253
this.addChild(this.highlightMarker);
227254
}

src/types/edge.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ViewGraph } from "./graphs/viewgraph";
33
import { Device } from "./devices/index"; // Import the Device class
44
import { deselectElement, selectElement } from "./viewportManager";
55
import { RightBar } from "..";
6-
import { Colors } from "../utils";
6+
import { Colors, ZIndexLevels } from "../utils";
77

88
export interface Position {
99
x: number;
@@ -62,6 +62,7 @@ export class Edge extends Graphics {
6262
this.moveTo(startPos.x, startPos.y);
6363
this.lineTo(endPos.x, endPos.y);
6464
this.stroke({ width: 3, color });
65+
this.zIndex = ZIndexLevels.Edge;
6566
this.startPos = startPos;
6667
this.endPos = endPos;
6768
}

src/utils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export enum Colors {
77
Green = 0x0000ff,
88
Red = 0xff0000,
99
White = 0xffffff,
10+
Black = 0x000000,
1011
}
1112

1213
export function drawCircle(
@@ -20,4 +21,12 @@ export function drawCircle(
2021
graphics.beginFill(color);
2122
graphics.drawCircle(x, y, radius);
2223
graphics.endFill();
24+
graphics.zIndex = ZIndexLevels.Packet;
25+
}
26+
27+
export enum ZIndexLevels {
28+
Device = 20,
29+
Edge = 15,
30+
Packet = 16,
31+
Label = 19,
2332
}

0 commit comments

Comments
 (0)