PageRenderTime 25ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Core/Dependencies/OgreSource/OgreMain/src/OSX/OgreConfigDialog.mm

https://bitbucket.org/barakianc/nvidia-physx-and-apex-in-gge
Objective C++ | 471 lines | 330 code | 80 blank | 61 comment | 27 complexity | cfd19fdb7b2edf11ddb976b17c376824 MD5 | raw file
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org/
  6. Copyright (c) 2000-2012 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #include "OgreLogManager.h"
  25. #include "OgreConfigDialog.h"
  26. namespace Ogre {
  27. ConfigDialog* dlg = NULL;
  28. ConfigDialog::ConfigDialog()
  29. {
  30. dlg = this;
  31. }
  32. ConfigDialog::~ConfigDialog()
  33. {
  34. [mWindowDelegate release]; mWindowDelegate = nil;
  35. }
  36. void ConfigDialog::initialise()
  37. {
  38. mWindowDelegate = [[OgreConfigWindowDelegate alloc] init];
  39. if (!mWindowDelegate)
  40. OGRE_EXCEPT (Exception::ERR_INTERNAL_ERROR, "Could not load config dialog",
  41. "ConfigDialog::initialise");
  42. NSArray *keys = [[NSArray alloc] initWithObjects:@"Full Screen", @"FSAA", @"Colour Depth", @"RTT Preferred Mode", @"Video Mode", @"sRGB Gamma Conversion", @"macAPI", nil];
  43. NSArray *fullScreenOptions = [[NSArray alloc] initWithObjects:@"Yes", @"No", nil];
  44. NSArray *colourDepthOptions = [[NSArray alloc] initWithObjects:@"32", @"16", nil];
  45. NSArray *rttOptions = [[NSArray alloc] initWithObjects:@"FBO", @"PBuffer", @"Copy", nil];
  46. NSMutableArray *videoModeOptions = [[NSMutableArray alloc] initWithCapacity:1];
  47. NSMutableArray *fsaaOptions = [[NSMutableArray alloc] initWithCapacity:1];
  48. NSArray *sRGBOptions = [[NSArray alloc] initWithObjects:@"Yes", @"No", nil];
  49. #ifdef __LP64__
  50. NSArray *macAPIOptions = [[NSArray alloc] initWithObjects:@"cocoa", nil];
  51. #else
  52. NSArray *macAPIOptions = [[NSArray alloc] initWithObjects:@"cocoa", @"carbon", nil];
  53. #endif
  54. const RenderSystemList& renderers = Root::getSingleton().getAvailableRenderers();
  55. // Add renderers and options that are detected per RenderSystem
  56. for (RenderSystemList::const_iterator pRend = renderers.begin(); pRend != renderers.end(); ++pRend)
  57. {
  58. RenderSystem* rs = *pRend;
  59. // Set defaults per RenderSystem
  60. rs->setConfigOption("Video Mode", "800 x 600");
  61. rs->setConfigOption("Colour Depth", "32");
  62. rs->setConfigOption("FSAA", "0");
  63. rs->setConfigOption("Full Screen", "No");
  64. rs->setConfigOption("RTT Preferred Mode", "FBO");
  65. rs->setConfigOption("sRGB Gamma Conversion", "No");
  66. #ifdef __LP64__
  67. rs->setConfigOption("macAPI", "cocoa");
  68. #else
  69. rs->setConfigOption("macAPI", "carbon");
  70. #endif
  71. // Add to the drop down
  72. NSString *renderSystemName = [[NSString alloc] initWithCString:rs->getName().c_str() encoding:NSASCIIStringEncoding];
  73. [[mWindowDelegate getRenderSystemsPopUp] addItemWithTitle:renderSystemName];
  74. [renderSystemName release];
  75. // Get detected option values and add them to our config dictionary
  76. const ConfigOptionMap& opts = rs->getConfigOptions();
  77. for (ConfigOptionMap::const_iterator pOpt = opts.begin(); pOpt != opts.end(); ++pOpt)
  78. {
  79. if(pOpt->first == "FSAA")
  80. {
  81. for(uint i = 0; i < pOpt->second.possibleValues.size(); i++)
  82. {
  83. NSString *optionString = [[NSString alloc] initWithCString:pOpt->second.possibleValues[i].c_str()
  84. encoding:NSASCIIStringEncoding];
  85. if(![fsaaOptions containsObject:optionString])
  86. [fsaaOptions addObject:optionString];
  87. [optionString release];
  88. }
  89. }
  90. else if(pOpt->first == "Video Mode")
  91. {
  92. for(uint i = 0; i < pOpt->second.possibleValues.size(); i++)
  93. {
  94. NSString *optionString = [[NSString alloc] initWithCString:pOpt->second.possibleValues[i].c_str()
  95. encoding:NSASCIIStringEncoding];
  96. if(![videoModeOptions containsObject:optionString])
  97. [videoModeOptions addObject:optionString];
  98. [optionString release];
  99. }
  100. }
  101. }
  102. }
  103. NSArray *objects = [[NSArray alloc] initWithObjects:fullScreenOptions, fsaaOptions,
  104. colourDepthOptions, rttOptions, videoModeOptions, sRGBOptions, macAPIOptions, nil];
  105. [mWindowDelegate setOptions:[NSDictionary dictionaryWithObjects:objects forKeys:keys]];
  106. // Clean up all those arrays
  107. [fullScreenOptions release];
  108. [fsaaOptions release];
  109. [colourDepthOptions release];
  110. [rttOptions release];
  111. [videoModeOptions release];
  112. [sRGBOptions release];
  113. [macAPIOptions release];
  114. [keys release];
  115. [objects release];
  116. // Reload table data
  117. [[mWindowDelegate getOptionsTable] reloadData];
  118. }
  119. bool ConfigDialog::display()
  120. {
  121. // Select previously selected rendersystem
  122. mSelectedRenderSystem = Root::getSingleton().getRenderSystem();
  123. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  124. initialise();
  125. // Run a modal dialog, Abort means cancel, Stop means Ok
  126. long retVal = 0;
  127. NSModalSession modalSession = [NSApp beginModalSessionForWindow:[mWindowDelegate getConfigWindow]];
  128. for (;;) {
  129. retVal = [NSApp runModalSession:modalSession];
  130. // User pressed a button
  131. if (retVal != NSRunContinuesResponse)
  132. break;
  133. }
  134. [NSApp endModalSession:modalSession];
  135. // Set the rendersystem
  136. Ogre::String selectedRenderSystemName = Ogre::String([[[[mWindowDelegate getRenderSystemsPopUp] selectedItem] title] UTF8String]);
  137. RenderSystem *rs = Ogre::Root::getSingleton().getRenderSystemByName(selectedRenderSystemName);
  138. Root::getSingleton().setRenderSystem(rs);
  139. // Relinquish control of the table
  140. [[mWindowDelegate getOptionsTable] setDataSource:nil];
  141. [[mWindowDelegate getOptionsTable] setDelegate:nil];
  142. // Drain the auto release pool
  143. [pool drain];
  144. return (retVal == NSRunStoppedResponse) ? true : false;
  145. }
  146. }
  147. @implementation OgreConfigWindowDelegate
  148. - (id)init
  149. {
  150. if((self = [super init]))
  151. {
  152. // This needs to be called in order to use Cocoa from a Carbon app
  153. NSApplicationLoad();
  154. // Construct the window manually
  155. mConfigWindow = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 512, 512)
  156. styleMask:(NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask)
  157. backing:NSBackingStoreBuffered
  158. defer:NO];
  159. // Make ourselves the delegate
  160. [mConfigWindow setDelegate:self];
  161. // First do the buttons
  162. mOkButton = [[NSButton alloc] initWithFrame:NSMakeRect(414, 12, 84, 32)];
  163. [mOkButton setButtonType:NSMomentaryPushInButton];
  164. [mOkButton setBezelStyle:NSRoundedBezelStyle];
  165. [mOkButton setTitle:NSLocalizedString(@"OK", @"okButtonString")];
  166. [mOkButton setAction:@selector(okButtonPressed:)];
  167. [mOkButton setTarget:self];
  168. [mOkButton setKeyEquivalent:@"\r"];
  169. [[mConfigWindow contentView] addSubview:mOkButton];
  170. mCancelButton = [[NSButton alloc] initWithFrame:NSMakeRect(330, 12, 84, 32)];
  171. [mCancelButton setButtonType:NSMomentaryPushInButton];
  172. [mCancelButton setBezelStyle:NSRoundedBezelStyle];
  173. [mCancelButton setAction:@selector(cancelButtonPressed:)];
  174. [mCancelButton setTarget:self];
  175. [mCancelButton setTitle:NSLocalizedString(@"Cancel", @"cancelButtonString")];
  176. [[mConfigWindow contentView] addSubview:mCancelButton];
  177. // Then the Ogre logo out of the framework bundle
  178. mOgreLogo = [[NSImageView alloc] initWithFrame:NSMakeRect(0, 295, 512, 220)];
  179. NSMutableString *logoPath = [[[NSBundle bundleForClass:[self class]] resourcePath] mutableCopy];
  180. [logoPath appendString:@"/ogrelogo.png"];
  181. NSImage *image = [[NSImage alloc] initWithContentsOfFile:logoPath];
  182. [logoPath release];
  183. [mOgreLogo setImage:image];
  184. [mOgreLogo setImageScaling:NSScaleToFit];
  185. [mOgreLogo setEditable:NO];
  186. [image release];
  187. [[mConfigWindow contentView] addSubview:mOgreLogo];
  188. // Popup menu for rendersystems. On OS X this is always OpenGL
  189. mRenderSystemsPopUp = [[NSPopUpButton alloc] initWithFrame:NSMakeRect(168, 259, 327, 26) pullsDown:NO];
  190. [[mConfigWindow contentView] addSubview:mRenderSystemsPopUp];
  191. NSTextField *renderSystemLabel = [[NSTextField alloc] initWithFrame:NSMakeRect(18, 265, 148, 17)];
  192. [renderSystemLabel setStringValue:NSLocalizedString(@"Rendering Subsystem", @"renderingSubsystemString")];
  193. [renderSystemLabel setEditable:NO];
  194. [renderSystemLabel setSelectable:NO];
  195. [renderSystemLabel setDrawsBackground:NO];
  196. [renderSystemLabel setAlignment:NSNaturalTextAlignment];
  197. [renderSystemLabel setBezeled:NO];
  198. [[mConfigWindow contentView] addSubview:renderSystemLabel];
  199. [renderSystemLabel release];
  200. // The pretty box to contain the table and options
  201. NSBox *tableBox = [[NSBox alloc] initWithFrame:NSMakeRect(19, 54, 477, 203)];
  202. [tableBox setTitle:NSLocalizedString(@"Rendering System Options", @"optionsBoxString")];
  203. [tableBox setContentViewMargins:NSMakeSize(0, 0)];
  204. [tableBox setFocusRingType:NSFocusRingTypeNone];
  205. [tableBox setBorderType:NSLineBorder];
  206. // Set up the tableview
  207. mOptionsTable = [[NSTableView alloc] init];
  208. [mOptionsTable setDelegate:self];
  209. [mOptionsTable setDataSource:self];
  210. [mOptionsTable setHeaderView:nil];
  211. [mOptionsTable setUsesAlternatingRowBackgroundColors:YES];
  212. [mOptionsTable sizeToFit];
  213. // Table column to hold option names
  214. NSTableColumn *column = [[NSTableColumn alloc] initWithIdentifier: @"optionName"];
  215. [column setEditable:NO];
  216. [column setMinWidth:437];
  217. [mOptionsTable addTableColumn:column];
  218. [column release];
  219. // Scroll view to hold the table in case the list grows some day
  220. NSScrollView *scrollView = [[NSScrollView alloc] initWithFrame:NSMakeRect(22, 42, 439, 135)];
  221. [scrollView setBorderType:NSBezelBorder];
  222. [scrollView setAutoresizesSubviews:YES];
  223. [scrollView setAutohidesScrollers:YES];
  224. [scrollView setDocumentView:mOptionsTable];
  225. [[tableBox contentView] addSubview:scrollView];
  226. [scrollView release];
  227. mOptionLabel = [[NSTextField alloc] initWithFrame:NSMakeRect(15, 15, 173, 17)];
  228. [mOptionLabel setStringValue:NSLocalizedString(@"Select an Option", @"optionLabelString")];
  229. [mOptionLabel setEditable:NO];
  230. [mOptionLabel setSelectable:NO];
  231. [mOptionLabel setDrawsBackground:NO];
  232. [mOptionLabel setAlignment:NSRightTextAlignment];
  233. [mOptionLabel setBezeled:NO];
  234. [[tableBox contentView] addSubview:mOptionLabel];
  235. mOptionsPopUp = [[NSPopUpButton alloc] initWithFrame:NSMakeRect(190, 10, 270, 26) pullsDown:NO];
  236. [[tableBox contentView] addSubview:mOptionsPopUp];
  237. [mOptionsPopUp setAction:@selector(popUpValueChanged:)];
  238. [mOptionsPopUp setTarget:self];
  239. [[mConfigWindow contentView] addSubview:tableBox];
  240. [tableBox release];
  241. }
  242. return self;
  243. }
  244. - (void)dealloc
  245. {
  246. [mConfigWindow release]; mConfigWindow = nil;
  247. [mOptionsPopUp release]; mOptionsPopUp = nil;
  248. [mOptionLabel release]; mOptionLabel = nil;
  249. [mOptionsTable release]; mOptionsTable = nil;
  250. [mRenderSystemsPopUp release]; mRenderSystemsPopUp = nil;
  251. [mOgreLogo release]; mOgreLogo = nil;
  252. [mCancelButton release]; mCancelButton = nil;
  253. [mOkButton release]; mOkButton = nil;
  254. [super dealloc];
  255. }
  256. #pragma mark Window and Control delegate methods
  257. - (void)popUpValueChanged:(id)sender
  258. {
  259. #pragma unused(sender)
  260. // Grab a copy of the selected RenderSystem name in Ogre::String format
  261. Ogre::String selectedRenderSystemName = Ogre::String([[[mRenderSystemsPopUp selectedItem] title] UTF8String]);
  262. // Save the current config value
  263. if((0 <= [mOptionsTable selectedRow]) && [mOptionsPopUp selectedItem])
  264. {
  265. Ogre::String value = Ogre::String([[[mOptionsPopUp selectedItem] title] UTF8String]);
  266. Ogre::String name = Ogre::String([[[[mOptions keyEnumerator] allObjects] objectAtIndex:[mOptionsTable selectedRow]] UTF8String]);
  267. Ogre::Root::getSingleton().getRenderSystemByName(selectedRenderSystemName)->setConfigOption(name, value);
  268. }
  269. }
  270. - (BOOL)windowShouldClose:(id)sender
  271. {
  272. #pragma unused(sender)
  273. // Hide the window
  274. [mConfigWindow orderOut:nil];
  275. [NSApp abortModal];
  276. return true;
  277. }
  278. - (void)cancelButtonPressed:(id)sender
  279. {
  280. #pragma unused(sender)
  281. // Hide the window
  282. [mConfigWindow orderOut:nil];
  283. [NSApp abortModal];
  284. }
  285. - (void)okButtonPressed:(id)sender
  286. {
  287. #pragma unused(sender)
  288. // Hide the window
  289. [mConfigWindow orderOut:nil];
  290. [NSApp stopModal];
  291. }
  292. #pragma mark NSTableView delegate and datasource methods
  293. #if defined(MAC_OS_X_VERSION_10_5) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
  294. - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
  295. #else
  296. - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
  297. #endif
  298. {
  299. #pragma unused(aTableView)
  300. return [[[mOptions keyEnumerator] allObjects] objectAtIndex:rowIndex];
  301. }
  302. #if defined(MAC_OS_X_VERSION_10_5) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
  303. - (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
  304. #else
  305. - (int)numberOfRowsInTableView:(NSTableView *)aTableView
  306. #endif
  307. {
  308. #pragma unused(aTableView)
  309. return [mOptions count];
  310. }
  311. // Intercept the request to select a new row. Update the popup's values.
  312. #if defined(MAC_OS_X_VERSION_10_5) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
  313. - (BOOL)tableView:(NSTableView *)aTableView shouldSelectRow:(NSInteger)rowIndex
  314. #else
  315. - (BOOL)tableView:(NSTableView *)aTableView shouldSelectRow:(int)rowIndex
  316. #endif
  317. {
  318. #pragma unused(aTableView)
  319. // Clear out the options popup menu
  320. [mOptionsPopUp removeAllItems];
  321. // Get the key for the selected table row
  322. NSString *key = [[[mOptions keyEnumerator] allObjects] objectAtIndex:rowIndex];
  323. // Add the available options
  324. [mOptionsPopUp addItemsWithTitles:[mOptions objectForKey:key]];
  325. // Grab a copy of the selected RenderSystem name in Ogre::String format
  326. Ogre::String selectedRenderSystemName = Ogre::String([[[mRenderSystemsPopUp selectedItem] title] UTF8String]);
  327. const Ogre::ConfigOptionMap& opts = Ogre::Root::getSingleton().getRenderSystemByName(selectedRenderSystemName)->getConfigOptions();
  328. // Select the item that is the current config option, if there is no current setting, just pick the top of the list
  329. Ogre::ConfigOptionMap::const_iterator it = opts.find([key UTF8String]);
  330. if (it != opts.end())
  331. [mOptionsPopUp selectItemWithTitle:[NSString stringWithCString:it->second.currentValue.c_str()
  332. encoding:NSASCIIStringEncoding]];
  333. if([mOptionsPopUp indexOfSelectedItem] < 0)
  334. [mOptionsPopUp selectItemAtIndex:0];
  335. // Always allow the new selection
  336. return YES;
  337. }
  338. #pragma mark Getters and Setters
  339. - (NSWindow *)getConfigWindow
  340. {
  341. return mConfigWindow;
  342. }
  343. - (void)setConfigWindow:(NSWindow *)window
  344. {
  345. mConfigWindow = window;
  346. }
  347. - (NSDictionary *)getOptions
  348. {
  349. return mOptions;
  350. }
  351. - (void)setOptions:(NSDictionary *)dict
  352. {
  353. mOptions = dict;
  354. }
  355. - (NSPopUpButton *)getRenderSystemsPopUp
  356. {
  357. return mRenderSystemsPopUp;
  358. }
  359. - (void)setRenderSystemsPopUp:(NSPopUpButton *)button
  360. {
  361. mRenderSystemsPopUp = button;
  362. }
  363. - (void)setOgreLogo:(NSImageView *)image
  364. {
  365. mOgreLogo = image;
  366. }
  367. - (NSImageView *)getOgreLogo
  368. {
  369. return mOgreLogo;
  370. }
  371. - (void)setOptionsTable:(NSTableView *)table
  372. {
  373. mOptionsTable = table;
  374. }
  375. - (NSTableView *)getOptionsTable
  376. {
  377. return mOptionsTable;
  378. }
  379. - (void)setOptionsPopUp:(NSPopUpButton *)button
  380. {
  381. mOptionsPopUp = button;
  382. }
  383. - (NSPopUpButton *)getOptionsPopUp
  384. {
  385. return mOptionsPopUp;
  386. }
  387. @end