| 1 | |
|---|
| 2 | |
|---|
| 3 | #include <osg/Math> |
|---|
| 4 | #include <osgWidget/WindowManager> |
|---|
| 5 | #include <osgWidget/Label> |
|---|
| 6 | |
|---|
| 7 | namespace osgWidget { |
|---|
| 8 | |
|---|
| 9 | Label::Label(const std::string& name, const std::string& label): |
|---|
| 10 | Widget (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 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | setColor(0.0f, 0.0f, 0.0f, 0.0f); |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | Label::Label(const Label& label, const osg::CopyOp& co): |
|---|
| 37 | Widget (label, co), |
|---|
| 38 | _textIndex (label._textIndex) { |
|---|
| 39 | _text = new osgText::Text(*label._text, co); |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | void Label::_calculateSize(const XYCoord& size) { |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | if(getWidth() < size.x()) setWidth(size.x()); |
|---|
| 46 | |
|---|
| 47 | if(getHeight() < size.y()) setHeight(size.y()); |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | void Label::parented(Window* parent) { |
|---|
| 52 | osg::Geode* geode = parent->getGeode(); |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | osgText::Text* text = dynamic_cast<osgText::Text*>(geode->getDrawable(_textIndex)); |
|---|
| 59 | |
|---|
| 60 | if(text) parent->getGeode()->setDrawable(_textIndex, _text.get()); |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | else _textIndex = parent->addDrawableAndGetIndex(_text.get()); |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | void Label::unparented(Window* parent) { |
|---|
| 67 | if(_textIndex) parent->getGeode()->removeDrawable(_text.get()); |
|---|
| 68 | |
|---|
| 69 | _textIndex = 0; |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | void 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 | |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 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 | |
|---|
| 105 | void Label::setLabel(const std::string& label) { |
|---|
| 106 | _text->setText(label); |
|---|
| 107 | |
|---|
| 108 | _calculateSize(getTextSize()); |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | void Label::setLabel(const osgText::String& label) |
|---|
| 112 | { |
|---|
| 113 | _text->setText(label); |
|---|
| 114 | |
|---|
| 115 | _calculateSize(getTextSize()); |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | void Label::setFont(const std::string& font) { |
|---|
| 119 | _text->setFont(font); |
|---|
| 120 | |
|---|
| 121 | _calculateSize(getTextSize()); |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | void Label::setFontSize(unsigned int size) { |
|---|
| 125 | _text->setCharacterSize(size); |
|---|
| 126 | _text->setFontResolution(size, size); |
|---|
| 127 | |
|---|
| 128 | _calculateSize(getTextSize()); |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | void Label::setFontColor(const Color& c) { |
|---|
| 132 | _text->setColor(c); |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | void Label::setShadow(point_type offset) { |
|---|
| 136 | _text->setBackdropType(osgText::Text::DROP_SHADOW_BOTTOM_RIGHT); |
|---|
| 137 | _text->setBackdropImplementation(osgText::Text::NO_DEPTH_BUFFER); |
|---|
| 138 | _text->setBackdropOffset(offset); |
|---|
| 139 | |
|---|
| 140 | _calculateSize(getTextSize()); |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | XYCoord Label::getTextSize() const { |
|---|
| 144 | osg::BoundingBox bb = _text->getBound(); |
|---|
| 145 | |
|---|
| 146 | return XYCoord( |
|---|
| 147 | osg::round(bb.xMax() - bb.xMin()), |
|---|
| 148 | osg::round(bb.yMax() - bb.yMin()) |
|---|
| 149 | ); |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | } |
|---|