forked from asalga/Horadrix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RetroLabel.pde
193 lines (154 loc) · 4.6 KB
/
RetroLabel.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
*
*/
public class RetroLabel extends RetroPanel{
public static final int JUSTIFY_MANUAL = 0;
public static final int JUSTIFY_LEFT = 1;
//public static final int JUSTIFY_RIGHT = 1;
private final int NEWLINE = 10;
private String text;
private RetroFont font;
//
private int horizontalSpacing;
private int verticalSpacing;
private boolean horizontalTrimming;
private int justification;
public RetroLabel(RetroFont font){
setFont(font);
setVerticalSpacing(1);
setHorizontalSpacing(1);
//setJustification(JUSTIFY_LEFT);
setHorizontalTrimming(false);
}
public void setHorizontalTrimming(boolean horizTrim){
horizontalTrimming = horizTrim;
}
/**
*/
public void setHorizontalSpacing(int spacing){
horizontalSpacing = spacing;
dirty = true;
}
public void setVerticalSpacing(int spacing){
verticalSpacing = spacing;
dirty = true;
}
/*
* Will immediately calculate the width
*/
public void setText(String text){
this.text = text;
dirty = true;
int newWidth = 0;
int newHeight = font.getGlyphHeight();
int longestLine = 0;
for(int letter = 0; letter < text.length(); letter++){
if((text.charAt(letter)) == 10){
newHeight += font.getGlyphHeight();
}
else{
PImage glyph = getGlyph(text.charAt(letter));
if(glyph != null){
newWidth += glyph.width + horizontalSpacing;
}
}
}
h = newHeight;
w = newWidth;
}
public void setFont(RetroFont font){
this.font = font;
dirty = true;
h = this.font.getGlyphHeight();
}
/**
*/
private int getStringWidth(String str){
return str.length() * font.getGlyphWidth() + (str.length() * horizontalSpacing );
}
//public void setJustification(int justify){
// justification = justify;
//}
/**
Draws the text in the label depending on the
justification of the panel is sits inside
*/
public void draw(){
super.draw();
// Return if there is nothing to draw
if(text == null || visible == false){
return;
}
if(justification == JUSTIFY_MANUAL){
int currX = x;
int lineIndex = 0;
for(int letter = 0; letter < text.length(); letter++){
if((int)text.charAt(letter) == NEWLINE){
lineIndex++;
currX = x;
continue;
}
PImage glyph = getGlyph(text.charAt(letter));
if(glyph != null){
image(glyph, currX, y + lineIndex * (font.getGlyphHeight() + verticalSpacing));
currX += glyph.width + horizontalSpacing;
}
}
currX += font.getGlyphWidth();
}
else{
// iterate over each word and see if it would fit into the
// panel. If not, add a line break
String[] words = text.split(" ");
int[] firstCharIndex = new int[words.length];
// get indices of fist char of every word
// for(int word = 0; word < words.length; word++){
// firstCharIndex[word] =
//}
firstCharIndex[0] = 0;
int iter = 1;
for(int letter = 0; letter < text.length(); letter++){
if(text.charAt(letter) == ' '){
firstCharIndex[iter] = letter + 1;
iter++;
}
}
int[] wordWidths;
// start drawing at the panel
int currXPos = x;
int lineIndex = 0;
// Iterate over all the words
for(int word = 0; word < words.length; word++){
int wordWidth = getStringWidth(words[word]);
if(justification == JUSTIFY_LEFT){
if(word != 0 && currXPos + wordWidth + 0 > getParent().getWidth() ){
lineIndex++;
currXPos = x;
}
}
// Iterate over the letter of each word
for(int letter = 0; letter < words[word].length(); letter++){
int firstChar = firstCharIndex[word];
//
if((int)words[word].charAt(letter) == NEWLINE){
lineIndex++;
currXPos = x;
continue;
}
PImage glyph = getGlyph(words[word].charAt(letter));
if(glyph != null){
image(glyph, currXPos, lineIndex * (font.getGlyphHeight() + verticalSpacing));
currXPos += font.getGlyphWidth() + horizontalSpacing;
}
}
currXPos += font.getGlyphWidth();
}
}
}
private PImage getGlyph(char ch){
if(horizontalTrimming == true){
return font.getTrimmedGlyph(ch );
}
return font.getGlyph( ch);
}
}