]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/elements/map/geo_node_pair.hxx
Refactor Canvas and add some helpers.
[simgear.git] / simgear / canvas / elements / map / geo_node_pair.hxx
1 // Groups together two nodes representing a geographic position (lat + lon)
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 #ifndef CANVAS_GEO_NODE_PAIR_HXX_
20 #define CANVAS_GEO_NODE_PAIR_HXX_
21
22 namespace simgear
23 {
24 namespace canvas
25 {
26   class GeoNodePair
27   {
28     public:
29       enum StatusFlags
30       {
31         LAT_MISSING = 1,
32         LON_MISSING = LAT_MISSING << 1,
33         INCOMPLETE = LAT_MISSING | LON_MISSING,
34         DIRTY = LON_MISSING << 1
35       };
36
37       GeoNodePair():
38         _status(INCOMPLETE),
39         _node_lat(0),
40         _node_lon(0)
41       {}
42
43       uint8_t getStatus() const
44       {
45         return _status;
46       }
47
48       void setDirty(bool flag = true)
49       {
50         if( flag )
51           _status |= DIRTY;
52         else
53           _status &= ~DIRTY;
54       }
55
56       bool isDirty() const
57       {
58         return (_status & DIRTY)!=0;
59       }
60
61       bool isComplete() const
62       {
63         return !(_status & INCOMPLETE);
64       }
65
66       void setNodeLat(SGPropertyNode* node)
67       {
68         _node_lat = node;
69         _status &= ~LAT_MISSING;
70
71         if( node == _node_lon )
72         {
73           _node_lon = 0;
74           _status |= LON_MISSING;
75         }
76       }
77
78       void setNodeLon(SGPropertyNode* node)
79       {
80         _node_lon = node;
81         _status &= ~LON_MISSING;
82
83         if( node == _node_lat )
84         {
85           _node_lat = 0;
86           _status |= LAT_MISSING;
87         }
88       }
89
90       const char* getLat() const
91       {
92         return _node_lat ? _node_lat->getStringValue() : "";
93       }
94
95       const char* getLon() const
96       {
97         return _node_lon ? _node_lon->getStringValue() : "";
98       }
99
100       void setTargetName(const std::string& name)
101       {
102         _target_name = name;
103       }
104
105       void setScreenPos(float x, float y)
106       {
107         assert( isComplete() );
108         SGPropertyNode *parent = _node_lat->getParent();
109         parent->getChild(_target_name, _node_lat->getIndex(), true)
110               ->setDoubleValue(x);
111         parent->getChild(_target_name, _node_lon->getIndex(), true)
112               ->setDoubleValue(y);
113       }
114
115       void print()
116       {
117         std::cout << "lat=" << (_node_lat ? _node_lat->getPath() : "")
118                   << ", lon=" << (_node_lon ? _node_lon->getPath() : "")
119                   << std::endl;
120       }
121
122     private:
123
124       uint8_t _status;
125       SGPropertyNode *_node_lat,
126                      *_node_lon;
127       std::string   _target_name;
128
129   };
130
131 } // namespace canvas
132 } // namespace simgear
133
134 #endif /* CANVAS_GEO_NODE_PAIR_HXX_ */