1 /* -*-c++-*- */
2 /* osgEarth - Geospatial SDK for OpenSceneGraph
3 * Copyright 2019 Pelican Mapping
4 * http://osgearth.org
5 *
6 * osgEarth is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
16 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
17 * IN THE SOFTWARE.
18 *
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program.  If not, see <http://www.gnu.org/licenses/>
21 */
22 
23 #include <osg/Notify>
24 #include <osgViewer/Viewer>
25 #include <osgEarth/MapNode>
26 #include <osgEarth/GLUtils>
27 #include <osgEarthUtil/EarthManipulator>
28 #include <osgEarthUtil/GeodeticGraticule>
29 #include <osgEarthUtil/Controls>
30 #include <osgEarthUtil/ExampleResources>
31 #include <osgEarthSymbology/Style>
32 
33 using namespace osgEarth;
34 using namespace osgEarth::Util;
35 using namespace osgEarth::Symbology;
36 namespace ui = osgEarth::Util::Controls;
37 
38 int
usage(char ** argv,const std::string & msg)39 usage( char** argv, const std::string& msg )
40 {
41     OE_NOTICE
42         << msg << std::endl
43         << "USAGE: " << argv[0] << " file.earth" << std::endl;
44     return -1;
45 }
46 
47 const osg::Vec4 colors[4] = { osg::Vec4(1,1,1,1), osg::Vec4(1,0,0,1), osg::Vec4(1,1,0,1), osg::Vec4(0,1,0,1) };
48 
49 struct App
50 {
51     GeodeticGraticule* graticule;
52 
53     int gridColorIndex;
54     int edgeColorIndex;
55 
AppApp56     App(GeodeticGraticule* g)
57     {
58         graticule = g;
59         gridColorIndex = 0;
60         edgeColorIndex = 1;
61     }
62 
cycleStylesApp63     void cycleStyles()
64     {
65         // Could also use the get/setGridLabelStyle API here, but this demonstrates
66         // changing the options and calling dirty() or apply().
67         Style gridLabelStyle = graticule->options().gridLabelStyle().get();
68         gridColorIndex = (gridColorIndex+1)%4;
69         gridLabelStyle.getOrCreate<TextSymbol>()->fill()->color() = colors[gridColorIndex];
70         graticule->options().gridLabelStyle() = gridLabelStyle;
71 
72         Style edgeLabelStyle = graticule->options().edgeLabelStyle().get(); //graticule->getEdgeLabelStyle();
73         edgeColorIndex = (edgeColorIndex+1)%4;
74         edgeLabelStyle.getOrCreate<TextSymbol>()->fill()->color() = colors[edgeColorIndex];
75         graticule->options().edgeLabelStyle() = edgeLabelStyle;
76 
77         graticule->dirty();
78     }
79 
toggleGridLabelsApp80     void toggleGridLabels()
81     {
82         bool vis = graticule->getGridLabelsVisible();
83         graticule->setGridLabelsVisible(!vis);
84     }
85 
toggleEdgeLabelsApp86     void toggleEdgeLabels()
87     {
88         bool vis = graticule->getEdgeLabelsVisible();
89         graticule->setEdgeLabelsVisible(!vis);
90     }
91 
toggleGridApp92     void toggleGrid()
93     {
94         bool vis = graticule->getGridLinesVisible();
95         graticule->setGridLinesVisible(!vis);
96     }
97 
toggleLayerApp98     void toggleLayer()
99     {
100         bool vis = graticule->getVisible();
101         graticule->setVisible(!vis);
102     }
103 };
104 
105 OE_UI_HANDLER(cycleStyles);
106 OE_UI_HANDLER(toggleGridLabels);
107 OE_UI_HANDLER(toggleEdgeLabels);
108 OE_UI_HANDLER(toggleGrid);
109 OE_UI_HANDLER(toggleLayer);
110 
makeUI(App & app)111 ui::Control* makeUI(App& app)
112 {
113     ui::VBox* b = new ui::VBox();
114     b->addChild(new ui::ButtonControl("Change styles", new cycleStyles(app)));
115     b->addChild(new ui::ButtonControl("Toggle grid labels", new toggleGridLabels(app)));
116     b->addChild(new ui::ButtonControl("Toggle edge labels", new toggleEdgeLabels(app)));
117     b->addChild(new ui::ButtonControl("Toggle grid visibility", new toggleGrid(app)));
118     b->addChild(new ui::ButtonControl("Toggle layer visibility", new toggleLayer(app)));
119     return b;
120 }
121 
122 int
main(int argc,char ** argv)123 main(int argc, char** argv)
124 {
125     osg::ArgumentParser arguments(&argc,argv);
126     osgViewer::Viewer viewer(arguments);
127 
128     GLUtils::setGlobalDefaults(viewer.getCamera()->getOrCreateStateSet());
129     viewer.setRealizeOperation(new GL3RealizeOperation());
130     viewer.getCamera()->setSmallFeatureCullingPixelSize(-1.0f);
131 
132     // load the .earth file from the command line.
133     MapNode* mapNode = MapNode::load(arguments);
134     if ( !mapNode )
135         return usage( argv, "Failed to load a map from the .earth file" );
136 
137     viewer.setSceneData(mapNode);
138     viewer.setCameraManipulator( new EarthManipulator() );
139 
140     GeodeticGraticule* graticule = new GeodeticGraticule();
141     mapNode->getMap()->addLayer(graticule);
142 
143     App app(graticule);
144     ui::ControlCanvas* canvas = ui::ControlCanvas::getOrCreate(&viewer);
145     canvas->addControl(makeUI(app));
146 
147     return viewer.run();
148 }
149