1 // A text on the canvas
3 // Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com>
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include <osgText/Text>
22 #include <Main/globals.hxx>
23 #include <Main/fg_props.hxx>
28 //----------------------------------------------------------------------------
29 Text::Text(SGPropertyNode* node):
30 Element(node, COLOR | COLOR_FILL | BOUNDING_BOX),
31 _text( new osgText::Text )
34 _text->setCharacterSizeMode(osgText::Text::SCREEN_COORDS);
36 // font size and property node
37 float character_size = _node->getFloatValue("size", 32);
38 _text->setCharacterSize( character_size );
39 _node->setFloatValue("size", character_size);
41 osg::ref_ptr<osg::Geode> geode = new osg::Geode;
42 geode->addDrawable(_text);
43 _transform->addChild(geode);
48 SGRawValueMethods<Text, const char*>
58 SGRawValueMethods<osgText::Text, float>
61 &osgText::Text::getBoundingBoxMargin,
62 &osgText::Text::setBoundingBoxMargin
65 typedef SGRawValueMethods<osgText::Text, int> TextIntMethods;
71 // FILLEDBOUNDINGBOX = 4
76 (TextIntMethods::getter_t)&osgText::Text::getDrawMode,
77 (TextIntMethods::setter_t)&osgText::Text::setDrawMode
82 //----------------------------------------------------------------------------
88 //----------------------------------------------------------------------------
89 void Text::setFont(const char* name)
91 _text->setFont( getFont(name) );
94 //----------------------------------------------------------------------------
95 void Text::setAlignment(const char* align)
97 const std::string align_string(align);
99 #define ENUM_MAPPING(enum_val, string_val) \
100 else if( align_string == string_val )\
101 _text->setAlignment( osgText::Text::enum_val );
102 #include "text-alignment.hxx"
105 _text->setAlignment(osgText::Text::LEFT_BASE_LINE);
108 //----------------------------------------------------------------------------
109 const char* Text::getAlignment() const
111 switch( _text->getAlignment() )
113 #define ENUM_MAPPING(enum_val, string_val) \
114 case osgText::Text::enum_val:\
116 #include "text-alignment.hxx"
123 //----------------------------------------------------------------------------
124 void Text::childChanged(SGPropertyNode* child)
126 if( child->getNameString() == "text" )
127 _text->setText(child->getStringValue());
128 else if( child->getNameString() == "size" )
129 _text->setCharacterSize( child->getFloatValue() );
130 else if( child->getNameString() == "font" )
131 setFont( child->getStringValue() );
135 _attributes_dirty |= BOUNDING_BOX;
138 //----------------------------------------------------------------------------
139 void Text::colorChanged(const osg::Vec4& color)
141 _text->setColor(color);
144 //----------------------------------------------------------------------------
145 void Text::colorFillChanged(const osg::Vec4& color)
147 _text->setBoundingBoxColor(color);
150 //----------------------------------------------------------------------------
151 Text::font_ptr Text::getFont(const std::string& name)
153 SGPath path = globals->resolve_ressource_path("Fonts/" + name);
160 "canvas::Text: No such font: " << name
169 "canvas::Text: using font file " << path.str()
172 font_ptr font = osgText::readFontFile(path.c_str());
178 "canvas::Text: Failed to open font file " << path.c_str()
184 } // namespace canvas