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 * This program 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
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program.  If not, see <http://www.gnu.org/licenses/>
18 */
19
20#ifndef OSGEARTHUTIL_MOUSE_COORDS_TOOL_H
21#define OSGEARTHUTIL_MOUSE_COORDS_TOOL_H 1
22
23#include <osgEarthUtil/Common>
24#include <osgEarthUtil/Controls>
25#include <osgEarthUtil/Formatter>
26
27#include <osgGA/GUIEventHandler>
28
29namespace osgEarth {
30    class MapNode;
31}
32
33namespace osgEarth { namespace Util
34{
35    using namespace Controls;
36
37    /**
38     * Tool that prints the map coordinates under the mouse into a
39     * LabelControl.
40     */
41    class OSGEARTHUTIL_EXPORT MouseCoordsTool : public osgGA::GUIEventHandler
42    {
43    public:
44        struct Callback : public osg::Referenced
45        {
46            // called when valid map coordinates are found under the mouse
47            virtual void set( const GeoPoint& coords, osg::View* view, MapNode* mapNode ) =0;
48
49            // called when no map coords are found under the mouse
50            virtual void reset( osg::View* view, MapNode* mapNode ) =0;
51
52            virtual ~Callback() { }
53        };
54
55    public:
56        /**
57         * Constructs a new handler
58         * @param label
59         *      Label control that will accept the coordinate readout
60         * @param mapNode
61         *      Map node from which to query coordinates
62         * @param label
63         *     (optional) label control to use for readout (automatically installs
64         *     a MouseCoordsLabelCallback)
65         * @param formatter
66         *     (optional) When used with the label, specifies a formatter to use
67         *     for the readout.
68         */
69        MouseCoordsTool( MapNode* mapNode, LabelControl* label =0L, Formatter* formatter =0L );
70
71        virtual ~MouseCoordsTool() { }
72
73        /**
74         * Adds a readout callback.
75         */
76        void addCallback( Callback* callback );
77
78
79    public: // GUIEventHandler
80
81        bool handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa );
82
83    protected:
84        MapNode*      _mapNode;
85        osg::NodePath _mapNodePath;
86        typedef std::vector< osg::ref_ptr<Callback> > Callbacks;
87        Callbacks _callbacks;
88    };
89
90
91    /**
92     * A sample callback that will display the mouse coords in a LabelControl.
93     */
94    class OSGEARTHUTIL_EXPORT MouseCoordsLabelCallback : public MouseCoordsTool::Callback
95    {
96    public:
97        MouseCoordsLabelCallback( LabelControl* label, Formatter* formatter =0L );
98
99        virtual ~MouseCoordsLabelCallback() { }
100
101        virtual void set( const GeoPoint& coords, osg::View* view, MapNode* mapNode );
102        virtual void reset( osg::View* view, MapNode* mapNode );
103
104    protected:
105        osg::observer_ptr<LabelControl> _label;
106        osg::ref_ptr<Formatter>         _formatter;
107    };
108
109} } // namespace osgEarthUtil
110
111#endif // OSGEARTHUTIL_MOUSE_COORDS_TOOL_H
112