]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/elements/CanvasText.cxx
d0680c365b35d33fc9336debad1f4e815d3942d5
[simgear.git] / simgear / canvas / elements / CanvasText.cxx
1 // A text on the Canvas
2 //
3 // Copyright (C) 2012  Thomas Geymayer <tomgey@gmail.com>
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Library General Public
7 // License as published by the Free Software Foundation; either
8 // version 2 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // Library General Public License for more details.
14 //
15 // You should have received a copy of the GNU Library General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
18
19 #include "CanvasText.hxx"
20 #include <simgear/canvas/Canvas.hxx>
21 #include <simgear/canvas/CanvasSystemAdapter.hxx>
22 #include <simgear/scene/util/parse_color.hxx>
23 #include <osgText/Text>
24
25 namespace simgear
26 {
27 namespace canvas
28 {
29   class Text::TextOSG:
30     public osgText::Text
31   {
32     public:
33
34       TextOSG(canvas::Text* text);
35
36       void setCharacterAspect(float aspect);
37       void setFill(const std::string& fill);
38       void setBackgroundColor(const std::string& fill);
39
40       osg::Vec2 handleHit(float x, float y);
41
42       virtual osg::BoundingBox computeBound() const;
43
44     protected:
45
46       canvas::Text *_text_element;
47   };
48
49   //----------------------------------------------------------------------------
50   Text::TextOSG::TextOSG(canvas::Text* text):
51     _text_element(text)
52   {
53
54   }
55
56   //----------------------------------------------------------------------------
57   void Text::TextOSG::setCharacterAspect(float aspect)
58   {
59     setCharacterSize(getCharacterHeight(), aspect);
60   }
61
62   //----------------------------------------------------------------------------
63   void Text::TextOSG::setFill(const std::string& fill)
64   {
65 //    if( fill == "none" )
66 //      TODO No text
67 //    else
68     osg::Vec4 color;
69     if( parseColor(fill, color) )
70       setColor( color );
71   }
72
73   //----------------------------------------------------------------------------
74   void Text::TextOSG::setBackgroundColor(const std::string& fill)
75   {
76     osg::Vec4 color;
77     if( parseColor(fill, color) )
78       setBoundingBoxColor( color );
79   }
80
81   //----------------------------------------------------------------------------
82   osg::Vec2 Text::TextOSG::handleHit(float x, float y)
83   {
84     float line_height = _characterHeight + _lineSpacing;
85
86     // TODO check with align other than TOP
87     float first_line_y = -0.5 * _lineSpacing;//_offset.y() - _characterHeight;
88     size_t line = std::max<int>(0, (y - first_line_y) / line_height);
89
90     if( _textureGlyphQuadMap.empty() )
91       return osg::Vec2(-1, -1);
92
93     // TODO check when it can be larger
94     assert( _textureGlyphQuadMap.size() == 1 );
95
96     const GlyphQuads& glyphquad = _textureGlyphQuadMap.begin()->second;
97     const GlyphQuads::Glyphs& glyphs = glyphquad._glyphs;
98     const GlyphQuads::Coords2& coords = glyphquad._coords;
99     const GlyphQuads::LineNumbers& line_numbers = glyphquad._lineNumbers;
100
101     const float HIT_FRACTION = 0.6;
102     const float character_width = getCharacterHeight()
103                                 * getCharacterAspectRatio();
104
105     y = (line + 0.5) * line_height;
106
107     bool line_found = false;
108     for(size_t i = 0; i < line_numbers.size(); ++i)
109     {
110       if( line_numbers[i] != line )
111       {
112         if( !line_found )
113         {
114           if( line_numbers[i] < line )
115             // Wait for the correct line...
116             continue;
117
118           // We have already passed the correct line -> It's empty...
119           return osg::Vec2(0, y);
120         }
121
122         // Next line and not returned -> not before any character
123         // -> return position after last character of line
124         return osg::Vec2(coords[(i - 1) * 4 + 2].x(), y);
125       }
126
127       line_found = true;
128
129       // Get threshold for mouse x position for setting cursor before or after
130       // current character
131       float threshold = coords[i * 4].x()
132                       + HIT_FRACTION * glyphs[i]->getHorizontalAdvance()
133                                      * character_width;
134
135       if( x <= threshold )
136       {
137         if( i == 0 || line_numbers[i - 1] != line )
138           // first character of line
139           x = coords[i * 4].x();
140         else if( coords[(i - 1) * 4].x() == coords[(i - 1) * 4 + 2].x() )
141           // If previous character width is zero set to begin of next character
142           // (Happens eg. with spaces)
143           x = coords[i * 4].x();
144         else
145           // position at center between characters
146           x = 0.5 * (coords[(i - 1) * 4 + 2].x() + coords[i * 4].x());
147
148         return osg::Vec2(x, y);
149       }
150     }
151
152     // Nothing found -> return position after last character
153     return osg::Vec2
154     (
155       coords.back().x(),
156       (_lineCount - 0.5) * line_height
157     );
158   }
159
160   //----------------------------------------------------------------------------
161   osg::BoundingBox Text::TextOSG::computeBound() const
162   {
163     osg::BoundingBox bb = osgText::Text::computeBound();
164     if( !bb.valid() )
165       return bb;
166
167     // TODO bounding box still doesn't seem always right (eg. with center
168     //      horizontal alignment not completely accurate)
169     bb._min.y() += _offset.y();
170     bb._max.y() += _offset.y();
171
172     _text_element->setBoundingBox(bb);
173
174     return bb;
175   }
176
177   //----------------------------------------------------------------------------
178   Text::Text( const CanvasWeakPtr& canvas,
179               const SGPropertyNode_ptr& node,
180               const Style& parent_style ):
181     Element(canvas, node, parent_style),
182     _text( new Text::TextOSG(this) )
183   {
184     setDrawable(_text);
185     _text->setCharacterSizeMode(osgText::Text::OBJECT_COORDS);
186     _text->setAxisAlignment(osgText::Text::USER_DEFINED_ROTATION);
187     _text->setRotation(osg::Quat(osg::PI, osg::X_AXIS));
188
189     addStyle("fill", &TextOSG::setFill, _text);
190     addStyle("background", &TextOSG::setBackgroundColor, _text);
191     addStyle("character-size",
192              static_cast<void (TextOSG::*)(float)>(&TextOSG::setCharacterSize),
193              _text);
194     addStyle("character-aspect-ratio", &TextOSG::setCharacterAspect, _text);
195     addStyle("padding", &TextOSG::setBoundingBoxMargin, _text);
196     //  TEXT              = 1 default
197     //  BOUNDINGBOX       = 2
198     //  FILLEDBOUNDINGBOX = 4
199     //  ALIGNMENT         = 8
200     addStyle<int>("draw-mode", &TextOSG::setDrawMode, _text);
201     addStyle("max-width", &TextOSG::setMaximumWidth, _text);
202     addStyle("font", &Text::setFont, this);
203     addStyle("alignment", &Text::setAlignment, this);
204     addStyle("text", &Text::setText, this);
205
206     setupStyle();
207   }
208
209   //----------------------------------------------------------------------------
210   Text::~Text()
211   {
212
213   }
214
215   //----------------------------------------------------------------------------
216   void Text::setText(const char* text)
217   {
218     _text->setText(text, osgText::String::ENCODING_UTF8);
219   }
220
221   //----------------------------------------------------------------------------
222   void Text::setFont(const char* name)
223   {
224     _text->setFont( _canvas.lock()->getSystemAdapter()->getFont(name) );
225   }
226
227   //----------------------------------------------------------------------------
228   void Text::setAlignment(const char* align)
229   {
230     const std::string align_string(align);
231     if( 0 ) return;
232 #define ENUM_MAPPING(enum_val, string_val) \
233     else if( align_string == string_val )\
234       _text->setAlignment( osgText::Text::enum_val );
235 #include "text-alignment.hxx"
236 #undef ENUM_MAPPING
237     else
238     {
239       if( !align_string.empty() )
240         SG_LOG
241         (
242           SG_GENERAL,
243           SG_WARN,
244           "canvas::Text: unknown alignment '" << align_string << "'"
245         );
246       _text->setAlignment(osgText::Text::LEFT_BASE_LINE);
247     }
248   }
249
250   //----------------------------------------------------------------------------
251 #if 0
252   const char* Text::getAlignment() const
253   {
254     switch( _text->getAlignment() )
255     {
256 #define ENUM_MAPPING(enum_val, string_val) \
257       case osgText::Text::enum_val:\
258         return string_val;
259 #include "text-alignment.hxx"
260 #undef ENUM_MAPPING
261       default:
262         return "unknown";
263     }
264   }
265 #endif
266
267   //----------------------------------------------------------------------------
268   void Text::childChanged(SGPropertyNode* child)
269   {
270     if( child->getParent() != _node )
271       return;
272
273     const std::string& name = child->getNameString();
274     if( name == "hit-y" )
275       handleHit
276       (
277         _node->getFloatValue("hit-x"),
278         _node->getFloatValue("hit-y")
279       );
280   }
281
282   //----------------------------------------------------------------------------
283   void Text::handleHit(float x, float y)
284   {
285     const osg::Vec2& pos = _text->handleHit(x, y);
286     _node->setFloatValue("cursor-x", pos.x());
287     _node->setFloatValue("cursor-y", pos.y());
288   }
289
290 } // namespace canvas
291 } // namespace simgear