2010-11-27 02:19:28 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Note, elements of GraphicsWindowX11 have used Prodcer/RenderSurface_X11.cpp as both
|
|
|
|
* a guide to use of X11/GLX and copiying directly in the case of setBorder().
|
|
|
|
* These elements are license under OSGPL as above, with Copyright (C) 2001-2004 Don Burns.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef OSGVIEWER_GRAPHICSWINDOWCOCOA
|
|
|
|
#define OSGVIEWER_GRAPHICSWINDOWCOCOA 1
|
|
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
|
|
|
|
#ifdef __OBJC__
|
|
|
|
@class GraphicsWindowIOSWindow;
|
|
|
|
@class GraphicsWindowIOSGLView;
|
|
|
|
@class GraphicsWindowIOSGLViewController;
|
|
|
|
@class EAGLContext;
|
|
|
|
@class UIWindow;
|
|
|
|
@class UIView;
|
2018-03-27 01:40:14 +08:00
|
|
|
@class UIViewController;
|
2010-11-27 02:19:28 +08:00
|
|
|
#else
|
|
|
|
class GraphicsWindowIOSGLView;
|
|
|
|
class GraphicsWindowIOSWindow;
|
|
|
|
class GraphicsWindowIOSGLViewController;
|
|
|
|
class EAGLContext;
|
|
|
|
class UIWindow;
|
|
|
|
class UIView;
|
2018-03-27 01:40:14 +08:00
|
|
|
class UIViewController;
|
2010-11-27 02:19:28 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <osgViewer/GraphicsWindow>
|
|
|
|
|
2010-11-30 17:26:18 +08:00
|
|
|
// Do not include any cocoa-header here, because this will pollute the name-space and causes compile errors
|
2010-11-27 02:19:28 +08:00
|
|
|
|
|
|
|
namespace osgViewer
|
|
|
|
{
|
2010-11-30 17:26:18 +08:00
|
|
|
/**
|
|
|
|
* Implementation of a GraphicsWindow for the iOS platform.
|
|
|
|
*/
|
2010-11-27 02:19:28 +08:00
|
|
|
class GraphicsWindowIOS : public osgViewer::GraphicsWindow
|
|
|
|
{
|
|
|
|
public:
|
2010-11-30 17:26:18 +08:00
|
|
|
class Implementation;
|
2010-11-27 02:19:28 +08:00
|
|
|
|
2010-11-30 17:26:18 +08:00
|
|
|
GraphicsWindowIOS(osg::GraphicsContext::Traits* traits) : GraphicsWindow(),
|
2010-11-27 02:19:28 +08:00
|
|
|
_valid(false),
|
|
|
|
_initialized(false),
|
|
|
|
_realized(false),
|
2010-11-30 17:26:18 +08:00
|
|
|
_window(NULL),
|
|
|
|
_view(NULL),
|
|
|
|
_viewController(NULL),
|
|
|
|
_context(NULL),
|
2010-11-27 02:19:28 +08:00
|
|
|
_ownsWindow(true),
|
2011-04-21 20:06:06 +08:00
|
|
|
_deviceOrientationFlags(WindowData::ALL_ORIENTATIONS),
|
2010-11-30 17:26:18 +08:00
|
|
|
_viewContentScaleFactor(-1.0f)
|
2010-11-27 02:19:28 +08:00
|
|
|
{
|
|
|
|
_traits = traits;
|
|
|
|
|
|
|
|
init();
|
2010-11-30 17:26:18 +08:00
|
|
|
|
2010-11-27 02:19:28 +08:00
|
|
|
if (valid())
|
|
|
|
{
|
|
|
|
setState( new osg::State );
|
|
|
|
getState()->setGraphicsContext(this);
|
|
|
|
|
2012-09-06 05:03:41 +08:00
|
|
|
if (_traits.valid() && _traits->sharedContext.valid())
|
2010-11-27 02:19:28 +08:00
|
|
|
{
|
|
|
|
getState()->setContextID( _traits->sharedContext->getState()->getContextID() );
|
|
|
|
incrementContextIDUsageCount( getState()->getContextID() );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
getState()->setContextID( osg::GraphicsContext::createNewContextID() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool isSameKindAs(const Object* object) const { return dynamic_cast<const GraphicsWindowIOS*>(object)!=0; }
|
|
|
|
virtual const char* libraryName() const { return "osgViewer"; }
|
2011-01-28 18:50:24 +08:00
|
|
|
virtual const char* className() const { return "GraphicsWindowIOS"; }
|
2010-11-27 02:19:28 +08:00
|
|
|
|
2018-03-27 01:40:14 +08:00
|
|
|
virtual void runOperations();
|
|
|
|
|
2010-11-27 02:19:28 +08:00
|
|
|
virtual bool valid() const { return _valid; }
|
|
|
|
|
|
|
|
/** Realise the GraphicsContext.*/
|
|
|
|
virtual bool realizeImplementation();
|
|
|
|
|
|
|
|
/** Return true if the graphics context has been realised and is ready to use.*/
|
|
|
|
virtual bool isRealizedImplementation() const { return _realized; }
|
|
|
|
|
|
|
|
/** Close the graphics context.*/
|
|
|
|
virtual void closeImplementation();
|
|
|
|
|
|
|
|
/** Make this graphics context current.*/
|
|
|
|
virtual bool makeCurrentImplementation();
|
|
|
|
|
|
|
|
/** Release the graphics context.*/
|
|
|
|
virtual bool releaseContextImplementation();
|
|
|
|
|
|
|
|
/** Swap the front and back buffers.*/
|
|
|
|
virtual void swapBuffersImplementation();
|
|
|
|
|
|
|
|
/** Check to see if any events have been generated.*/
|
2013-05-24 17:35:58 +08:00
|
|
|
virtual bool checkEvents();
|
2010-11-27 02:19:28 +08:00
|
|
|
|
|
|
|
/** Set Window decoration.*/
|
|
|
|
virtual bool setWindowDecorationImplementation(bool flag);
|
|
|
|
|
|
|
|
/** Get focus.*/
|
|
|
|
virtual void grabFocus();
|
|
|
|
|
|
|
|
/** Get focus on if the pointer is in this window.*/
|
|
|
|
virtual void grabFocusIfPointerInWindow();
|
|
|
|
|
2010-11-30 17:26:18 +08:00
|
|
|
/** Raise the window to the top.*/
|
|
|
|
virtual void raiseWindow();
|
2010-11-27 02:19:28 +08:00
|
|
|
|
|
|
|
virtual void resizedImplementation(int x, int y, int width, int height);
|
|
|
|
|
|
|
|
virtual bool setWindowRectangleImplementation(int x, int y, int width, int height);
|
|
|
|
|
|
|
|
virtual void setWindowName (const std::string & name);
|
|
|
|
virtual void useCursor(bool cursorOn);
|
|
|
|
virtual void setCursor(MouseCursor mouseCursor);
|
|
|
|
|
|
|
|
// WindowData is used to pass in the an existing UIWindow to be used to display our glView
|
|
|
|
class WindowData : public osg::Referenced
|
|
|
|
{
|
|
|
|
public:
|
2011-04-21 20:06:06 +08:00
|
|
|
enum DeviceOrientation{
|
2011-05-16 20:44:39 +08:00
|
|
|
IGNORE_ORIENTATION = 0,
|
2011-04-21 20:06:06 +08:00
|
|
|
PORTRAIT_ORIENTATION = 1<<0,
|
|
|
|
PORTRAIT_UPSIDEDOWN_ORIENTATION = 1<<1,
|
|
|
|
LANDSCAPE_LEFT_ORIENTATION = 1<<2,
|
|
|
|
LANDSCAPE_RIGHT_ORIENTATION = 1<<3,
|
|
|
|
ALL_ORIENTATIONS = PORTRAIT_ORIENTATION | PORTRAIT_UPSIDEDOWN_ORIENTATION | LANDSCAPE_LEFT_ORIENTATION | LANDSCAPE_RIGHT_ORIENTATION
|
|
|
|
};
|
|
|
|
typedef unsigned int DeviceOrientationFlags;
|
|
|
|
|
2018-03-27 01:40:14 +08:00
|
|
|
WindowData(UIView* window_or_view = NULL, UIViewController* parentController = NULL, DeviceOrientationFlags orientationFlags = ALL_ORIENTATIONS, float scaleFactor = -1.0f)
|
2011-05-16 20:44:39 +08:00
|
|
|
: _windowOrView(window_or_view),
|
2018-03-27 01:40:14 +08:00
|
|
|
_parentController(parentController),
|
2011-04-21 20:06:06 +08:00
|
|
|
_deviceOrientationFlags(orientationFlags),
|
2012-12-06 01:15:53 +08:00
|
|
|
_viewContentScaleFactor(scaleFactor),
|
2013-01-07 20:17:26 +08:00
|
|
|
_createTransparentView(false),
|
|
|
|
_useRetainedBacking(false)
|
2010-11-27 02:19:28 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-04-21 20:06:06 +08:00
|
|
|
void setAdaptToDeviceOrientation(DeviceOrientationFlags flags) { _deviceOrientationFlags = flags; }
|
2010-11-30 17:26:18 +08:00
|
|
|
|
|
|
|
void setViewContentScaleFactor(float scaleFactor) { _viewContentScaleFactor = scaleFactor; }
|
2010-11-27 02:19:28 +08:00
|
|
|
|
2011-05-16 20:44:39 +08:00
|
|
|
UIView* getWindowOrParentView() const { return _windowOrView; }
|
2018-03-27 01:40:14 +08:00
|
|
|
UIViewController* getController() const;
|
2012-12-06 01:15:53 +08:00
|
|
|
|
|
|
|
bool getCreateTransparentView() { return _createTransparentView; }
|
|
|
|
void setCreateTransparentView(bool b) { _createTransparentView = b; }
|
2013-01-07 20:17:26 +08:00
|
|
|
|
|
|
|
bool getUseRetainedBacking() { return _useRetainedBacking; }
|
|
|
|
void setUseRetainedBacking(bool b) { _useRetainedBacking = b; }
|
|
|
|
|
2010-11-27 02:19:28 +08:00
|
|
|
private:
|
|
|
|
|
2011-05-16 20:44:39 +08:00
|
|
|
UIView* _windowOrView;
|
2018-03-27 01:40:14 +08:00
|
|
|
UIViewController* _parentController;
|
2011-04-21 20:06:06 +08:00
|
|
|
DeviceOrientationFlags _deviceOrientationFlags;
|
|
|
|
float _viewContentScaleFactor;
|
2012-12-06 01:15:53 +08:00
|
|
|
bool _createTransparentView;
|
2013-01-07 20:17:26 +08:00
|
|
|
bool _useRetainedBacking;
|
2010-11-27 02:19:28 +08:00
|
|
|
|
|
|
|
friend class GraphicsWindowIOS;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
EAGLContext* getContext() { return _context; }
|
|
|
|
GraphicsWindowIOSWindow* getWindow() { return _window; }
|
2011-05-16 20:44:39 +08:00
|
|
|
GraphicsWindowIOSGLView* getView() { return _view; }
|
2010-11-27 02:19:28 +08:00
|
|
|
void setVSync(bool f);
|
|
|
|
|
|
|
|
/** adapts a resize / move of the window, coords in global screen space */
|
|
|
|
void adaptResize(int x, int y, int w, int h);
|
|
|
|
|
|
|
|
|
2011-04-21 20:06:06 +08:00
|
|
|
WindowData::DeviceOrientationFlags getDeviceOrientationFlags() const { return _deviceOrientationFlags; }
|
2010-11-27 02:19:28 +08:00
|
|
|
|
2011-05-16 20:44:39 +08:00
|
|
|
void setDeviceOrientationFlags(WindowData::DeviceOrientationFlags flags) { _deviceOrientationFlags = flags; }
|
2010-11-30 17:26:18 +08:00
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
//helper funs for converting points to pixels
|
|
|
|
osg::Vec2 pointToPixel(const osg::Vec2& point);
|
|
|
|
osg::Vec2 pixelToPoint(const osg::Vec2& pixel);
|
2010-11-27 02:19:28 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
void init();
|
|
|
|
|
|
|
|
void transformMouseXY(float& x, float& y);
|
|
|
|
|
|
|
|
virtual ~GraphicsWindowIOS();
|
|
|
|
|
|
|
|
|
|
|
|
bool _valid;
|
|
|
|
bool _initialized;
|
|
|
|
bool _realized;
|
|
|
|
bool _useWindowDecoration;
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2011-04-21 20:06:06 +08:00
|
|
|
GraphicsWindowIOSWindow* _window;
|
|
|
|
GraphicsWindowIOSGLView* _view;
|
2010-11-27 02:19:28 +08:00
|
|
|
GraphicsWindowIOSGLViewController* _viewController;
|
2011-04-21 20:06:06 +08:00
|
|
|
EAGLContext* _context;
|
|
|
|
bool _updateContext;
|
2010-11-30 17:26:18 +08:00
|
|
|
|
2011-04-21 20:06:06 +08:00
|
|
|
bool _ownsWindow;
|
|
|
|
|
|
|
|
WindowData::DeviceOrientationFlags _deviceOrientationFlags;
|
2010-11-30 17:26:18 +08:00
|
|
|
|
2011-04-21 20:06:06 +08:00
|
|
|
float _viewContentScaleFactor;
|
2010-11-30 17:26:18 +08:00
|
|
|
|
2010-11-27 02:19:28 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
#endif
|