osxWindow.mm 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #import "platformOSX/platformOSX.h"
  23. #include "platformOSX/osxOpenGLDevice.h"
  24. //------------------------------------------------------------------------------
  25. // Get the video settings from the prefs.
  26. static void osxGetInitialResolution(U32 &width, U32 &height, U32 &bpp, bool &fullScreen)
  27. {
  28. const char* resString;
  29. char *tempBuf;
  30. osxPlatState * platState = [osxPlatState sharedPlatState];
  31. // cache the desktop size of the selected screen in platState
  32. Video::getDesktopResolution();
  33. // load pref variables, properly choose windowed / fullscreen
  34. fullScreen = Con::getBoolVariable("$pref::Video::fullScreen");
  35. if (fullScreen)
  36. resString = Con::getVariable("$pref::Video::resolution");
  37. else
  38. resString = Con::getVariable("$pref::Video::windowedRes");
  39. // dStrtok is destructive, work on a copy...
  40. tempBuf = new char[dStrlen(resString) + 1];
  41. dStrcpy(tempBuf, resString);
  42. // set window size
  43. //DAW: Added min size checks for windowSize
  44. width = (U32)dAtoi(dStrtok(tempBuf, " x\0"));
  45. if (width <= 0)
  46. width = [platState windowWidth];
  47. height = (U32)dAtoi(dStrtok( NULL, " x\0"));
  48. if (height <= 0)
  49. height = [platState windowHeight];
  50. // bit depth
  51. if (fullScreen)
  52. {
  53. dAtoi(dStrtok(NULL, "\0"));
  54. if ( bpp <= 0 )
  55. bpp = 16;
  56. }
  57. else
  58. bpp = platState.desktopBitsPixel == 24 ? 32 : platState.desktopBitsPixel;
  59. delete [] tempBuf;
  60. }
  61. //-----------------------------------------------------------------------------
  62. // This is the critical platform function for initializing graphics. This
  63. // will create a NSWindow, a custom OSXTorqueView, a DisplayDevice, and
  64. // kick off the Video:: work
  65. void Platform::initWindow(const Point2I &initialSize, const char *name)
  66. {
  67. bool fullScreen;
  68. U32 width, height, bpp;
  69. osxGetInitialResolution(width, height, bpp, fullScreen);
  70. // Create the NSWindow
  71. osxPlatState * platState = [osxPlatState sharedPlatState];
  72. NSRect frame = NSMakeRect(0, 0, [platState windowWidth], [platState windowHeight]);
  73. NSWindow *tempWindow = [[[NSWindow alloc] initWithContentRect:frame
  74. styleMask:NSTitledWindowMask | NSClosableWindowMask | NSResizableWindowMask
  75. backing:NSBackingStoreBuffered
  76. defer:NO] autorelease];
  77. [tempWindow setBackgroundColor:[NSColor blackColor]];
  78. // The full frame for a window must consider the title bar height as well
  79. // Thus, our NSWindow must be larger than the passed width and height
  80. frame = [NSWindow frameRectForContentRect:frame styleMask:NSTitledWindowMask];
  81. [tempWindow setFrame:frame display:YES];
  82. [platState setWindow:tempWindow];
  83. [platState setWindowSize:initialSize.x height:initialSize.y];
  84. [platState updateWindowTitle:name];
  85. // Set up TorqueView and add it here:
  86. OSXTorqueView* torqueView = [[OSXTorqueView alloc] initWithFrame:frame];
  87. [torqueView initialize];
  88. [platState setTorqueView:torqueView];
  89. [[platState window] setContentView:[platState torqueView]];
  90. [[NSNotificationCenter defaultCenter] addObserver:[platState torqueView] selector:@selector(windowFinishedLiveResize:) name:NSWindowDidEndLiveResizeNotification object:[platState window]];
  91. // Create the DisplayDevice and install it. In this case, our osxOpenGLDevice
  92. osxOpenGLDevice* device = new osxOpenGLDevice();
  93. Video::installDevice(device);
  94. bool deviceWasSet = Video::setDevice(device->mDeviceName, width, height, bpp, fullScreen);
  95. if (!deviceWasSet)
  96. AssertFatal(false, "Platform::initWindow could not find a compatible display device!");
  97. // Show the window and all its contents
  98. [[platState window] makeKeyAndOrderFront:NSApp];
  99. [[platState window] center];
  100. }
  101. //-----------------------------------------------------------------------------
  102. // Changes the text in the NSWindow title
  103. void Platform::setWindowTitle( const char* title )
  104. {
  105. osxPlatState * platState = [osxPlatState sharedPlatState];
  106. [platState updateWindowTitle:title];
  107. }
  108. //-----------------------------------------------------------------------------
  109. // Calls osxPlatState::setWindowSize
  110. void Platform::setWindowSize( U32 newWidth, U32 newHeight )
  111. {
  112. osxPlatState * platState = [osxPlatState sharedPlatState];
  113. [platState setWindowSize:newWidth height:newHeight];
  114. }
  115. //-----------------------------------------------------------------------------
  116. // Returns osxPlatState::getWindowSize
  117. const Point2I& Platform::getWindowSize()
  118. {
  119. osxPlatState * platState = [osxPlatState sharedPlatState];
  120. return [platState getWindowSize];
  121. }
  122. //-----------------------------------------------------------------------------
  123. // Simulates the user clicking the minimize button by momentarily highlighting
  124. // the button, then minimizing the window.
  125. void Platform::minimizeWindow()
  126. {
  127. NSApplication *application = [NSApplication sharedApplication];
  128. NSWindow *keyWindow = [application keyWindow];
  129. [keyWindow miniaturize:keyWindow];
  130. }
  131. //-----------------------------------------------------------------------------
  132. // De-minimizes the window.
  133. void Platform::restoreWindow()
  134. {
  135. NSApplication *application = [NSApplication sharedApplication];
  136. NSWindow *keyWindow = [application keyWindow];
  137. [keyWindow deminiaturize:keyWindow];
  138. }
  139. //-----------------------------------------------------------------------------
  140. // Sets whether the mouse is locked to the appWindow. Thist changes how the
  141. // mouse input is processed on movement, but does not affect any actual window
  142. // or view properties.
  143. void Platform::setMouseLock(bool locked)
  144. {
  145. osxPlatState* platState = [osxPlatState sharedPlatState];
  146. [platState setMouseLocked:YES];
  147. }
  148. //-----------------------------------------------------------------------------
  149. // Launch the default OS browser. This has nothing to do with the QT browser
  150. bool Platform::openWebBrowser( const char* webAddress )
  151. {
  152. if(Video::isFullScreen())
  153. Video::toggleFullScreen();
  154. NSString* convertedAddress = [NSString stringWithUTF8String:webAddress];
  155. bool result = [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:convertedAddress]];
  156. if (!result)
  157. Con::errorf("Platform::openWebBrowser could not open web address:%s", webAddress);
  158. return result;
  159. }