]> git.mxchange.org Git - flightgear.git/blob - src/Canvas/elements/text.cxx
Fix Win32 build, hopefully.
[flightgear.git] / src / Canvas / elements / text.cxx
1 // A text on the canvas
2 //
3 // Copyright (C) 2012  Thomas Geymayer <tomgey@gmail.com>
4 //
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.
9 //
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.
14 //
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.
18
19 #include "text.hxx"
20 #include <osgText/Text>
21
22 #include <Main/globals.hxx>
23 #include <Main/fg_props.hxx>
24
25 namespace canvas
26 {
27
28   //----------------------------------------------------------------------------
29   Text::Text(SGPropertyNode* node):
30     Element(node, COLOR | COLOR_FILL | BOUNDING_BOX),
31     _text( new osgText::Text )
32   {
33     _drawable = _text;
34     _text->setCharacterSizeMode(osgText::Text::SCREEN_COORDS);
35
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);
40
41     osg::ref_ptr<osg::Geode> geode = new osg::Geode;
42     geode->addDrawable(_text);
43     _transform->addChild(geode);
44
45     _node->tie
46     (
47       "alignment",
48       SGRawValueMethods<Text, const char*>
49       (
50         *this,
51         &Text::getAlignment,
52         &Text::setAlignment
53       )
54     );
55     _node->tie
56     (
57       "padding",
58       SGRawValueMethods<osgText::Text, float>
59       (
60         *_text.get(),
61         &osgText::Text::getBoundingBoxMargin,
62         &osgText::Text::setBoundingBoxMargin
63       )
64     );
65     typedef SGRawValueMethods<osgText::Text, int> TextIntMethods;
66     _node->tie
67     (
68       "draw-mode",
69       //  TEXT              = 1 default
70       //  BOUNDINGBOX       = 2
71       //  FILLEDBOUNDINGBOX = 4
72       //  ALIGNMENT         = 8
73       TextIntMethods
74       (
75         *_text.get(),
76         (TextIntMethods::getter_t)&osgText::Text::getDrawMode,
77         (TextIntMethods::setter_t)&osgText::Text::setDrawMode
78       )
79     );
80   }
81
82   //----------------------------------------------------------------------------
83   Text::~Text()
84   {
85
86   }
87
88   //----------------------------------------------------------------------------
89   void Text::setFont(const char* name)
90   {
91     _text->setFont( getFont(name) );
92   }
93
94   //----------------------------------------------------------------------------
95   void Text::setAlignment(const char* align)
96   {
97     const std::string align_string(align);
98     if( 0 ) return;
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"
103 #undef ENUM_MAPPING
104     else
105       _text->setAlignment(osgText::Text::LEFT_BASE_LINE);
106   }
107
108   //----------------------------------------------------------------------------
109   const char* Text::getAlignment() const
110   {
111     switch( _text->getAlignment() )
112     {
113 #define ENUM_MAPPING(enum_val, string_val) \
114       case osgText::Text::enum_val:\
115         return string_val;
116 #include "text-alignment.hxx"
117 #undef ENUM_MAPPING
118       default:
119         return "unknown";
120     }
121   }
122
123   //----------------------------------------------------------------------------
124   void Text::childChanged(SGPropertyNode* child)
125   {
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() );
132     else
133       return;
134
135     _attributes_dirty |= BOUNDING_BOX;
136   }
137
138   //----------------------------------------------------------------------------
139   void Text::colorChanged(const osg::Vec4& color)
140   {
141     _text->setColor(color);
142   }
143
144   //----------------------------------------------------------------------------
145   void Text::colorFillChanged(const osg::Vec4& color)
146   {
147     _text->setBoundingBoxColor(color);
148   }
149
150   //----------------------------------------------------------------------------
151   Text::font_ptr Text::getFont(const std::string& name)
152   {
153     SGPath path = globals->resolve_ressource_path("Fonts/" + name);
154     if( path.isNull() )
155     {
156       SG_LOG
157       (
158         SG_GL,
159         SG_ALERT,
160         "canvas::Text: No such font: " << name
161       );
162       return font_ptr();
163     }
164
165     SG_LOG
166     (
167       SG_GL,
168       SG_INFO,
169       "canvas::Text: using font file " << path.str()
170     );
171
172     font_ptr font = osgText::readFontFile(path.c_str());
173     if( !font )
174       SG_LOG
175       (
176         SG_GL,
177         SG_ALERT,
178         "canvas::Text: Failed to open font file " << path.c_str()
179       );
180
181     return font;
182   }
183
184 } // namespace canvas