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 32class GraphicsWindowIOSGLView; 33class GraphicsWindowIOSWindow; 34class GraphicsWindowIOSGLViewController; 35class EAGLContext; 36class UIWindow; 37class 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 44namespace osgViewer 45{ 46/** 47 * Implementation of a GraphicsWindow for the iOS platform. 48 */ 49class 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 bool 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 _createTransparentView(false), 153 _useRetainedBacking(false) 154 { 155 } 156 157 void setAdaptToDeviceOrientation(DeviceOrientationFlags flags) { _deviceOrientationFlags = flags; } 158 159 void setViewContentScaleFactor(float scaleFactor) { _viewContentScaleFactor = scaleFactor; } 160 161 UIView* getWindowOrParentView() const { return _windowOrView; } 162 163 bool getCreateTransparentView() { return _createTransparentView; } 164 void setCreateTransparentView(bool b) { _createTransparentView = b; } 165 166 bool getUseRetainedBacking() { return _useRetainedBacking; } 167 void setUseRetainedBacking(bool b) { _useRetainedBacking = b; } 168 169 private: 170 171 UIView* _windowOrView; 172 DeviceOrientationFlags _deviceOrientationFlags; 173 float _viewContentScaleFactor; 174 bool _createTransparentView; 175 bool _useRetainedBacking; 176 177 friend class GraphicsWindowIOS; 178 179 }; 180 181 EAGLContext* getContext() { return _context; } 182 GraphicsWindowIOSWindow* getWindow() { return _window; } 183 GraphicsWindowIOSGLView* getView() { return _view; } 184 void setVSync(bool f); 185 186 /** adapts a resize / move of the window, coords in global screen space */ 187 void adaptResize(int x, int y, int w, int h); 188 189 190 WindowData::DeviceOrientationFlags getDeviceOrientationFlags() const { return _deviceOrientationFlags; } 191 192 void setDeviceOrientationFlags(WindowData::DeviceOrientationFlags flags) { _deviceOrientationFlags = flags; } 193 194 195 // 196 //helper funs for converting points to pixels 197 osg::Vec2 pointToPixel(const osg::Vec2& point); 198 osg::Vec2 pixelToPoint(const osg::Vec2& pixel); 199 200 protected: 201 202 void init(); 203 204 void transformMouseXY(float& x, float& y); 205 206 virtual ~GraphicsWindowIOS(); 207 208 209 bool _valid; 210 bool _initialized; 211 bool _realized; 212 bool _useWindowDecoration; 213 214 215 private: 216 217 GraphicsWindowIOSWindow* _window; 218 GraphicsWindowIOSGLView* _view; 219 GraphicsWindowIOSGLViewController* _viewController; 220 EAGLContext* _context; 221 bool _updateContext; 222 223 bool _ownsWindow; 224 225 WindowData::DeviceOrientationFlags _deviceOrientationFlags; 226 227 float _viewContentScaleFactor; 228 229}; 230 231} 232 233#endif 234#endif 235