| 1 | /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield |
|---|
| 2 | * |
|---|
| 3 | * This library is open source and may be redistributed and/or modified under |
|---|
| 4 | * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or |
|---|
| 5 | * (at your option) any later version. The full license is in LICENSE file |
|---|
| 6 | * included with this distribution, and on the openscenegraph.org website. |
|---|
| 7 | * |
|---|
| 8 | * This library is distributed in the hope that it will be useful, |
|---|
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 11 | * OpenSceneGraph Public License for more details. |
|---|
| 12 | */ |
|---|
| 13 | |
|---|
| 14 | #ifndef OSGVIEWER_GRAPHICWINDOW |
|---|
| 15 | #define OSGVIEWER_GRAPHICWINDOW 1 |
|---|
| 16 | |
|---|
| 17 | #include <osg/GraphicsContext> |
|---|
| 18 | #include <osg/Notify> |
|---|
| 19 | |
|---|
| 20 | #include <osgGA/EventQueue> |
|---|
| 21 | #include <osgGA/GUIActionAdapter> |
|---|
| 22 | |
|---|
| 23 | #include <osgViewer/Export> |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | extern "C" |
|---|
| 27 | { |
|---|
| 28 | typedef void (* CGraphicsWindowFunction) (void); |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | namespace osgViewer { |
|---|
| 32 | |
|---|
| 33 | class View; |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | /** Base class for providing Windowing API agnostic access to creating and managing graphics window and events. |
|---|
| 37 | * Note, the GraphicsWindow is subclassed from osg::GraphicsContext, and to provide an implemention you'll need to implement its |
|---|
| 38 | * range of pure virtual functions, you'll find these all have naming convention methodNameImplemention(..). |
|---|
| 39 | * GraphicsWindow adds the event queue on top of the GraphicsContext, thereby adding a mechanism for adapting Windowing events |
|---|
| 40 | * as well as basics graphics context work, you should wire up custom GraphicsWindowImplementation to push their events through |
|---|
| 41 | * into the EventQueue. */ |
|---|
| 42 | class OSGVIEWER_EXPORT GraphicsWindow : public osg::GraphicsContext, public osgGA::GUIActionAdapter |
|---|
| 43 | { |
|---|
| 44 | public: |
|---|
| 45 | |
|---|
| 46 | GraphicsWindow() { _eventQueue = new osgGA::EventQueue; _eventQueue->setGraphicsContext(this); } |
|---|
| 47 | |
|---|
| 48 | virtual bool isSameKindAs(const Object* object) const { return dynamic_cast<const GraphicsWindow*>(object)!=0; } |
|---|
| 49 | virtual const char* libraryName() const { return "osgViewer"; } |
|---|
| 50 | virtual const char* className() const { return "GraphicsWindow"; } |
|---|
| 51 | |
|---|
| 52 | void setEventQueue(osgGA::EventQueue* eventQueue) { _eventQueue = eventQueue; } |
|---|
| 53 | osgGA::EventQueue* getEventQueue() { return _eventQueue.get(); } |
|---|
| 54 | const osgGA::EventQueue* getEventQueue() const { return _eventQueue.get(); } |
|---|
| 55 | |
|---|
| 56 | virtual void checkEvents() {} |
|---|
| 57 | |
|---|
| 58 | /** Set the window's position and size.*/ |
|---|
| 59 | void setWindowRectangle(int x, int y, int width, int height) |
|---|
| 60 | { |
|---|
| 61 | if (setWindowRectangleImplementation(x ,y ,width, height) && _traits.valid()) |
|---|
| 62 | { |
|---|
| 63 | resized(x,y,width,height); |
|---|
| 64 | } |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | /** implementation of setWindowRectangle, should be implemented by derived classes */ |
|---|
| 68 | virtual bool setWindowRectangleImplementation(int /*x*/, int /*y*/, int /*width*/, int /*height*/) { osg::notify(osg::NOTICE)<<"GraphicsWindow::setWindowRectangleImplementation(..) not implemented."<<std::endl; return false; } |
|---|
| 69 | |
|---|
| 70 | /** Get the window's position and size.*/ |
|---|
| 71 | virtual void getWindowRectangle(int& x, int& y, int& width, int& height) { if (_traits.valid()) { x = _traits->x; y = _traits->y; width = _traits->width; height = _traits->height; } } |
|---|
| 72 | |
|---|
| 73 | /** Set Window decoration.*/ |
|---|
| 74 | void setWindowDecoration(bool flag) |
|---|
| 75 | { |
|---|
| 76 | if (setWindowDecorationImplementation(flag) && _traits.valid()) |
|---|
| 77 | { |
|---|
| 78 | _traits->windowDecoration = flag; |
|---|
| 79 | } |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | /** implementation of setWindowDecoration, should be implemented by derived classes */ |
|---|
| 83 | virtual bool setWindowDecorationImplementation(bool /*flag*/) { osg::notify(osg::NOTICE)<<"GraphicsWindow::setWindowDecorationImplementation(..) not implemented."<<std::endl; return false; } |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | /** Set Window decoration.*/ |
|---|
| 87 | virtual bool getWindowDecoration() const { return _traits.valid() ? _traits->windowDecoration : false; } |
|---|
| 88 | |
|---|
| 89 | /** Get focus.*/ |
|---|
| 90 | virtual void grabFocus() { osg::notify(osg::NOTICE)<<"GraphicsWindow::grabFocus(..) not implemented."<<std::endl; } |
|---|
| 91 | |
|---|
| 92 | /** Get focus on if the pointer is in this window.*/ |
|---|
| 93 | virtual void grabFocusIfPointerInWindow() { osg::notify(osg::NOTICE)<<"GraphicsWindow::grabFocusIfPointerInWindow(..) not implemented."<<std::endl; } |
|---|
| 94 | |
|---|
| 95 | /** Raise the window to the top.*/ |
|---|
| 96 | virtual void raiseWindow() { osg::notify(osg::NOTICE)<<"GraphicsWindow::raiseWindow(..) not implemented."<<std::endl; } |
|---|
| 97 | |
|---|
| 98 | /** Mouse cursor types, the same ones already present with ancient glut ... */ |
|---|
| 99 | enum MouseCursor { |
|---|
| 100 | InheritCursor, |
|---|
| 101 | NoCursor, |
|---|
| 102 | RightArrowCursor, |
|---|
| 103 | LeftArrowCursor, |
|---|
| 104 | InfoCursor, |
|---|
| 105 | DestroyCursor, |
|---|
| 106 | HelpCursor, |
|---|
| 107 | CycleCursor, |
|---|
| 108 | SprayCursor, |
|---|
| 109 | WaitCursor, |
|---|
| 110 | TextCursor, |
|---|
| 111 | CrosshairCursor, |
|---|
| 112 | HandCursor, |
|---|
| 113 | UpDownCursor, |
|---|
| 114 | LeftRightCursor, |
|---|
| 115 | TopSideCursor, |
|---|
| 116 | BottomSideCursor, |
|---|
| 117 | LeftSideCursor, |
|---|
| 118 | RightSideCursor, |
|---|
| 119 | TopLeftCorner, |
|---|
| 120 | TopRightCorner, |
|---|
| 121 | BottomRightCorner, |
|---|
| 122 | BottomLeftCorner |
|---|
| 123 | }; |
|---|
| 124 | |
|---|
| 125 | /** Set the name of the window */ |
|---|
| 126 | virtual void setWindowName(const std::string& /*name*/) { osg::notify(osg::NOTICE)<<"GraphicsWindow::setWindowName(..) not implemented."<<std::endl; } |
|---|
| 127 | |
|---|
| 128 | /** Return the name of the window */ |
|---|
| 129 | virtual std::string getWindowName() { return _traits.valid() ? _traits->windowName : ""; } |
|---|
| 130 | |
|---|
| 131 | /** Switch on/off the cursor.*/ |
|---|
| 132 | virtual void useCursor(bool cursorOn) { setCursor(cursorOn ? InheritCursor : NoCursor); } |
|---|
| 133 | |
|---|
| 134 | /** Set mouse cursor to a specific shape.*/ |
|---|
| 135 | virtual void setCursor(MouseCursor /*mouseCursor*/) { osg::notify(osg::NOTICE)<<"GraphicsWindow::setCursor(..) not implemented."<<std::endl; } |
|---|
| 136 | |
|---|
| 137 | /** Create a new mouse cursor from the usual bitmap data.*/ |
|---|
| 138 | //virtual MouseCursor createCursor(const char *data, const char *mask, unsigned w, unsigned h, unsigned hotx, unsigned hoty) { osg::notify(osg::NOTICE)<<"GraphicsWindow::createCursor(..) not implemented."<<std::endl; } |
|---|
| 139 | |
|---|
| 140 | /** Set sync-to-vblank. */ |
|---|
| 141 | virtual void setSyncToVBlank(bool on) |
|---|
| 142 | { |
|---|
| 143 | osg::notify(osg::NOTICE) << "GraphicsWindow::setSyncToVBlank(" << on << ") not implemented." << std::endl; |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | bool getSyncToVBlank() const { return _traits.valid() ? _traits->vsync : true; } |
|---|
| 147 | |
|---|
| 148 | /** Set swap group. */ |
|---|
| 149 | virtual void setSwapGroup(bool on, GLuint group, GLuint barrier) |
|---|
| 150 | { |
|---|
| 151 | osg::notify(osg::NOTICE) << "GraphicsWindow::setSwapGroup(" << on << " " << group << " " << barrier << ") not implemented." << std::endl; |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | void getSwapGroup(bool& on, GLuint& group, GLuint& barrier) const { on = _traits->swapGroupEnabled; group = _traits->swapGroup; barrier = _traits->swapBarrier; } |
|---|
| 155 | |
|---|
| 156 | public: |
|---|
| 157 | |
|---|
| 158 | /** Return whether a valid and usable GraphicsContext has been created.*/ |
|---|
| 159 | virtual bool valid() const { osg::notify(osg::NOTICE)<<"GraphicsWindow::valid() not implemented."<<std::endl; return false; } |
|---|
| 160 | |
|---|
| 161 | /** Realize the GraphicsContext implementation, |
|---|
| 162 | * Pure virtual - must be implemented by concrete implementations of GraphicsContext. */ |
|---|
| 163 | virtual bool realizeImplementation() { osg::notify(osg::NOTICE)<<"GraphicsWindow::realizeImplementation() not implemented."<<std::endl; return false; } |
|---|
| 164 | |
|---|
| 165 | /** Return true if the graphics context has been realized, and is ready to use, implementation. |
|---|
| 166 | * Pure virtual - must be implemented by concrete implementations of GraphicsContext. */ |
|---|
| 167 | virtual bool isRealizedImplementation() const { osg::notify(osg::NOTICE)<<"GraphicsWindow::isRealizedImplementation() not implemented."<<std::endl; return false; } |
|---|
| 168 | |
|---|
| 169 | /** Close the graphics context implementation. |
|---|
| 170 | * Pure virtual - must be implemented by concrete implementations of GraphicsContext. */ |
|---|
| 171 | virtual void closeImplementation() { osg::notify(osg::NOTICE)<<"GraphicsWindow::closeImplementation() not implemented."<<std::endl; } |
|---|
| 172 | |
|---|
| 173 | /** Make this graphics context current implementation. |
|---|
| 174 | * Pure virtual - must be implemented by concrete implementations of GraphicsContext. */ |
|---|
| 175 | virtual bool makeCurrentImplementation() { osg::notify(osg::NOTICE)<<"GraphicsWindow::makeCurrentImplementation() not implemented."<<std::endl; return false;} |
|---|
| 176 | |
|---|
| 177 | /** Make this graphics context current with specified read context implementation. |
|---|
| 178 | * Pure virtual - must be implemented by concrete implementations of GraphicsContext. */ |
|---|
| 179 | virtual bool makeContextCurrentImplementation(GraphicsContext* /*readContext*/) { osg::notify(osg::NOTICE)<<"GraphicsWindow::makeContextCurrentImplementation(..) not implemented."<<std::endl; return false;} |
|---|
| 180 | |
|---|
| 181 | /** Release the graphics context.*/ |
|---|
| 182 | virtual bool releaseContextImplementation() { osg::notify(osg::NOTICE)<<"GraphicsWindow::releaseContextImplementation(..) not implemented."<<std::endl; return false; } |
|---|
| 183 | |
|---|
| 184 | /** Pure virtual, Bind the graphics context to associated texture implementation. |
|---|
| 185 | * Pure virtual - must be implemented by concrete implementations of GraphicsContext. */ |
|---|
| 186 | virtual void bindPBufferToTextureImplementation(GLenum /*buffer*/) { osg::notify(osg::NOTICE)<<"GraphicsWindow::bindPBufferToTextureImplementation(..) not implemented."<<std::endl; } |
|---|
| 187 | |
|---|
| 188 | /** Swap the front and back buffers implementation. |
|---|
| 189 | * Pure virtual - must be implemented by concrete implementations of GraphicsContext. */ |
|---|
| 190 | virtual void swapBuffersImplementation() { osg::notify(osg::NOTICE)<<"GraphicsWindow:: swapBuffersImplementation() not implemented."<<std::endl; } |
|---|
| 191 | |
|---|
| 192 | public: |
|---|
| 193 | |
|---|
| 194 | typedef std::list<osgViewer::View*> Views; |
|---|
| 195 | /** Returns the list of views (osgViewer::View) attached to this GraphicsWindow. |
|---|
| 196 | * Internally, the method walks through all the cameras and collects all the views attached to the cameras.*/ |
|---|
| 197 | void getViews(Views& views); |
|---|
| 198 | |
|---|
| 199 | // Override from GUIActionAdapter |
|---|
| 200 | virtual void requestRedraw(); |
|---|
| 201 | |
|---|
| 202 | // Override from GUIActionAdapter |
|---|
| 203 | virtual void requestContinuousUpdate(bool /*needed*/=true) {} |
|---|
| 204 | |
|---|
| 205 | // Override from GUIActionAdapter |
|---|
| 206 | virtual void requestWarpPointer(float /*x*/,float /*y*/) {} |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | protected: |
|---|
| 210 | |
|---|
| 211 | osg::ref_ptr<osgGA::EventQueue> _eventQueue; |
|---|
| 212 | |
|---|
| 213 | }; |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | class GraphicsWindowEmbedded : public GraphicsWindow |
|---|
| 217 | { |
|---|
| 218 | public: |
|---|
| 219 | |
|---|
| 220 | GraphicsWindowEmbedded(osg::GraphicsContext::Traits* traits=0) |
|---|
| 221 | { |
|---|
| 222 | _traits = traits; |
|---|
| 223 | |
|---|
| 224 | init(); |
|---|
| 225 | |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | GraphicsWindowEmbedded(int x, int y, int width, int height) |
|---|
| 229 | { |
|---|
| 230 | _traits = new GraphicsContext::Traits; |
|---|
| 231 | _traits->x = x; |
|---|
| 232 | _traits->y = y; |
|---|
| 233 | _traits->width = width; |
|---|
| 234 | _traits->height = height; |
|---|
| 235 | |
|---|
| 236 | init(); |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | virtual bool isSameKindAs(const Object* object) const { return dynamic_cast<const GraphicsWindowEmbedded*>(object)!=0; } |
|---|
| 240 | virtual const char* libraryName() const { return "osgViewer"; } |
|---|
| 241 | virtual const char* className() const { return "GraphicsWindowEmbedded"; } |
|---|
| 242 | |
|---|
| 243 | void init() |
|---|
| 244 | { |
|---|
| 245 | if (valid()) |
|---|
| 246 | { |
|---|
| 247 | setState( new osg::State ); |
|---|
| 248 | getState()->setGraphicsContext(this); |
|---|
| 249 | |
|---|
| 250 | if (_traits.valid() && _traits->sharedContext) |
|---|
| 251 | { |
|---|
| 252 | getState()->setContextID( _traits->sharedContext->getState()->getContextID() ); |
|---|
| 253 | incrementContextIDUsageCount( getState()->getContextID() ); |
|---|
| 254 | } |
|---|
| 255 | else |
|---|
| 256 | { |
|---|
| 257 | getState()->setContextID( osg::GraphicsContext::createNewContextID() ); |
|---|
| 258 | } |
|---|
| 259 | } |
|---|
| 260 | } |
|---|
| 261 | |
|---|
| 262 | // dummy implementations, assume that graphics context is *always* current and valid. |
|---|
| 263 | virtual bool valid() const { return true; } |
|---|
| 264 | virtual bool realizeImplementation() { return true; } |
|---|
| 265 | virtual bool isRealizedImplementation() const { return true; } |
|---|
| 266 | virtual void closeImplementation() {} |
|---|
| 267 | virtual bool makeCurrentImplementation() { return true; } |
|---|
| 268 | virtual bool releaseContextImplementation() { return true; } |
|---|
| 269 | virtual void swapBuffersImplementation() {} |
|---|
| 270 | virtual void grabFocus() {} |
|---|
| 271 | virtual void grabFocusIfPointerInWindow() {} |
|---|
| 272 | virtual void raiseWindow() {} |
|---|
| 273 | }; |
|---|
| 274 | |
|---|
| 275 | struct GraphicsWindowFunctionProxy |
|---|
| 276 | { |
|---|
| 277 | GraphicsWindowFunctionProxy(CGraphicsWindowFunction function) { (function)(); } |
|---|
| 278 | }; |
|---|
| 279 | |
|---|
| 280 | #define USE_GRAPICSWINDOW_IMPLEMENTATION(ext) \ |
|---|
| 281 | extern "C" void graphicswindow_##ext(void); \ |
|---|
| 282 | static osgViewer::GraphicsWindowFunctionProxy graphicswindowproxy_##ext(graphicswindow_##ext); |
|---|
| 283 | |
|---|
| 284 | #if defined(_WIN32) |
|---|
| 285 | #define USE_GRAPHICSWINDOW() USE_GRAPICSWINDOW_IMPLEMENTATION(Win32) |
|---|
| 286 | #elif defined(__APPLE__) |
|---|
| 287 | #define USE_GRAPHICSWINDOW() USE_GRAPICSWINDOW_IMPLEMENTATION(Carbon) |
|---|
| 288 | #else |
|---|
| 289 | #define USE_GRAPHICSWINDOW() USE_GRAPICSWINDOW_IMPLEMENTATION(X11) |
|---|
| 290 | #endif |
|---|
| 291 | |
|---|
| 292 | } |
|---|
| 293 | |
|---|
| 294 | #endif |
|---|