]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/elements/CanvasMap.cxx
Canvas: Prepare for DOM Level 2 like event model.
[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   const std::string GEO = "-geo";
46
47   //----------------------------------------------------------------------------
48   Map::Map( const CanvasWeakPtr& canvas,
49             const SGPropertyNode_ptr& node,
50             const Style& parent_style,
51             Element* parent ):
52     Group(canvas, node, parent_style, parent),
53     // TODO make projection configurable
54     _projection(new SansonFlamsteedProjection),
55     _projection_dirty(true)
56   {
57
58   }
59
60   //----------------------------------------------------------------------------
61   Map::~Map()
62   {
63
64   }
65
66   //----------------------------------------------------------------------------
67   void Map::update(double dt)
68   {
69     for( GeoNodes::iterator it = _geo_nodes.begin();
70          it != _geo_nodes.end();
71          ++it )
72     {
73       GeoNodePair* geo_node = it->second.get();
74       if(    !geo_node->isComplete()
75           || (!geo_node->isDirty() && !_projection_dirty) )
76         continue;
77
78       GeoCoord lat = parseGeoCoord(geo_node->getLat());
79       if( lat.type != GeoCoord::LATITUDE )
80         continue;
81
82       GeoCoord lon = parseGeoCoord(geo_node->getLon());
83       if( lon.type != GeoCoord::LONGITUDE )
84         continue;
85
86       Projection::ScreenPosition pos =
87         _projection->worldToScreen(lat.value, lon.value);
88
89       geo_node->setScreenPos(pos.x, pos.y);
90
91 //      geo_node->print();
92       geo_node->setDirty(false);
93     }
94     _projection_dirty = false;
95
96     Group::update(dt);
97   }
98
99   //----------------------------------------------------------------------------
100   void Map::childAdded(SGPropertyNode* parent, SGPropertyNode* child)
101   {
102     if( !boost::ends_with(child->getNameString(), GEO) )
103       return Element::childAdded(parent, child);
104
105     _geo_nodes[child].reset(new GeoNodePair());
106   }
107
108   //----------------------------------------------------------------------------
109   void Map::childRemoved(SGPropertyNode* parent, SGPropertyNode* child)
110   {
111     if( !boost::ends_with(child->getNameString(), GEO) )
112       return Element::childRemoved(parent, child);
113
114     // TODO remove from other node
115     _geo_nodes.erase(child);
116   }
117
118   //----------------------------------------------------------------------------
119   void Map::valueChanged(SGPropertyNode * child)
120   {
121     const std::string& name = child->getNameString();
122
123     if( !boost::ends_with(name, GEO) )
124       return Group::valueChanged(child);
125
126     GeoNodes::iterator it_geo_node = _geo_nodes.find(child);
127     if( it_geo_node == _geo_nodes.end() )
128       LOG_GEO_RET("geo node not found!")
129     GeoNodePair* geo_node = it_geo_node->second.get();
130
131     geo_node->setDirty();
132
133     if( geo_node->getStatus() & GeoNodePair::INCOMPLETE )
134     {
135       // Detect lat, lon tuples...
136       GeoCoord coord = parseGeoCoord(child->getStringValue());
137       int index_other = -1;
138
139       switch( coord.type )
140       {
141         case GeoCoord::LATITUDE:
142           index_other = child->getIndex() + 1;
143           geo_node->setNodeLat(child);
144           break;
145         case GeoCoord::LONGITUDE:
146           index_other = child->getIndex() - 1;
147           geo_node->setNodeLon(child);
148           break;
149         default:
150           LOG_GEO_RET("Invalid geo coord")
151       }
152
153       SGPropertyNode *other = child->getParent()->getChild(name, index_other);
154       if( !other )
155         return;
156
157       GeoCoord coord_other = parseGeoCoord(other->getStringValue());
158       if(    coord_other.type == GeoCoord::INVALID
159           || coord_other.type == coord.type )
160         return;
161
162       GeoNodes::iterator it_geo_node_other = _geo_nodes.find(other);
163       if( it_geo_node_other == _geo_nodes.end() )
164         LOG_GEO_RET("other geo node not found!")
165       GeoNodePair* geo_node_other = it_geo_node_other->second.get();
166
167       // Let use both nodes use the same GeoNodePair instance
168       if( geo_node_other != geo_node )
169         it_geo_node_other->second = it_geo_node->second;
170
171       if( coord_other.type == GeoCoord::LATITUDE )
172         geo_node->setNodeLat(other);
173       else
174         geo_node->setNodeLon(other);
175
176       // Set name for resulting screen coordinate nodes
177       geo_node->setTargetName( name.substr(0, name.length() - GEO.length()) );
178     }
179   }
180
181   //----------------------------------------------------------------------------
182   void Map::childChanged(SGPropertyNode * child)
183   {
184     if( child->getParent() != _node )
185       return Group::childChanged(child);
186
187     if(    child->getNameString() == "ref-lat"
188         || child->getNameString() == "ref-lon" )
189       _projection->setWorldPosition( _node->getDoubleValue("ref-lat"),
190                                      _node->getDoubleValue("ref-lon") );
191     else if( child->getNameString() == "hdg" )
192       _projection->setOrientation(child->getFloatValue());
193     else if( child->getNameString() == "range" )
194       _projection->setRange(child->getDoubleValue());
195     else
196       return Group::childChanged(child);
197
198     _projection_dirty = true;
199   }
200
201   //----------------------------------------------------------------------------
202   Map::GeoCoord Map::parseGeoCoord(const std::string& val) const
203   {
204     GeoCoord coord;
205     if( val.length() < 2 )
206       return coord;
207
208     if( val[0] == 'N' || val[0] == 'S' )
209       coord.type = GeoCoord::LATITUDE;
210     else if( val[0] == 'E' || val[0] == 'W' )
211       coord.type = GeoCoord::LONGITUDE;
212     else
213       return coord;
214
215     char* end;
216     coord.value = strtod(&val[1], &end);
217
218     if( end != &val[val.length()] )
219     {
220       coord.type = GeoCoord::INVALID;
221       return coord;
222     }
223
224     if( val[0] == 'S' || val[0] == 'W' )
225       coord.value *= -1;
226
227     return coord;
228   }
229
230 } // namespace canvas
231 } // namespace simgear