Esempio n. 1
0
bool handle(const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &aa)
{
    if (ea.getHandled())
        return false;

    switch (ea.getEventType())
    {
    case (osgGA::GUIEventAdapter::KEYUP):
    {
        if (ea.getKey() == osgGA::GUIEventAdapter::KEY_Escape)
        {
            osgViewer::View *view = dynamic_cast<osgViewer::View*>(&aa);
            if (view)
                view->getViewerBase()->setDone(true);

            return true;
        }
    }

    default:
        return false;
    }

    return false;
}
Esempio n. 2
0
bool LODScaleHandler::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
{
    osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa);
    osg::Camera* camera = view ? view->getCamera() : 0;
    if (!camera) return false;

    if (ea.getHandled()) return false;

    switch(ea.getEventType())
    {
        case(osgGA::GUIEventAdapter::KEYUP):
        {
            if (ea.getKey() == _keyEventIncreaseLODScale)
            {
                camera->setLODScale(camera->getLODScale()*1.1);
                osg::notify(osg::NOTICE)<<"LODScale = "<<camera->getLODScale()<<std::endl;
                return true;
            }

            else if (ea.getKey() == _keyEventDecreaseLODScale)
            {
                camera->setLODScale(camera->getLODScale()/1.1);
                osg::notify(osg::NOTICE)<<"LODScale = "<<camera->getLODScale()<<std::endl;
                return true;
            }        

            break;
        }
    default:
        break;
    }

    return false;
}
bool KeyboardMouseManipulator::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)	{
	bool bRes = false;

	int nResEvent = ea.getEventType();

	if (nResEvent == GUIEventAdapter::FRAME)	{
		bRes = OrbitManipulator::handleFrame(ea, aa);
	}

	if (nResEvent == GUIEventAdapter::RELEASE)	{
		bRes = OrbitManipulator::handleMouseRelease(ea, aa);
	}

	if (nResEvent == GUIEventAdapter::PUSH)	{
		bRes = OrbitManipulator::handleMousePush(ea, aa);
	}

	if (nResEvent == GUIEventAdapter::KEYDOWN)	{
		bRes = VR::KeyboardMouseManipulator::keyDown(ea, aa);
	}

	if (nResEvent == GUIEventAdapter::KEYUP)	{
		bRes = VR::KeyboardMouseManipulator::keyUp(ea, aa);
	}

	if(ea.getHandled())
		return false;

	if (nResEvent == GUIEventAdapter::DRAG)	{
		bRes = OrbitManipulator::handleMouseDrag(ea, aa);		
	}

	return(bRes);
}
Esempio n. 4
0
        virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& /*aa*/, osg::Object* object, osg::NodeVisitor* /*nv*/)
        {
            osg::Switch* sw = dynamic_cast<osg::Switch*>(object);
            if (!sw) return false;

            if (ea.getHandled()) return false;

            switch(ea.getEventType())
            {
                case(osgGA::GUIEventAdapter::KEYDOWN):
                {
                    if (ea.getKey()=='n')
                    {
                        ++_childNum;
                        if (_childNum >= sw->getNumChildren()) _childNum = 0;

                        sw->setSingleChildOn(_childNum);
                        return true;
                    }
                    break;
                }
                default:
                    break;
            }
            return false;
        }
Esempio n. 5
0
	bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
	{
		if (ea.getHandled()) return false;

		switch (ea.getEventType())
		{
		case(osgGA::GUIEventAdapter::KEYUP) :
		{
			if (ea.getKey() == 'o')
			{
				osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa);
				osg::Node* node = view ? view->getSceneData() : 0;
				if (node)
				{
					osgDB::writeNodeFile(*node, "hud.osgt");
					osgDB::writeNodeFile(*node, "hud.osgb");
				}
				return true;
			}

			if (ea.getKey() == _key)
			{
				osg::notify(osg::NOTICE) << "event handler" << std::endl;
				_snapImage->_snapImage = true;
				return true;
			}

			break;
		}
		default:
			break;
		}

		return false;
	}
Esempio n. 6
0
bool StatsHandler::handle(const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &aa)
{
    if (ea.getHandled()) return false;

    switch(ea.getEventType())
    {
        case(osgGA::GUIEventAdapter::KEYDOWN):
        {
            if (ea.getKey()== _key)
            {
                osgViewer::View* myview = dynamic_cast<osgViewer::View*>(&aa);
                if (!myview) return false;

                osgViewer::ViewerBase* viewer = myview->getViewerBase();

                toggle(viewer);

                aa.requestRedraw();
                return true;
            }
            break;
        }
        case osgGA::GUIEventAdapter::RESIZE:
        {
            setWindowSize(ea.getWindowWidth(), ea.getWindowHeight());
            break;
        }
        default:
            break;
    }
    return false;
}
Esempio n. 7
0
bool UFOManipulator::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter &aa)
{
    switch(ea.getEventType())
    {
        case(osgGA::GUIEventAdapter::FRAME):
            _frame(ea,aa);
            return false;
        default:
            break;
    }

    if (ea.getHandled()) return false;

    switch(ea.getEventType())
    {
        case(osgGA::GUIEventAdapter::KEYUP):
            _keyUp( ea, aa );
            return false;
            break;

        case(osgGA::GUIEventAdapter::KEYDOWN):
            _keyDown(ea, aa);
            return false;
            break;

        case(osgGA::GUIEventAdapter::FRAME):
            _frame(ea,aa);
            return false;
            break;

        default:
            return false;
    }
}
Esempio n. 8
0
    bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
    {
        osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>(&aa);
        if (!viewer) return false;

        if (ea.getHandled()) return false;

        if(ea.getEventType()==GUIEventAdapter::KEYDOWN)
        {

            switch( ea.getKey() )
            {
                case osgGA::GUIEventAdapter::KEY_Right:
                    // Toggle next switch
                    nextSwitch();
                    return true;
                    break;
                case osgGA::GUIEventAdapter::KEY_Up:
                    // Increase animation speed
                    multiplyAnimation(2);
                    return true;
                    break;
                case osgGA::GUIEventAdapter::KEY_Down:
                    // Decrease animation speed
                    multiplyAnimation(0.5);
                    return true;
                    break;
            }
        }
        return false;
    }
Esempio n. 9
0
    bool handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa )
    { 
        if ( ea.getHandled() ) return false;
        if ( ea.getEventType() == osgGA::GUIEventAdapter::PUSH &&
             ea.getButtonMask() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON &&
             (ea.getModKeyMask() & osgGA::GUIEventAdapter::MODKEY_CTRL) != 0 &&
             terrain_srs.valid() &&
             layer.valid() )
        {
            osgViewer::View* view = dynamic_cast<osgViewer::View*>( &aa );
            if ( !view ) return false;
            osgUtil::LineSegmentIntersector::Intersections hits;
            if ( view->computeIntersections( ea.getX(), ea.getY(), hits ) )
            {
                osgUtil::LineSegmentIntersector::Intersection first = *hits.begin();
                osg::Vec3d hit = first.getWorldIntersectPoint() - first.getWorldIntersectNormal()*0.2;
                osgGIS::GeoPoint world( hit, terrain_srs.get() );
                osgGIS::GeoPoint result = terrain_srs->getGeographicSRS()->transform( world );

                osgGIS::FeatureCursor cursor = layer->getCursor( result );
                highlight( cursor );

                std::stringstream buf;

                buf << "World: " << world.toString() << std::endl
                    << "Geo: " << result.toString() << std::endl
                    << "SRS: " << terrain_srs->getName() << std::endl;
                int line_count = 2;

                for( cursor.reset(); cursor.hasNext(); )
                {
                    osgGIS::Feature* f = cursor.next();
                    osgGIS::AttributeList attrs = f->getAttributes();
                    for( osgGIS::AttributeList::const_iterator i = attrs.begin(); i != attrs.end(); i++ )
                    {
                        std::string key = i->getKey();
                        if ( key.length() > 0 )
                        {
                            buf << key << " : " << i->asString() << std::endl;
                            line_count++;
                        }
                    }
                    break;
                }

                if ( buf.str().length() == 0 )
                {
                    buf << "Control-Left-Click to query";
                    line_count++;
                }
                hud_text->setText( buf.str() );
                hud_text->setPosition( osg::Vec3( 10, line_count*TEXT_SIZE*1.1f, 0 ) );
                hud_text->dirtyDisplayList();
            }
        }

        return false; // never "handled"
    }
Esempio n. 10
0
bool Dragger::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
{
    if (ea.getHandled()) return false;

    osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa);
    if (!view) return false;

    if (ea.getEventType() == osgGA::GUIEventAdapter::PUSH)
    {
        Picker picker( view, this );
        Picker::Hits hits;

        if ( picker.pick( ea.getX(), ea.getY(), hits ) )
        {
            _dragging = true;
            aa.requestRedraw();
            return true;
        }
    }
    else if (ea.getEventType() == osgGA::GUIEventAdapter::RELEASE)
    {
        _dragging = false;
        aa.requestRedraw();
    }
    else if (ea.getEventType() == osgGA::GUIEventAdapter::DRAG)
    {
        if (_dragging)
        {
            osg::Vec3d world;
            if ( _mapNode->getTerrain()->getWorldCoordsUnderMouse(view, ea.getX(), ea.getY(), world) )
            {
                GeoPoint mapPoint;
                _mapNode->getMap()->worldPointToMapPoint(world, mapPoint);
                setPosition( mapPoint );
                aa.requestRedraw();
                return true;
            }
        }
    }   
    else if (ea.getEventType() == osgGA::GUIEventAdapter::MOVE)
    {
        Picker picker( view, this );
        Picker::Hits hits;

        if ( picker.pick( ea.getX(), ea.getY(), hits ) )
        {
            setHover( true );
        }
        else
        {
            setHover( false );
        }        
        aa.requestRedraw();
    }
    return false;
}
Esempio n. 11
0
bool CameraManipulator::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us)
{
    switch(ea.getEventType())
    {
        case(osgGA::GUIEventAdapter::FRAME):
        {
			return handleFrame(ea, us);
        }
        default:
            break;
    }

    if (ea.getHandled()) return false;

    switch(ea.getEventType())
    {
        case(osgGA::GUIEventAdapter::PUSH):
        {
			return handlePush(ea, us);
        }
        case(osgGA::GUIEventAdapter::RELEASE):
        {
            return handleRelease(ea, us);
        }
        case(osgGA::GUIEventAdapter::DRAG):
        case(osgGA::GUIEventAdapter::SCROLL):
        {
			return handleScroll(ea, us);
        }
        case(osgGA::GUIEventAdapter::MOVE):
        {
            return false;
        }
		case(osgGA::GUIEventAdapter::KEYDOWN):
		{
			return handleKeyDown(ea, us);
		}
		case(osgGA::GUIEventAdapter::KEYUP):
		{
			return handleKeyUp( ea, us );
		}
        case(osgGA::GUIEventAdapter::FRAME):
		{
            if (_thrown)
            {
                if (calcMovement()) us.requestRedraw();
            }

            return false;
		}
        default:
            return false;
    }
}
Esempio n. 12
0
    virtual bool handle(const osgGA::GUIEventAdapter& ea,
                        osgGA::GUIActionAdapter& /*aa*/,
                        Object*, NodeVisitor* /*nv*/)
    {
        if (ea.getHandled()) return false;

        ref_ptr<Depth> depth;
        if (!_depth.lock(depth)) return false;

        switch(ea.getEventType())
        {
        case(osgGA::GUIEventAdapter::KEYUP):
        {
            if (ea.getKey() == 'i')
            {
                _appState->invertRange = !_appState->invertRange;
                if (!_appState->invertRange)
                {
                    _appState->camera->setClearDepth(1.0f);
                    depth->setFunction(Depth::LESS);
                    depth->setRange(0.0f, 1.0f);
                    _appState->textInverted->setNodeMask(0u);
                }
                else
                {
                    _appState->camera->setClearDepth(0.0f);
                    depth->setFunction(Depth::GEQUAL);
                    depth->setRange(1.0f, 0.0f);
                    _appState->textInverted->setNodeMask(~0u);
                }
                return true;
            }
            else if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Up ||
                     ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Up)
            {
                _appState->zNear *= 2.0;
                _appState->updateNear();
                return true;
            }
            else if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Down ||
                     ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Down)
            {
                _appState->zNear *= .5;
                _appState->updateNear();
                return true;
            }
            break;
        }
        default:
            break;
        }
        return false;
    }
Esempio n. 13
0
bool InteractiveImageHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa, osg::Object*, osg::NodeVisitor* nv)
{
    if (ea.getHandled()) return false;

    if (!_image) return false;

    switch(ea.getEventType())
    {
        case(osgGA::GUIEventAdapter::MOVE):
        case(osgGA::GUIEventAdapter::DRAG):
        case(osgGA::GUIEventAdapter::PUSH):
        case(osgGA::GUIEventAdapter::RELEASE):
        {
            osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa);
            int x,y;
            if (mousePosition(view, nv, ea, x, y))
            {
                return _image->sendPointerEvent(x, y, ea.getButtonMask());
            }
            break;
        }
        case(osgGA::GUIEventAdapter::KEYDOWN):
        case(osgGA::GUIEventAdapter::KEYUP):
        {
            osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa);
            int x,y;
            bool sendKeyEvent = mousePosition(view, nv, ea, x, y);

            if (sendKeyEvent)
            {
                return _image->sendKeyEvent(ea.getKey(), ea.getEventType()==osgGA::GUIEventAdapter::KEYDOWN);
            }
            break;
        }
        case (osgGA::GUIEventAdapter::RESIZE):
        {
            if (_fullscreen && _camera.valid())
            {
                _camera->setViewport(0, 0, ea.getWindowWidth(), ea.getWindowHeight());

                resize(ea.getWindowWidth(), ea.getWindowHeight());
                return true;
            }
            break;
        }

        default:
            return false;
    }
    return false;
}
Esempio n. 14
0
bool ToggleSyncToVBlankHandler::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
{
    osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa);
    if (!view) return false;

    osgViewer::ViewerBase* viewer = view->getViewerBase();

    if (viewer == NULL)
    {
        return false;
    }

    if (ea.getHandled()) return false;

    switch(ea.getEventType())
    {
        case(osgGA::GUIEventAdapter::KEYUP):
        {
            if (ea.getKey() == _keyEventToggleSyncToVBlank)
            {
                // Increase resolution
                osgViewer::Viewer::Windows    windows;

                viewer->getWindows(windows);
                for(osgViewer::Viewer::Windows::iterator itr = windows.begin();
                    itr != windows.end();
                    ++itr)
                {
                    (*itr)->setSyncToVBlank( !(*itr)->getSyncToVBlank() );
                }

                aa.requestRedraw();
                return true;
            }

            break;
        }
    default:
        break;
    }

    return false;
}
Esempio n. 15
0
bool PickEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa, osg::Object*, osg::NodeVisitor* nv)
{
    if (ea.getHandled()) return false;

    switch(ea.getEventType())
    {
        case(osgGA::GUIEventAdapter::MOVE):
        case(osgGA::GUIEventAdapter::PUSH):
        case(osgGA::GUIEventAdapter::RELEASE):
        {
            osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>(&aa);
            osgUtil::LineSegmentIntersector::Intersections intersections;
            if (viewer->computeIntersections(ea.getX(),ea.getY(), nv->getNodePath(), intersections))
            {
                for(osgUtil::LineSegmentIntersector::Intersections::iterator hitr=intersections.begin();
                    hitr!=intersections.end();
                    ++hitr)
                {
                    if (ea.getEventType()==osgGA::GUIEventAdapter::MOVE)
                    {
                        OSG_INFO<<"Tooltip..."<<std::endl;
                    }
                    else if (ea.getEventType()==osgGA::GUIEventAdapter::RELEASE)
                    {
                        doOperation();
                        return true;
                    }
                }
            }
            break;
        }
        case(osgGA::GUIEventAdapter::KEYDOWN):
        {
            //OSG_NOTICE<<"PickEventHandler KEYDOWN "<<(char)ea.getKey()<<std::endl;
            //if (object) OSG_NOTICE<<"    "<<object->className()<<std::endl;
            break;
        }
        default:
            break;
    }
    return false;
}
Esempio n. 16
0
bool KeyEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& /*aa*/, osg::Object*, osg::NodeVisitor* /*nv*/)
{
    if (ea.getHandled()) return false;

    switch(ea.getEventType())
    {
        case(osgGA::GUIEventAdapter::KEYDOWN):
        {
            if (ea.getKey()==_key)
            {
                doOperation();
                return true;
            }
            break;
        }
        default:
            break;
    }
    return false;
}
Esempio n. 17
0
    bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter&)
    {
        if (ea.getHandled()) return false;

        switch(ea.getEventType())
        {
            case(osgGA::GUIEventAdapter::KEYUP):
            {
                if (ea.getKey() == _key)
                {
                    osg::notify(osg::NOTICE)<<"event handler"<<std::endl;
                    _snapImage->_snapImage = true;
                    return true;
                }

                break;
            }
        default:
            break;
        }

        return false;
    }
Esempio n. 18
0
 virtual bool handle(const osgGA::GUIEventAdapter& ea,
                     osgGA::GUIActionAdapter& aa,
                     Object*, NodeVisitor* /*nv*/)
 {
     if (ea.getHandled()) return false;
     osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>(&aa);
     if (!viewer) return false;
     switch(ea.getEventType())
     {
     case osgGA::GUIEventAdapter::KEYUP:
     {
         if (ea.getKey()=='d')
         {
             _appState->displayScene = !_appState->displayScene;
             _appState->updateDisplayedTexture();
             return true;
         }
         else if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Right ||
                  ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Right)
         {
             _appState->advanceConfig(1);
             return true;
         }
         else if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Left ||
                  ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Left)
         {
             _appState->advanceConfig(-1);
             return true;
         }
         break;
     }
     default:
         break;
     }
     return false;
 }
Esempio n. 19
0
bool WindowSizeHandler::handle(const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &aa)
{
    osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa);
    if (!view) return false;
    
    osgViewer::ViewerBase* viewer = view->getViewerBase();

    if (viewer == NULL)
    {
        return false;
    }

    if (ea.getHandled()) return false;

    switch(ea.getEventType())
    {
        case(osgGA::GUIEventAdapter::KEYUP):
        {
            if (_toggleFullscreen == true && ea.getKey() == _keyEventToggleFullscreen)
            {
                osgViewer::Viewer::Windows    windows;

                viewer->getWindows(windows);
                for(osgViewer::Viewer::Windows::iterator itr = windows.begin();
                    itr != windows.end();
                    ++itr)
                {
                    toggleFullscreen(*itr);
                }
                return true;
            }
            else if (_changeWindowedResolution == true && ea.getKey() == _keyEventWindowedResolutionUp)
            {
                // Increase resolution
                osgViewer::Viewer::Windows    windows;

                viewer->getWindows(windows);
                for(osgViewer::Viewer::Windows::iterator itr = windows.begin();
                    itr != windows.end();
                    ++itr)
                {
                    changeWindowedResolution(*itr, true);
                }
                return true;
            }
            else if (_changeWindowedResolution == true && ea.getKey() == _keyEventWindowedResolutionDown)
            {
                // Decrease resolution
                osgViewer::Viewer::Windows    windows;

                viewer->getWindows(windows);
                for(osgViewer::Viewer::Windows::iterator itr = windows.begin();
                    itr != windows.end();
                    ++itr)
                {
                    changeWindowedResolution(*itr, false);
                }
                return true;
            }
            break;
        }
    default:
        break;
    }
    return false;
}
Esempio n. 20
0
bool PickEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa, osg::Object*, osg::NodeVisitor* nv)
{
    if (ea.getHandled()) return false;

    switch(ea.getEventType())
    {
        case(osgGA::GUIEventAdapter::MOVE):
        case(osgGA::GUIEventAdapter::PUSH):
        case(osgGA::GUIEventAdapter::DRAG):
        case(osgGA::GUIEventAdapter::RELEASE):
        {
            if(ea.getEventType() == osgGA::GUIEventAdapter::PUSH)
            {
                _drawablesOnPush.clear();
            }
            osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>(&aa);
            osgUtil::LineSegmentIntersector::Intersections intersections;
            if (viewer->computeIntersections(ea, nv->getNodePath(), intersections))
            {
                for(osgUtil::LineSegmentIntersector::Intersections::iterator hitr=intersections.begin();
                    hitr!=intersections.end();
                    ++hitr)
                {
                    if (_operation == FORWARD_MOUSE_EVENT)
                    {
                        osg::ref_ptr<osgGA::GUIEventAdapter> cloned_ea = osg::clone(&ea);

                        // clear touch-data as this prevents sending the event as mouse-event
                        cloned_ea->setTouchData(NULL);

                        // reproject mouse-coord
                        const osg::BoundingBox bb(hitr->drawable->getBound());
                        const osg::Vec3& p(hitr->localIntersectionPoint);

                        float transformed_x = (p.x() - bb.xMin()) / (bb.xMax() - bb.xMin());
                        float transformed_y = (p.z() - bb.zMin()) / (bb.zMax() - bb.zMin());

                        cloned_ea->setX(ea.getXmin() + transformed_x * (ea.getXmax() - ea.getXmin()));
                        cloned_ea->setY(ea.getYmin() + transformed_y * (ea.getYmax() - ea.getYmin()));
                        cloned_ea->setMouseYOrientation(osgGA::GUIEventAdapter::Y_INCREASING_UPWARDS);



                        // std::cout << transformed_x << "/" << transformed_x << " -> " << cloned_ea->getX() << "/" <<cloned_ea->getY() << std::endl;

                        SlideEventHandler::instance()->forwardEventToDevices(cloned_ea.get());
                    }
                    else if ((_operation == FORWARD_TOUCH_EVENT) && ea.isMultiTouchEvent())
                    {
                        osg::ref_ptr<osgGA::GUIEventAdapter> cloned_ea = osg::clone(&ea);
                        cloned_ea->setMouseYOrientation(osgGA::GUIEventAdapter::Y_INCREASING_DOWNWARDS);

                        osgGA::GUIEventAdapter::TouchData* touch_data = cloned_ea->getTouchData();



                        // reproject touch-points
                        const osg::BoundingBox bb(hitr->drawable->getBound());

                        osg::Camera* camera = viewer->getCamera();
                        osg::Matrix matrix = osg::computeLocalToWorld(hitr->nodePath, false) * camera->getViewMatrix() * camera->getProjectionMatrix();
                        matrix.postMult(camera->getViewport()->computeWindowMatrix());

                        osg::Matrixd inverse;
                        inverse.invert(matrix);

                        // transform touch-points into local coord-system
                        unsigned int j(0);
                        for(osgGA::GUIEventAdapter::TouchData::iterator i = touch_data->begin(); i != touch_data->end(); ++i, ++j)
                        {
                            osg::Vec3 local = osg::Vec3(i->x, i->y, 0) * inverse;

                            // std::cout << local << " hit: " << hitr->localIntersectionPoint << std::endl;

                            local.x() = (local.x() - bb.xMin()) / (bb.xMax() - bb.xMin());
                            local.z() = (local.z() - bb.zMin()) / (bb.zMax() - bb.zMin());

                            local.x() = (ea.getXmin() + local.x() * (ea.getXmax() - ea.getXmin()));
                            local.z() = (ea.getYmin() + local.z() * (ea.getYmax() - ea.getYmin()));

                            // std::cout << ea.getX() << "/" << ea.getY() << " -- " << i->x << " " << i->y << " -> " << local.x() <<"/" << local.z() << std::endl;

                            i->x = local.x();
                            i->y = 1 + local.z(); // no idea why I have to add 1 to get y in the range [0..1]
                        }


                        // std::cout << transformed_x << "/" << transformed_x << " -> " << cloned_ea->getX() << "/" <<cloned_ea->getY() << std::endl;


                        SlideEventHandler::instance()->forwardEventToDevices(cloned_ea.get());
                    }
                    else
                    {
                        if (ea.getEventType()==osgGA::GUIEventAdapter::PUSH)
                        {
                            _drawablesOnPush.insert( hitr->drawable.get() );
                        }
                        else if (ea.getEventType()==osgGA::GUIEventAdapter::MOVE)
                        {
                            OSG_INFO<<"Tooltip..."<<std::endl;
                        }
                        else if (ea.getEventType()==osgGA::GUIEventAdapter::RELEASE)
                        {
                            if (_drawablesOnPush.find(hitr->drawable.get()) != _drawablesOnPush.end())
                                doOperation();
                            return true;
                        }
                    }
                }
            }
            break;
        }
        case(osgGA::GUIEventAdapter::KEYDOWN):
        {
            //OSG_NOTICE<<"PickEventHandler KEYDOWN "<<(char)ea.getKey()<<std::endl;
            //if (object) OSG_NOTICE<<"    "<<object->className()<<std::endl;
            break;
        }
        default:
            break;
    }
    return false;
}
/** Handles events. Returns true if handled, false otherwise.*/
bool OrbitCameraManipulator::handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa )
{
	switch( ea.getEventType() )
	{

	case osgGA::GUIEventAdapter::FRAME:
		return handleFrame( ea, aa );

	case osgGA::GUIEventAdapter::RESIZE:
		return handleResize( ea, aa );

	default:
		break;
	}

	if( ea.getHandled() )
	{
		return false;
	}

	computeRayPointer( ea, aa );

	bool handled = false;
	switch( ea.getEventType() )
	{
	case osgGA::GUIEventAdapter::MOVE:
		handled = handleMouseMove( ea, aa );
		break;

	case osgGA::GUIEventAdapter::DRAG:
		handled = handleMouseDrag( ea, aa );
		break;

	case osgGA::GUIEventAdapter::PUSH:
		handled = handleMousePush( ea, aa );
		break;

	case osgGA::GUIEventAdapter::RELEASE:
		handled = handleMouseRelease( ea, aa );
		break;

	case osgGA::GUIEventAdapter::KEYDOWN:
		handled = handleKeyDown( ea, aa );
		break;

	case osgGA::GUIEventAdapter::KEYUP:
		m_control_key_down = false;
		handled = handleKeyUp( ea, aa );
		break;

	case osgGA::GUIEventAdapter::SCROLL:
		if( _flags & PROCESS_MOUSE_WHEEL )
			handled = handleMouseWheel( ea, aa );
		else
			handled = false;
		break;

	default:
		handled = false;
	}

	return handled;
}
Esempio n. 22
0
bool Dragger::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
{
    if (ea.getHandled()) return false;

    osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa);
    if (!view) return false;
    if (!_mapNode.valid()) return false;

    if (ea.getEventType() == osgGA::GUIEventAdapter::PUSH)
    {
        Picker picker( view, this );
        Picker::Hits hits;

        if ( picker.pick( ea.getX(), ea.getY(), hits ) )
        {
            _dragging = true;

            //Check for and handle vertical dragging if necessary
            bool pressedAlt = _modKeyMask && (ea.getModKeyMask() & _modKeyMask) > 0;
            _elevationDragging = (_defaultMode == Dragger::DRAGMODE_VERTICAL && !pressedAlt) || (_defaultMode == Dragger::DRAGMODE_HORIZONTAL && pressedAlt);

            if (_elevationDragging)
            {
              _pointer.reset();

              // set movement range
              // TODO: values 0.0 and 300000.0 are rather experimental
              GeoPoint posStart(_position.getSRS(), _position.x(), _position.y(), 0.0, ALTMODE_ABSOLUTE);
              osg::Vec3d posStartXYZ;
              posStart.toWorld(posStartXYZ);

              GeoPoint posEnd(_position.getSRS(), _position.x(), _position.y(), 300000.0, ALTMODE_ABSOLUTE);
              osg::Vec3d posEndXYZ;
              posEnd.toWorld(posEndXYZ);

              _projector->setLine(posStartXYZ, posEndXYZ);

              // set camera
              osgUtil::LineSegmentIntersector::Intersections intersections;
              osg::Node::NodeMask intersectionMask = 0xffffffff;
              osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa);
              if (view->computeIntersections(ea.getX(),ea.getY(),intersections, intersectionMask))
              {
                  for (osgUtil::LineSegmentIntersector::Intersections::iterator hitr = intersections.begin(); hitr != intersections.end(); ++hitr)
                  {
                      _pointer.addIntersection(hitr->nodePath, hitr->getLocalIntersectPoint());
                  }

                  bool draggerFound = false;
                  for (osgManipulator::PointerInfo::IntersectionList::iterator piit = _pointer._hitList.begin(); piit != _pointer._hitList.end(); ++piit)
                  {
                      for (osg::NodePath::iterator itr = piit->first.begin(); itr != piit->first.end(); ++itr)
                      {
                          Dragger* dragger = dynamic_cast<Dragger*>(*itr);
                          if (dragger==this)
                          {
                            draggerFound = true;
                              osg::Camera *rootCamera = view->getCamera();
                              osg::NodePath nodePath = _pointer._hitList.front().first;
                              osg::NodePath::reverse_iterator ritr;
                              for (ritr = nodePath.rbegin(); ritr != nodePath.rend(); ++ritr)
                              {
                                  osg::Camera* camera = dynamic_cast<osg::Camera*>(*ritr);
                                  if (camera && (camera->getReferenceFrame()!=osg::Transform::RELATIVE_RF || camera->getParents().empty()))
                                  {
                                       rootCamera = camera;
                                       break;
                                  }
                              }
                              _pointer.setCamera(rootCamera);
                              _pointer.setMousePosition(ea.getX(), ea.getY());

                              break;
                          }
                      }

                      if (draggerFound)
                        break;
                  }
              }
            }

            aa.requestRedraw();
            return true;
        }
    }
    else if (ea.getEventType() == osgGA::GUIEventAdapter::RELEASE)
    {
        _elevationDragging = false;

        if ( _dragging )
        {
            _dragging = false;
            firePositionChanged();
        }

        aa.requestRedraw();
    }
    else if (ea.getEventType() == osgGA::GUIEventAdapter::DRAG)
    {
        if (_elevationDragging) 
        {
            _pointer._hitIter = _pointer._hitList.begin();
            _pointer.setMousePosition(ea.getX(), ea.getY());

            if (_projector->project(_pointer, _startProjectedPoint)) 
            {
                //Get the absolute mapPoint that they've drug it to.
                GeoPoint projectedPos;
                projectedPos.fromWorld(_position.getSRS(), _startProjectedPoint);

                // make sure point is not dragged down below
                // TODO: think of a better solution / HeightAboveTerrain performance issues?
                if (projectedPos.z() >= _verticalMinimum)
                {
                    //If the current position is relative, we need to convert the absolute world point to relative.
                    //If the point is absolute then just emit the absolute point.
                    if (_position.altitudeMode() == ALTMODE_RELATIVE)
                    {
                        projectedPos.transformZ(ALTMODE_RELATIVE, getMapNode()->getTerrain());
                    }

                    setPosition( projectedPos );
                    aa.requestRedraw();
                }
            }

            return true;
        }
        
        if (_dragging)
        {
            osg::Vec3d world;
            if ( getMapNode() && getMapNode()->getTerrain()->getWorldCoordsUnderMouse(view, ea.getX(), ea.getY(), world) )
            {
                //Get the absolute mapPoint that they've drug it to.
                GeoPoint mapPoint;
                mapPoint.fromWorld( getMapNode()->getMapSRS(), world );
                //_mapNode->getMap()->worldPointToMapPoint(world, mapPoint);

                //If the current position is relative, we need to convert the absolute world point to relative.
                //If the point is absolute then just emit the absolute point.
                if (_position.altitudeMode() == ALTMODE_RELATIVE)
                {
                    mapPoint.alt() = _position.alt();
                    mapPoint.altitudeMode() = ALTMODE_RELATIVE;
                }

                setPosition( mapPoint );
                aa.requestRedraw();
                return true;
            }
        }
    }   
    else if (ea.getEventType() == osgGA::GUIEventAdapter::MOVE)
    {
        Picker picker( view, this );
        Picker::Hits hits;

        if ( picker.pick( ea.getX(), ea.getY(), hits ) )
        {
            setHover( true );
        }
        else
        {
            setHover( false );
        }        
        aa.requestRedraw();
    }
    return false;
}
Esempio n. 23
0
bool FPSManipulator::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& us)
{
   /* switch(ea.getEventType())
    {
        case(GUIEventAdapter::FRAME):
            if (_thrown)
            {
                if (calcMovement()) us.requestRedraw();
            }
            return false;
        default:
            break;
    }*/

    if (ea.getHandled()) return false;

    switch(ea.getEventType())
    {
		//if a key is pressed flush the previous states
       /* case(GUIEventAdapter::PUSH):
        {
            flushMouseEventStack();
            addMouseEvent(ea);
           
            return true;
        }

        case(GUIEventAdapter::RELEASE):
        {
            if (ea.getButtonMask()==0)
            {
            
                double timeSinceLastRecordEvent = _ga_t0.valid() ? (ea.getTime() - _ga_t0->getTime()) : DBL_MAX;
                if (timeSinceLastRecordEvent>0.02) flushMouseEventStack();

                calcMovement();
                 

            }
            else
            {
                flushMouseEventStack();
                addMouseEvent(ea);

            }
            return true;
        }*/

     /*   case(GUIEventAdapter::DRAG):
        {
            addMouseEvent(ea);
            if (calcMovement()) us.requestRedraw();
            us.requestContinuousUpdate(false);
            _thrown = false;
            return true;
        }*/

       /* case(GUIEventAdapter::MOVE):
        {
            return false;
        }*/

        case(osgGA::GUIEventAdapter::KEYDOWN):
            if (ea.getKey()== osgGA::GUIEventAdapter::KEY_Space) //home on space bar
            {
                flushMouseEventStack();

                home(0.);

                return true;
            }
            return false;

        case(osgGA::GUIEventAdapter::FRAME):
			{
                addMouseEvent(ea);

				calcMovement();
			}
				return false;
        default:
            return false;
    }
}
Esempio n. 24
0
bool PickEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa, osg::Object*, osg::NodeVisitor* nv)
{
    if (ea.getHandled()) return false;

    switch(ea.getEventType())
    {
        case(osgGA::GUIEventAdapter::MOVE):
        case(osgGA::GUIEventAdapter::PUSH):
        case(osgGA::GUIEventAdapter::DRAG):
        case(osgGA::GUIEventAdapter::RELEASE):
        {
            if(ea.getEventType() == osgGA::GUIEventAdapter::PUSH)
            {
                _drawablesOnPush.clear();
            }
            osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>(&aa);
            osgUtil::LineSegmentIntersector::Intersections intersections;
            if (viewer->computeIntersections(ea, nv->getNodePath(), intersections))
            {
                for(osgUtil::LineSegmentIntersector::Intersections::iterator hitr=intersections.begin();
                    hitr!=intersections.end();
                    ++hitr)
                {
                    if (_operation == FORWARD_EVENT)
                    {
                        osg::ref_ptr<osgGA::GUIEventAdapter> cloned_ea = osg::clone(&ea);
                        const osg::BoundingBox bb(hitr->drawable->getBound());
                        const osg::Vec3& p(hitr->localIntersectionPoint);
                        
                        float transformed_x = (p.x() - bb.xMin()) / (bb.xMax() - bb.xMin());
                        float transformed_y = (p.z() - bb.zMin()) / (bb.zMax() - bb.zMin());
                        
                        cloned_ea->setX(ea.getXmin() + transformed_x * (ea.getXmax() - ea.getXmin()));
                        cloned_ea->setY(ea.getYmin() + transformed_y * (ea.getYmax() - ea.getYmin()));
                        cloned_ea->setMouseYOrientation(osgGA::GUIEventAdapter::Y_INCREASING_UPWARDS);
                        
                        // std::cout << transformed_x << "/" << transformed_x << " -> " << cloned_ea->getX() << "/" <<cloned_ea->getY() << std::endl;
                        
                        
                        // dispatch cloned event to devices
                        osgViewer::View::Devices& devices = viewer->getDevices();
                        for(osgViewer::View::Devices::iterator i = devices.begin(); i != devices.end(); ++i)
                        {
                            if((*i)->getCapabilities() & osgGA::Device::SEND_EVENTS)
                            {
                                (*i)->sendEvent(*cloned_ea);
                            }
                        }
                    }
                    else 
                    {
                        if (ea.getEventType()==osgGA::GUIEventAdapter::PUSH)
                        {
                            _drawablesOnPush.insert( hitr->drawable.get() );
                        }
                        else if (ea.getEventType()==osgGA::GUIEventAdapter::MOVE)
                        {
                            OSG_INFO<<"Tooltip..."<<std::endl;
                        }
                        else if (ea.getEventType()==osgGA::GUIEventAdapter::RELEASE)
                        {
                            if (_drawablesOnPush.find(hitr->drawable.get()) != _drawablesOnPush.end())
                                doOperation();
                            return true;
                        }
                    }
                }
            }
            break;
        }
        case(osgGA::GUIEventAdapter::KEYDOWN):
        {
            //OSG_NOTICE<<"PickEventHandler KEYDOWN "<<(char)ea.getKey()<<std::endl;
            //if (object) OSG_NOTICE<<"    "<<object->className()<<std::endl;
            break;
        }
        default:
            break;
    }
    return false;
}
Esempio n. 25
0
bool ElevationDragger::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
{
    bool ret = true;
    if (ea.getHandled()) {
        ret = false;
    }
    else
    {
        bool handled = false;
        if (_elevationMode)
        {
            if (ea.getEventType() == osgGA::GUIEventAdapter::PUSH)
            {
                ret = osgEarth::Dragger::handle(ea, aa);
                if (ret) 
                {
                    bool pressedAlt = ((ea.getModKeyMask() & _modKeyMask) > 0);
                    if (pressedAlt)
                    {
                        _pointer.reset();

                        // set movement range
                        // TODO: values 0.0 and 300000.0 are rather experimental
                        GeoPoint posStart(_position.getSRS(), _position.x(), _position.y(), 0.0, ALTMODE_ABSOLUTE);
                        osg::Vec3d posStartXYZ;
                        posStart.toWorld(posStartXYZ);

                        GeoPoint posEnd(_position.getSRS(), _position.x(), _position.y(), 300000.0, ALTMODE_ABSOLUTE);
                        osg::Vec3d posEndXYZ;
                        posEnd.toWorld(posEndXYZ);

                        _projector->setLine(posStartXYZ, posEndXYZ);

                        // set camera
                        osgUtil::LineSegmentIntersector::Intersections intersections;
                        osg::Node::NodeMask intersectionMask = 0xffffffff;
                        osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa);
                        if (view->computeIntersections(ea.getX(),ea.getY(),intersections, intersectionMask))
                        {
                            for (osgUtil::LineSegmentIntersector::Intersections::iterator hitr = intersections.begin(); hitr != intersections.end(); ++hitr)
                            {
                                _pointer.addIntersection(hitr->nodePath, hitr->getLocalIntersectPoint());
                            }
                            for (osg::NodePath::iterator itr = _pointer._hitList.front().first.begin(); itr != _pointer._hitList.front().first.end(); ++itr)
                            {
                                ElevationDragger* dragger = dynamic_cast<ElevationDragger*>(*itr);
                                if (dragger==this)
                                {
                                    osg::Camera *rootCamera = view->getCamera();
                                    osg::NodePath nodePath = _pointer._hitList.front().first;
                                    osg::NodePath::reverse_iterator ritr;
                                    for (ritr = nodePath.rbegin(); ritr != nodePath.rend(); ++ritr)
                                    {
                                        osg::Camera* camera = dynamic_cast<osg::Camera*>(*ritr);
                                        if (camera && (camera->getReferenceFrame()!=osg::Transform::RELATIVE_RF || camera->getParents().empty()))
                                        {
                                             rootCamera = camera;
                                             break;
                                        }
                                    }
                                    _pointer.setCamera(rootCamera);
                                    _pointer.setMousePosition(ea.getX(), ea.getY());
                                }
                            }
                        }

                        _elevationDragging = true;
                    }
                }
                handled = true;
            }
            else if (ea.getEventType() == osgGA::GUIEventAdapter::RELEASE)
            {
                _elevationDragging = false;
            }
            else if (ea.getEventType() == osgGA::GUIEventAdapter::DRAG) 
            {
                if (_elevationDragging) 
                {
                    _pointer._hitIter = _pointer._hitList.begin();
                    _pointer.setMousePosition(ea.getX(), ea.getY());

                    if (_projector->project(_pointer, _startProjectedPoint)) 
                    {
                        //Get the absolute mapPoint that they've drug it to.
                        GeoPoint projectedPos;
                        projectedPos.fromWorld(_position.getSRS(), _startProjectedPoint);

                        // make sure point is not dragged down below
                        // TODO: think of a better solution / HeightAboveTerrain performance issues?
                        if (projectedPos.z() > 0)
                        {
                            //If the current position is relative, we need to convert the absolute world point to relative.
                            //If the point is absolute then just emit the absolute point.
                            if (_position.altitudeMode() == ALTMODE_RELATIVE)
                            {
                                projectedPos.alt() = _position.alt();
                                projectedPos.altitudeMode() = ALTMODE_RELATIVE;
                            }

                            setPosition( projectedPos );
                            aa.requestRedraw();
                        }
                    }

                    handled = true;
                }
            }
        }
        
        if (!handled) {
            ret = osgEarth::Dragger::handle(ea, aa);
        }
    }
    return ret;
}
Esempio n. 26
0
bool Dragger::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
{
    if (ea.getHandled()) return false;

    osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa);
    if (!view) return false;
    if (!_mapNode.valid()) return false;

    if (ea.getEventType() == osgGA::GUIEventAdapter::PUSH)
    {
        Picker picker( view, this );
        Picker::Hits hits;

        if ( picker.pick( ea.getX(), ea.getY(), hits ) )
        {
            _dragging = true;
            aa.requestRedraw();
            return true;
        }
    }
    else if (ea.getEventType() == osgGA::GUIEventAdapter::RELEASE)
    {
        _dragging = false;
        aa.requestRedraw();
    }
    else if (ea.getEventType() == osgGA::GUIEventAdapter::DRAG)
    {
        if (_dragging)
        {
            osg::Vec3d world;
            if ( _mapNode->getTerrain()->getWorldCoordsUnderMouse(view, ea.getX(), ea.getY(), world) )
            {
                //Get the absolute mapPoint that they've drug it to.
                GeoPoint mapPoint;
                mapPoint.fromWorld( _mapNode->getMapSRS(), world );
                //_mapNode->getMap()->worldPointToMapPoint(world, mapPoint);

                //If the current position is relative, we need to convert the absolute world point to relative.
                //If the point is absolute then just emit the absolute point.
                if (_position.altitudeMode() == ALTMODE_RELATIVE)
                {
                    mapPoint.alt() = _position.alt();
                    mapPoint.altitudeMode() = ALTMODE_RELATIVE;
                }
                setPosition( mapPoint );
                aa.requestRedraw();
                return true;
            }
        }
    }   
    else if (ea.getEventType() == osgGA::GUIEventAdapter::MOVE)
    {
        Picker picker( view, this );
        Picker::Hits hits;

        if ( picker.pick( ea.getX(), ea.getY(), hits ) )
        {
            setHover( true );
        }
        else
        {
            setHover( false );
        }        
        aa.requestRedraw();
    }
    return false;
}
Esempio n. 27
0
File: Dragger.cpp Progetto: 4ker/osg
bool Dragger::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
{
    if (ea.getHandled()) return false;

    osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa);
    if (!view) return false;

    bool handled = false;

    bool activationPermitted = true;
    if (_activationModKeyMask!=0 || _activationMouseButtonMask!=0 || _activationKeyEvent!=0)
    {
        _activationPermittedByModKeyMask = (_activationModKeyMask!=0) ?
            ((ea.getModKeyMask() & _activationModKeyMask)!=0) :
            false;

        _activationPermittedByMouseButtonMask = (_activationMouseButtonMask!=0) ?
            ((ea.getButtonMask() & _activationMouseButtonMask)!=0) :
            false;

        if (_activationKeyEvent!=0)
        {
            switch (ea.getEventType())
            {
                case osgGA::GUIEventAdapter::KEYDOWN:
                {
                    if (ea.getKey()==_activationKeyEvent) _activationPermittedByKeyEvent = true;
                    break;
                }
                case osgGA::GUIEventAdapter::KEYUP:
                {
                    if (ea.getKey()==_activationKeyEvent) _activationPermittedByKeyEvent = false;
                    break;
                }
                default:
                    break;
            }
        }

        activationPermitted =  _activationPermittedByModKeyMask || _activationPermittedByMouseButtonMask || _activationPermittedByKeyEvent;

    }

    if (activationPermitted || _draggerActive)
    {
        switch (ea.getEventType())
        {
            case osgGA::GUIEventAdapter::PUSH:
            {
                osgUtil::LineSegmentIntersector::Intersections intersections;

                _pointer.reset();

                if (view->computeIntersections(ea ,intersections, _intersectionMask))
                {
                    for(osgUtil::LineSegmentIntersector::Intersections::iterator hitr = intersections.begin();
                        hitr != intersections.end();
                        ++hitr)
                    {
                        _pointer.addIntersection(hitr->nodePath, hitr->getLocalIntersectPoint());
                    }
                    for (osg::NodePath::iterator itr = _pointer._hitList.front().first.begin();
                            itr != _pointer._hitList.front().first.end();
                            ++itr)
                    {
                        osgManipulator::Dragger* dragger = dynamic_cast<osgManipulator::Dragger*>(*itr);
                        if (dragger)
                        {
                            if (dragger==this)
                            {
                                osg::Camera *rootCamera = view->getCamera();
                                osg::NodePath nodePath = _pointer._hitList.front().first;
                                osg::NodePath::reverse_iterator ritr;
                                for(ritr = nodePath.rbegin();
                                    ritr != nodePath.rend();
                                    ++ritr)
                                {
                                    osg::Camera* camera = dynamic_cast<osg::Camera*>(*ritr);
                                    if (camera && (camera->getReferenceFrame()!=osg::Transform::RELATIVE_RF || camera->getParents().empty()))
                                    {
                                         rootCamera = camera;
                                         break;
                                    }
                                }

                                _pointer.setCamera(rootCamera);
                                _pointer.setMousePosition(ea.getX(), ea.getY());

                                if(dragger->handle(_pointer, ea, aa))
                                {
                                    dragger->setDraggerActive(true);
                                    handled = true;
                                }
                            }
                        }
                    }
                }
                break;
            }
            case osgGA::GUIEventAdapter::DRAG:
            case osgGA::GUIEventAdapter::RELEASE:
            {
                if (_draggerActive)
                {
                    _pointer._hitIter = _pointer._hitList.begin();
//                    _pointer.setCamera(view->getCamera());
                    _pointer.setMousePosition(ea.getX(), ea.getY());

                    if(handle(_pointer, ea, aa))
                    {
                        handled = true;
                    }
                }
                break;
            }
            default:
                break;
        }

        if (_draggerActive && ea.getEventType() == osgGA::GUIEventAdapter::RELEASE)
        {
            setDraggerActive(false);
            _pointer.reset();
        }
    }

    return handled;
}
Esempio n. 28
0
bool ThreadingHandler::handle(const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &aa)
{
    osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa);
    if (!view) return false;
    
    osgViewer::ViewerBase* viewerBase = view->getViewerBase();
    osgViewer::Viewer* viewer = dynamic_cast<Viewer*>(viewerBase);

    if (viewerBase == NULL)
    {
        return false;
    }

    if (ea.getHandled()) return false;

    switch(ea.getEventType())
    {
        case(osgGA::GUIEventAdapter::KEYUP):
        {
            double    delta = osg::Timer::instance()->delta_s(_tickOrLastKeyPress, osg::Timer::instance()->tick());

            if (_changeThreadingModel == true && ea.getKey() == _keyEventChangeThreadingModel && delta > 1.0)
            {
                _tickOrLastKeyPress = osg::Timer::instance()->tick();

                switch(viewerBase->getThreadingModel())
                {
                case(osgViewer::ViewerBase::SingleThreaded):
                    viewerBase->setThreadingModel(osgViewer::ViewerBase::CullDrawThreadPerContext);
                    osg::notify(osg::NOTICE)<<"Threading model 'CullDrawThreadPerContext' selected."<<std::endl;
                    break;
                case(osgViewer::ViewerBase::CullDrawThreadPerContext):
                    viewerBase->setThreadingModel(osgViewer::ViewerBase::DrawThreadPerContext);
                    osg::notify(osg::NOTICE)<<"Threading model 'DrawThreadPerContext' selected."<<std::endl;
                    break;
                case(osgViewer::ViewerBase::DrawThreadPerContext):
                    viewerBase->setThreadingModel(osgViewer::ViewerBase::CullThreadPerCameraDrawThreadPerContext);
                    osg::notify(osg::NOTICE)<<"Threading model 'CullThreadPerCameraDrawThreadPerContext' selected."<<std::endl;
                    break;
                case(osgViewer::ViewerBase::CullThreadPerCameraDrawThreadPerContext):
                    viewerBase->setThreadingModel(osgViewer::ViewerBase::SingleThreaded);
                    osg::notify(osg::NOTICE)<<"Threading model 'SingleThreaded' selected."<<std::endl;
                    break;
#if 1                    
                case(osgViewer::ViewerBase::AutomaticSelection):
                    viewerBase->setThreadingModel(osgViewer::ViewerBase::SingleThreaded);
                    osg::notify(osg::NOTICE)<<"Threading model 'AutomaticSelection' selected."<<std::endl;
#else                    
                case(osgViewer::ViewerBase::AutomaticSelection):
                    viewerBase->setThreadingModel(viewer->suggestBestThreadingModel());
                    osg::notify(osg::NOTICE)<<"Threading model 'AutomaticSelection' selected."<<std::endl;
#endif
                    break;
                }
                return true;
            }
            if (viewer && _changeEndBarrierPosition == true && ea.getKey() == _keyEventChangeEndBarrierPosition)
            {
                switch(viewer->getEndBarrierPosition())
                {
                case(osgViewer::Viewer::BeforeSwapBuffers):
                    viewer->setEndBarrierPosition(osgViewer::Viewer::AfterSwapBuffers);
                    osg::notify(osg::NOTICE)<<"Threading model 'AfterSwapBuffers' selected."<<std::endl;
                    break;
                case(osgViewer::Viewer::AfterSwapBuffers):
                    viewer->setEndBarrierPosition(osgViewer::Viewer::BeforeSwapBuffers);
                    osg::notify(osg::NOTICE)<<"Threading model 'BeforeSwapBuffers' selected."<<std::endl;
                    break;
                }
                return true;
            }
            break;
        }
    default:
        break;
    }
    return false;
}
Esempio n. 29
0
bool RecordCameraPathHandler::handle(const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &aa)
{
    osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa);

    if (view == NULL)
    {
        return false;
    }

    if(ea.getEventType()==osgGA::GUIEventAdapter::FRAME)
    {
        // Calculate our current delta (difference) in time between the last frame and
        // current frame, regardless of whether we actually store a ControlPoint...
        osg::Timer_t time = osg::Timer::instance()->tick();
        double delta = osg::Timer::instance()->delta_s(_lastFrameTime, time);
        _lastFrameTime = time;

        // If our internal _delta is finally large enough to warrant a ControlPoint
        // insertion, do so now. Be sure and reset the internal _delta, so we can start
        // calculating when the next insert should happen.
        if (_currentlyRecording && _delta >= _interval)
        {
            const osg::Matrixd& m = view->getCamera()->getInverseViewMatrix();
            double animationPathTime = osg::Timer::instance()->delta_s(_animStartTime, time);            
            _animPath->insert(animationPathTime, osg::AnimationPath::ControlPoint(m.getTrans(), m.getRotate()));
            _delta = 0.0f;

            if (_fout)
            {
                _animPath->write(_animPath->getTimeControlPointMap().find(animationPathTime), _fout);
                _fout.flush();
            }

        }
        else _delta += delta;
        
        return true;
    }

    if (ea.getHandled()) return false;

    switch(ea.getEventType())
    {
        case(osgGA::GUIEventAdapter::KEYUP):
        {
            // The user has requested to toggle recording.
            if (ea.getKey() ==_keyEventToggleRecord)
            {
                // The user has requested to BEGIN recording.
                if (!_currentlyRecording)
                {
                    _currentlyRecording = true;
                    _animStartTime = osg::Timer::instance()->tick();
                    _animPath->clear();
                    
                    if (!_filename.empty())
                    {
                        std::stringstream ss;
                        ss << osgDB::getNameLessExtension(_filename);
                        if ( _autoinc != -1 )
                        {
                            ss << "_"<<std::setfill( '0' ) << std::setw( 2 ) << _autoinc;
                            _autoinc++;
                        }
                        ss << "."<<osgDB::getFileExtension(_filename);
                        
                        osg::notify(osg::NOTICE) << "Recording camera path to file " << ss.str() << std::endl;
                        _fout.open( ss.str().c_str() );

                        // make sure doubles are not trucated by default stream precision = 6
                        _fout.precision( 15 );
                    }
                    else
                    {
                        osg::notify(osg::NOTICE)<<"Recording camera path."<<std::endl;
                    }
                }

                // THe user has requested to STOP recording, write the file!
                else
                {
                    _currentlyRecording = false;
                    _delta = 0.0f;

                    if (_fout) _fout.close();
                }

                return true;
            }

            // The user has requested to toggle playback. You'll notice in the code below that
            // we take over the current manipulator; it was originally recommended that we
            // check for a KeySwitchManipulator, create one if not present, and then add this
            // to either the newly created one or the existing one. However, the code do that was
            // EXTREMELY dirty, so I opted for a simpler solution. At a later date, someone may
            // want to implement the original recommendation (which is in a mailing list reply
            // from June 1st by Robert in a thread called "osgviewer Camera Animation (preliminary)".
            else if (ea.getKey() == _keyEventTogglePlayback)
            {
                if (_currentlyRecording)
                {
                    _currentlyRecording = false;
                    _delta = 0.0f;

                    // In the future this will need to be written continuously, rather
                    // than all at once.
                    std::ofstream out(_filename.c_str());
                    osg::notify(osg::NOTICE)<<"Writing camera file: "<<_filename<<std::endl;
                    _animPath->write(out);
                    out.close();
                }

                // The user has requested to BEGIN playback.
                if (!_currentlyPlaying)
                {
                    _animPathManipulator = new osgGA::AnimationPathManipulator(_animPath.get());
                    _animPathManipulator->home(ea,aa);


                    // If we successfully found our _filename file, set it and keep a copy
                    // around of the original MatrixManipulator to restore later.
                    if (_animPathManipulator.valid() && _animPathManipulator->valid())
                    {
                        _oldManipulator = view->getCameraManipulator();
                        view->setCameraManipulator(_animPathManipulator.get());
                        _currentlyPlaying = true;
                    }
                }

                // The user has requested to STOP playback.
                else
                {
                    // Restore the old manipulator if necessary and stop playback.
                    if(_oldManipulator.valid()) view->setCameraManipulator(_oldManipulator.get());
                    _currentlyPlaying = false;
                    _oldManipulator = 0;
                }

                return true;
            }        

            break;
        }
    default:
        break;
    }

    return false;
}
Esempio n. 30
0
bool SlideEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa)
{

    if (!_viewer)
    {
        _viewer = dynamic_cast<osgViewer::Viewer*>(&aa);
        selectSlide(0);
        home();
        OSG_NOTICE<<"Assigned viewer. to SlideEventHandler"<<std::endl;
    }
    //else  OSG_NOTICE<<"SlideEventHandler::handle() "<<ea.getTime()<<std::endl;


    if (ea.getHandled()) return false;

    switch(ea.getEventType())
    {
        case(osgGA::GUIEventAdapter::FRAME):
        {
            if (_autoSteppingActive && !_pause)
            {
                double time = ea.time();

                if (_firstTraversal)
                {
                    _firstTraversal = false;
                    _previousTime = time;
                }
                else if (time-_previousTime>=getCurrentTimeDelayBetweenSlides())
                {
                    // _previousTime = time;

                    if (!_hold)
                    {
                        // increment the previous by the required time delay, note relative to the current
                        // to keep the time relative to an absolute time signal, thus avoid drift of timing.
                        _previousTime += getCurrentTimeDelayBetweenSlides();

                        nextLayerOrSlide();
                    }
                    else
                    {
                        // we're holding of the move to next layer to slide, but we need slip the time forward accordingly
                        // componensate for the extra time that this frame is recieving.
                        _previousTime = time-getCurrentTimeDelayBetweenSlides();
                    }
                }
            }
            return false;
        }

        case(osgGA::GUIEventAdapter::KEYDOWN):
        {
            double time = ea.time();
            double deltaTime = time - _timeLastKeyPresses;
            if (deltaTime < _minimumTimeBetweenKeyPresses)
            {
                 break;
            }

            _timeLastKeyPresses = time;

            if (ea.getKey()=='g')
            {
                if (!_autoSteppingActive)
                {
                    _autoSteppingActive = true;
                    _previousTime = ea.time();
                }
                return true;
            }
            else if (ea.getKey()=='h')
            {
                if (_autoSteppingActive)
                {
                    _autoSteppingActive = false;
                    _previousTime = ea.time();
                }
                return true;
            }
            else if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Home ||
                     ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Home)
            {
                _autoSteppingActive = false;
                selectSlide(0);
                home(ea,aa);
                return true;
            }
            else if (ea.getKey()==osgGA::GUIEventAdapter::KEY_End ||
                     ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_End)
            {
                _autoSteppingActive = false;
                selectSlide(LAST_POSITION,LAST_POSITION);
                home(ea,aa);
                return true;
            }
            else if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Down ||
                     ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Down)
            {
                _autoSteppingActive = false;
                nextLayer();
                return true;
            }
            else if (ea.getKey()=='n')
            {
                _autoSteppingActive = false;
                nextLayerOrSlide();
                return true;
            }
            else if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Up ||
                     ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Up)
            {
                _autoSteppingActive = false;
                previousLayer();
                return true;
            }
            else if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Page_Down ||
                     ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Page_Down)
            {
                _autoSteppingActive = false;
                nextLayerOrSlide();
                return true;
            }
            else if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Page_Up ||
                     ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Page_Up)
            {
                _autoSteppingActive = false;
                previousLayerOrSlide();
                return true;
            }
            else if (ea.getKey()=='N' ||
                     ea.getKey()==osgGA::GUIEventAdapter::KEY_Right ||
                     ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Right)
            {
                _autoSteppingActive = false;
                nextSlide();
                home(ea,aa);
                return true;
            }
            else if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Left ||
                     ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Left)
            {
                _autoSteppingActive = false;
                previousSlide();
                home(ea,aa);
                return true;
            }
            else if (ea.getKey()=='p')
            {
                if (!_pause)
                {
                    _pause = true;
#if 0
                    resetUpdateCallbackActivity(ALL_OBJECTS);
#endif
                    _activeOperators.setPause(_pause);
                }

                return true;

            }
            else if (ea.getKey()=='o')
            {
                if (_pause)
                {
                    _pause = false;
#if 0
                    resetUpdateCallbackActivity(ALL_OBJECTS);
#endif
                    _activeOperators.setPause(_pause);
                }
                return true;

            }
            else if (ea.getKey()=='h')
            {
                _hold = true;
                return true;
            }
            else if (ea.getKey()=='r')
            {
#if 0
                resetUpdateCallbacks(ALL_OBJECTS);
#endif
                _activeOperators.reset();
                return true;
            }
/*
            else if (ea.getKey()=='c')
            {
                _cursorOn = !_cursorOn;

                for( unsigned int i = 0; i < _viewer->getCameraConfig()->getNumberOfCameras(); i++ )
                {
                    Producer::Camera* cam = _viewer->getCameraConfig()->getCamera(i);
                    Producer::RenderSurface* rs = cam->getRenderSurface();
                    rs->useCursor(_cursorOn);
                }

                return true;
            }
*/
            else if (ea.getKey()=='u')
            {
                updateAlpha(true,false,ea.getXnormalized(),ea.getYnormalized());
                return true;
            }
            else if (ea.getKey()=='i')
            {
                updateAlpha(false,true,ea.getXnormalized(),ea.getYnormalized());
                return true;
            }
            else if (ea.getKey()=='k')
            {
                updateLight(ea.getXnormalized(),ea.getYnormalized());
                return true;
            }

            return false;
        }
        case(osgGA::GUIEventAdapter::KEYUP):
        {
            if (ea.getKey()=='h')
            {
                _hold = false;
                return true;
            }
            else if (ea.getKey()=='u')
            {
                setRequestReload(true);
                return true;
            }
            return false;
        }
        default:
            return false;
    }
    return false;
}