root/OpenSceneGraph/trunk/examples/osgviewerWX/osgsimpleviewerWX.cpp @ 6812

Revision 6725, 4.8 kB (checked in by robert, 7 years ago)

Reverting Luigi's addition of argv[1] as it breaks the build.

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 "osgsimpleviewerWX.h"
13#include <osgGA/TrackballManipulator>
14#include <osgDB/ReadFile>
15#include <wx/image.h>
16#include <wx/menu.h>
17
18// `Main program' equivalent, creating windows and returning main app frame
19bool wxOsgApp::OnInit()
20{
21    // Create the main frame window
22    MainFrame *frame = new MainFrame(NULL, wxT("wxWidgets OSG Sample"),
23        wxDefaultPosition, wxDefaultSize);
24
25    // create osg canvas
26    //    - initialize
27    SimpleViewerWX *viewerWindow = new SimpleViewerWX(frame, wxID_ANY, wxDefaultPosition,
28        wxSize(200, 200), wxSUNKEN_BORDER);
29       
30    // load the scene.
31    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile("cow.osg");
32    if (!loadedModel)
33    {
34        return false;
35    }
36    viewerWindow->setSceneData(loadedModel.get());
37    viewerWindow->setCameraManipulator(new osgGA::TrackballManipulator);
38
39    frame->SetSimpleViewer(viewerWindow);
40
41    /* Show the frame */
42    frame->Show(true);
43
44    return true;
45}
46
47IMPLEMENT_APP(wxOsgApp)
48
49BEGIN_EVENT_TABLE(MainFrame, wxFrame)
50    EVT_IDLE(MainFrame::OnIdle)
51END_EVENT_TABLE()
52
53/* My frame constructor */
54MainFrame::MainFrame(wxFrame *frame, const wxString& title, const wxPoint& pos,
55    const wxSize& size, long style)
56    : wxFrame(frame, wxID_ANY, title, pos, size, style)
57{
58    _viewerWindow = NULL;
59}
60
61void MainFrame::SetSimpleViewer(SimpleViewerWX *viewer)
62{
63    _viewerWindow = viewer;
64}
65
66void MainFrame::OnIdle(wxIdleEvent &event)
67{
68    _viewerWindow->render();
69    event.RequestMore();
70}
71
72BEGIN_EVENT_TABLE(GraphicsWindowWX, wxGLCanvas)
73    EVT_SIZE            (GraphicsWindowWX::OnSize            )
74    EVT_PAINT            (GraphicsWindowWX::OnPaint            )
75    EVT_ERASE_BACKGROUND(GraphicsWindowWX::OnEraseBackground)
76    EVT_KEY_DOWN        (GraphicsWindowWX::OnKeyDown        )
77    EVT_KEY_UP            (GraphicsWindowWX::OnKeyUp            )
78    EVT_MOUSE_EVENTS    (GraphicsWindowWX::OnMouse            )
79END_EVENT_TABLE()
80
81GraphicsWindowWX::GraphicsWindowWX(wxWindow *parent, wxWindowID id,
82    const wxPoint& pos, const wxSize& size, long style, const wxString& name)
83    : wxGLCanvas(parent, id, pos, size, style|wxFULL_REPAINT_ON_RESIZE, name)
84{
85    // default cursor to standard
86    _oldCursor = *wxSTANDARD_CURSOR;
87}
88
89GraphicsWindowWX::~GraphicsWindowWX()
90{
91}
92
93void GraphicsWindowWX::OnPaint( wxPaintEvent& WXUNUSED(event) )
94{
95    /* must always be here */
96    wxPaintDC dc(this);
97}
98
99void GraphicsWindowWX::OnSize(wxSizeEvent& event)
100{
101    // this is also necessary to update the context on some platforms
102    wxGLCanvas::OnSize(event);
103
104    // set GL viewport (not called by wxGLCanvas::OnSize on all platforms...)
105    int width, height;
106    GetClientSize(&width, &height);
107   
108    // update the window dimensions, in case the window has been resized.
109    getEventQueue()->windowResize(0, 0, width, height);
110}
111
112void GraphicsWindowWX::OnEraseBackground(wxEraseEvent& WXUNUSED(event))
113{
114    /* Do nothing, to avoid flashing on MSW */
115}
116
117void GraphicsWindowWX::OnKeyDown(wxKeyEvent &event)
118{
119    int key = event.GetKeyCode();
120    getEventQueue()->keyPress(key);
121
122    // propagate event
123    event.Skip();
124}
125
126void GraphicsWindowWX::OnKeyUp(wxKeyEvent &event)
127{
128    int key = event.GetKeyCode();
129    getEventQueue()->keyRelease(key);   
130
131    // propagate event
132    event.Skip();
133}
134
135void GraphicsWindowWX::OnMouse(wxMouseEvent& event)
136{
137    if (event.ButtonDown()) {
138        int button = event.GetButton();
139        getEventQueue()->mouseButtonPress(event.GetX(), event.GetY(), button);
140    }
141    else if (event.ButtonUp()) {
142        int button = event.GetButton();
143        getEventQueue()->mouseButtonRelease(event.GetX(), event.GetY(), button);
144    }
145    else if (event.Dragging()) {
146        getEventQueue()->mouseMotion(event.GetX(), event.GetY());
147    }
148}
149
150void GraphicsWindowWX::grabFocus()
151{
152    // focus this window
153    SetFocus();
154}
155
156void GraphicsWindowWX::grabFocusIfPointerInWindow()
157{
158    // focus this window, if the pointer is in the window
159    wxPoint pos = wxGetMousePosition();
160    if (this == wxFindWindowAtPoint(pos)) {
161        SetFocus();
162    }
163}
164
165void GraphicsWindowWX::useCursor(bool cursorOn)
166{
167    if (cursorOn) {
168
169        // show the old cursor
170        SetCursor(_oldCursor);
171    }
172    else {
173
174        // remember the old cursor
175        _oldCursor = GetCursor();
176
177        // hide the cursor
178        //    - can't find a way to do this neatly, so create a 1x1, transparent image
179        wxImage image(1,1);
180        image.SetMask(true);
181        image.SetMaskColour(0, 0, 0);
182        wxCursor cursor(image);
183        SetCursor(cursor);
184    }
185}
186
187bool GraphicsWindowWX::makeCurrentImplementation()
188{
189    SetCurrent();
190    return true;
191}
192
193void GraphicsWindowWX::swapBuffersImplementation()
194{
195    SwapBuffers();
196}
Note: See TracBrowser for help on using the browser.