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"
This commit is contained in:
Robert Osfield 2008-05-26 21:09:54 +00:00
parent 189049f9bd
commit ae303e38e9
2 changed files with 52 additions and 29 deletions

View File

@ -68,7 +68,6 @@ bool wxOsgApp::OnInit()
viewer->setSceneData(loadedModel.get());
viewer->setCameraManipulator(new osgGA::TrackballManipulator);
frame->SetViewer(viewer);
/* Show the frame */
@ -103,12 +102,21 @@ void MainFrame::OnIdle(wxIdleEvent &event)
}
BEGIN_EVENT_TABLE(GraphicsWindowWX, wxGLCanvas)
EVT_SIZE (GraphicsWindowWX::OnSize )
EVT_PAINT (GraphicsWindowWX::OnPaint )
EVT_ERASE_BACKGROUND(GraphicsWindowWX::OnEraseBackground)
EVT_KEY_DOWN (GraphicsWindowWX::OnKeyDown )
EVT_KEY_UP (GraphicsWindowWX::OnKeyUp )
EVT_MOUSE_EVENTS (GraphicsWindowWX::OnMouse )
EVT_SIZE (GraphicsWindowWX::OnSize)
EVT_PAINT (GraphicsWindowWX::OnPaint)
EVT_ERASE_BACKGROUND (GraphicsWindowWX::OnEraseBackground)
EVT_CHAR (GraphicsWindowWX::OnChar)
EVT_KEY_UP (GraphicsWindowWX::OnKeyUp)
EVT_ENTER_WINDOW (GraphicsWindowWX::OnMouseEnter)
EVT_LEFT_DOWN (GraphicsWindowWX::OnMouseDown)
EVT_MIDDLE_DOWN (GraphicsWindowWX::OnMouseDown)
EVT_RIGHT_DOWN (GraphicsWindowWX::OnMouseDown)
EVT_LEFT_UP (GraphicsWindowWX::OnMouseUp)
EVT_MIDDLE_UP (GraphicsWindowWX::OnMouseUp)
EVT_RIGHT_UP (GraphicsWindowWX::OnMouseUp)
EVT_MOTION (GraphicsWindowWX::OnMouseMotion)
END_EVENT_TABLE()
GraphicsWindowWX::GraphicsWindowWX(wxWindow *parent, wxWindowID id,
@ -176,7 +184,7 @@ void GraphicsWindowWX::OnEraseBackground(wxEraseEvent& WXUNUSED(event))
/* Do nothing, to avoid flashing on MSW */
}
void GraphicsWindowWX::OnKeyDown(wxKeyEvent &event)
void GraphicsWindowWX::OnChar(wxKeyEvent &event)
{
#if wxUSE_UNICODE
int key = event.GetUnicodeKey();
@ -185,8 +193,8 @@ void GraphicsWindowWX::OnKeyDown(wxKeyEvent &event)
#endif
getEventQueue()->keyPress(key);
// propagate event
event.Skip();
// If this key event is not processed here, we should call
// event.Skip() to allow processing to continue.
}
void GraphicsWindowWX::OnKeyUp(wxKeyEvent &event)
@ -198,25 +206,34 @@ void GraphicsWindowWX::OnKeyUp(wxKeyEvent &event)
#endif
getEventQueue()->keyRelease(key);
// propagate event
event.Skip();
// If this key event is not processed here, we should call
// event.Skip() to allow processing to continue.
}
void GraphicsWindowWX::OnMouse(wxMouseEvent& event)
void GraphicsWindowWX::OnMouseEnter(wxMouseEvent &event)
{
if (event.ButtonDown()) {
int button = event.GetButton();
getEventQueue()->mouseButtonPress(event.GetX(), event.GetY(), button);
}
else if (event.ButtonUp()) {
int button = event.GetButton();
getEventQueue()->mouseButtonRelease(event.GetX(), event.GetY(), button);
}
else if (event.Dragging()) {
getEventQueue()->mouseMotion(event.GetX(), event.GetY());
}
// Set focus to ourselves, so keyboard events get directed to us
SetFocus();
}
void GraphicsWindowWX::OnMouseDown(wxMouseEvent &event)
{
getEventQueue()->mouseButtonPress(event.GetX(), event.GetY(),
event.GetButton());
}
void GraphicsWindowWX::OnMouseUp(wxMouseEvent &event)
{
getEventQueue()->mouseButtonRelease(event.GetX(), event.GetY(),
event.GetButton());
}
void GraphicsWindowWX::OnMouseMotion(wxMouseEvent &event)
{
getEventQueue()->mouseMotion(event.GetX(), event.GetY());
}
void GraphicsWindowWX::grabFocus()
{
// focus this window

View File

@ -18,15 +18,21 @@ public:
int *attributes = 0);
~GraphicsWindowWX();
void init();
void OnPaint(wxPaintEvent& event);
void OnSize(wxSizeEvent& event);
void OnEraseBackground(wxEraseEvent& event);
void OnKeyDown(wxKeyEvent &event);
void OnChar(wxKeyEvent &event);
void OnKeyUp(wxKeyEvent &event);
void OnMouse(wxMouseEvent &event);
void OnMouseEnter(wxMouseEvent &event);
void OnMouseDown(wxMouseEvent &event);
void OnMouseUp(wxMouseEvent &event);
void OnMouseMotion(wxMouseEvent &event);
//
// GraphicsWindow interface
@ -38,7 +44,7 @@ public:
bool makeCurrentImplementation();
void swapBuffersImplementation();
// note implemented yet...just use dummy implementation to get working.
// not implemented yet...just use dummy implementation to get working.
virtual bool valid() const { return true; }
virtual bool realizeImplementation() { return true; }
virtual bool isRealizedImplementation() const { return true; }
@ -54,7 +60,7 @@ private:
class MainFrame : public wxFrame
{
public:
MainFrame(wxFrame *frame, const wxString& title, const wxPoint& pos,
MainFrame(wxFrame *frame, const wxString& title, const wxPoint& pos,
const wxSize& size, long style = wxDEFAULT_FRAME_STYLE);
void SetViewer(osgViewer::Viewer *viewer);