Introduced new scheme for handling mouse events with osgViewer. The new scheme enables robust event handling even when using distortion correction render to texture Cameras.

This commit is contained in:
Robert Osfield
2013-05-03 19:26:27 +00:00
parent 63088ab63e
commit 668d351765
36 changed files with 1116 additions and 698 deletions

View File

@@ -21,7 +21,14 @@ bool MouseHandler::handle(
// If we're scrolling, we need to inform the WindowManager of that.
_wm->setScrollingMotion(gea.getScrollingMotion());
return (this->*ma)(gea.getX(), gea.getY(), gea.getButton());
// osgWidget assumes origin is bottom left of window so make sure mouse coordinate are increaseing y upwards and are scaled to window size.
float x = (gea.getX()-gea.getXmin())/(gea.getXmax()-gea.getXmin())*static_cast<float>(gea.getWindowWidth());
float y = (gea.getY()-gea.getYmin())/(gea.getYmax()-gea.getYmin())*static_cast<float>(gea.getWindowHeight());
if (gea.getMouseYOrientation()==osgGA::GUIEventAdapter::Y_INCREASING_DOWNWARDS) y = static_cast<float>(gea.getWindowHeight())-y;
//OSG_NOTICE<<"MouseHandler(x="<<x<<", y="<<y<<")"<<std::endl;
return (this->*ma)(x, y, gea.getButton());
}
return false;

View File

@@ -511,7 +511,13 @@ Color Widget::getImageColorAtXY(point_type x, point_type y) const {
point_type height = fabs((*texs)[LR].y() - (*texs)[UR].y());
point_type X = ((x / getWidth()) * width) + (*texs)[LL].x();
point_type Y = (((getHeight() - y) / getHeight()) * height) + (*texs)[UR].y();
point_type Y = ((y / getHeight()) * height) + (*texs)[LR].y();
if (X<0.0 || X>1.0 || Y<0.0 || Y>1.0)
{
OSG_INFO<<"Widget::getImageColorAtXY("<<x<<", "<<y<<") Texture coordinate out of range, X="<<X<<", Y="<<Y<<std::endl;
return Color();
}
return image->getColor(TexCoord(X, Y));
}

View File

@@ -208,7 +208,7 @@ void WindowManager::_updatePickWindow(const WidgetList* wl, point_type x, point_
_getPointerXYDiff(xdiff, ydiff);
ss
<< "At XY Coords: " << x << ", " << _height - y
<< "At XY Coords: " << x << ", " << y
<< " ( diff " << xdiff << ", " << ydiff << " )"
<< std::endl
;
@@ -296,10 +296,20 @@ void WindowManager::childRemoved(unsigned int start, unsigned int numChildren) {
// This method performs intersection testing at the given XY coords, and returns true if
// any intersections were found. It will break after processing the first pickable Window
// it finds.
bool WindowManager::pickAtXY(float x, float y, WidgetList& wl) {
bool WindowManager::pickAtXY(float x, float y, WidgetList& wl)
{
Intersections intr;
if(_view->computeIntersections(x, y, intr, _nodeMask)) {
osg::Camera* camera = _view->getCamera();
osgViewer::GraphicsWindow* gw = dynamic_cast<osgViewer::GraphicsWindow*>(camera->getGraphicsContext());
if (gw)
{
_view->computeIntersections(camera, osgUtil::Intersector::WINDOW, x, y, intr, _nodeMask);
}
if (!intr.empty())
{
// Get the first Window at the XY coordinates; if you want a Window to be
// non-pickable, set the NodeMask to something else.
Window* activeWin = 0;
@@ -347,48 +357,6 @@ bool WindowManager::pickAtXY(float x, float y, WidgetList& wl) {
return false;
}
/*
bool WindowManager::pickAtXY(float x, float y, WidgetList& wl) {
Intersections intr;
if(!_view->computeIntersections(x, y, intr, _nodeMask)) return false;
typedef std::vector<osg::observer_ptr<Window> > WindowVector;
WindowVector windows;
Window* activeWin = 0;
for(Intersections::iterator i = intr.begin(); i != intr.end(); i++) {
Window* win = dynamic_cast<Window*>(i->nodePath.back()->getParent(0));
if(
!win ||
(win->getVisibilityMode() == Window::VM_PARTIAL && !win->isPointerXYWithinVisible(x, y))
) {
continue;
}
if(activeWin != win) {
activeWin = win;
windows.push_back(win);
}
}
if(!windows.size()) return false;
std::sort(windows.begin(), windows.end(), WindowBinNumberCompare());
for(WindowVector::iterator i = windows.begin(); i != windows.end(); i++) {
warn() << "- " << i->get()->getName() << " " << i->get()->getOrCreateStateSet()->getBinNumber() << std::endl;
}
warn() << std::endl;
return false;
}
*/
bool WindowManager::setFocused(Window* window) {
Event ev(this);