| 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 | /* Note, elements of GraphicsWindowX11 have used Prodcer/RenderSurface_X11.cpp as both |
|---|
| 15 | * a guide to use of X11/GLX and copiying directly in the case of setBorder(). |
|---|
| 16 | * These elements are license under OSGPL as above, with Copyright (C) 2001-2004 Don Burns. |
|---|
| 17 | */ |
|---|
| 18 | |
|---|
| 19 | #ifndef OSGVIEWER_GRAPHICSWINDOWCOCOA |
|---|
| 20 | #define OSGVIEWER_GRAPHICSWINDOWCOCOA 1 |
|---|
| 21 | |
|---|
| 22 | #ifdef __APPLE__ |
|---|
| 23 | |
|---|
| 24 | #ifdef __OBJC__ |
|---|
| 25 | @class GraphicsWindowIOSWindow; |
|---|
| 26 | @class GraphicsWindowIOSGLView; |
|---|
| 27 | @class GraphicsWindowIOSGLViewController; |
|---|
| 28 | @class EAGLContext; |
|---|
| 29 | @class UIWindow; |
|---|
| 30 | @class UIView; |
|---|
| 31 | #else |
|---|
| 32 | class GraphicsWindowIOSGLView; |
|---|
| 33 | class GraphicsWindowIOSWindow; |
|---|
| 34 | class GraphicsWindowIOSGLViewController; |
|---|
| 35 | class EAGLContext; |
|---|
| 36 | class UIWindow; |
|---|
| 37 | class UIView; |
|---|
| 38 | #endif |
|---|
| 39 | |
|---|
| 40 | #include <osgViewer/GraphicsWindow> |
|---|
| 41 | |
|---|
| 42 | // Do not include any cocoa-header here, because this will pollute the name-space and causes compile errors |
|---|
| 43 | |
|---|
| 44 | namespace osgViewer |
|---|
| 45 | { |
|---|
| 46 | /** |
|---|
| 47 | * Implementation of a GraphicsWindow for the iOS platform. |
|---|
| 48 | */ |
|---|
| 49 | class GraphicsWindowIOS : public osgViewer::GraphicsWindow |
|---|
| 50 | { |
|---|
| 51 | public: |
|---|
| 52 | class Implementation; |
|---|
| 53 | |
|---|
| 54 | GraphicsWindowIOS(osg::GraphicsContext::Traits* traits) : GraphicsWindow(), |
|---|
| 55 | _valid(false), |
|---|
| 56 | _initialized(false), |
|---|
| 57 | _realized(false), |
|---|
| 58 | _window(NULL), |
|---|
| 59 | _view(NULL), |
|---|
| 60 | _viewController(NULL), |
|---|
| 61 | _context(NULL), |
|---|
| 62 | _ownsWindow(true), |
|---|
| 63 | _deviceOrientationFlags(WindowData::ALL_ORIENTATIONS), |
|---|
| 64 | _viewContentScaleFactor(-1.0f) |
|---|
| 65 | { |
|---|
| 66 | _traits = traits; |
|---|
| 67 | |
|---|
| 68 | init(); |
|---|
| 69 | |
|---|
| 70 | if (valid()) |
|---|
| 71 | { |
|---|
| 72 | setState( new osg::State ); |
|---|
| 73 | getState()->setGraphicsContext(this); |
|---|
| 74 | |
|---|
| 75 | if (_traits.valid() && _traits->sharedContext.valid()) |
|---|
| 76 | { |
|---|
| 77 | getState()->setContextID( _traits->sharedContext->getState()->getContextID() ); |
|---|
| 78 | incrementContextIDUsageCount( getState()->getContextID() ); |
|---|
| 79 | } |
|---|
| 80 | else |
|---|
| 81 | { |
|---|
| 82 | getState()->setContextID( osg::GraphicsContext::createNewContextID() ); |
|---|
| 83 | } |
|---|
| 84 | } |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | virtual bool isSameKindAs(const Object* object) const { return dynamic_cast<const GraphicsWindowIOS*>(object)!=0; } |
|---|
| 88 | virtual const char* libraryName() const { return "osgViewer"; } |
|---|
| 89 | virtual const char* className() const { return "GraphicsWindowIOS"; } |
|---|
| 90 | |
|---|
| 91 | virtual bool valid() const { return _valid; } |
|---|
| 92 | |
|---|
| 93 | /** Realise the GraphicsContext.*/ |
|---|
| 94 | virtual bool realizeImplementation(); |
|---|
| 95 | |
|---|
| 96 | /** Return true if the graphics context has been realised and is ready to use.*/ |
|---|
| 97 | virtual bool isRealizedImplementation() const { return _realized; } |
|---|
| 98 | |
|---|
| 99 | /** Close the graphics context.*/ |
|---|
| 100 | virtual void closeImplementation(); |
|---|
| 101 | |
|---|
| 102 | /** Make this graphics context current.*/ |
|---|
| 103 | virtual bool makeCurrentImplementation(); |
|---|
| 104 | |
|---|
| 105 | /** Release the graphics context.*/ |
|---|
| 106 | virtual bool releaseContextImplementation(); |
|---|
| 107 | |
|---|
| 108 | /** Swap the front and back buffers.*/ |
|---|
| 109 | virtual void swapBuffersImplementation(); |
|---|
| 110 | |
|---|
| 111 | /** Check to see if any events have been generated.*/ |
|---|
| 112 | virtual void checkEvents(); |
|---|
| 113 | |
|---|
| 114 | /** Set Window decoration.*/ |
|---|
| 115 | virtual bool setWindowDecorationImplementation(bool flag); |
|---|
| 116 | |
|---|
| 117 | /** Get focus.*/ |
|---|
| 118 | virtual void grabFocus(); |
|---|
| 119 | |
|---|
| 120 | /** Get focus on if the pointer is in this window.*/ |
|---|
| 121 | virtual void grabFocusIfPointerInWindow(); |
|---|
| 122 | |
|---|
| 123 | /** Raise the window to the top.*/ |
|---|
| 124 | virtual void raiseWindow(); |
|---|
| 125 | |
|---|
| 126 | virtual void resizedImplementation(int x, int y, int width, int height); |
|---|
| 127 | |
|---|
| 128 | virtual bool setWindowRectangleImplementation(int x, int y, int width, int height); |
|---|
| 129 | |
|---|
| 130 | virtual void setWindowName (const std::string & name); |
|---|
| 131 | virtual void useCursor(bool cursorOn); |
|---|
| 132 | virtual void setCursor(MouseCursor mouseCursor); |
|---|
| 133 | |
|---|
| 134 | // WindowData is used to pass in the an existing UIWindow to be used to display our glView |
|---|
| 135 | class WindowData : public osg::Referenced |
|---|
| 136 | { |
|---|
| 137 | public: |
|---|
| 138 | enum DeviceOrientation{ |
|---|
| 139 | IGNORE_ORIENTATION = 0, |
|---|
| 140 | PORTRAIT_ORIENTATION = 1<<0, |
|---|
| 141 | PORTRAIT_UPSIDEDOWN_ORIENTATION = 1<<1, |
|---|
| 142 | LANDSCAPE_LEFT_ORIENTATION = 1<<2, |
|---|
| 143 | LANDSCAPE_RIGHT_ORIENTATION = 1<<3, |
|---|
| 144 | ALL_ORIENTATIONS = PORTRAIT_ORIENTATION | PORTRAIT_UPSIDEDOWN_ORIENTATION | LANDSCAPE_LEFT_ORIENTATION | LANDSCAPE_RIGHT_ORIENTATION |
|---|
| 145 | }; |
|---|
| 146 | typedef unsigned int DeviceOrientationFlags; |
|---|
| 147 | |
|---|
| 148 | WindowData(UIView* window_or_view = NULL, DeviceOrientationFlags orientationFlags = ALL_ORIENTATIONS, float scaleFactor = -1.0f) |
|---|
| 149 | : _windowOrView(window_or_view), |
|---|
| 150 | _deviceOrientationFlags(orientationFlags), |
|---|
| 151 | _viewContentScaleFactor(scaleFactor) |
|---|
| 152 | { |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | void setAdaptToDeviceOrientation(DeviceOrientationFlags flags) { _deviceOrientationFlags = flags; } |
|---|
| 156 | |
|---|
| 157 | void setViewContentScaleFactor(float scaleFactor) { _viewContentScaleFactor = scaleFactor; } |
|---|
| 158 | |
|---|
| 159 | UIView* getWindowOrParentView() const { return _windowOrView; } |
|---|
| 160 | private: |
|---|
| 161 | |
|---|
| 162 | UIView* _windowOrView; |
|---|
| 163 | DeviceOrientationFlags _deviceOrientationFlags; |
|---|
| 164 | float _viewContentScaleFactor; |
|---|
| 165 | |
|---|
| 166 | friend class GraphicsWindowIOS; |
|---|
| 167 | |
|---|
| 168 | }; |
|---|
| 169 | |
|---|
| 170 | EAGLContext* getContext() { return _context; } |
|---|
| 171 | GraphicsWindowIOSWindow* getWindow() { return _window; } |
|---|
| 172 | GraphicsWindowIOSGLView* getView() { return _view; } |
|---|
| 173 | void setVSync(bool f); |
|---|
| 174 | |
|---|
| 175 | /** adapts a resize / move of the window, coords in global screen space */ |
|---|
| 176 | void adaptResize(int x, int y, int w, int h); |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | WindowData::DeviceOrientationFlags getDeviceOrientationFlags() const { return _deviceOrientationFlags; } |
|---|
| 180 | |
|---|
| 181 | void setDeviceOrientationFlags(WindowData::DeviceOrientationFlags flags) { _deviceOrientationFlags = flags; } |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | // |
|---|
| 185 | //helper funs for converting points to pixels |
|---|
| 186 | osg::Vec2 pointToPixel(const osg::Vec2& point); |
|---|
| 187 | osg::Vec2 pixelToPoint(const osg::Vec2& pixel); |
|---|
| 188 | |
|---|
| 189 | protected: |
|---|
| 190 | |
|---|
| 191 | void init(); |
|---|
| 192 | |
|---|
| 193 | void transformMouseXY(float& x, float& y); |
|---|
| 194 | |
|---|
| 195 | virtual ~GraphicsWindowIOS(); |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | bool _valid; |
|---|
| 199 | bool _initialized; |
|---|
| 200 | bool _realized; |
|---|
| 201 | bool _useWindowDecoration; |
|---|
| 202 | |
|---|
| 203 | |
|---|
| 204 | private: |
|---|
| 205 | |
|---|
| 206 | GraphicsWindowIOSWindow* _window; |
|---|
| 207 | GraphicsWindowIOSGLView* _view; |
|---|
| 208 | GraphicsWindowIOSGLViewController* _viewController; |
|---|
| 209 | EAGLContext* _context; |
|---|
| 210 | bool _updateContext; |
|---|
| 211 | |
|---|
| 212 | bool _ownsWindow; |
|---|
| 213 | |
|---|
| 214 | WindowData::DeviceOrientationFlags _deviceOrientationFlags; |
|---|
| 215 | |
|---|
| 216 | float _viewContentScaleFactor; |
|---|
| 217 | |
|---|
| 218 | }; |
|---|
| 219 | |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | #endif |
|---|
| 223 | #endif |
|---|