Skip to content

Commit 2968aed

Browse files
authored
refactor: set SVG size from code and CSS (#122)
This PR moves the device size from the SVG source to the CSS and JS code. As part of this it also removes some unused code. This also includes making the `Sprite` a field of `Device`, instead of its parent class.
1 parent 056c37a commit 2968aed

File tree

11 files changed

+73
-42
lines changed

11 files changed

+73
-42
lines changed

src/assets/pc.svg

Lines changed: 2 additions & 2 deletions
Loading

src/assets/router.svg

Lines changed: 2 additions & 2 deletions
Loading

src/assets/server.svg

Lines changed: 2 additions & 2 deletions
Loading

src/assets/switch.svg

Lines changed: 1 addition & 2 deletions
Loading

src/index.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,20 +78,14 @@ async function loadAssets(otherPromises: Promise<void>[]) {
7878
const leftBar = LeftBar.getFrom(document, ctx);
7979
leftBar.setButtonsByLayer(layerSelect.value);
8080

81-
// Ticker logic
82-
// app.ticker.add(() => { });
8381
const lBar = document.getElementById("left-bar");
8482
const rBar = document.getElementById("right-bar");
8583
const tBar = document.getElementById("top-bar");
8684

8785
// Resize logic
8886
function resize() {
89-
const leftBarWidth = lBar ? lBar.offsetWidth : 100;
90-
const rightBarWidth = rBar ? rBar.offsetWidth : 250;
91-
const topBarHeight = tBar ? tBar.offsetHeight : 40;
92-
93-
const newWidth = window.innerWidth - leftBarWidth - rightBarWidth;
94-
const newHeight = window.innerHeight - topBarHeight;
87+
const newWidth = window.innerWidth - lBar.offsetWidth - rBar.offsetWidth;
88+
const newHeight = window.innerHeight - tBar.offsetHeight;
9589

9690
app.renderer.resize(newWidth, newHeight);
9791
viewport.resize(newWidth, newHeight);

src/styles/global.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ body {
1111
background-color: #f2f2f2; /* Sets a light gray background for the page */
1212
}
1313

14+
/* Make SVGs take all the space from their parent */
15+
svg {
16+
width: 95%;
17+
height: 95%;
18+
}
19+
1420
/* Custom scrollbar styles */
1521

1622
/* Scrollbar width */

src/types/devices/device.ts

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
Graphics,
66
TextStyle,
77
Text,
8+
Container,
89
} from "pixi.js";
910
import { ViewGraph } from "./../graphs/viewgraph";
1011
import {
@@ -27,8 +28,6 @@ import { CreateDevice } from "./utils";
2728

2829
export { Layer } from "./layer";
2930

30-
export const DEVICE_SIZE = 20;
31-
3231
export enum DeviceType {
3332
Router = 0,
3433
Host = 1,
@@ -46,7 +45,9 @@ export function layerFromType(type: DeviceType) {
4645
}
4746
}
4847

49-
export abstract class Device extends Sprite {
48+
export abstract class Device extends Container {
49+
private sprite: Sprite;
50+
5051
readonly id: DeviceId;
5152
readonly viewgraph: ViewGraph;
5253
connections = new Set<DeviceId>();
@@ -57,6 +58,13 @@ export abstract class Device extends Sprite {
5758
static connectionTarget: Device | null = null;
5859
static startPosition: Position | null = null;
5960

61+
get width(): number {
62+
return this.sprite.width;
63+
}
64+
get height(): number {
65+
return this.sprite.height;
66+
}
67+
6068
ip: IpAddress;
6169
ipMask: IpAddress;
6270

@@ -68,19 +76,24 @@ export abstract class Device extends Sprite {
6876

6977
constructor(
7078
id: DeviceId,
71-
svg: string,
79+
texture: Texture,
7280
viewgraph: ViewGraph,
7381
position: Position,
7482
ip: IpAddress,
7583
ipMask: IpAddress,
7684
) {
77-
super(Texture.from(svg));
85+
super();
86+
7887
this.id = id;
7988
this.viewgraph = viewgraph;
8089
this.ip = ip;
8190
this.ipMask = ipMask;
8291

83-
this.anchor.set(0.5);
92+
this.sprite = new Sprite(texture);
93+
94+
this.sprite.anchor.set(0.5);
95+
this.sprite.setSize(50);
96+
this.addChild(this.sprite);
8497

8598
this.x = position.x;
8699
this.y = position.y;
@@ -105,10 +118,11 @@ export abstract class Device extends Sprite {
105118
fontSize: 12,
106119
fill: Colors.Black,
107120
align: "center",
121+
fontWeight: "bold",
108122
});
109123
const idText = new Text({ text: `ID: ${this.id}`, style: textStyle });
110124
idText.anchor.set(0.5);
111-
idText.y = this.height - 15;
125+
idText.y = this.height * 0.8;
112126
idText.zIndex = ZIndexLevels.Label;
113127
this.addChild(idText); // Add the ID text as a child of the device
114128
}
@@ -131,11 +145,6 @@ export abstract class Device extends Sprite {
131145
this.connections.delete(id);
132146
}
133147

134-
resize(sprite: Sprite): void {
135-
sprite.width = sprite.width / 70;
136-
sprite.height = sprite.height / DEVICE_SIZE;
137-
}
138-
139148
// TODO: Most probably it will be different for each type of device
140149
handlePacket(packet: Packet) {
141150
switch (packet.type) {
@@ -229,13 +238,10 @@ export abstract class Device extends Sprite {
229238
// Create the square as a selection marker
230239
this.highlightMarker = new Graphics();
231240

232-
this.highlightMarker.roundRect(
233-
-this.width / 2,
234-
-this.height / 2,
235-
this.width,
236-
this.height,
237-
5,
238-
);
241+
// NOTE: we get the original size since pixijs autoscales the sprite's children
242+
const { width, height } = this;
243+
244+
this.highlightMarker.roundRect(-width / 2, -height / 2, width, height, 5);
239245
this.highlightMarker.stroke({
240246
width: 3,
241247
color: Colors.Violet,

src/types/devices/host.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,18 @@ import {
1515
RunningProgram,
1616
} from "../../programs";
1717
import { Packet } from "../packet";
18+
import { Texture } from "pixi.js";
1819

1920
export class Host extends Device {
21+
static DEVICE_TEXTURE: Texture;
22+
23+
static getTexture() {
24+
if (!Host.DEVICE_TEXTURE) {
25+
Host.DEVICE_TEXTURE = Texture.from(PcImage);
26+
}
27+
return Host.DEVICE_TEXTURE;
28+
}
29+
2030
private runningPrograms = new Map<Pid, Program>();
2131
private lastProgramId = 0;
2232

@@ -27,7 +37,7 @@ export class Host extends Device {
2737
ip: IpAddress,
2838
mask: IpAddress,
2939
) {
30-
super(id, PcImage, viewgraph, position, ip, mask);
40+
super(id, Host.getTexture(), viewgraph, position, ip, mask);
3141
this.loadRunningPrograms();
3242
}
3343

src/types/devices/router.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,26 @@ import { DeviceInfo, RightBar } from "../../graphics/right_bar";
66
import { IpAddress } from "../../packets/ip";
77
import { DeviceId, isRouter } from "../graphs/datagraph";
88
import { Packet } from "../packet";
9+
import { Texture } from "pixi.js";
910

1011
export class Router extends Device {
12+
static DEVICE_TEXTURE: Texture;
13+
14+
static getTexture() {
15+
if (!Router.DEVICE_TEXTURE) {
16+
Router.DEVICE_TEXTURE = Texture.from(RouterImage);
17+
}
18+
return Router.DEVICE_TEXTURE;
19+
}
20+
1121
constructor(
1222
id: DeviceId,
1323
viewgraph: ViewGraph,
1424
position: Position,
1525
ip: IpAddress,
1626
mask: IpAddress,
1727
) {
18-
super(id, RouterImage, viewgraph, position, ip, mask);
28+
super(id, Router.getTexture(), viewgraph, position, ip, mask);
1929
}
2030

2131
showInfo(): void {

src/types/devices/switch.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,20 @@ import { Position } from "../common";
55
import { DeviceInfo, RightBar } from "../../graphics/right_bar";
66
import { DeviceId } from "../graphs/datagraph";
77
import { Packet } from "../packet";
8+
import { Texture } from "pixi.js";
89

910
export class Switch extends Device {
11+
static DEVICE_TEXTURE: Texture;
12+
13+
static getTexture() {
14+
if (!Switch.DEVICE_TEXTURE) {
15+
Switch.DEVICE_TEXTURE = Texture.from(SwitchImage);
16+
}
17+
return Switch.DEVICE_TEXTURE;
18+
}
19+
1020
constructor(id: DeviceId, viewgraph: ViewGraph, position: Position) {
11-
super(id, SwitchImage, viewgraph, position, null, null);
21+
super(id, Switch.getTexture(), viewgraph, position, null, null);
1222
}
1323

1424
showInfo(): void {

0 commit comments

Comments
 (0)