2006-10-05 18:06:19 +08:00
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
2006-11-02 20:27:15 +08:00
#ifndef OSGVIEWER_GRAPHICWINDOW
#define OSGVIEWER_GRAPHICWINDOW 1
2006-10-05 18:06:19 +08:00
#include <osg/GraphicsContext>
#include <osg/Notify>
#include <osgGA/EventQueue>
2006-11-29 04:35:31 +08:00
#include <osgGA/GUIActionAdapter>
2006-10-05 18:06:19 +08:00
2006-11-02 20:27:15 +08:00
#include <osgViewer/Export>
2007-09-26 17:50:32 +08:00
2007-06-10 17:51:29 +08:00
extern "C"
{
typedef void (* CGraphicsWindowFunction) (void);
}
2006-11-02 20:27:15 +08:00
namespace osgViewer {
2006-10-05 18:06:19 +08:00
2011-04-19 20:01:38 +08:00
class View;
2007-12-11 01:30:18 +08:00
/** Base class for providing Windowing API agnostic access to creating and managing graphics window and events.
2015-04-08 02:01:12 +08:00
* Note, the GraphicsWindow is subclassed from osg::GraphicsContext, and to provide an implementation you'll need to implement its
* range of pure virtual functions, you'll find these all have naming convention methodNameImplementation(..).
2007-12-11 01:30:18 +08:00
* GraphicsWindow adds the event queue on top of the GraphicsContext, thereby adding a mechanism for adapting Windowing events
2006-10-05 18:06:19 +08:00
* as well as basics graphics context work, you should wire up custom GraphicsWindowImplementation to push their events through
* into the EventQueue. */
2006-11-02 20:27:15 +08:00
class OSGVIEWER_EXPORT GraphicsWindow : public osg::GraphicsContext, public osgGA::GUIActionAdapter
2006-10-05 18:06:19 +08:00
{
public:
2011-04-19 19:46:05 +08:00
2008-06-03 01:34:47 +08:00
GraphicsWindow() { _eventQueue = new osgGA::EventQueue; _eventQueue->setGraphicsContext(this); }
2006-10-05 18:06:19 +08:00
2007-06-12 23:32:04 +08:00
virtual bool isSameKindAs(const Object* object) const { return dynamic_cast<const GraphicsWindow*>(object)!=0; }
virtual const char* libraryName() const { return "osgViewer"; }
virtual const char* className() const { return "GraphicsWindow"; }
2007-01-09 00:20:10 +08:00
2006-10-05 18:06:19 +08:00
void setEventQueue(osgGA::EventQueue* eventQueue) { _eventQueue = eventQueue; }
osgGA::EventQueue* getEventQueue() { return _eventQueue.get(); }
const osgGA::EventQueue* getEventQueue() const { return _eventQueue.get(); }
2013-05-24 17:35:58 +08:00
/** Check events, return true if events have been received.*/
2013-07-19 14:20:46 +08:00
virtual bool checkEvents() { return !(_eventQueue->empty()); }
2011-04-19 19:46:05 +08:00
2007-04-13 21:22:52 +08:00
/** Set the window's position and size.*/
2007-06-11 03:53:18 +08:00
void setWindowRectangle(int x, int y, int width, int height)
2011-04-19 19:46:05 +08:00
{
2007-06-11 03:53:18 +08:00
if (setWindowRectangleImplementation(x ,y ,width, height) && _traits.valid())
{
resized(x,y,width,height);
}
}
2011-04-19 19:46:05 +08:00
2007-06-11 03:53:18 +08:00
/** implementation of setWindowRectangle, should be implemented by derived classes */
virtual bool setWindowRectangleImplementation(int /*x*/, int /*y*/, int /*width*/, int /*height*/) { osg::notify(osg::NOTICE)<<"GraphicsWindow::setWindowRectangleImplementation(..) not implemented."<<std::endl; return false; }
2007-04-13 21:22:52 +08:00
/** Get the window's position and size.*/
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; } }
/** Set Window decoration.*/
2007-06-11 03:53:18 +08:00
void setWindowDecoration(bool flag)
2011-04-19 19:46:05 +08:00
{
2007-06-11 03:53:18 +08:00
if (setWindowDecorationImplementation(flag) && _traits.valid())
{
_traits->windowDecoration = flag;
}
}
2011-04-19 19:46:05 +08:00
2007-06-11 03:53:18 +08:00
/** implementation of setWindowDecoration, should be implemented by derived classes */
virtual bool setWindowDecorationImplementation(bool /*flag*/) { osg::notify(osg::NOTICE)<<"GraphicsWindow::setWindowDecorationImplementation(..) not implemented."<<std::endl; return false; }
2006-12-21 05:13:29 +08:00
2006-12-21 19:20:42 +08:00
/** Set Window decoration.*/
2007-04-13 21:22:52 +08:00
virtual bool getWindowDecoration() const { return _traits.valid() ? _traits->windowDecoration : false; }
2006-12-21 19:20:42 +08:00
/** Get focus.*/
2007-04-13 21:22:52 +08:00
virtual void grabFocus() { osg::notify(osg::NOTICE)<<"GraphicsWindow::grabFocus(..) not implemented."<<std::endl; }
2011-04-19 19:46:05 +08:00
2006-12-21 19:20:42 +08:00
/** Get focus on if the pointer is in this window.*/
2007-04-13 21:22:52 +08:00
virtual void grabFocusIfPointerInWindow() { osg::notify(osg::NOTICE)<<"GraphicsWindow::grabFocusIfPointerInWindow(..) not implemented."<<std::endl; }
2006-12-21 19:20:42 +08:00
2008-08-16 01:32:26 +08:00
/** Raise the window to the top.*/
virtual void raiseWindow() { osg::notify(osg::NOTICE)<<"GraphicsWindow::raiseWindow(..) not implemented."<<std::endl; }
2007-06-02 03:43:28 +08:00
/** Mouse cursor types, the same ones already present with ancient glut ... */
enum MouseCursor {
InheritCursor,
NoCursor,
RightArrowCursor,
LeftArrowCursor,
InfoCursor,
DestroyCursor,
HelpCursor,
CycleCursor,
SprayCursor,
WaitCursor,
TextCursor,
CrosshairCursor,
2009-04-22 19:20:19 +08:00
HandCursor,
2007-06-02 03:43:28 +08:00
UpDownCursor,
LeftRightCursor,
TopSideCursor,
BottomSideCursor,
LeftSideCursor,
RightSideCursor,
TopLeftCorner,
TopRightCorner,
BottomRightCorner,
BottomLeftCorner
};
2007-09-26 17:50:32 +08:00
/** Set the name of the window */
virtual void setWindowName(const std::string& /*name*/) { osg::notify(osg::NOTICE)<<"GraphicsWindow::setWindowName(..) not implemented."<<std::endl; }
/** Return the name of the window */
virtual std::string getWindowName() { return _traits.valid() ? _traits->windowName : ""; }
2007-01-02 20:50:57 +08:00
/** Switch on/off the cursor.*/
2007-06-02 03:43:28 +08:00
virtual void useCursor(bool cursorOn) { setCursor(cursorOn ? InheritCursor : NoCursor); }
/** Set mouse cursor to a specific shape.*/
virtual void setCursor(MouseCursor /*mouseCursor*/) { osg::notify(osg::NOTICE)<<"GraphicsWindow::setCursor(..) not implemented."<<std::endl; }
/** Create a new mouse cursor from the usual bitmap data.*/
//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; }
2007-01-02 20:50:57 +08:00
2010-04-22 01:16:13 +08:00
/** Set sync-to-vblank. */
virtual void setSyncToVBlank(bool on)
{
osg::notify(osg::NOTICE) << "GraphicsWindow::setSyncToVBlank(" << on << ") not implemented." << std::endl;
}
bool getSyncToVBlank() const { return _traits.valid() ? _traits->vsync : true; }
2011-04-21 21:34:03 +08:00
/** Set swap group. */
virtual void setSwapGroup(bool on, GLuint group, GLuint barrier)
{
osg::notify(osg::NOTICE) << "GraphicsWindow::setSwapGroup(" << on << " " << group << " " << barrier << ") not implemented." << std::endl;
}
void getSwapGroup(bool& on, GLuint& group, GLuint& barrier) const { on = _traits->swapGroupEnabled; group = _traits->swapGroup; barrier = _traits->swapBarrier; }
2006-10-05 18:06:19 +08:00
public:
2007-01-09 00:20:10 +08:00
/** Return whether a valid and usable GraphicsContext has been created.*/
2007-09-24 18:36:39 +08:00
virtual bool valid() const { osg::notify(osg::NOTICE)<<"GraphicsWindow::valid() not implemented."<<std::endl; return false; }
2007-01-09 00:20:10 +08:00
2011-04-19 19:46:05 +08:00
/** Realize the GraphicsContext implementation,
2007-12-11 01:30:18 +08:00
* Pure virtual - must be implemented by concrete implementations of GraphicsContext. */
2006-10-05 18:06:19 +08:00
virtual bool realizeImplementation() { osg::notify(osg::NOTICE)<<"GraphicsWindow::realizeImplementation() not implemented."<<std::endl; return false; }
2007-12-11 01:30:18 +08:00
/** Return true if the graphics context has been realized, and is ready to use, implementation.
* Pure virtual - must be implemented by concrete implementations of GraphicsContext. */
2006-10-05 18:06:19 +08:00
virtual bool isRealizedImplementation() const { osg::notify(osg::NOTICE)<<"GraphicsWindow::isRealizedImplementation() not implemented."<<std::endl; return false; }
/** Close the graphics context implementation.
2007-12-11 01:30:18 +08:00
* Pure virtual - must be implemented by concrete implementations of GraphicsContext. */
2006-10-05 18:06:19 +08:00
virtual void closeImplementation() { osg::notify(osg::NOTICE)<<"GraphicsWindow::closeImplementation() not implemented."<<std::endl; }
/** Make this graphics context current implementation.
2007-12-11 01:30:18 +08:00
* Pure virtual - must be implemented by concrete implementations of GraphicsContext. */
2007-01-09 03:29:59 +08:00
virtual bool makeCurrentImplementation() { osg::notify(osg::NOTICE)<<"GraphicsWindow::makeCurrentImplementation() not implemented."<<std::endl; return false;}
2011-04-19 19:46:05 +08:00
2006-10-05 18:06:19 +08:00
/** Make this graphics context current with specified read context implementation.
2007-12-11 01:30:18 +08:00
* Pure virtual - must be implemented by concrete implementations of GraphicsContext. */
2007-01-09 03:29:59 +08:00
virtual bool makeContextCurrentImplementation(GraphicsContext* /*readContext*/) { osg::notify(osg::NOTICE)<<"GraphicsWindow::makeContextCurrentImplementation(..) not implemented."<<std::endl; return false;}
/** Release the graphics context.*/
virtual bool releaseContextImplementation() { osg::notify(osg::NOTICE)<<"GraphicsWindow::releaseContextImplementation(..) not implemented."<<std::endl; return false; }
2006-10-05 18:06:19 +08:00
/** Pure virtual, Bind the graphics context to associated texture implementation.
2007-12-11 01:30:18 +08:00
* Pure virtual - must be implemented by concrete implementations of GraphicsContext. */
2007-01-09 03:29:59 +08:00
virtual void bindPBufferToTextureImplementation(GLenum /*buffer*/) { osg::notify(osg::NOTICE)<<"GraphicsWindow::bindPBufferToTextureImplementation(..) not implemented."<<std::endl; }
2006-10-05 18:06:19 +08:00
/** Swap the front and back buffers implementation.
2007-12-11 01:30:18 +08:00
* Pure virtual - must be implemented by concrete implementations of GraphicsContext. */
2006-10-05 18:06:19 +08:00
virtual void swapBuffersImplementation() { osg::notify(osg::NOTICE)<<"GraphicsWindow:: swapBuffersImplementation() not implemented."<<std::endl; }
public:
2011-04-19 20:01:38 +08:00
typedef std::list<osgViewer::View*> Views;
/** Returns the list of views (osgViewer::View) attached to this GraphicsWindow.
* Internally, the method walks through all the cameras and collects all the views attached to the cameras.*/
void getViews(Views& views);
2006-10-05 18:06:19 +08:00
// Override from GUIActionAdapter
2011-04-19 20:01:38 +08:00
virtual void requestRedraw();
2006-10-05 18:06:19 +08:00
// Override from GUIActionAdapter
virtual void requestContinuousUpdate(bool /*needed*/=true) {}
// Override from GUIActionAdapter
virtual void requestWarpPointer(float /*x*/,float /*y*/) {}
protected:
2011-04-19 19:46:05 +08:00
2006-10-05 18:06:19 +08:00
osg::ref_ptr<osgGA::EventQueue> _eventQueue;
2011-04-19 19:46:05 +08:00
2006-10-05 18:06:19 +08:00
};
2007-06-02 23:31:21 +08:00
class GraphicsWindowEmbedded : public GraphicsWindow
{
public:
2011-04-19 19:46:05 +08:00
2007-06-02 23:31:21 +08:00
GraphicsWindowEmbedded(osg::GraphicsContext::Traits* traits=0)
{
_traits = traits;
init();
2011-04-19 19:46:05 +08:00
2007-06-02 23:31:21 +08:00
}
GraphicsWindowEmbedded(int x, int y, int width, int height)
{
_traits = new GraphicsContext::Traits;
_traits->x = x;
_traits->y = y;
_traits->width = width;
_traits->height = height;
2011-04-19 19:46:05 +08:00
2007-06-02 23:31:21 +08:00
init();
}
2007-06-12 23:32:04 +08:00
virtual bool isSameKindAs(const Object* object) const { return dynamic_cast<const GraphicsWindowEmbedded*>(object)!=0; }
virtual const char* libraryName() const { return "osgViewer"; }
virtual const char* className() const { return "GraphicsWindowEmbedded"; }
2007-06-02 23:31:21 +08:00
void init()
{
if (valid())
{
setState( new osg::State );
getState()->setGraphicsContext(this);
2012-09-06 05:03:41 +08:00
if (_traits.valid() && _traits->sharedContext.valid())
2007-06-02 23:31:21 +08:00
{
getState()->setContextID( _traits->sharedContext->getState()->getContextID() );
2011-04-19 19:46:05 +08:00
incrementContextIDUsageCount( getState()->getContextID() );
2007-06-02 23:31:21 +08:00
}
else
{
getState()->setContextID( osg::GraphicsContext::createNewContextID() );
}
}
}
2007-12-11 01:30:18 +08:00
// dummy implementations, assume that graphics context is *always* current and valid.
2007-06-02 23:31:21 +08:00
virtual bool valid() const { return true; }
virtual bool realizeImplementation() { return true; }
virtual bool isRealizedImplementation() const { return true; }
virtual void closeImplementation() {}
virtual bool makeCurrentImplementation() { return true; }
virtual bool releaseContextImplementation() { return true; }
virtual void swapBuffersImplementation() {}
virtual void grabFocus() {}
virtual void grabFocusIfPointerInWindow() {}
2008-08-16 01:32:26 +08:00
virtual void raiseWindow() {}
2007-06-02 23:31:21 +08:00
};
2018-05-18 21:58:53 +08:00
2007-06-15 03:17:31 +08:00
struct GraphicsWindowFunctionProxy
2007-06-10 17:51:29 +08:00
{
2007-06-15 03:17:31 +08:00
GraphicsWindowFunctionProxy(CGraphicsWindowFunction function) { (function)(); }
2007-06-10 17:51:29 +08:00
};
#define USE_GRAPICSWINDOW_IMPLEMENTATION(ext) \
extern "C" void graphicswindow_##ext(void); \
2007-06-15 03:17:31 +08:00
static osgViewer::GraphicsWindowFunctionProxy graphicswindowproxy_##ext(graphicswindow_##ext);
2007-06-10 17:51:29 +08:00
2021-01-15 05:40:16 +08:00
#if defined(OSG_WINDOWING_SYSTEM_NONE)
2021-01-23 06:34:00 +08:00
#define USE_GRAPHICSWINDOW()
2021-01-15 05:40:16 +08:00
#elif defined(_WIN32)
2007-06-10 17:51:29 +08:00
#define USE_GRAPHICSWINDOW() USE_GRAPICSWINDOW_IMPLEMENTATION(Win32)
#elif defined(__APPLE__)
2019-09-11 16:22:16 +08:00
#if defined(OSG_WINDOWING_SYSTEM_CARBON)
2007-06-10 17:51:29 +08:00
#define USE_GRAPHICSWINDOW() USE_GRAPICSWINDOW_IMPLEMENTATION(Carbon)
2019-09-11 16:22:16 +08:00
#else
#define USE_GRAPHICSWINDOW() USE_GRAPICSWINDOW_IMPLEMENTATION(Cocoa)
#endif
2007-06-10 17:51:29 +08:00
#else
#define USE_GRAPHICSWINDOW() USE_GRAPICSWINDOW_IMPLEMENTATION(X11)
#endif
2006-10-05 18:06:19 +08:00
}
#endif