From 1a16d6d6b0005e18f2b0ae7e9f4cad7d35867d6b Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Mon, 13 Mar 2006 13:19:37 +0000 Subject: [PATCH] Added an EventQueue directly into osgProducer::Viewer. --- include/osgGA/EventQueue | 2 +- include/osgProducer/KeyboardMouseCallback | 1 + include/osgProducer/Viewer | 8 ++++++ src/osgGA/EventQueue.cpp | 3 ++- src/osgProducer/KeyboardMouseCallback.cpp | 24 ++++++++--------- src/osgProducer/Viewer.cpp | 26 +++++++++++++------ src/osgWrappers/osgGA/EventQueue.cpp | 2 +- .../osgProducer/KeyboardMouseCallback.cpp | 3 ++- src/osgWrappers/osgProducer/Viewer.cpp | 4 +++ 9 files changed, 49 insertions(+), 24 deletions(-) diff --git a/include/osgGA/EventQueue b/include/osgGA/EventQueue index 69dd6d5ab..78f24a0d4 100644 --- a/include/osgGA/EventQueue +++ b/include/osgGA/EventQueue @@ -31,7 +31,7 @@ class OSGGA_EXPORT EventQueue : public osg::Referenced { public: - EventQueue(); + EventQueue(GUIEventAdapter::MouseYOrientation mouseYOrientation=GUIEventAdapter::Y_INCREASING_DOWNWARDS); typedef std::list< osg::ref_ptr > Events; diff --git a/include/osgProducer/KeyboardMouseCallback b/include/osgProducer/KeyboardMouseCallback index 576817e46..c3fd8e9d4 100644 --- a/include/osgProducer/KeyboardMouseCallback +++ b/include/osgProducer/KeyboardMouseCallback @@ -63,6 +63,7 @@ class OSGPRODUCER_EXPORT KeyboardMouseCallback : public Producer::KeyboardMouseC // local methods and members typedef osgGA::EventQueue::Events EventQueue; + void setEventQueue(osgGA::EventQueue* eventQueue) { _eventQueue = eventQueue; } osgGA::EventQueue* getEventQueue() { return _eventQueue.get(); } bool takeEventQueue(EventQueue& queue); diff --git a/include/osgProducer/Viewer b/include/osgProducer/Viewer index ec288b3e1..b06ca3f77 100644 --- a/include/osgProducer/Viewer +++ b/include/osgProducer/Viewer @@ -255,6 +255,12 @@ class OSGPRODUCER_EXPORT Viewer : public OsgCameraGroup, public osgGA::GUIAction /** Compute, from normalized mouse coords, for all Cameras, intersections with the scene.*/ bool computeIntersections(float x,float y,osgUtil::IntersectVisitor::HitList& hits,osg::Node::NodeMask traversalMask = 0xffffffff); + /** Set the EventQueue - a thread safe queue for registering events.*/ + void setEventQueue(osgGA::EventQueue* eventQueue) { _eventQueue = eventQueue; } + + /** Get the EventQueue.*/ + osgGA::EventQueue* getEventQueue() { return _eventQueue.get(); } + void setKeyboardMouse(Producer::KeyboardMouse* kbm); Producer::KeyboardMouse* getKeyboardMouse() { return _kbm.get(); } const Producer::KeyboardMouse* getKeyboardMouse() const { return _kbm.get(); } @@ -310,6 +316,8 @@ class OSGPRODUCER_EXPORT Viewer : public OsgCameraGroup, public osgGA::GUIAction bool _writeImageWhenDone; std::string _writeImageFileName; + osg::ref_ptr _eventQueue; + osg::ref_ptr _kbm; osg::ref_ptr _kbmcb; diff --git a/src/osgGA/EventQueue.cpp b/src/osgGA/EventQueue.cpp index 1b4e3811e..23c119263 100644 --- a/src/osgGA/EventQueue.cpp +++ b/src/osgGA/EventQueue.cpp @@ -16,10 +16,11 @@ using namespace osgGA; -EventQueue::EventQueue() +EventQueue::EventQueue(GUIEventAdapter::MouseYOrientation mouseYOrientation) { _startTick = osg::Timer::instance()->tick(); _accumulateEventState = new GUIEventAdapter(); + _accumulateEventState->setMouseYOrientation(mouseYOrientation); } EventQueue::~EventQueue() diff --git a/src/osgProducer/KeyboardMouseCallback.cpp b/src/osgProducer/KeyboardMouseCallback.cpp index b774db81c..54659dbc9 100644 --- a/src/osgProducer/KeyboardMouseCallback.cpp +++ b/src/osgProducer/KeyboardMouseCallback.cpp @@ -14,39 +14,37 @@ KeyboardMouseCallback::KeyboardMouseCallback(Producer::KeyboardMouse* keyboardMo _done(done), _escapeKeySetsDone(escapeKeySetsDone) { - _eventQueue = new osgGA::EventQueue; - _eventQueue->getCurrentEventState()->setMouseYOrientation(osgGA::GUIEventAdapter::Y_INCREASING_UPWARDS); updateWindowSize(); } void KeyboardMouseCallback::mouseScroll( Producer::KeyboardMouseCallback::ScrollingMotion sm ) { updateWindowSize(); - _eventQueue->mouseScroll((osgGA::GUIEventAdapter::ScrollingMotion)sm); + if (_eventQueue.valid()) _eventQueue->mouseScroll((osgGA::GUIEventAdapter::ScrollingMotion)sm); } void KeyboardMouseCallback::buttonPress( float mx, float my, unsigned int mbutton ) { updateWindowSize(); - _eventQueue->mouseButtonPress(mx,my,mbutton); + if (_eventQueue.valid()) _eventQueue->mouseButtonPress(mx,my,mbutton); } void KeyboardMouseCallback::buttonRelease( float mx, float my, unsigned int mbutton ) { updateWindowSize(); - _eventQueue->mouseButtonRelease(mx,my,mbutton); + if (_eventQueue.valid()) _eventQueue->mouseButtonRelease(mx,my,mbutton); } void KeyboardMouseCallback::doubleButtonPress( float mx, float my, unsigned int mbutton ) { updateWindowSize(); - _eventQueue->mouseButtonPress(mx,my,mbutton); + if (_eventQueue.valid()) _eventQueue->mouseButtonPress(mx,my,mbutton); } void KeyboardMouseCallback::keyPress( Producer::KeyCharacter key ) { updateWindowSize(); - _eventQueue->keyPress((osgGA::GUIEventAdapter::KeySymbol)key); + if (_eventQueue.valid()) _eventQueue->keyPress((osgGA::GUIEventAdapter::KeySymbol)key); // check against adapted key symbol. if (_escapeKeySetsDone && @@ -56,7 +54,7 @@ void KeyboardMouseCallback::keyPress( Producer::KeyCharacter key ) void KeyboardMouseCallback::keyRelease( Producer::KeyCharacter key ) { updateWindowSize(); - _eventQueue->keyRelease((osgGA::GUIEventAdapter::KeySymbol)key); + if (_eventQueue.valid()) _eventQueue->keyRelease((osgGA::GUIEventAdapter::KeySymbol)key); } void KeyboardMouseCallback::specialKeyPress( Producer::KeyCharacter key ) @@ -74,30 +72,32 @@ void KeyboardMouseCallback::specialKeyRelease( Producer::KeyCharacter key ) void KeyboardMouseCallback::windowConfig( int x, int y, unsigned int width, unsigned int height ) { updateWindowSize(); - _eventQueue->windowResize(x,y,x+width,y+height); + if (_eventQueue.valid()) _eventQueue->windowResize(x,y,x+width,y+height); } void KeyboardMouseCallback::mouseMotion( float mx, float my) { updateWindowSize(); - _eventQueue->mouseMotion(mx,my); + if (_eventQueue.valid()) _eventQueue->mouseMotion(mx,my); } void KeyboardMouseCallback::passiveMouseMotion( float mx, float my) { updateWindowSize(); - _eventQueue->mouseMotion(mx,my); + if (_eventQueue.valid()) _eventQueue->mouseMotion(mx,my); } void KeyboardMouseCallback::mouseWarp( float mx, float my) { updateWindowSize(); - _eventQueue->mouseMotion(mx,my); // need mouse warp?? + if (_eventQueue.valid()) _eventQueue->mouseMotion(mx,my); // need mouse warp?? } void KeyboardMouseCallback::updateWindowSize() { + if (!_eventQueue) return; + osgGA::GUIEventAdapter* ea = _eventQueue->getCurrentEventState(); Producer::InputArea* ia = _keyboardMouse->getInputArea(); diff --git a/src/osgProducer/Viewer.cpp b/src/osgProducer/Viewer.cpp index 18485f54c..fb691cd43 100644 --- a/src/osgProducer/Viewer.cpp +++ b/src/osgProducer/Viewer.cpp @@ -144,6 +144,7 @@ Viewer::Viewer(): _recordingAnimationPath(false), _recordingStartTime(0.0) { + _eventQueue = new osgGA::EventQueue(osgGA::GUIEventAdapter::Y_INCREASING_UPWARDS); _position[0] = 0.0; _position[1] = 0.0; _position[2] = 0.0; @@ -162,6 +163,7 @@ Viewer::Viewer(Producer::CameraConfig *cfg): _recordingAnimationPath(false), _recordingStartTime(0.0) { + _eventQueue = new osgGA::EventQueue(osgGA::GUIEventAdapter::Y_INCREASING_UPWARDS); _position[0] = 0.0; _position[1] = 0.0; _position[2] = 0.0; @@ -181,6 +183,7 @@ Viewer::Viewer(const std::string& configFile): _recordingAnimationPath(false), _recordingStartTime(0.0) { + _eventQueue = new osgGA::EventQueue(osgGA::GUIEventAdapter::Y_INCREASING_UPWARDS); _position[0] = 0.0; _position[1] = 0.0; _position[2] = 0.0; @@ -199,6 +202,7 @@ Viewer::Viewer(osg::ArgumentParser& arguments): _recordingAnimationPath(false), _recordingStartTime(0.0) { + _eventQueue = new osgGA::EventQueue(osgGA::GUIEventAdapter::Y_INCREASING_UPWARDS); _position[0] = 0.0; _position[1] = 0.0; _position[2] = 0.0; @@ -342,7 +346,11 @@ void Viewer::setKeyboardMouse(Producer::KeyboardMouse* kbm) void Viewer::setKeyboardMouseCallback(osgProducer::KeyboardMouseCallback* kbmcb) { _kbmcb = kbmcb; - if (_kbm.valid() && _kbmcb.valid()) _kbm->setCallback(_kbmcb.get()); + if (_kbm.valid() && _kbmcb.valid()) + { + _kbm->setCallback(_kbmcb.get()); + _kbmcb->setEventQueue(_eventQueue.get()); + } } void Viewer::setUpViewer(unsigned int options) @@ -353,17 +361,19 @@ void Viewer::setUpViewer(unsigned int options) if (!_kbm) { - _kbm = ia ? + setKeyboardMouse(ia ? (new Producer::KeyboardMouse(ia)) : - (new Producer::KeyboardMouse(getCamera(0)->getRenderSurface())); + (new Producer::KeyboardMouse(getCamera(0)->getRenderSurface())) ); } // set the keyboard mouse callback to catch the events from the windows. if (!_kbmcb) - _kbmcb = new osgProducer::KeyboardMouseCallback( _kbm.get(), _done, (options & ESCAPE_SETS_DONE)!=0 ); + { + setKeyboardMouseCallback(new osgProducer::KeyboardMouseCallback( _kbm.get(), _done, (options & ESCAPE_SETS_DONE)!=0 )); + } - _kbmcb->getEventQueue()->setStartTick(_start_tick); + getEventQueue()->setStartTick(_start_tick); // register the callback with the keyboard mouse manger. _kbm->setCallback( _kbmcb.get() ); @@ -567,7 +577,7 @@ void Viewer::update() #endif // create an event to signal the new frame. - _kbmcb->getEventQueue()->frame(_frameStamp->getReferenceTime()); + getEventQueue()->frame(_frameStamp->getReferenceTime()); // get the event since the last frame. osgProducer::KeyboardMouseCallback::EventQueue queue; @@ -915,8 +925,8 @@ void Viewer::requestWarpPointer(float x,float y) { osg::notify(osg::INFO) << "requestWarpPointer x= "<