root/OpenSceneGraph/trunk/examples/osgviewerWX/osgviewerWX.cpp @ 6815

Revision 6815, 6.0 kB (checked in by robert, 7 years ago)

Added osgViewer::GraphicsWindowEmbedded? and ported GLUT, SDL and WX examples across from SimpleViewer? to Viewer.

Line 
1// For compilers that support precompilation, includes "wx.h".
2#include "wx/wxprec.h"
3
4#ifdef __BORLANDC__
5#pragma hdrstop
6#endif
7
8#ifndef WX_PRECOMP
9#include "wx/wx.h"
10#endif
11
12#include "osgviewerWX.h"
13
14
15#include <osgViewer/StatsHandler>
16#include <osgGA/TrackballManipulator>
17#include <osgDB/ReadFile>
18#include <wx/image.h>
19#include <wx/menu.h>
20
21// `Main program' equivalent, creating windows and returning main app frame
22bool wxOsgApp::OnInit()
23{
24    // Create the main frame window
25    MainFrame *frame = new MainFrame(NULL, wxT("wxWidgets OSG Sample"),
26        wxDefaultPosition, wxDefaultSize);
27
28    // create osg canvas
29    //    - initialize
30   
31
32    int width = 800;
33    int height = 600;
34
35    GraphicsWindowWX* gw = new GraphicsWindowWX(frame, wxID_ANY, wxDefaultPosition,
36                                                wxSize(width, height), wxSUNKEN_BORDER);
37   
38   
39    osgViewer::Viewer *viewer = new osgViewer::Viewer;
40    viewer->getCamera()->setGraphicsContext(gw);
41    viewer->getCamera()->setViewport(0,0,width,height);
42    viewer->addEventHandler(new osgViewer::StatsHandler);
43    viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);
44       
45    // load the scene.
46    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile("cow.osg");
47    if (!loadedModel)
48    {
49        return false;
50    }
51   
52    viewer->setSceneData(loadedModel.get());
53    viewer->setCameraManipulator(new osgGA::TrackballManipulator);
54
55    frame->SetViewer(viewer);
56
57    /* Show the frame */
58    frame->Show(true);
59
60    return true;
61}
62
63IMPLEMENT_APP(wxOsgApp)
64
65BEGIN_EVENT_TABLE(MainFrame, wxFrame)
66    EVT_IDLE(MainFrame::OnIdle)
67END_EVENT_TABLE()
68
69/* My frame constructor */
70MainFrame::MainFrame(wxFrame *frame, const wxString& title, const wxPoint& pos,
71    const wxSize& size, long style)
72    : wxFrame(frame, wxID_ANY, title, pos, size, style)
73{
74}
75
76void MainFrame::SetViewer(osgViewer::Viewer *viewer)
77{
78    _viewer = viewer;
79}
80
81void MainFrame::OnIdle(wxIdleEvent &event)
82{
83    _viewer->frame();
84   
85    event.RequestMore();
86}
87
88BEGIN_EVENT_TABLE(GraphicsWindowWX, wxGLCanvas)
89    EVT_SIZE            (GraphicsWindowWX::OnSize            )
90    EVT_PAINT            (GraphicsWindowWX::OnPaint            )
91    EVT_ERASE_BACKGROUND(GraphicsWindowWX::OnEraseBackground)
92    EVT_KEY_DOWN        (GraphicsWindowWX::OnKeyDown        )
93    EVT_KEY_UP            (GraphicsWindowWX::OnKeyUp            )
94    EVT_MOUSE_EVENTS    (GraphicsWindowWX::OnMouse            )
95END_EVENT_TABLE()
96
97GraphicsWindowWX::GraphicsWindowWX(wxWindow *parent, wxWindowID id,
98    const wxPoint& pos, const wxSize& size, long style, const wxString& name)
99    : wxGLCanvas(parent, id, pos, size, style|wxFULL_REPAINT_ON_RESIZE, name)
100{
101    // default cursor to standard
102    _oldCursor = *wxSTANDARD_CURSOR;
103   
104    _traits = new GraphicsContext::Traits;
105    _traits->x = pos.x;
106    _traits->y = pos.y;
107    _traits->width = size.x;
108    _traits->height = size.y;
109
110    init();
111   
112}
113
114void GraphicsWindowWX::init()
115{
116    if (valid())
117    {
118        setState( new osg::State );
119        getState()->setGraphicsContext(this);
120
121        if (_traits.valid() && _traits->sharedContext)
122        {
123            getState()->setContextID( _traits->sharedContext->getState()->getContextID() );
124            incrementContextIDUsageCount( getState()->getContextID() );   
125        }
126        else
127        {
128            getState()->setContextID( osg::GraphicsContext::createNewContextID() );
129        }
130    }
131}
132
133GraphicsWindowWX::~GraphicsWindowWX()
134{
135}
136
137void GraphicsWindowWX::OnPaint( wxPaintEvent& WXUNUSED(event) )
138{
139    /* must always be here */
140    wxPaintDC dc(this);
141}
142
143void GraphicsWindowWX::OnSize(wxSizeEvent& event)
144{
145    // this is also necessary to update the context on some platforms
146    wxGLCanvas::OnSize(event);
147
148    // set GL viewport (not called by wxGLCanvas::OnSize on all platforms...)
149    int width, height;
150    GetClientSize(&width, &height);
151   
152    // update the window dimensions, in case the window has been resized.
153    getEventQueue()->windowResize(0, 0, width, height);
154    resized(0,0,width,height);
155}
156
157void GraphicsWindowWX::OnEraseBackground(wxEraseEvent& WXUNUSED(event))
158{
159    /* Do nothing, to avoid flashing on MSW */
160}
161
162void GraphicsWindowWX::OnKeyDown(wxKeyEvent &event)
163{
164#if 1
165    int key = event.GetUnicodeKey();
166#else
167    int key = event.GetKeyCode();
168#endif
169    getEventQueue()->keyPress(key);   
170
171    // propagate event
172    event.Skip();
173}
174
175void GraphicsWindowWX::OnKeyUp(wxKeyEvent &event)
176{
177#if 1
178    int key = event.GetUnicodeKey();
179#else
180    int key = event.GetKeyCode();
181#endif
182    getEventQueue()->keyRelease(key);   
183
184    // propagate event
185    event.Skip();
186}
187
188void GraphicsWindowWX::OnMouse(wxMouseEvent& event)
189{
190    if (event.ButtonDown()) {
191        int button = event.GetButton();
192        getEventQueue()->mouseButtonPress(event.GetX(), event.GetY(), button);
193    }
194    else if (event.ButtonUp()) {
195        int button = event.GetButton();
196        getEventQueue()->mouseButtonRelease(event.GetX(), event.GetY(), button);
197    }
198    else if (event.Dragging()) {
199        getEventQueue()->mouseMotion(event.GetX(), event.GetY());
200    }
201}
202
203void GraphicsWindowWX::grabFocus()
204{
205    // focus this window
206    SetFocus();
207}
208
209void GraphicsWindowWX::grabFocusIfPointerInWindow()
210{
211    // focus this window, if the pointer is in the window
212    wxPoint pos = wxGetMousePosition();
213    if (this == wxFindWindowAtPoint(pos)) {
214        SetFocus();
215    }
216}
217
218void GraphicsWindowWX::useCursor(bool cursorOn)
219{
220    if (cursorOn) {
221
222        // show the old cursor
223        SetCursor(_oldCursor);
224    }
225    else {
226
227        // remember the old cursor
228        _oldCursor = GetCursor();
229
230        // hide the cursor
231        //    - can't find a way to do this neatly, so create a 1x1, transparent image
232        wxImage image(1,1);
233        image.SetMask(true);
234        image.SetMaskColour(0, 0, 0);
235        wxCursor cursor(image);
236        SetCursor(cursor);
237    }
238}
239
240bool GraphicsWindowWX::makeCurrentImplementation()
241{
242    SetCurrent();
243    return true;
244}
245
246void GraphicsWindowWX::swapBuffersImplementation()
247{
248    SwapBuffers();
249}
Note: See TracBrowser for help on using the browser.