Show
Ignore:
Timestamp:
05/26/08 23:09:54 (7 years ago)
Author:
robert
Message:

From Paul Melis, "Here is an update to the osgviewerWX example. Keyboard events were not always received because the GraphicsWindowWX wasn't receiving focus. It now receives focus when the mouse enters the window.
* I split the mouse handling from a monolithic method to separate ones, slightly cleaner than a whole bunch of if()'s, especially with another case of the mouse entering the canvas.
* I changed the EVT_KEY_DOWN handler to an EVT_CHAR handler, although that now makes the up and down handler assymetric. The new down-handler returns translated key codes, so when you press the S key (without anything else), it actually returns 's' and not 'S' as the EVT_KEY_DOWN did. This means that statistics can be called up in the viewer window, while the example previously only printed a "Stats output:" line to the console. I'm not truly happy that the up handler returns _untranslated_ key codes. But solving this completely would probably mean adding some table that translated from wxWidgets' untranslated key codes to OSG's internal ones. This might be interesting to add, as anyone using OSG + wxWidgets in any serious manner would also have to add this.
* I commented out the evt.Skip()'s in the keyboard handlers as these would only be necessary if there were some key events that are not handled. But currently all key events are simply forwarded.
* I changed the handling of a mouse drag to a more general mouse move"

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • OpenSceneGraph/trunk/examples/osgviewerWX/osgviewerWX.cpp

    r7950 r8350  
    6969    viewer->setSceneData(loadedModel.get()); 
    7070    viewer->setCameraManipulator(new osgGA::TrackballManipulator); 
    71  
    7271    frame->SetViewer(viewer); 
    7372 
     
    104103 
    105104BEGIN_EVENT_TABLE(GraphicsWindowWX, wxGLCanvas) 
    106     EVT_SIZE            (GraphicsWindowWX::OnSize            ) 
    107     EVT_PAINT            (GraphicsWindowWX::OnPaint            ) 
    108     EVT_ERASE_BACKGROUND(GraphicsWindowWX::OnEraseBackground) 
    109     EVT_KEY_DOWN        (GraphicsWindowWX::OnKeyDown        ) 
    110     EVT_KEY_UP            (GraphicsWindowWX::OnKeyUp            ) 
    111     EVT_MOUSE_EVENTS    (GraphicsWindowWX::OnMouse            ) 
     105    EVT_SIZE                (GraphicsWindowWX::OnSize) 
     106    EVT_PAINT               (GraphicsWindowWX::OnPaint) 
     107    EVT_ERASE_BACKGROUND    (GraphicsWindowWX::OnEraseBackground) 
     108 
     109    EVT_CHAR                (GraphicsWindowWX::OnChar) 
     110    EVT_KEY_UP              (GraphicsWindowWX::OnKeyUp) 
     111 
     112    EVT_ENTER_WINDOW    (GraphicsWindowWX::OnMouseEnter) 
     113    EVT_LEFT_DOWN       (GraphicsWindowWX::OnMouseDown) 
     114    EVT_MIDDLE_DOWN     (GraphicsWindowWX::OnMouseDown) 
     115    EVT_RIGHT_DOWN      (GraphicsWindowWX::OnMouseDown) 
     116    EVT_LEFT_UP         (GraphicsWindowWX::OnMouseUp) 
     117    EVT_MIDDLE_UP       (GraphicsWindowWX::OnMouseUp) 
     118    EVT_RIGHT_UP        (GraphicsWindowWX::OnMouseUp) 
     119    EVT_MOTION          (GraphicsWindowWX::OnMouseMotion) 
    112120END_EVENT_TABLE() 
    113121 
     
    177185} 
    178186 
    179 void GraphicsWindowWX::OnKeyDown(wxKeyEvent &event) 
     187void GraphicsWindowWX::OnChar(wxKeyEvent &event) 
    180188{ 
    181189#if wxUSE_UNICODE 
     
    186194    getEventQueue()->keyPress(key); 
    187195 
    188     // propagate event 
    189     event.Skip(); 
     196    // If this key event is not processed here, we should call 
     197    // event.Skip() to allow processing to continue. 
    190198} 
    191199 
     
    199207    getEventQueue()->keyRelease(key); 
    200208 
    201     // propagate event 
    202     event.Skip(); 
    203 } 
    204  
    205 void GraphicsWindowWX::OnMouse(wxMouseEvent& event) 
    206 { 
    207     if (event.ButtonDown()) { 
    208         int button = event.GetButton(); 
    209         getEventQueue()->mouseButtonPress(event.GetX(), event.GetY(), button); 
    210     } 
    211     else if (event.ButtonUp()) { 
    212         int button = event.GetButton(); 
    213         getEventQueue()->mouseButtonRelease(event.GetX(), event.GetY(), button); 
    214     } 
    215     else if (event.Dragging()) { 
    216         getEventQueue()->mouseMotion(event.GetX(), event.GetY()); 
    217     } 
    218 } 
     209    // If this key event is not processed here, we should call 
     210    // event.Skip() to allow processing to continue. 
     211} 
     212 
     213void GraphicsWindowWX::OnMouseEnter(wxMouseEvent &event) 
     214{ 
     215    // Set focus to ourselves, so keyboard events get directed to us 
     216    SetFocus(); 
     217} 
     218 
     219void GraphicsWindowWX::OnMouseDown(wxMouseEvent &event) 
     220{ 
     221    getEventQueue()->mouseButtonPress(event.GetX(), event.GetY(), 
     222        event.GetButton()); 
     223} 
     224 
     225void GraphicsWindowWX::OnMouseUp(wxMouseEvent &event) 
     226{ 
     227    getEventQueue()->mouseButtonRelease(event.GetX(), event.GetY(), 
     228        event.GetButton()); 
     229} 
     230 
     231void GraphicsWindowWX::OnMouseMotion(wxMouseEvent &event) 
     232{ 
     233    getEventQueue()->mouseMotion(event.GetX(), event.GetY()); 
     234} 
     235 
    219236 
    220237void GraphicsWindowWX::grabFocus()