From e09e07d45b13b6199ad3ac534b81c0b4354b3394 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Mon, 2 Jun 2008 17:34:47 +0000 Subject: [PATCH] Added support for assigning GraphicsContext to individual GUIEventAdapter events, and use of this within osgViewer::View to better track the sources of events. --- include/osgGA/EventQueue | 4 ++++ include/osgGA/GUIEventAdapter | 5 +++++ include/osgViewer/GraphicsWindow | 2 +- include/osgViewer/View | 4 ++-- src/osgGA/GUIEventAdapter.cpp | 9 +++++---- src/osgViewer/CompositeViewer.cpp | 18 +++++++----------- src/osgViewer/GraphicsWindowCarbon.cpp | 2 +- src/osgViewer/GraphicsWindowWin32.cpp | 2 +- src/osgViewer/GraphicsWindowX11.cpp | 4 +++- src/osgViewer/View.cpp | 26 ++++++++++++++++---------- 10 files changed, 45 insertions(+), 31 deletions(-) diff --git a/include/osgGA/EventQueue b/include/osgGA/EventQueue index 878310684..efd408cfc 100644 --- a/include/osgGA/EventQueue +++ b/include/osgGA/EventQueue @@ -59,6 +59,10 @@ class OSGGA_EXPORT EventQueue : public osg::Referenced bool getUseFixedMouseInputRange() { return _useFixedMouseInputRange; } + /** Set the graphics context associated with this event queue.*/ + void setGraphicsContext(osg::GraphicsContext* context) { getCurrentEventState()->setGraphicsContext(context); } + + /** Set the mouse input range.*/ void setMouseInputRange(float xMin, float yMin, float xMax, float yMax) { getCurrentEventState()->setInputRange(xMin, yMin, xMax, yMax); } diff --git a/include/osgGA/GUIEventAdapter b/include/osgGA/GUIEventAdapter index 3e1cdd729..077e31c54 100644 --- a/include/osgGA/GUIEventAdapter +++ b/include/osgGA/GUIEventAdapter @@ -16,6 +16,7 @@ #include #include +#include #include namespace osgGA{ @@ -290,6 +291,9 @@ public: /** deprecated function for getting time of event. */ double time() const { return _time; } + + void setGraphicsContext(osg::GraphicsContext* context) { _context = context; } + const osg::GraphicsContext* getGraphicsContext() const { return _context.get(); } /** set window rectangle. */ @@ -429,6 +433,7 @@ public: EventType _eventType; double _time; + osg::observer_ptr _context; int _windowX; int _windowY; int _windowWidth; diff --git a/include/osgViewer/GraphicsWindow b/include/osgViewer/GraphicsWindow index 5c03831e1..ce6aa52ba 100644 --- a/include/osgViewer/GraphicsWindow +++ b/include/osgViewer/GraphicsWindow @@ -40,7 +40,7 @@ class OSGVIEWER_EXPORT GraphicsWindow : public osg::GraphicsContext, public osgG { public: - GraphicsWindow() { _eventQueue = new osgGA::EventQueue; } + GraphicsWindow() { _eventQueue = new osgGA::EventQueue; _eventQueue->setGraphicsContext(this); } virtual bool isSameKindAs(const Object* object) const { return dynamic_cast(object)!=0; } virtual const char* libraryName() const { return "osgViewer"; } diff --git a/include/osgViewer/View b/include/osgViewer/View index 5e17cd88b..8f8422e46 100644 --- a/include/osgViewer/View +++ b/include/osgViewer/View @@ -176,7 +176,7 @@ class OSGVIEWER_EXPORT View : public osg::View, public osgGA::GUIActionAdapter /** Get the camera which contains the pointer position x,y specified master cameras window/eye coords. * Also passes back the local window coords for the graphics context associated with the camera passed back. */ const osg::Camera* getCameraContainingPosition(float x, float y, float& local_x, float& local_y) const; - + /** Compute intersections between a ray through the specified master cameras window/eye coords and a specified node. * Note, when a master cameras has slaves and no viewport itself its coordinate frame will be in clip space i.e. -1,-1 to 1,1, * while if its has a viewport the coordintates will be relative to its viewport dimensions. @@ -186,7 +186,7 @@ class OSGVIEWER_EXPORT View : public osg::View, public osgGA::GUIActionAdapter /** Compute intersections between a ray through the specified master cameras window/eye coords and a specified nodePath's subgraph. */ bool computeIntersections(float x,float y, osg::NodePath& nodePath, osgUtil::LineSegmentIntersector::Intersections& intersections,osg::Node::NodeMask traversalMask = 0xffffffff); - + virtual void requestRedraw(); virtual void requestContinuousUpdate(bool needed=true); diff --git a/src/osgGA/GUIEventAdapter.cpp b/src/osgGA/GUIEventAdapter.cpp index 4e313595b..1491b5cc4 100644 --- a/src/osgGA/GUIEventAdapter.cpp +++ b/src/osgGA/GUIEventAdapter.cpp @@ -32,12 +32,12 @@ GUIEventAdapter::GUIEventAdapter(): _windowHeight(1024), _key(0), _button(0), - _Xmin(0.0), + _Xmin(-1.0), _Xmax(1.0), - _Ymin(0.0), + _Ymin(-1.0), _Ymax(1.0), - _mx(0.5), - _my(0.5), + _mx(0.0), + _my(0.0), _pressure(0.0), _tiltX(0.0), _tiltY(0.0), @@ -56,6 +56,7 @@ GUIEventAdapter::GUIEventAdapter(const GUIEventAdapter& rhs,const osg::CopyOp& c _handled(rhs._handled), _eventType(rhs._eventType), _time(rhs._time), + _context(rhs._context), _windowX(rhs._windowX), _windowY(rhs._windowY), _windowWidth(rhs._windowWidth), diff --git a/src/osgViewer/CompositeViewer.cpp b/src/osgViewer/CompositeViewer.cpp index 0e0ae1655..c4fd6ee82 100644 --- a/src/osgViewer/CompositeViewer.cpp +++ b/src/osgViewer/CompositeViewer.cpp @@ -626,6 +626,8 @@ void CompositeViewer::eventTraversal() { osgGA::GUIEventAdapter* event = itr->get(); + //osg::notify(osg::NOTICE)<<"event->getGraphicsContext()="<getGraphicsContext()<getX(); @@ -668,15 +670,11 @@ void CompositeViewer::eventTraversal() // If this camera is not a slave camera if (camera->getView()->getCamera() == camera) { - const osg::GraphicsContext::Traits* traits = gw ? gw->getTraits() : 0; - if (traits) - { - eventState->setInputRange( 0, 0, traits->width, traits->height); - } - else - { - eventState->setInputRange(-1.0, -1.0, 1.0, 1.0); - } + eventState->setGraphicsContext(gw); + eventState->setInputRange( viewport->x(), viewport->y(), + viewport->x()+viewport->width(), + viewport->y()+viewport->height()); + } else { @@ -744,8 +742,6 @@ void CompositeViewer::eventTraversal() event->setButtonMask(eventState->getButtonMask()); event->setMouseYOrientation(eventState->getMouseYOrientation()); } - //osg::notify(osg::NOTICE)<<" mouse x = "<getX()<<" y="<getY()<getCurrentEventState()->getGraphicsContext()="<getCurrentEventState()->getGraphicsContext()<x; int windowY = _traits->y; diff --git a/src/osgViewer/View.cpp b/src/osgViewer/View.cpp index 02c74387c..165a6e1ab 100644 --- a/src/osgViewer/View.cpp +++ b/src/osgViewer/View.cpp @@ -1703,24 +1703,29 @@ bool View::containsCamera(const osg::Camera* camera) const const osg::Camera* View::getCameraContainingPosition(float x, float y, float& local_x, float& local_y) const { const osgGA::GUIEventAdapter* eventState = getEventQueue()->getCurrentEventState(); + const osgViewer::GraphicsWindow* gw = dynamic_cast(eventState->getGraphicsContext()); + bool view_invert_y = eventState->getMouseYOrientation()==osgGA::GUIEventAdapter::Y_INCREASING_DOWNWARDS; - // osg::notify(osg::NOTICE)<<"View::getCameraContainingPosition("<= (viewport->x()-epsilon) && new_y >= (viewport->y()-epsilon) && new_x < (viewport->x()+viewport->width()-1.0+epsilon) && new_y <= (viewport->y()+viewport->height()-1.0+epsilon) ) @@ -1728,7 +1733,7 @@ const osg::Camera* View::getCameraContainingPosition(float x, float y, float& lo local_x = new_x; local_y = new_y; - // osg::notify(osg::NOTICE)<<"Returning master camera"<getViewMatrix() * getCamera()->getProjectionMatrix(); + // convert to non dimensional x = (x - eventState->getXmin()) * 2.0 / (eventState->getXmax()-eventState->getXmin()) - 1.0; y = (y - eventState->getYmin())* 2.0 / (eventState->getYmax()-eventState->getYmin()) - 1.0; @@ -1748,7 +1754,7 @@ const osg::Camera* View::getCameraContainingPosition(float x, float y, float& lo slave._camera->getAllowEventFocus() && slave._camera->getRenderTargetImplementation()==osg::Camera::FRAME_BUFFER) { - // osg::notify(osg::NOTICE)<<"Testing slave camera "<getName()<getName()<getViewport() : 0;