]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/SGText.cxx
Merge branch 'jmt/waypt' into next
[simgear.git] / simgear / scene / model / SGText.cxx
1 // SGText.cxx - Manage text in the scene graph
2 // Copyright (C) 2009 Torsten Dreyer Torsten (_at_) t3r *dot* de
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License as
6 // published by the Free Software Foundation; either version 2 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but
10 // WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 //
18
19 #ifdef HAVE_CONFIG_H
20 #  include <simgear_config.h>
21 #endif
22
23 #include "SGText.hxx"
24
25 #include <simgear/math/SGMath.hxx>
26 #include <simgear/misc/sg_path.hxx>
27
28 #include <osg/Geode>
29 #include <osg/MatrixTransform>
30 #include <osgText/Text>
31 #include <osgText/Font>
32
33 class SGText::UpdateCallback : public osg::NodeCallback {
34 public:
35   UpdateCallback( osgText::Text * aText, SGConstPropertyNode_ptr aProperty, double aScale, double aOffset, double aTruncate, double aNumeric, const char * aFormat ) :
36     text( aText ),
37     property( aProperty ),
38     scale( aScale ),
39     offset( aOffset ),
40     truncate( aTruncate ),
41     numeric( aNumeric ),
42     format( aFormat )
43   {
44     if( format.size() == 0 ) {
45       if( numeric ) format = "%f";
46       else format = "%s";
47     }
48   }
49
50 private:
51   virtual void operator()(osg::Node * node, osg::NodeVisitor *nv );
52   osgText::Text * text;
53   SGConstPropertyNode_ptr property;
54   double scale;
55   double offset;
56   bool truncate;
57   bool numeric;
58   string format;
59 };
60
61 void SGText::UpdateCallback::operator()(osg::Node * node, osg::NodeVisitor *nv ) 
62 {
63   // FIXME:
64   // hopefully the users never specifies bad formats here
65   // this should better be something more robust
66   char buf[256];
67   if( numeric ) {
68     double d = property->getDoubleValue() * scale + offset;
69     if (truncate)  d = (d < 0) ? -floor(-d) : floor(d);
70     snprintf( buf, sizeof(buf)-1, format.c_str(), d );
71   } else {
72     snprintf( buf, sizeof(buf)-1, format.c_str(), property->getStringValue() );
73   }
74   if( text->getText().createUTF8EncodedString().compare( buf )  ) {
75     // be lazy and set the text only if the property has changed.
76     // update() computes the glyph representation which looks 
77     // more expensive than a the above string compare.
78     text->setText( buf );
79     text->update();
80   }
81   traverse( node, nv );
82 }
83
84 osg::Node * SGText::appendText(const SGPropertyNode* configNode, 
85   SGPropertyNode* modelRoot, const osgDB::ReaderWriter::Options* options)
86 {
87   SGConstPropertyNode_ptr p;
88
89   SG_LOG(SG_GENERAL, SG_DEBUG, "Creating a text object");
90
91   osgText::Text * text = new osgText::Text();
92   osg::Geode * g = new osg::Geode;
93   g->addDrawable( text );
94
95   SGPath path("Fonts" );
96   path.append( configNode->getStringValue( "font", "Helvetica" ));
97   text->setFont( path.str() );
98
99   text->setCharacterSize(configNode->getDoubleValue("character-size", 1.0 ), 
100                          configNode->getDoubleValue("character-aspect-ratio", 1.0 ));
101   
102   if( (p = configNode->getNode( "font-resolution" )) != NULL )
103     text->setFontResolution( p->getIntValue( "width", 32 ), p->getIntValue( "height", 32 ) );
104
105   if( (p = configNode->getNode( "kerning" )) != NULL ) {
106     string kerning = p->getStringValue();
107     if( kerning.compare( "default" ) == 0 ) {
108       text->setKerningType( osgText::KERNING_DEFAULT );
109     } else if( kerning.compare( "unfitted" ) == 0 ) {
110       text->setKerningType( osgText::KERNING_UNFITTED );
111     } else if( kerning.compare( "none" ) == 0 ) {
112       text->setKerningType( osgText::KERNING_NONE );
113     } else {
114       SG_LOG(SG_GENERAL, SG_ALERT, "ignoring unknown kerning'" << kerning << "'." );
115     }
116   }
117
118   if( ( p = configNode->getNode( "axis-alignment" )) != NULL ) {
119     string axisAlignment = p->getStringValue();
120     if( axisAlignment.compare( "xy-plane" ) == 0 ) {
121       text->setAxisAlignment( osgText::Text::XY_PLANE );
122     } else if( axisAlignment.compare( "reversed-xy-plane" ) == 0 ) {
123       text->setAxisAlignment( osgText::Text::REVERSED_XY_PLANE );
124     } else if( axisAlignment.compare( "xz-plane" ) == 0 ) {
125       text->setAxisAlignment( osgText::Text::XZ_PLANE );
126     } else if( axisAlignment.compare( "reversed-xz-plane" ) == 0 ) {
127       text->setAxisAlignment( osgText::Text::REVERSED_XZ_PLANE );
128     } else if( axisAlignment.compare( "yz-plane" ) == 0 ) {
129       text->setAxisAlignment( osgText::Text::YZ_PLANE );
130     } else if( axisAlignment.compare( "reversed-yz-plane" ) == 0 ) {
131       text->setAxisAlignment( osgText::Text::REVERSED_YZ_PLANE );
132     } else if( axisAlignment.compare( "screen" ) == 0 ) {
133       text->setAxisAlignment( osgText::Text::SCREEN );
134     } else {
135       SG_LOG(SG_GENERAL, SG_ALERT, "ignoring unknown axis-alignment'" << axisAlignment << "'." );
136     }
137   }
138
139   unsigned drawMode = osgText::Text::TEXT;
140   if( (p = configNode->getNode( "draw-text" )) != NULL && p->getBoolValue() == false )
141     drawMode &= ~ osgText::Text::TEXT;
142
143   if( (p = configNode->getNode( "draw-alignment" )) != NULL && p->getBoolValue() == true )
144     drawMode |= osgText::Text::ALIGNMENT;
145
146   if( (p = configNode->getNode( "draw-boundingbox" )) != NULL && p->getBoolValue() == true )
147     drawMode |= osgText::Text::BOUNDINGBOX;
148
149   text->setDrawMode( drawMode );
150
151   if( (p = configNode->getNode( "alignment" )) != NULL ) {
152     string alignment = p->getStringValue();
153     if( alignment.compare( "left-top" ) == 0 ) {
154       text->setAlignment( osgText::Text::LEFT_TOP );
155     } else if( alignment.compare( "left-center" ) == 0 ) {
156       text->setAlignment( osgText::Text::LEFT_CENTER );
157     } else if( alignment.compare( "left-bottom" ) == 0 ) {
158       text->setAlignment( osgText::Text::LEFT_BOTTOM );
159     } else if( alignment.compare( "center-top" ) == 0 ) {
160       text->setAlignment( osgText::Text::CENTER_TOP );
161     } else if( alignment.compare( "center-center" ) == 0 ) {
162       text->setAlignment( osgText::Text::CENTER_CENTER );
163     } else if( alignment.compare( "center-bottom" ) == 0 ) {
164       text->setAlignment( osgText::Text::CENTER_BOTTOM );
165     } else if( alignment.compare( "right-top" ) == 0 ) {
166       text->setAlignment( osgText::Text::RIGHT_TOP );
167     } else if( alignment.compare( "right-center" ) == 0 ) {
168       text->setAlignment( osgText::Text::RIGHT_CENTER );
169     } else if( alignment.compare( "right-bottom" ) == 0 ) {
170       text->setAlignment( osgText::Text::RIGHT_BOTTOM );
171     } else if( alignment.compare( "left-baseline" ) == 0 ) {
172       text->setAlignment( osgText::Text::LEFT_BASE_LINE );
173     } else if( alignment.compare( "center-baseline" ) == 0 ) {
174       text->setAlignment( osgText::Text::CENTER_BASE_LINE );
175     } else if( alignment.compare( "right-baseline" ) == 0 ) {
176       text->setAlignment( osgText::Text::RIGHT_BASE_LINE );
177     } else if( alignment.compare( "baseline" ) == 0 ) {
178       text->setAlignment( osgText::Text::BASE_LINE );
179     } else {
180       SG_LOG(SG_GENERAL, SG_ALERT, "ignoring unknown text-alignment '" << alignment <<"'." );
181     }
182   }
183
184   if( (p = configNode->getNode( "layout" )) != NULL ) {
185     string layout = p->getStringValue();
186     if( layout.compare( "left-to-right" ) == 0 ) {
187       text->setLayout( osgText::Text::LEFT_TO_RIGHT );
188     } else if( layout.compare( "right-to-left" ) == 0 ) {
189       text->setLayout( osgText::Text::RIGHT_TO_LEFT );
190     } else if( layout.compare( "vertical" ) == 0 ) {
191       text->setLayout( osgText::Text::VERTICAL );
192     } else {
193       SG_LOG(SG_GENERAL, SG_ALERT, "ignoring unknown layout '" << layout <<"'." );
194     }
195   }
196   
197   if( (p = configNode->getNode( "max-width" )) != NULL )
198     text->setMaximumWidth( p->getDoubleValue() );
199
200   if( (p = configNode->getNode( "max-height" )) != NULL )
201     text->setMaximumHeight( p->getDoubleValue() );
202
203   // FIXME: Should we support <chunks><chunk/><chunk/></chunks> here?
204   string type = configNode->getStringValue( "type", "literal" );
205   if( type == "literal" ) {
206     text->setText( configNode->getStringValue( "text", "" ) );
207   } else {
208     SGConstPropertyNode_ptr property = modelRoot->getNode( configNode->getStringValue( "property", "foo" ), true );
209     const char * format = configNode->getStringValue( "format", "" );
210     double scale = configNode->getDoubleValue( "scale", 1.0 );
211     double offset = configNode->getDoubleValue( "offset", 0.0 );
212     bool   truncate = configNode->getBoolValue( "truncate", false );
213
214     if( (p = configNode->getNode( "property")) != NULL ) {
215       p = modelRoot->getNode( p->getStringValue(), true );
216       UpdateCallback * uc = new UpdateCallback( text, property, scale, offset, truncate, type == "number-value", format );
217       g->setUpdateCallback( uc );
218     }
219   }
220
221   osg::Node * reply = NULL;
222   if( (p = configNode->getNode( "offsets")) == NULL ) {
223     reply = g;
224   } else {
225     // Set up the alignment node ("stolen" from animation.cxx)
226     // XXX Order of rotations is probably not correct.
227     osg::MatrixTransform *align = new osg::MatrixTransform;
228     osg::Matrix res_matrix;
229     res_matrix.makeRotate(
230         p->getFloatValue("pitch-deg", 0.0)*SG_DEGREES_TO_RADIANS,
231         osg::Vec3(0, 1, 0),
232         p->getFloatValue("roll-deg", 0.0)*SG_DEGREES_TO_RADIANS,
233         osg::Vec3(1, 0, 0),
234         p->getFloatValue("heading-deg", 0.0)*SG_DEGREES_TO_RADIANS,
235         osg::Vec3(0, 0, 1));
236
237     osg::Matrix tmat;
238     tmat.makeTranslate(configNode->getFloatValue("offsets/x-m", 0.0),
239                        configNode->getFloatValue("offsets/y-m", 0.0),
240                        configNode->getFloatValue("offsets/z-m", 0.0));
241
242     align->setMatrix(res_matrix * tmat);
243     align->addChild( g );
244     reply = align;
245   }
246
247   if( (p = configNode->getNode( "name" )) != NULL )
248     reply->setName(p->getStringValue());
249   else
250     reply->setName("text");
251   return reply;
252 }