| 1 | |
|---|
| 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 | #include <osgGA/TrackballManipulator> |
|---|
| 14 | #include <osgDB/ReadFile> |
|---|
| 15 | #include <wx/image.h> |
|---|
| 16 | #include <wx/menu.h> |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | bool wxOsgApp::OnInit() |
|---|
| 20 | { |
|---|
| 21 | |
|---|
| 22 | MainFrame *frame = new MainFrame(NULL, wxT("wxWidgets OSG Sample"), |
|---|
| 23 | wxDefaultPosition, wxDefaultSize); |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | SimpleViewerWX *viewerWindow = new SimpleViewerWX(frame, wxID_ANY, wxDefaultPosition, |
|---|
| 28 | wxSize(200, 200), wxSUNKEN_BORDER); |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 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 | |
|---|
| 42 | frame->Show(true); |
|---|
| 43 | |
|---|
| 44 | return true; |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | IMPLEMENT_APP(wxOsgApp) |
|---|
| 48 | |
|---|
| 49 | BEGIN_EVENT_TABLE(MainFrame, wxFrame) |
|---|
| 50 | EVT_IDLE(MainFrame::OnIdle) |
|---|
| 51 | END_EVENT_TABLE() |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | MainFrame::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 | |
|---|
| 61 | void MainFrame::SetSimpleViewer(SimpleViewerWX *viewer) |
|---|
| 62 | { |
|---|
| 63 | _viewerWindow = viewer; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | void MainFrame::OnIdle(wxIdleEvent &event) |
|---|
| 67 | { |
|---|
| 68 | _viewerWindow->render(); |
|---|
| 69 | event.RequestMore(); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | BEGIN_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 ) |
|---|
| 79 | END_EVENT_TABLE() |
|---|
| 80 | |
|---|
| 81 | GraphicsWindowWX::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 | |
|---|
| 86 | _oldCursor = *wxSTANDARD_CURSOR; |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | GraphicsWindowWX::~GraphicsWindowWX() |
|---|
| 90 | { |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | void GraphicsWindowWX::OnPaint( wxPaintEvent& WXUNUSED(event) ) |
|---|
| 94 | { |
|---|
| 95 | |
|---|
| 96 | wxPaintDC dc(this); |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | void GraphicsWindowWX::OnSize(wxSizeEvent& event) |
|---|
| 100 | { |
|---|
| 101 | |
|---|
| 102 | wxGLCanvas::OnSize(event); |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | int width, height; |
|---|
| 106 | GetClientSize(&width, &height); |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | getEventQueue()->windowResize(0, 0, width, height); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | void GraphicsWindowWX::OnEraseBackground(wxEraseEvent& WXUNUSED(event)) |
|---|
| 113 | { |
|---|
| 114 | |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | void GraphicsWindowWX::OnKeyDown(wxKeyEvent &event) |
|---|
| 118 | { |
|---|
| 119 | int key = event.GetKeyCode(); |
|---|
| 120 | getEventQueue()->keyPress(key); |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | event.Skip(); |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | void GraphicsWindowWX::OnKeyUp(wxKeyEvent &event) |
|---|
| 127 | { |
|---|
| 128 | int key = event.GetKeyCode(); |
|---|
| 129 | getEventQueue()->keyRelease(key); |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | event.Skip(); |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | void 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 | |
|---|
| 150 | void GraphicsWindowWX::grabFocus() |
|---|
| 151 | { |
|---|
| 152 | |
|---|
| 153 | SetFocus(); |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | void GraphicsWindowWX::grabFocusIfPointerInWindow() |
|---|
| 157 | { |
|---|
| 158 | |
|---|
| 159 | wxPoint pos = wxGetMousePosition(); |
|---|
| 160 | if (this == wxFindWindowAtPoint(pos)) { |
|---|
| 161 | SetFocus(); |
|---|
| 162 | } |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | void GraphicsWindowWX::useCursor(bool cursorOn) |
|---|
| 166 | { |
|---|
| 167 | if (cursorOn) { |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | SetCursor(_oldCursor); |
|---|
| 171 | } |
|---|
| 172 | else { |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | _oldCursor = GetCursor(); |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 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 | |
|---|
| 187 | bool GraphicsWindowWX::makeCurrentImplementation() |
|---|
| 188 | { |
|---|
| 189 | SetCurrent(); |
|---|
| 190 | return true; |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | void GraphicsWindowWX::swapBuffersImplementation() |
|---|
| 194 | { |
|---|
| 195 | SwapBuffers(); |
|---|
| 196 | } |
|---|