]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/elements/CanvasMap.cxx
Canvas::Text: add line-height property.
[simgear.git] / simgear / canvas / elements / CanvasMap.cxx
1 // A group of 2D Canvas elements which get automatically transformed according
2 // to the map parameters.
3 //
4 // Copyright (C) 2012  Thomas Geymayer <tomgey@gmail.com>
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Library General Public
8 // License as published by the Free Software Foundation; either
9 // version 2 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Library General Public License for more details.
15 //
16 // You should have received a copy of the GNU Library General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
19
20 #include "CanvasMap.hxx"
21 #include "map/geo_node_pair.hxx"
22 #include "map/projection.hxx"
23
24 #include <cmath>
25
26 #include <boost/algorithm/string/predicate.hpp>
27
28 #define LOG_GEO_RET(msg) \
29   {\
30     SG_LOG\
31     (\
32       SG_GENERAL,\
33       SG_WARN,\
34       msg << " (" << child->getStringValue()\
35                   << ", " << child->getPath() << ")"\
36     );\
37     return;\
38   }
39
40 namespace simgear
41 {
42 namespace canvas
43 {
44
45   //----------------------------------------------------------------------------
46   const std::string GEO = "-geo";
47   const std::string Map::TYPE_NAME = "map";
48
49   //----------------------------------------------------------------------------
50   Map::Map( const CanvasWeakPtr& canvas,
51             const SGPropertyNode_ptr& node,
52             const Style& parent_style,
53             Element* parent ):
54     Group(canvas, node, parent_style, parent),
55     // TODO make projection configurable
56     _projection(new SansonFlamsteedProjection),
57     _projection_dirty(true)
58   {
59
60   }
61
62   //----------------------------------------------------------------------------
63   Map::~Map()
64   {
65
66   }
67
68   //----------------------------------------------------------------------------
69   void Map::update(double dt)
70   {
71     for( GeoNodes::iterator it = _geo_nodes.begin();
72          it != _geo_nodes.end();
73          ++it )
74     {
75       GeoNodePair* geo_node = it->second.get();
76       if(    !geo_node->isComplete()
77           || (!geo_node->isDirty() && !_projection_dirty) )
78         continue;
79
80       GeoCoord lat = parseGeoCoord(geo_node->getLat());
81       if( lat.type != GeoCoord::LATITUDE )
82         continue;
83
84       GeoCoord lon = parseGeoCoord(geo_node->getLon());
85       if( lon.type != GeoCoord::LONGITUDE )
86         continue;
87
88       Projection::ScreenPosition pos =
89         _projection->worldToScreen(lat.value, lon.value);
90
91       geo_node->setScreenPos(pos.x, pos.y);
92
93 //      geo_node->print();
94       geo_node->setDirty(false);
95     }
96     _projection_dirty = false;
97
98     Group::update(dt);
99   }
100
101   //----------------------------------------------------------------------------
102   void Map::childAdded(SGPropertyNode* parent, SGPropertyNode* child)
103   {
104     if( !boost::ends_with(child->getNameString(), GEO) )
105       return Element::childAdded(parent, child);
106
107     _geo_nodes[child].reset(new GeoNodePair());
108   }
109
110   //----------------------------------------------------------------------------
111   void Map::childRemoved(SGPropertyNode* parent, SGPropertyNode* child)
112   {
113     if( !boost::ends_with(child->getNameString(), GEO) )
114       return Element::childRemoved(parent, child);
115
116     // TODO remove from other node
117     _geo_nodes.erase(child);
118   }
119
120   //----------------------------------------------------------------------------
121   void Map::valueChanged(SGPropertyNode * child)
122   {
123     const std::string& name = child->getNameString();
124
125     if( !boost::ends_with(name, GEO) )
126       return Group::valueChanged(child);
127
128     GeoNodes::iterator it_geo_node = _geo_nodes.find(child);
129     if( it_geo_node == _geo_nodes.end() )
130       LOG_GEO_RET("geo node not found!")
131     GeoNodePair* geo_node = it_geo_node->second.get();
132
133     geo_node->setDirty();
134
135     if( geo_node->getStatus() & GeoNodePair::INCOMPLETE )
136     {
137       // Detect lat, lon tuples...
138       GeoCoord coord = parseGeoCoord(child->getStringValue());
139       int index_other = -1;
140
141       switch( coord.type )
142       {
143         case GeoCoord::LATITUDE:
144           index_other = child->getIndex() + 1;
145           geo_node->setNodeLat(child);
146           break;
147         case GeoCoord::LONGITUDE:
148           index_other = child->getIndex() - 1;
149           geo_node->setNodeLon(child);
150           break;
151         default:
152           LOG_GEO_RET("Invalid geo coord")
153       }
154
155       SGPropertyNode *other = child->getParent()->getChild(name, index_other);
156       if( !other )
157         return;
158
159       GeoCoord coord_other = parseGeoCoord(other->getStringValue());
160       if(    coord_other.type == GeoCoord::INVALID
161           || coord_other.type == coord.type )
162         return;
163
164       GeoNodes::iterator it_geo_node_other = _geo_nodes.find(other);
165       if( it_geo_node_other == _geo_nodes.end() )
166         LOG_GEO_RET("other geo node not found!")
167       GeoNodePair* geo_node_other = it_geo_node_other->second.get();
168
169       // Let use both nodes use the same GeoNodePair instance
170       if( geo_node_other != geo_node )
171         it_geo_node_other->second = it_geo_node->second;
172
173       if( coord_other.type == GeoCoord::LATITUDE )
174         geo_node->setNodeLat(other);
175       else
176         geo_node->setNodeLon(other);
177
178       // Set name for resulting screen coordinate nodes
179       geo_node->setTargetName( name.substr(0, name.length() - GEO.length()) );
180     }
181   }
182
183   //----------------------------------------------------------------------------
184   void Map::childChanged(SGPropertyNode * child)
185   {
186     if( child->getParent() != _node )
187       return Group::childChanged(child);
188
189     if(    child->getNameString() == "ref-lat"
190         || child->getNameString() == "ref-lon" )
191       _projection->setWorldPosition( _node->getDoubleValue("ref-lat"),
192                                      _node->getDoubleValue("ref-lon") );
193     else if( child->getNameString() == "hdg" )
194       _projection->setOrientation(child->getFloatValue());
195     else if( child->getNameString() == "range" )
196       _projection->setRange(child->getDoubleValue());
197     else
198       return Group::childChanged(child);
199
200     _projection_dirty = true;
201   }
202
203   //----------------------------------------------------------------------------
204   Map::GeoCoord Map::parseGeoCoord(const std::string& val) const
205   {
206     GeoCoord coord;
207     if( val.length() < 2 )
208       return coord;
209
210     if( val[0] == 'N' || val[0] == 'S' )
211       coord.type = GeoCoord::LATITUDE;
212     else if( val[0] == 'E' || val[0] == 'W' )
213       coord.type = GeoCoord::LONGITUDE;
214     else
215       return coord;
216
217     char* end;
218     coord.value = strtod(&val[1], &end);
219
220     if( end != &val[val.length()] )
221     {
222       coord.type = GeoCoord::INVALID;
223       return coord;
224     }
225
226     if( val[0] == 'S' || val[0] == 'W' )
227       coord.value *= -1;
228
229     return coord;
230   }
231
232 } // namespace canvas
233 } // namespace simgear