Esempio n. 1
0
// 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)
{
    Intersections intr;


    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;

        // Iterate over every picked result and create a list of Widgets that belong
        // to that Window.
        for(Intersections::iterator i = intr.begin(); i != intr.end(); i++) {
            Window* win = dynamic_cast<Window*>(i->nodePath.back()->getParent(0));

            // Make sure that our window is valid, and that our pick is within the
            // "visible area" of the Window.
            if(
                !win ||
                (win->getVisibilityMode() == Window::VM_PARTIAL && !win->isPointerXYWithinVisible(x, y))
            ) {
                continue;
            }

            // Set our activeWin, so that we know when we've got all the Widgets
            // that belong to it.
            if(!activeWin) activeWin = win;

            // If we've found a new Widnow, break out!
            else if(activeWin != win) break;

            Widget* widget = dynamic_cast<Widget*>(i->drawable.get());

            if(!widget) continue;

            // We need to return a list of every Widget that was picked, so
            // that the handler can operate on it accordingly.
            else wl.push_back(widget);
        }

        if(wl.size()) {
            // Potentially VERY expensive; only to be used for debugging. :)
            if(_flags & WM_PICK_DEBUG) _updatePickWindow(&wl, x, y);

            return true;
        }
    }

    if(_flags & WM_PICK_DEBUG) _updatePickWindow(0, x, y);

    return false;
}