DYTSrouce/src/scene/ui/CompositeHandle.cpp

73 lines
1.9 KiB
C++
Raw Normal View History

2025-01-12 17:35:51 +00:00
#include "CompositeHandle.h"
2025-03-25 16:04:39 +00:00
#include <osgEarthUtil/EarthManipulator>
2025-01-12 17:35:51 +00:00
#include "CompositeWidgetManager.h"
#include <iostream>
2025-03-25 16:04:39 +00:00
#define M_PI 3.14159265358979323846 /* mathematical constant pi */
///Radian to degree conversion factor
static const double RAD2DEG = 180.0 / M_PI;
///Degree to radian conversion factor
static const double DEG2RAD = M_PI / 180.0;
inline double angFix360(double in) {
in -= 45.0;
if ((in < 0.0) || (in >= 360.0)) {
in = fmod(in, 360.0);
if (in < 0.0)
in += 360.0;
}
return in;
}
inline bool areEqual(double a, double b, double t = 1.0e-6) {
return fabs(a - b) < t;
}
2025-03-13 00:42:41 +00:00
CompositeHandle::CompositeHandle(CompositeWidgetManager* cw)
: _cw(cw),
_operate(cw->GetcanvasO()),
_N(cw->GetcanvasN()),
_light(cw->GetcanvasFX()),
_background(cw->GetcanvasBackGround()),
_num(0),
_move(false)
2025-01-12 17:35:51 +00:00
{
}
bool CompositeHandle::handle(const osgGA::GUIEventAdapter&ea, osgGA::GUIActionAdapter& aa)
{
2025-03-13 00:42:41 +00:00
if (ea.getEventType() == osgGA::GUIEventAdapter::FRAME) {
2025-03-25 16:04:39 +00:00
constexpr double TWO_DECIMAL_PLACES = 1e-02;
double heading = 0.0;
osgViewer::View* activeView = static_cast<osgViewer::View*>(aa.asView());
const osgEarth::Util::EarthManipulator* manip = dynamic_cast<const osgEarth::Util::EarthManipulator*>(activeView->getCameraManipulator());
if (manip != NULL)
{
manip->getCompositeEulerAngles(&heading);
// Convert to degrees
heading = angFix360(heading * RAD2DEG);
}
else
{
// Fall back to the viewpoint's heading
heading = angFix360(manip->getViewpoint().heading()->as(osgEarth::Units::DEGREES));
}
// make sure that anything equivalent to 0.00 is displayed as 0.00
if (areEqual(heading, 0.0, TWO_DECIMAL_PLACES) || areEqual(heading, 360.0, TWO_DECIMAL_PLACES)) {
heading = 0.0;
}
if (!areEqual(_N->getRotate(), -heading, TWO_DECIMAL_PLACES))
{
_N->setRotate(-heading);
_N->update();
}
2025-01-12 17:35:51 +00:00
}
2025-03-13 00:42:41 +00:00
// compass_->update_();
2025-01-12 17:35:51 +00:00
return false;
}