source: geovis/branches/rex/MouseCoordsTool.h @ 6570

Last change on this file since 6570 was 6570, checked in by ldelgass, 7 years ago

First pass at porting to new Map layer API

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