-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathMetricItem.pde
62 lines (48 loc) · 1.54 KB
/
MetricItem.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright (C) 2013 Adam Tornhill
//
// Distributed under the GNU General Public License v3.0,
// see http://www.gnu.org/licenses/gpl.html
// The metric items correspond to one line in the input.
// Each one of them gets represented as an rectangle in the map.
class MetricItem extends SimpleMapItem {
private final String artifact;
private final int TEXT_MARGIN = 3;
public MetricItem(String artifact, int weight) {
this.artifact = artifact;
setSize(weight); // controls the relative size of this item's box
}
public String name() {
return artifact;
}
public void draw() {
drawAsRectangle();
labelWithArtifactName();
}
private void drawAsRectangle() {
strokeWeight(0.25);
int alphaChannelReflectWeight = (int)getSize()*2;
fill(255, 0, 0, alphaChannelReflectWeight);
rect(x, y, w, h);
}
private void labelWithArtifactName() {
calculateOptimalFontSize();
fill(0);
textAlign(CENTER, CENTER);
text(artifact, x + w/2, y + h/2);
}
private void calculateOptimalFontSize() {
// Increase the font until the text fills either the width or the height of
// our rectangular box:
for (int i = minFontSize; i <= maxFontSize; i++) {
textFont(font,i);
if (optimumTextSize()) {
break;
}
}
}
private boolean optimumTextSize() {
final boolean fitsInWidth = w < textWidth(artifact) + TEXT_MARGIN;
final boolean fitsInHeight = h < (textAscent()+textDescent()) + TEXT_MARGIN;
return fitsInWidth || fitsInHeight;
}
}