xoreos  0.0.5
label.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
24 #include "src/graphics/font.h"
25 
28 
30 
31 namespace Engines {
32 
33 namespace NWN {
34 
36  NWNWidgetWithCaption(gui, tag), _width(0.0), _height(0.0) {
37 
38 }
39 
41 }
42 
44  float r, float g, float b, float a, float halign, float valign) {
45 
46  // Not all labels are wide enough for their text
47  float textWidth = 1.0 + FontMan.get(font).getFont().getLineWidth(text);
48  float textHeight = FontMan.get(font).getFont().getHeight();
49  float oldWidth = _width;
50 
51  // Only for labels that can contain at max 1 line
52  if ((2 * textHeight) > _height)
53  _width = MAX(_width, textWidth);
54 
55  NWNWidgetWithCaption::initCaption(font, text, r, g, b, a, halign, valign);
56 
57  float x, y, z;
58  getPosition(x, y, z);
59  // Anchor for right aligned labels is right X position
61  setPosition(x - _width, y, z);
62  // If the label is big enough it does not get recentered
64  setPosition(x - (_width / 2), y, z);
65 }
66 
68  if (!_caption)
69  return;
70 
71  // Not all labels are wide enough for their text
72  float textWidth = 1.0 + FontMan.get(_caption->getFont()).getFont().getLineWidth(text);
73  if (textWidth > _width) {
74  float x, y, z;
75  getPosition(x, y, z);
76  setPosition(x + ((_width - textWidth) * getHorizontalAlign()), y, z);
77  setSize(textWidth, _height);
78  }
79 
81 
82 }
83 
84 float WidgetLabel::getWidth () const {
85  return _width;
86 }
87 
88 float WidgetLabel::getHeight() const {
89  return _height;
90 }
91 
92 void WidgetLabel::setSize(float width, float height) {
93  _width = width;
94  _height = height;
95  if (_caption)
96  _caption->setSize(_width, _height);
97 }
98 
99 } // End of namespace NWN
100 
101 } // End of namespace Engines
A class holding an UTF-8 string.
Definition: ustring.h:48
A text object.
void setText(const Common::UString &text)
virtual void getPosition(float &x, float &y, float &z) const
Get the widget&#39;s position.
Definition: widget.cpp:140
void setPosition(float x, float y, float z)
Set the widget&#39;s position.
The Aurora font manager.
A GUI.
Definition: gui.h:43
virtual void initCaption(const Common::UString &font, const Common::UString &text, float r=1.0f, float g=1.0f, float b=1.0f, float a=1.0f, float halign=Graphics::Aurora::kHAlignLeft, float valign=Graphics::Aurora::kVAlignTop)
Common::ScopedPtr< Graphics::Aurora::Text > _caption
const float kHAlignRight
Definition: types.h:44
const float kHAlignCenter
Definition: types.h:43
float getHeight() const
Get the widget&#39;s height.
Definition: label.cpp:88
uint32 _height
Definition: h263.cpp:54
WidgetLabel(::Engines::GUI &gui, const Common::UString &tag)
Definition: label.cpp:35
A NWN widget with a text caption.
uint32 _width
Definition: h263.cpp:53
float getWidth() const
Get the widget&#39;s width.
Definition: label.cpp:84
T MAX(T a, T b)
Definition: util.h:71
void initCaption(const Common::UString &font, const Common::UString &text, float r=1.0f, float g=1.0f, float b=1.0f, float a=1.0f, float halign=Graphics::Aurora::kHAlignLeft, float valign=Graphics::Aurora::kVAlignTop)
Definition: label.cpp:43
A font.
A NWN label widget.
void setSize(float width, float height)
Definition: label.cpp:92
#define FontMan
Shortcut for accessing the font manager.
Definition: fontman.h:105
void setText(const Common::UString &text)
Definition: label.cpp:67