/
labelwidget.h
200 lines (168 loc) · 6.32 KB
/
labelwidget.h
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
194
195
196
197
198
199
200
/** @file labelwidget.h
*
* @authors Copyright (c) 2013 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#ifndef DENG_CLIENT_LABELWIDGET_H
#define DENG_CLIENT_LABELWIDGET_H
#include <de/Image>
#include <de/GLBuffer>
#include <de/GLUniform>
#include "../uidefs.h"
#include "GuiWidget"
#include "ProceduralImage"
#include "ui/Data"
/**
* Widget showing a label text and/or image.
*
* LabelWidget offers several parameters for controlling the layout of the text
* and image components. The widget is also able to independently determine its
* size to exactly fit its contents (according to the LabelWidget::SizePolicy).
*
* The alignment parameters are applied as follows:
*
* - LabelWidget::setAlignment() defines where the content of the widget is
* aligned as a group, once the relative positions of the image and the text
* have been determined.
*
* - LabelWidget::setTextAlignment() defines where the text will be positioned
* in relation to the image. For instance, if the text alignment is AlignRight,
* the text will be placed on the right side of the image. If there is no
* image, this has no effect.
*
* - LabelWidget::setImageAlignment() defines how the image is aligned in
* relation to text when both are visible. For instance, if text is aligned to
* AlignRight (appearing on the right side of the image), then an image
* alignment of AlignTop would align the top of the image with the top of the
* text. AlignBottom would align the bottom of the image with the bottom of the
* text. This value must be on the perpendicular axis when compared to text
* alignment (otherwise it has no effect).
*
* - LabelWidget::setTextLineAlignment() defines the alignment of each individual
* wrapped line of text within the text block.
*
* Additionally, LabelWidget::setImageFit() defines how the image will be
* scaled inside the area reserved for the image.
*
* @ingroup gui
*/
class LabelWidget : public GuiWidget
{
public:
LabelWidget(de::String const &name = "");
void setText(de::String const &text);
void setImage(de::Image const &image);
/**
* Sets the image drawn in the label. Procedural images can generate any
* geometry on the fly, so the image can be fully animated.
*
* @param procImage Procedural image. LabelWidget takes ownership.
*/
void setImage(ProceduralImage *procImage);
de::String text() const;
/**
* Returns the actual size of the text in pixels.
*/
de::Vector2ui textSize() const;
/**
* Sets the gap between the text and image. Defaults to "label.gap".
*
* @param styleRuleId Id of a rule in the style.
*/
void setTextGap(de::DotPath const &styleRuleId);
enum AlignmentMode {
AlignByCombination,
AlignOnlyByImage,
AlignOnlyByText
};
/**
* Sets the alignment of the entire contents of the widget inside its
* rectangle.
*
* @param align Alignment for all content.
* @param alignMode Mode of alignment (by combo/text/image).
*/
void setAlignment(ui::Alignment const &align,
AlignmentMode alignMode = AlignByCombination);
void setTextAlignment(ui::Alignment const &textAlign);
void setTextLineAlignment(ui::Alignment const &textLineAlign);
/**
* Sets the alignment of the image when there is both an image
* and a text in the label.
*
* @param imageAlign Alignment for the image.
*/
void setImageAlignment(ui::Alignment const &imageAlign);
void setImageFit(ui::ContentFit const &fit);
/**
* Sets the maximum width used for text. By default, the maximum width is determined
* automatically based on the layout of the label content.
*
* @param pixels Maximum width of text, or 0 to determine automatically.
*/
void setMaximumTextWidth(int pixels);
/**
* Sets an alternative style for text. By default, the rich text styling comes
* from Style.
*
* @param richStyle Rich text styling.
*/
void setTextStyle(de::Font::RichFormat::IStyle const *richStyle);
/**
* The image's actual size will be overridden by this size.
* @param size Image size.
*/
void setOverrideImageSize(de::Vector2f const &size);
void setOverrideImageSize(float widthAndHeight);
void setImageScale(float scaleFactor);
void setImageColor(de::Vector4f const &imageColor);
bool hasImage() const;
/**
* Allows or disallows the label to expand vertically to fit the provided
* content. By default, labels do not adjust their own size.
*
* @param expand @c true to allow the widget to modify its own height.
*/
void setSizePolicy(ui::SizePolicy horizontal, ui::SizePolicy vertical) {
setWidthPolicy(horizontal);
setHeightPolicy(vertical);
}
void setWidthPolicy(ui::SizePolicy policy);
void setHeightPolicy(ui::SizePolicy policy);
// Events.
void viewResized();
void update();
void drawContent();
struct ContentLayout {
de::Rectanglef image;
de::Rectanglei text;
};
void contentLayout(ContentLayout &layout);
public:
static LabelWidget *newWithText(de::String const &label, GuiWidget *parent = 0);
protected:
void glInit();
void glDeinit();
void glMakeGeometry(DefaultVertexBuf::Builder &verts);
void updateStyle();
/**
* Called before drawing to update the model-view-projection matrix.
* Derived classes may override this to set a custom matrix for the label.
*/
virtual void updateModelViewProjection(de::GLUniform &uMvp);
private:
DENG2_PRIVATE(d)
};
#endif // DENG_CLIENT_LABELWIDGET_H