source: geovis/trunk/MouseCoordsTool.h @ 4790

Last change on this file since 4790 was 4349, checked in by ldelgass, 10 years ago

Improvements to coordinate display, scale bar

File size: 3.9 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            _label->setText(osgEarth::Stringify()
45                            << _prefix
46                            <<  _formatter->format(mapCoords));
47                            //<< ", " << mapCoords.z());
48        }
49        _pt = mapCoords;
50        _havePoint = true;
51    }
52
53    void reset(osg::View *view, osgEarth::MapNode *mapNode)
54    {
55        TRACE("Out of range");
56        // Out of range of map extents
57        if (_label.valid()) {
58            _label->setText("");
59        }
60        _havePoint = false;
61    }
62
63    bool report(double *x, double *y, double *z)
64    {
65        if (_havePoint) {
66            *x = _pt.x();
67            *y = _pt.y();
68            *z = _pt.z();
69            _havePoint = false;
70            return true;
71        }
72        return false;
73    }
74
75    osgEarth::Util::Controls::LabelControl *getLabel()
76    { return _label.get(); }
77
78    osgEarth::Util::Formatter *getFormatter()
79    { return _formatter.get(); }
80
81private:
82    osg::observer_ptr<osgEarth::Util::Controls::LabelControl> _label;
83    osg::ref_ptr<osgEarth::Util::Formatter> _formatter;
84    std::string _prefix;
85    bool _havePoint;
86    osgEarth::GeoPoint _pt;
87};
88
89/**
90 * Tool that prints the map coordinates under the mouse into a
91 * LabelControl.
92 */
93class MouseCoordsTool : public osgEarth::Util::MouseCoordsTool
94{
95public:
96    MouseCoordsTool(osgEarth::MapNode* mapNode) :
97        osgEarth::Util::MouseCoordsTool(mapNode)
98    {}
99
100    virtual ~MouseCoordsTool()
101    {}
102
103    virtual bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
104    {
105        if (ea.getEventType() == ea.PUSH ||
106            ea.getEventType() == ea.RELEASE ||
107            ea.getEventType() == ea.MOVE ||
108            ea.getEventType() == ea.DRAG) {
109            // mouse Y from OSG is 1 at bottom of window to +height at top of window
110            // mouse Y from TK  is 0 at top of window to +(height-1) at bottom of window
111            //TRACE("coords: %g,%g", ea.getX(), ea.getY());
112            osg::Vec3d world;
113            if (_mapNode->getTerrain()->getWorldCoordsUnderMouse(aa.asView(), ea.getX(), ea.getY(), world)) {
114                osgEarth::GeoPoint map;
115                map.fromWorld(_mapNode->getMapSRS(), world);
116
117                for (osgEarth::Util::MouseCoordsTool::Callbacks::iterator i = _callbacks.begin();
118                     i != _callbacks.end(); ++i) {
119                    i->get()->set(map, aa.asView(), _mapNode);
120                }
121            } else {
122                for (osgEarth::Util::MouseCoordsTool::Callbacks::iterator i = _callbacks.begin();
123                     i != _callbacks.end(); ++i) {
124                    i->get()->reset(aa.asView(), _mapNode);
125                }
126            }
127        }
128        return false;
129    }
130};
131
132}
133
134#endif
Note: See TracBrowser for help on using the repository browser.