| 28 | | // ----------- Begining of glue classes to adapter Producer's keyboard mouse events to osgGA's abstraction events. |
| 29 | | class MyKeyboardMouseCallback : public Producer::KeyboardMouseCallback |
| 30 | | { |
| 31 | | public: |
| 32 | | |
| 33 | | MyKeyboardMouseCallback(osgGA::EventQueue* eventQueue) : |
| 34 | | _done(false), |
| 35 | | _eventQueue(eventQueue) |
| 36 | | { |
| 37 | | } |
| 38 | | |
| 39 | | virtual void shutdown() |
| 40 | | { |
| 41 | | _done = true; |
| 42 | | } |
| 43 | | |
| 44 | | virtual void specialKeyPress( Producer::KeyCharacter key ) |
| 45 | | { |
| 46 | | if (key==Producer::KeyChar_Escape) |
| 47 | | shutdown(); |
| 48 | | |
| 49 | | _eventQueue->keyPress( (osgGA::GUIEventAdapter::KeySymbol) key ); |
| 50 | | } |
| 51 | | |
| 52 | | virtual void specialKeyRelease( Producer::KeyCharacter key ) |
| 53 | | { |
| 54 | | _eventQueue->keyRelease( (osgGA::GUIEventAdapter::KeySymbol) key ); |
| 55 | | } |
| 56 | | |
| 57 | | virtual void keyPress( Producer::KeyCharacter key) |
| 58 | | { |
| 59 | | _eventQueue->keyPress( (osgGA::GUIEventAdapter::KeySymbol) key ); |
| 60 | | } |
| 61 | | |
| 62 | | virtual void keyRelease( Producer::KeyCharacter key) |
| 63 | | { |
| 64 | | _eventQueue->keyRelease( (osgGA::GUIEventAdapter::KeySymbol) key ); |
| 65 | | } |
| 66 | | |
| 67 | | virtual void mouseMotion( float mx, float my ) |
| 68 | | { |
| 69 | | _eventQueue->mouseMotion( mx, my ); |
| 70 | | } |
| 71 | | |
| 72 | | virtual void buttonPress( float mx, float my, unsigned int mbutton ) |
| 73 | | { |
| 74 | | _eventQueue->mouseButtonPress(mx, my, mbutton); |
| 75 | | } |
| 76 | | |
| 77 | | virtual void buttonRelease( float mx, float my, unsigned int mbutton ) |
| 78 | | { |
| 79 | | _eventQueue->mouseButtonRelease(mx, my, mbutton); |
| 80 | | } |
| 81 | | |
| 82 | | bool done() { return _done; } |
| 83 | | |
| 84 | | private: |
| 85 | | |
| 86 | | bool _done; |
| 87 | | osg::ref_ptr<osgGA::EventQueue> _eventQueue; |
| 88 | | }; |
| 89 | | |
| 90 | | class MyActionAdapter : public osgGA::GUIActionAdapter, public osg::Referenced |
| 91 | | { |
| 92 | | public: |
| 93 | | // Override from GUIActionAdapter |
| 94 | | virtual void requestRedraw() {} |
| 95 | | |
| 96 | | // Override from GUIActionAdapter |
| 97 | | virtual void requestContinuousUpdate(bool =true) {} |
| 98 | | |
| 99 | | // Override from GUIActionAdapter |
| 100 | | virtual void requestWarpPointer(float ,float ) {} |
| 101 | | |
| 102 | | }; |
| 103 | | |
| 104 | | // ----------- End of glue classes to adapter Producer's keyboard mouse events to osgGA's abstraction events. |
| 105 | | |
| | 25 | #include <iostream> |
| | 58 | class ExitHandler : public osgGA::GUIEventHandler |
| | 59 | { |
| | 60 | public: |
| | 61 | |
| | 62 | ExitHandler(): |
| | 63 | _done(false) {} |
| | 64 | |
| | 65 | bool done() const { return _done; } |
| | 66 | |
| | 67 | bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter&) |
| | 68 | { |
| | 69 | switch(ea.getEventType()) |
| | 70 | { |
| | 71 | case(osgGA::GUIEventAdapter::KEYUP): |
| | 72 | { |
| | 73 | if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Escape) |
| | 74 | { |
| | 75 | _done = true; |
| | 76 | } |
| | 77 | return false; |
| | 78 | } |
| | 79 | case(osgGA::GUIEventAdapter::CLOSE_WINDOW): |
| | 80 | case(osgGA::GUIEventAdapter::QUIT_APPLICATION): |
| | 81 | { |
| | 82 | _done = true; |
| | 83 | } |
| | 84 | default: break; |
| | 85 | } |
| | 86 | |
| | 87 | return false; |
| | 88 | } |
| | 89 | |
| | 90 | bool _done; |
| | 91 | }; |
| | 92 | |
| | 93 | |
| | 94 | |
| 333 | | osg::ref_ptr<Producer::RenderSurface> renderSurface = new Producer::RenderSurface; |
| 334 | | renderSurface->setWindowName("osgkeyboardmouse"); |
| 335 | | renderSurface->setWindowRectangle(100,100,800,600); |
| 336 | | renderSurface->useBorder(true); |
| 337 | | renderSurface->realize(); |
| 338 | | |
| | 291 | osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits; |
| | 292 | traits->x = 200; |
| | 293 | traits->y = 200; |
| | 294 | traits->width = 800; |
| | 295 | traits->height = 600; |
| | 296 | traits->windowDecoration = true; |
| | 297 | traits->doubleBuffer = true; |
| | 298 | traits->sharedContext = 0; |
| | 299 | |
| | 300 | osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get()); |
| | 301 | osgViewer::GraphicsWindow* gw = dynamic_cast<osgViewer::GraphicsWindow*>(gc.get()); |
| | 302 | if (!gw) |
| | 303 | { |
| | 304 | osg::notify(osg::NOTICE)<<"Error: unable to create graphics window."<<std::endl; |
| | 305 | return 1; |
| | 306 | } |
| | 307 | |
| | 308 | gw->realize(); |
| | 309 | gw->makeCurrent(); |
| 359 | | |
| 360 | | // set the window dimensions |
| 361 | | viewer.getEventQueue()->getCurrentEventState()->setWindowRectangle(100,100,800,600); |
| 362 | | |
| 363 | | // set the mouse input range. |
| 364 | | // Producer defaults to using non-dimensional units, so we pass this onto osgGA, most windowing toolkits use pixel coords so use the window size instead. |
| 365 | | viewer.getEventQueue()->setUseFixedMouseInputRange(true); |
| 366 | | viewer.getEventQueue()->setMouseInputRange(-1.0, -1.0, 1.0, 1.0); |
| 367 | | |
| 368 | | // Producer has the y axis increase upwards, like OpenGL, and contary to most Windowing toolkits. |
| 369 | | // we need to construct the event queue so that it knows about this convention. |
| 370 | | viewer.getEventQueue()->getCurrentEventState()->setMouseYOrientation(osgGA::GUIEventAdapter::Y_INCREASING_UPWARDS); |
| 371 | | |
| | 327 | |
| | 328 | |
| | 329 | // add the exit handler' |
| | 330 | ExitHandler* exitHandler = new ExitHandler; |
| | 331 | viewer.addEventHandler(exitHandler); |