Skip to content

Commit 167c265

Browse files
committed
Merge pull request #524 from fodinabor/UIMultilineLabelFixes
Fix UIMultilineLabels to return the correct width and height.
2 parents 008b97c + cee22aa commit 167c265

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

Modules/Contents/UI/Include/PolyUIElement.h

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,20 +149,29 @@ namespace Polycode {
149149
*/
150150
void setColor(Color color);
151151

152+
/**
153+
* @return the max width as a Number
154+
*/
155+
Number getWidth();
156+
157+
/**
158+
* @return the max height as a Number
159+
*/
160+
Number getHeight();
161+
152162
~UIMultilineLabel();
153163
protected:
154164

155165
int labelSize;
156166
String labelFontName;
157167
int labelAAMode;
158168
int spacing;
159-
169+
int linesCount;
170+
160171
void clearLabels();
161172
std::vector<UILabel*> labels;
162173
};
163-
164-
165-
174+
166175
class _PolyExport UIImage : public UIRect {
167176
public:
168177
UIImage(String imagePath);

Modules/Contents/UI/Source/PolyUIElement.cpp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ void UIMultilineLabel::setText(const String& text) {
4242
clearLabels();
4343

4444
std::vector<String> lines = text.split("\n");
45-
46-
Number lineSize = spacing;
45+
linesCount = lines.size();
46+
47+
Number lineSize = spacing;
4748
Number yPos = 0.0;
4849
for(int i=0; i < lines.size(); i++) {
4950
if(lines[i] == "") {
@@ -96,13 +97,31 @@ void UIMultilineLabel::clearLabels() {
9697
labels.clear();
9798
}
9899

100+
Number UIMultilineLabel::getWidth(){
101+
Number maxWidth = 0;
102+
for (int i = 0; i < labels.size(); i++) {
103+
if (labels[i]->getWidth() > maxWidth){
104+
maxWidth = labels[i]->getWidth();
105+
}
106+
}
107+
return maxWidth;
108+
}
109+
110+
Number UIMultilineLabel::getHeight(){
111+
Number retHeight = 0;
112+
for (int i = 0; i < labels.size(); i++) {
113+
retHeight += labels[i]->getHeight() + spacing;
114+
}
115+
retHeight += (linesCount - labels.size()) * (labelSize + spacing);
116+
return retHeight;
117+
}
118+
99119
UIMultilineLabel::~UIMultilineLabel() {
100120
if(!ownsChildren) {
101121
clearLabels();
102122
}
103123
}
104124

105-
106125
UILabel::UILabel(const String& text, int size, const String& fontName, int amode) : UIElement() {
107126

108127
Config *conf = CoreServices::getInstance()->getConfig();

0 commit comments

Comments
 (0)