root/OpenSceneGraph/trunk/src/osgWidget/Label.cpp @ 9287

Revision 9287, 4.5 kB (checked in by robert, 5 years ago)

From Jeremy Moles, updates to osgWidget

Merged my Robert Osfield from OpenSceneGraph-osgWidget-dev.

Line 
1// -*-c++-*- osgWidget - Code by: Jeremy Moles (cubicool) 2007-2008
2
3#include <osg/Math>
4#include <osgWidget/WindowManager>
5#include <osgWidget/Label>
6
7namespace osgWidget {
8
9Label::Label(const std::string& name, const std::string& label):
10Widget     (name, 0, 0),
11_text      (new osgText::Text()),
12_textIndex (0) {
13    _text->setAlignment(osgText::Text::LEFT_BOTTOM);
14    _text->setDataVariance(osg::Object::DYNAMIC);
15
16    if(label.size()) {
17        _text->setText(label);
18
19        _calculateSize(getTextSize());
20    }
21
22    // TODO: Make a patch for this!
23    // If you're wondering why we don't use this let me explain...
24    //
25    // _text->setAlignment(osgText::Text::CENTER_CENTER);
26    //
27    // When you set the position of an osgText::Text object which has a CENTER_CENTER
28    // alignment, the internal implementation of osgText may give it values that have
29    // a "decimal" portion, which is NO GOOD on orthographic 2D displays where we
30    // want "pixel perfect" ratios. Thus, until I can remedy this internally with
31    // osgText::Text, I will need to calculate the center myself.
32
33    setColor(0.0f, 0.0f, 0.0f, 0.0f);
34}
35
36Label::Label(const Label& label, const osg::CopyOp& co):
37Widget     (label, co),
38_textIndex (label._textIndex) {
39    _text = new osgText::Text(*label._text, co);
40}
41
42void Label::_calculateSize(const XYCoord& size) {
43    // if(size.x() && size.y()) setMinimumSize(size.x(), size.y());
44
45    if(getWidth() < size.x()) setWidth(size.x());
46   
47    if(getHeight() < size.y()) setHeight(size.y());
48}
49
50// TODO: This will almost certainly get out of sync. :(
51void Label::parented(Window* parent) {
52    osg::Geode* geode = parent->getGeode();
53
54    // If we've been cloned, use the index of the old text Drawable if it's already there.
55    // However, we have a problem here: imagine a Label gets cloned AFTER being added to
56    // a Window; it'll have a _textIndex, but that _textIndex won't apply to the
57    // currently cloned object. In this case, we'll need to check to be SURE.
58    osgText::Text* text = dynamic_cast<osgText::Text*>(geode->getDrawable(_textIndex));
59   
60    if(text) parent->getGeode()->setDrawable(_textIndex, _text.get());
61
62    // Otherwise, add it as new.
63    else _textIndex = parent->addDrawableAndGetIndex(_text.get());
64}
65
66void Label::unparented(Window* parent) {
67    if(_textIndex) parent->getGeode()->removeDrawable(_text.get());
68
69    _textIndex = 0;
70}
71
72void Label::positioned() {
73    XYCoord    size = getTextSize();
74    point_type x    = osg::round(((getWidth() - size.x()) / 2.0f) + getX());
75    point_type y    = osg::round(((getHeight() - size.y()) / 2.0f) + getY());
76    point_type z    = _calculateZ(getLayer() + 1);
77
78    // These values are permisable with CENTER_CENTER mode is active.
79    // point_type x  = round(getX() + (getWidth() / 2.0f));
80    // point_type y  = round(getY() + (getHeight() / 2.0f));
81
82    /*
83    warn() << "Label widget size : " << getWidth() << " x " << getHeight() << std::endl;
84    warn() << "Label widget tsize: " << getWidthTotal() << " x " << getHeightTotal() << std::endl;
85    warn() << "Label XY coords   : " << getX() << " x " << getY() << std::endl;
86    warn() << "Label BB in size  : " << size.x() << " x " << size.y() << std::endl;
87    warn() << "Label xy position : " << x << " y " << y << std::endl;
88    warn() << "------------------------------------" << std::endl;
89    */
90
91    const WindowManager* wm = _getWindowManager();
92
93    if(wm && wm->isUsingRenderBins()) {
94        _text->getOrCreateStateSet()->setRenderBinDetails(
95            static_cast<int>(z * OSGWIDGET_RENDERBIN_MOD),
96            "RenderBin"
97        );
98
99        z = 0.0f;
100    }
101
102    _text->setPosition(osg::Vec3(x, y, z));
103}
104
105void Label::setLabel(const std::string& label) {
106    _text->setText(label);
107
108    _calculateSize(getTextSize());
109}
110
111void Label::setFont(const std::string& font) {
112    _text->setFont(font);
113   
114    _calculateSize(getTextSize());
115}
116
117void Label::setFontSize(unsigned int size) {
118    _text->setCharacterSize(size);
119    _text->setFontResolution(size, size);
120   
121    _calculateSize(getTextSize());
122}
123
124void Label::setFontColor(const Color& c) {
125    _text->setColor(c);
126}
127
128void Label::setShadow(point_type offset) {
129    _text->setBackdropType(osgText::Text::DROP_SHADOW_BOTTOM_RIGHT);
130    _text->setBackdropImplementation(osgText::Text::NO_DEPTH_BUFFER);
131    _text->setBackdropOffset(offset);
132
133    _calculateSize(getTextSize());
134}
135
136XYCoord Label::getTextSize() const {
137    osg::BoundingBox bb = _text->getBound();
138
139    return XYCoord(
140        osg::round(bb.xMax() - bb.xMin()),
141        osg::round(bb.yMax() - bb.yMin())
142    );
143}
144
145}
Note: See TracBrowser for help on using the browser.