Skip to content

Commit 669cc24

Browse files
authored
Merge pull request #58 from key4hep/feat/object-title
Show object name at top when drawing
2 parents bc2b522 + 136f20a commit 669cc24

File tree

3 files changed

+74
-73
lines changed

3 files changed

+74
-73
lines changed

js/lib/graphic-primitives.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,27 @@ export function drawStraightLink(ctx, link) {
107107
ctx.stroke();
108108
ctx.restore();
109109
}
110+
111+
export function drawObjectHeader(ctx, object) {
112+
ctx.save();
113+
ctx.font = "bold 16px sans-serif";
114+
const text = object.constructor.name;
115+
const boxCenterX = object.x + object.width / 2;
116+
const textWidth = ctx.measureText(text).width;
117+
const x = boxCenterX - ctx.measureText(text).width / 2;
118+
const topY = object.y + 20;
119+
120+
if (textWidth > object.width) {
121+
const lines = text.split(/(?=[A-Z])/);
122+
for (const [i, lineText] of lines.entries()) {
123+
ctx.fillText(
124+
lineText,
125+
boxCenterX - ctx.measureText(lineText).width / 2,
126+
topY + i * 20
127+
);
128+
}
129+
} else {
130+
ctx.fillText(text, x, topY);
131+
}
132+
ctx.restore();
133+
}

js/types/objects.js

Lines changed: 49 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,39 @@ import {
22
drawTex,
33
drawRoundedRect,
44
drawTextLines,
5+
drawObjectHeader,
56
} from "../lib/graphic-primitives.js";
67
import { getName } from "../lib/getName.js";
78
import { linkTypes } from "./links.js";
89
import { parseCharge } from "../lib/parseCharge.js";
910

11+
const TOP_MARGIN = 45;
12+
1013
class EDMObject {
1114
constructor() {
1215
this.x = NaN;
1316
this.y = NaN;
1417
this.index = NaN;
1518
this.collectionId = NaN;
1619
this.width = 120;
17-
this.height = 240;
20+
this.height = 260;
1821
this.lineColor = "black";
1922
this.lineWidth = 2;
2023
this.color = "white";
2124
}
2225

23-
draw(ctx) {}
26+
draw(ctx) {
27+
drawRoundedRect(
28+
ctx,
29+
this.x,
30+
this.y,
31+
this.width,
32+
this.height,
33+
this.color,
34+
this.radius
35+
);
36+
drawObjectHeader(ctx, this);
37+
}
2438

2539
isHere(mouseX, mouseY) {
2640
return (
@@ -44,10 +58,11 @@ class EDMObject {
4458
export class MCParticle extends EDMObject {
4559
constructor() {
4660
super();
47-
4861
this.row = -1;
49-
5062
this.texImg = null;
63+
this.color = "#dff6ff";
64+
this.radius = 15;
65+
this.height = 270;
5166
}
5267

5368
updateTexImg(text) {
@@ -65,21 +80,13 @@ export class MCParticle extends EDMObject {
6580
draw(ctx) {
6681
const boxCenterX = this.x + this.width / 2;
6782

68-
drawRoundedRect(
69-
ctx,
70-
this.x,
71-
this.y,
72-
this.width,
73-
this.height,
74-
"#dff6ff",
75-
15
76-
);
83+
super.draw(ctx);
7784

7885
if (this.texImg.complete) {
7986
drawTex(
8087
ctx,
8188
boxCenterX,
82-
this.y + this.height * 0.4,
89+
this.y + TOP_MARGIN + 80,
8390
this.texImg,
8491
this.width
8592
);
@@ -88,20 +95,20 @@ export class MCParticle extends EDMObject {
8895
drawTex(
8996
ctx,
9097
boxCenterX,
91-
this.y + this.height * 0.4,
98+
this.y + TOP_MARGIN + 80,
9299
this.texImg,
93100
this.width
94101
);
95102
};
96103
}
97104

98-
const topY = this.y + 20;
105+
const topY = this.y + TOP_MARGIN;
99106
const topLines = [];
100107
topLines.push("ID: " + this.index);
101108
topLines.push("Gen. stat.: " + this.generatorStatus);
102109
topLines.push("Sim. stat.: " + this.simulatorStatus);
103110

104-
const bottomY = this.y + this.height * 0.6;
111+
const bottomY = this.y + this.height * 0.65;
105112
const bottomLines = [];
106113
bottomLines.push("p = " + this.momentum + " GeV");
107114
bottomLines.push("d = " + this.vertex + " mm");
@@ -200,23 +207,17 @@ class ReconstructedParticle extends EDMObject {
200207
constructor() {
201208
super();
202209
this.width = 140;
203-
this.height = 180;
210+
this.height = 190;
211+
this.color = "#fbffdf";
212+
this.radius = 30;
204213
}
205214

206215
draw(ctx) {
207216
const boxCenterX = this.x + this.width / 2;
208217

209-
drawRoundedRect(
210-
ctx,
211-
this.x,
212-
this.y,
213-
this.width,
214-
this.height,
215-
"#fbffdf",
216-
30
217-
);
218+
super.draw(ctx);
218219

219-
const topY = this.y + 20;
220+
const topY = this.y + 1.5 * TOP_MARGIN;
220221
const lines = [];
221222

222223
lines.push("ID: " + this.index);
@@ -245,23 +246,17 @@ class Cluster extends EDMObject {
245246
constructor() {
246247
super();
247248
this.width = 140;
248-
this.height = 180;
249+
this.height = 170;
250+
this.color = "#ffe8df";
251+
this.radius = 20;
249252
}
250253

251254
draw(ctx) {
252255
const boxCenterX = this.x + this.width / 2;
253256

254-
drawRoundedRect(
255-
ctx,
256-
this.x,
257-
this.y,
258-
this.width,
259-
this.height,
260-
"#ffe8df",
261-
20
262-
);
257+
super.draw(ctx);
263258

264-
const topY = this.y + 20;
259+
const topY = this.y + TOP_MARGIN;
265260
const lines = [];
266261
lines.push("ID: " + this.index);
267262
lines.push("type: " + this.type);
@@ -284,23 +279,17 @@ class Track extends EDMObject {
284279
constructor() {
285280
super();
286281
this.width = 140;
287-
this.height = 180;
282+
this.height = 150;
283+
this.color = "#fff6df";
284+
this.radius = 25;
288285
}
289286

290287
draw(ctx) {
291288
const boxCenterX = this.x + this.width / 2;
292289

293-
drawRoundedRect(
294-
ctx,
295-
this.x,
296-
this.y,
297-
this.width,
298-
this.height,
299-
"#fff6df",
300-
25
301-
);
290+
super.draw(ctx);
302291

303-
const topY = this.y + 20;
292+
const topY = this.y + TOP_MARGIN;
304293

305294
const lines = [];
306295
lines.push("ID: " + this.index);
@@ -324,23 +313,17 @@ class ParticleID extends EDMObject {
324313
constructor() {
325314
super();
326315
this.width = 140;
327-
this.height = 120;
316+
this.height = 140;
317+
this.color = "#c9edf7";
318+
this.radius = 25;
328319
}
329320

330321
draw(ctx) {
331322
const boxCenterX = this.x + this.width / 2;
332323

333-
drawRoundedRect(
334-
ctx,
335-
this.x,
336-
this.y,
337-
this.width,
338-
this.height,
339-
"#c9edf7",
340-
25
341-
);
324+
super.draw(ctx);
342325

343-
const topY = this.y + 20;
326+
const topY = this.y + TOP_MARGIN;
344327

345328
const lines = [];
346329
lines.push("ID: " + this.index);
@@ -359,23 +342,17 @@ class Vertex extends EDMObject {
359342
constructor() {
360343
super();
361344
this.width = 140;
362-
this.height = 140;
345+
this.height = 150;
346+
this.color = "#f5d3ef";
347+
this.radius = 25;
363348
}
364349

365350
draw(ctx) {
366351
const boxCenterX = this.x + this.width / 2;
367352

368-
drawRoundedRect(
369-
ctx,
370-
this.x,
371-
this.y,
372-
this.width,
373-
this.height,
374-
"#f5d3ef",
375-
25
376-
);
353+
super.draw(ctx);
377354

378-
const topY = this.y + 20;
355+
const topY = this.y + TOP_MARGIN;
379356

380357
const lines = [];
381358
lines.push("ID: " + this.index);

test/objects.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ describe("Link", () => {
108108
secondObject.x = 140;
109109
secondObject.y = 250;
110110

111-
expect(link.isVisible(0, 0, 250, 250)).toBe(true);
111+
expect(link.isVisible(0, 0, 300, 300)).toBe(true);
112112
});
113113

114114
it("should return false if the link is not visible", () => {

0 commit comments

Comments
 (0)