]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/SGText.cxx
Add preliminary spot light animation
[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 using std::string;
34
35 class SGText::UpdateCallback : public osg::NodeCallback {
36 public:
37   UpdateCallback( osgText::Text * aText, SGConstPropertyNode_ptr aProperty, double aScale, double aOffset, bool aTruncate, bool aNumeric, const char * aFormat ) :
38     text( aText ),
39     property( aProperty ),
40     scale( aScale ),
41     offset( aOffset ),
42     truncate( aTruncate ),
43     numeric( aNumeric ),
44     format( aFormat )
45   {
46     if( format.size() == 0 ) {
47       if( numeric ) format = "%f";
48       else format = "%s";
49     }
50   }
51
52 private:
53   virtual void operator()(osg::Node * node, osg::NodeVisitor *nv );
54   osgText::Text * text;
55   SGConstPropertyNode_ptr property;
56   double scale;
57   double offset;
58   bool truncate;
59   bool numeric;
60   string format;
61 };
62
63 void SGText::UpdateCallback::operator()(osg::Node * node, osg::NodeVisitor *nv ) 
64 {
65   // FIXME:
66   // hopefully the users never specifies bad formats here
67   // this should better be something more robust
68   char buf[256];
69   if( numeric ) {
70     double d = property->getDoubleValue() * scale + offset;
71     if (truncate)  d = (d < 0) ? -floor(-d) : floor(d);
72     snprintf( buf, sizeof(buf)-1, format.c_str(), d );
73   } else {
74     snprintf( buf, sizeof(buf)-1, format.c_str(), property->getStringValue() );
75   }
76   if( text->getText().createUTF8EncodedString().compare( buf )  ) {
77     // be lazy and set the text only if the property has changed.
78     // update() computes the glyph representation which looks 
79     // more expensive than a the above string compare.
80     text->setText( buf );
81     text->update();
82   }
83   traverse( node, nv );
84 }
85
86 osg::Node * SGText::appendText(const SGPropertyNode* configNode, 
87   SGPropertyNode* modelRoot, const osgDB::Options* options)
88 {
89   SGConstPropertyNode_ptr p;
90
91   SG_LOG(SG_GENERAL, SG_DEBUG, "Creating a text object");
92
93   osgText::Text * text = new osgText::Text();
94   osg::Geode * g = new osg::Geode;
95   g->addDrawable( text );
96
97   SGPath path("Fonts" );
98   path.append( configNode->getStringValue( "font", "Helvetica" ));
99   text->setFont( path.str() );
100
101   text->setCharacterSize(configNode->getDoubleValue("character-size", 1.0 ), 
102                          configNode->getDoubleValue("character-aspect-ratio", 1.0 ));
103   
104   if( (p = configNode->getNode( "font-resolution" )) != NULL )
105     text->setFontResolution( p->getIntValue( "width", 32 ), p->getIntValue( "height", 32 ) );
106
107   if( (p = configNode->getNode( "kerning" )) != NULL ) {
108     string kerning = p->getStringValue();
109     if( kerning.compare( "default" ) == 0 ) {
110       text->setKerningType( osgText::KERNING_DEFAULT );
111     } else if( kerning.compare( "unfitted" ) == 0 ) {
112       text->setKerningType( osgText::KERNING_UNFITTED );
113     } else if( kerning.compare( "none" ) == 0 ) {
114       text->setKerningType( osgText::KERNING_NONE );
115     } else {
116       SG_LOG(SG_GENERAL, SG_ALERT, "ignoring unknown kerning'" << kerning << "'." );
117     }
118   }
119
120   if( ( p = configNode->getNode( "axis-alignment" )) != NULL ) {
121     string axisAlignment = p->getStringValue();
122     if( axisAlignment.compare( "xy-plane" ) == 0 ) {
123       text->setAxisAlignment( osgText::Text::XY_PLANE );
124     } else if( axisAlignment.compare( "reversed-xy-plane" ) == 0 ) {
125       text->setAxisAlignment( osgText::Text::REVERSED_XY_PLANE );
126     } else if( axisAlignment.compare( "xz-plane" ) == 0 ) {
127       text->setAxisAlignment( osgText::Text::XZ_PLANE );
128     } else if( axisAlignment.compare( "reversed-xz-plane" ) == 0 ) {
129       text->setAxisAlignment( osgText::Text::REVERSED_XZ_PLANE );
130     } else if( axisAlignment.compare( "yz-plane" ) == 0 ) {
131       text->setAxisAlignment( osgText::Text::YZ_PLANE );
132     } else if( axisAlignment.compare( "reversed-yz-plane" ) == 0 ) {
133       text->setAxisAlignment( osgText::Text::REVERSED_YZ_PLANE );
134     } else if( axisAlignment.compare( "screen" ) == 0 ) {
135       text->setAxisAlignment( osgText::Text::SCREEN );
136     } else {
137       SG_LOG(SG_GENERAL, SG_ALERT, "ignoring unknown axis-alignment'" << axisAlignment << "'." );
138     }
139   }
140
141   unsigned drawMode = osgText::Text::TEXT;
142   if( (p = configNode->getNode( "draw-text" )) != NULL && p->getBoolValue() == false )
143     drawMode &= ~ osgText::Text::TEXT;
144
145   if( (p = configNode->getNode( "draw-alignment" )) != NULL && p->getBoolValue() == true )
146     drawMode |= osgText::Text::ALIGNMENT;
147
148   if( (p = configNode->getNode( "draw-boundingbox" )) != NULL && p->getBoolValue() == true )
149     drawMode |= osgText::Text::BOUNDINGBOX;
150
151   text->setDrawMode( drawMode );
152
153   if( (p = configNode->getNode( "alignment" )) != NULL ) {
154     string alignment = p->getStringValue();
155     if( alignment.compare( "left-top" ) == 0 ) {
156       text->setAlignment( osgText::Text::LEFT_TOP );
157     } else if( alignment.compare( "left-center" ) == 0 ) {
158       text->setAlignment( osgText::Text::LEFT_CENTER );
159     } else if( alignment.compare( "left-bottom" ) == 0 ) {
160       text->setAlignment( osgText::Text::LEFT_BOTTOM );
161     } else if( alignment.compare( "center-top" ) == 0 ) {
162       text->setAlignment( osgText::Text::CENTER_TOP );
163     } else if( alignment.compare( "center-center" ) == 0 ) {
164       text->setAlignment( osgText::Text::CENTER_CENTER );
165     } else if( alignment.compare( "center-bottom" ) == 0 ) {
166       text->setAlignment( osgText::Text::CENTER_BOTTOM );
167     } else if( alignment.compare( "right-top" ) == 0 ) {
168       text->setAlignment( osgText::Text::RIGHT_TOP );
169     } else if( alignment.compare( "right-center" ) == 0 ) {
170       text->setAlignment( osgText::Text::RIGHT_CENTER );
171     } else if( alignment.compare( "right-bottom" ) == 0 ) {
172       text->setAlignment( osgText::Text::RIGHT_BOTTOM );
173     } else if( alignment.compare( "left-baseline" ) == 0 ) {
174       text->setAlignment( osgText::Text::LEFT_BASE_LINE );
175     } else if( alignment.compare( "center-baseline" ) == 0 ) {
176       text->setAlignment( osgText::Text::CENTER_BASE_LINE );
177     } else if( alignment.compare( "right-baseline" ) == 0 ) {
178       text->setAlignment( osgText::Text::RIGHT_BASE_LINE );
179     } else if( alignment.compare( "baseline" ) == 0 ) {
180       text->setAlignment( osgText::Text::BASE_LINE );
181     } else {
182       SG_LOG(SG_GENERAL, SG_ALERT, "ignoring unknown text-alignment '" << alignment <<"'." );
183     }
184   }
185
186   if( (p = configNode->getNode( "layout" )) != NULL ) {
187     string layout = p->getStringValue();
188     if( layout.compare( "left-to-right" ) == 0 ) {
189       text->setLayout( osgText::Text::LEFT_TO_RIGHT );
190     } else if( layout.compare( "right-to-left" ) == 0 ) {
191       text->setLayout( osgText::Text::RIGHT_TO_LEFT );
192     } else if( layout.compare( "vertical" ) == 0 ) {
193       text->setLayout( osgText::Text::VERTICAL );
194     } else {
195       SG_LOG(SG_GENERAL, SG_ALERT, "ignoring unknown layout '" << layout <<"'." );
196     }
197   }
198   
199   if( (p = configNode->getNode( "max-width" )) != NULL )
200     text->setMaximumWidth( p->getDoubleValue() );
201
202   if( (p = configNode->getNode( "max-height" )) != NULL )
203     text->setMaximumHeight( p->getDoubleValue() );
204
205   // FIXME: Should we support <chunks><chunk/><chunk/></chunks> here?
206   string type = configNode->getStringValue( "type", "literal" );
207   if( type == "literal" ) {
208     text->setText( configNode->getStringValue( "text", "" ) );
209   } else {
210     SGConstPropertyNode_ptr property = modelRoot->getNode( configNode->getStringValue( "property", "foo" ), true );
211     const char * format = configNode->getStringValue( "format", "" );
212     double scale = configNode->getDoubleValue( "scale", 1.0 );
213     double offset = configNode->getDoubleValue( "offset", 0.0 );
214     bool   truncate = configNode->getBoolValue( "truncate", false );
215
216     if( (p = configNode->getNode( "property")) != NULL ) {
217       p = modelRoot->getNode( p->getStringValue(), true );
218       UpdateCallback * uc = new UpdateCallback( text, property, scale, offset, truncate, type == "number-value", format );
219       g->setUpdateCallback( uc );
220     }
221   }
222
223   osg::Node * reply = NULL;
224   if( (p = configNode->getNode( "offsets")) == NULL ) {
225     reply = g;
226   } else {
227     // Set up the alignment node ("stolen" from animation.cxx)
228     // XXX Order of rotations is probably not correct.
229     osg::MatrixTransform *align = new osg::MatrixTransform;
230     osg::Matrix res_matrix;
231     res_matrix.makeRotate(
232         p->getFloatValue("pitch-deg", 0.0)*SG_DEGREES_TO_RADIANS,
233         osg::Vec3(0, 1, 0),
234         p->getFloatValue("roll-deg", 0.0)*SG_DEGREES_TO_RADIANS,
235         osg::Vec3(1, 0, 0),
236         p->getFloatValue("heading-deg", 0.0)*SG_DEGREES_TO_RADIANS,
237         osg::Vec3(0, 0, 1));
238
239     osg::Matrix tmat;
240     tmat.makeTranslate(configNode->getFloatValue("offsets/x-m", 0.0),
241                        configNode->getFloatValue("offsets/y-m", 0.0),
242                        configNode->getFloatValue("offsets/z-m", 0.0));
243
244     align->setMatrix(res_matrix * tmat);
245     align->addChild( g );
246     reply = align;
247   }
248
249   if( (p = configNode->getNode( "name" )) != NULL )
250     reply->setName(p->getStringValue());
251   else
252     reply->setName("text");
253   return reply;
254 }