source: geovis/trunk/MouseCoordsTool.h @ 6672

Last change on this file since 6672 was 6487, checked in by ldelgass, 8 years ago

Display elevation in coordinate readout if an elevation layer is present in the
map.

File size: 4.2 KB
Line 
1/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 * Copyright (C) 2014  HUBzero Foundation, LLC
4 *
5 * Author: Leif Delgass <ldelgass@purdue.edu>
6 */
7
8#ifndef GEOVIS_MOUSE_COORDS_TOOL_H
9#define GEOVIS_MOUSE_COORDS_TOOL_H
10
11#include <osgEarth/MapNode>
12
13#include <osgEarthUtil/Common>
14#include <osgEarthUtil/Controls>
15#include <osgEarthUtil/Formatter>
16
17#include <osgGA/GUIEventHandler>
18
19#include "Trace.h"
20
21namespace GeoVis {
22
23class MouseCoordsCallback : public osgEarth::Util::MouseCoordsTool::Callback
24{
25public:
26    MouseCoordsCallback(osgEarth::Util::Controls::LabelControl *label,
27                        osgEarth::Util::Formatter *formatter) :
28        osgEarth::Util::MouseCoordsTool::Callback(),
29        _label(label),
30        _formatter(formatter),
31        _havePoint(false)
32    {
33        if (dynamic_cast<osgEarth::Util::MGRSFormatter *>(formatter) != NULL) {
34            _prefix = "MGRS: ";
35        } else {
36            _prefix = "Lat/Long: ";
37        }
38    }
39
40    void set(const osgEarth::GeoPoint& mapCoords, osg::View *view, osgEarth::MapNode *mapNode)
41    {
42        TRACE("%g %g %g", mapCoords.x(), mapCoords.y(), mapCoords.z());
43        if (_label.valid()) {
44            if (mapNode->getMap()->getNumElevationLayers() > 0) {
45                _label->setText(osgEarth::Stringify()
46                                << _prefix
47                                <<  _formatter->format(mapCoords)
48                                << "\nElev: " << mapCoords.z() << " m");
49            } else {
50                _label->setText(osgEarth::Stringify()
51                                << _prefix
52                                <<  _formatter->format(mapCoords));
53            }
54        }
55        _pt = mapCoords;
56        _havePoint = true;
57    }
58
59    void reset(osg::View *view, osgEarth::MapNode *mapNode)
60    {
61        TRACE("Out of range");
62        // Out of range of map extents
63        if (_label.valid()) {
64            _label->setText(osgEarth::Stringify()
65                            << _prefix
66                            << "N/A");
67        }
68        _havePoint = false;
69    }
70
71    bool report(double *x, double *y, double *z)
72    {
73        if (_havePoint) {
74            *x = _pt.x();
75            *y = _pt.y();
76            *z = _pt.z();
77            _havePoint = false;
78            return true;
79        }
80        return false;
81    }
82
83    osgEarth::Util::Controls::LabelControl *getLabel()
84    { return _label.get(); }
85
86    osgEarth::Util::Formatter *getFormatter()
87    { return _formatter.get(); }
88
89private:
90    osg::observer_ptr<osgEarth::Util::Controls::LabelControl> _label;
91    osg::ref_ptr<osgEarth::Util::Formatter> _formatter;
92    std::string _prefix;
93    bool _havePoint;
94    osgEarth::GeoPoint _pt;
95};
96
97/**
98 * Tool that prints the map coordinates under the mouse into a
99 * LabelControl.
100 */
101class MouseCoordsTool : public osgEarth::Util::MouseCoordsTool
102{
103public:
104    MouseCoordsTool(osgEarth::MapNode* mapNode) :
105        osgEarth::Util::MouseCoordsTool(mapNode)
106    {}
107
108    virtual ~MouseCoordsTool()
109    {}
110
111    virtual bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
112    {
113        if (ea.getEventType() == ea.PUSH ||
114            ea.getEventType() == ea.RELEASE ||
115            ea.getEventType() == ea.MOVE ||
116            ea.getEventType() == ea.DRAG) {
117            // mouse Y from OSG is 1 at bottom of window to +height at top of window
118            // mouse Y from TK  is 0 at top of window to +(height-1) at bottom of window
119            //TRACE("coords: %g,%g", ea.getX(), ea.getY());
120            osg::Vec3d world;
121            if (_mapNode->getTerrain()->getWorldCoordsUnderMouse(aa.asView(), ea.getX(), ea.getY(), world)) {
122                osgEarth::GeoPoint map;
123                map.fromWorld(_mapNode->getMapSRS(), world);
124
125                for (osgEarth::Util::MouseCoordsTool::Callbacks::iterator i = _callbacks.begin();
126                     i != _callbacks.end(); ++i) {
127                    i->get()->set(map, aa.asView(), _mapNode);
128                }
129            } else {
130                for (osgEarth::Util::MouseCoordsTool::Callbacks::iterator i = _callbacks.begin();
131                     i != _callbacks.end(); ++i) {
132                    i->get()->reset(aa.asView(), _mapNode);
133                }
134            }
135        }
136        return false;
137    }
138};
139
140}
141
142#endif
Note: See TracBrowser for help on using the repository browser.