toolbox.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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. $toyAllCategoryIndex = 1;
  23. $defaultToySelected = false;
  24. //-----------------------------------------------------------------------------
  25. function ToyCategorySelectList::onSelect(%this)
  26. {
  27. // Fetch the index.
  28. %index = %this.currentToyCategory;
  29. $defaultToySelected = false;
  30. ToyListArray.initialize(%index);
  31. ToyListScroller.computeSizes();
  32. ToyListScroller.scrollToTop();
  33. // Unload the active toy.
  34. unloadToy();
  35. // Flag as the default toy selected.
  36. if ($defaultToySelected)
  37. {
  38. loadToy($defaultModuleID);
  39. }
  40. else
  41. {
  42. if ( ToyListArray.getCount() > 0 )
  43. {
  44. %firstToyButton = ToyListArray.getObject(0);
  45. if (isObject(%firstToyButton))
  46. %firstToyButton.performSelect();
  47. }
  48. }
  49. }
  50. //-----------------------------------------------------------------------------
  51. function ToyCategorySelectList::initialize(%this)
  52. {
  53. %this.toyCategories[$toyAllCategoryIndex] = "All";
  54. %this.toyCategories[$toyAllCategoryIndex+1] = "Physics";
  55. %this.toyCategories[$toyAllCategoryIndex+2] = "Features";
  56. %this.toyCategories[$toyAllCategoryIndex+3] = "Stress Testing";
  57. %this.toyCategories[$toyAllCategoryIndex+4] = "Fun and Games";
  58. %this.toyCategories[$toyAllCategoryIndex+5] = "Custom";
  59. %this.maxToyCategories = $toyAllCategoryIndex + 6;
  60. // Set the "All" category as the default.
  61. // NOTE: This is important to use so that the user-configurable default toy
  62. // can be selected at start-up.
  63. %this.currentToyCategory = $toyAllCategoryIndex;
  64. %this.setText("All");
  65. %this.onSelect();
  66. }
  67. //-----------------------------------------------------------------------------
  68. function ToyCategorySelectList::nextCategory(%this)
  69. {
  70. if (%this.currentToyCategory >= %this.maxToyCategories)
  71. return;
  72. %this.currentToyCategory++;
  73. %text = %this.toyCategories[%this.currentToyCategory];
  74. %this.setText(%text);
  75. %this.onSelect();
  76. }
  77. //-----------------------------------------------------------------------------
  78. function ToyCategorySelectList::previousCategory(%this)
  79. {
  80. if (%this.currentToyCategory <= $toyAllCategoryIndex)
  81. return;
  82. %this.currentToyCategory--;
  83. %text = %this.toyCategories[%this.currentToyCategory];
  84. %this.setText(%text);
  85. %this.onSelect();
  86. }
  87. //-----------------------------------------------------------------------------
  88. function initializeToolbox()
  89. {
  90. // Populate the stock colors.
  91. %colorCount = getStockColorCount();
  92. for ( %i = 0; %i < %colorCount; %i++ )
  93. {
  94. // Fetch stock color name.
  95. %colorName = getStockColorName(%i);
  96. // Add to the list.
  97. BackgroundColorSelectList.add( getStockColorName(%i), %i );
  98. // Select the color if it's the default one.
  99. if ( %colorName $= $pref::Sandbox::defaultBackgroundColor )
  100. BackgroundColorSelectList.setSelected( %i );
  101. }
  102. BackgroundColorSelectList.sort();
  103. ToyCategorySelectList.initialize();
  104. // Is this on the desktop?
  105. if ( $platform $= "windows" || $platform $= "macos" )
  106. {
  107. // Yes, so make the controls screen controls visible.
  108. ResolutionSelectLabel.Visible = true;
  109. ResolutionSelectList.Visible = true;
  110. FullscreenOptionLabel.Visible = true;
  111. FullscreenOptionButton.Visible = true;
  112. // Fetch the active resolution.
  113. %activeResolution = getRes();
  114. %activeWidth = getWord(%activeResolution, 0);
  115. %activeHeight = getWord(%activeResolution, 1);
  116. %activeBpp = getWord(%activeResolution, 2);
  117. // Fetch the resolutions.
  118. %resolutionList = getResolutionList( $pref::Video::displayDevice );
  119. %resolutionCount = getWordCount( %resolutionList ) / 3;
  120. %inputIndex = 0;
  121. %outputIndex = 0;
  122. for( %i = 0; %i < %resolutionCount; %i++ )
  123. {
  124. // Fetch the resolution entry.
  125. %width = getWord( %resolutionList, %inputIndex );
  126. %height = getWord( %resolutionList, %inputIndex+1 );
  127. %bpp = getWord( %resolutionList, %inputIndex+2 );
  128. %inputIndex += 3;
  129. // Skip the 16-bit ones.
  130. if ( %bpp == 16 )
  131. continue;
  132. // Store the resolution.
  133. $sandboxResolutions[%outputIndex] = %width SPC %height SPC %bpp;
  134. // Add to the list.
  135. ResolutionSelectList.add( %width @ "x" @ %height SPC "(" @ %bpp @ ")", %outputIndex );
  136. // Select the resolution if it's the default one.
  137. if ( %width == %activeWidth && %height == %activeHeight && %bpp == %activeBpp )
  138. ResolutionSelectList.setSelected( %outputIndex );
  139. %outputIndex++;
  140. }
  141. }
  142. // Configure the main overlay.
  143. SandboxWindow.add(MainOverlay);
  144. %horizPosition = getWord(SandboxWindow.Extent, 0) - getWord(MainOverlay.Extent, 0);
  145. %verticalPosition = getWord(SandboxWindow.Extent, 1) - getWord(MainOverlay.Extent, 1);
  146. MainOverlay.position = %horizPosition SPC %verticalPosition;
  147. }
  148. //-----------------------------------------------------------------------------
  149. function toggleToolbox(%make)
  150. {
  151. // Finish if being released.
  152. if ( !%make )
  153. return;
  154. // Finish if the console is awake.
  155. if ( ConsoleDialog.isAwake() )
  156. return;
  157. // Is the toolbox awake?
  158. if ( ToolboxDialog.isAwake() )
  159. {
  160. // Yes, so deactivate it.
  161. if ( $enableDirectInput )
  162. activateKeyboard();
  163. Canvas.popDialog(ToolboxDialog);
  164. MainOverlay.setVisible(1);
  165. return;
  166. }
  167. // Activate it.
  168. if ( $enableDirectInput )
  169. deactivateKeyboard();
  170. MainOverlay.setVisible(0);
  171. Canvas.pushDialog(ToolboxDialog);
  172. }
  173. //-----------------------------------------------------------------------------
  174. function BackgroundColorSelectList::onSelect(%this)
  175. {
  176. // Fetch the index.
  177. $activeSceneColor = %this.getSelected();
  178. // Finish if the sandbox scene is not available.
  179. if ( !isObject(SandboxScene) )
  180. return;
  181. // Set the scene color.
  182. SandboxScene.BackgroundColor = getStockColorName($activeSceneColor);
  183. SandboxScene.UseBackgroundColor = true;
  184. }
  185. //-----------------------------------------------------------------------------
  186. function ResolutionSelectList::onSelect(%this)
  187. {
  188. // Finish if the sandbox scene is not available.
  189. if ( !isObject(SandboxScene) )
  190. return;
  191. // Fetch the index.
  192. %index = %this.getSelected();
  193. // Fetch resolution.
  194. %resolution = $sandboxResolutions[%index];
  195. // Set the screen mode.
  196. setScreenMode( GetWord( %resolution , 0 ), GetWord( %resolution, 1 ), GetWord( %resolution, 2 ), $pref::Video::fullScreen );
  197. }
  198. //-----------------------------------------------------------------------------
  199. function PauseSceneModeButton::onClick(%this)
  200. {
  201. // Sanity!
  202. if ( !isObject(SandboxScene) )
  203. {
  204. error( "Cannot pause/unpause the Sandbox scene as it does not exist." );
  205. return;
  206. }
  207. // Toggle the scene pause.
  208. SandboxScene.setScenePause( !SandboxScene.getScenePause() );
  209. }
  210. //-----------------------------------------------------------------------------
  211. function ReloadToyOverlayButton::onClick(%this)
  212. {
  213. // Finish if no toy is loaded.
  214. if ( !isObject(Sandbox.ActiveToy) )
  215. return;
  216. // Reset custom controls.
  217. resetCustomControls();
  218. // Reload the toy.
  219. loadToy( Sandbox.ActiveToy );
  220. }
  221. //-----------------------------------------------------------------------------
  222. function RestartToyOverlayButton::onClick(%this)
  223. {
  224. // Finish if no toy is loaded.
  225. if ( !isObject(Sandbox.ActiveToy) )
  226. return;
  227. // Reset the toy.
  228. if ( Sandbox.ActiveToy.ScopeSet.isMethod("reset") )
  229. Sandbox.ActiveToy.ScopeSet.reset();
  230. }
  231. //-----------------------------------------------------------------------------
  232. function updateToolboxOptions()
  233. {
  234. // Finish if the sandbox scene is not available.
  235. if ( !isObject(SandboxScene) )
  236. return;
  237. // Set the scene color.
  238. SandboxScene.BackgroundColor = getStockColorName($activeSceneColor);
  239. SandboxScene.UseBackgroundColor = true;
  240. // Set option.
  241. if ( $pref::Sandbox::metricsOption )
  242. SandboxScene.setDebugOn( "metrics" );
  243. else
  244. SandboxScene.setDebugOff( "metrics" );
  245. // Set option.
  246. if ( $pref::Sandbox::fpsmetricsOption )
  247. SandboxScene.setDebugOn( "fps" );
  248. else
  249. SandboxScene.setDebugOff( "fps" );
  250. // Set option.
  251. if ( $pref::Sandbox::jointsOption )
  252. SandboxScene.setDebugOn( "joints" );
  253. else
  254. SandboxScene.setDebugOff( "joints" );
  255. // Set option.
  256. if ( $pref::Sandbox::wireframeOption )
  257. SandboxScene.setDebugOn( "wireframe" );
  258. else
  259. SandboxScene.setDebugOff( "wireframe" );
  260. // Set option.
  261. if ( $pref::Sandbox::aabbOption )
  262. SandboxScene.setDebugOn( "aabb" );
  263. else
  264. SandboxScene.setDebugOff( "aabb" );
  265. // Set option.
  266. if ( $pref::Sandbox::oobbOption )
  267. SandboxScene.setDebugOn( "oobb" );
  268. else
  269. SandboxScene.setDebugOff( "oobb" );
  270. // Set option.
  271. if ( $pref::Sandbox::sleepOption )
  272. SandboxScene.setDebugOn( "sleep" );
  273. else
  274. SandboxScene.setDebugOff( "sleep" );
  275. // Set option.
  276. if ( $pref::Sandbox::collisionOption )
  277. SandboxScene.setDebugOn( "collision" );
  278. else
  279. SandboxScene.setDebugOff( "collision" );
  280. // Set option.
  281. if ( $pref::Sandbox::positionOption )
  282. SandboxScene.setDebugOn( "position" );
  283. else
  284. SandboxScene.setDebugOff( "position" );
  285. // Set option.
  286. if ( $pref::Sandbox::sortOption )
  287. SandboxScene.setDebugOn( "sort" );
  288. else
  289. SandboxScene.setDebugOff( "sort" );
  290. // Set the options check-boxe.
  291. MetricsOptionCheckBox.setStateOn( $pref::Sandbox::metricsOption );
  292. FpsMetricsOptionCheckBox.setStateOn( $pref::Sandbox::fpsmetricsOption );
  293. JointsOptionCheckBox.setStateOn( $pref::Sandbox::jointsOption );
  294. WireframeOptionCheckBox.setStateOn( $pref::Sandbox::wireframeOption );
  295. AABBOptionCheckBox.setStateOn( $pref::Sandbox::aabbOption );
  296. OOBBOptionCheckBox.setStateOn( $pref::Sandbox::oobbOption );
  297. SleepOptionCheckBox.setStateOn( $pref::Sandbox::sleepOption );
  298. CollisionOptionCheckBox.setStateOn( $pref::Sandbox::collisionOption );
  299. PositionOptionCheckBox.setStateOn( $pref::Sandbox::positionOption );
  300. SortOptionCheckBox.setStateOn( $pref::Sandbox::sortOption );
  301. BatchOptionCheckBox.setStateOn( SandboxScene.getBatchingEnabled() );
  302. // Is this on the desktop?
  303. //if ( $platform $= "windows" || $platform $= "macos" )
  304. if ( $platform !$= "iOS" )
  305. {
  306. // Set the fullscreen check-box.
  307. FullscreenOptionButton.setStateOn( $pref::Video::fullScreen );
  308. // Set the full-screen mode appropriately.
  309. if ( isFullScreen() != $pref::Video::fullScreen )
  310. toggleFullScreen();
  311. }
  312. }
  313. //-----------------------------------------------------------------------------
  314. function setFullscreenOption( %flag )
  315. {
  316. $pref::Video::fullScreen = %flag;
  317. updateToolboxOptions();
  318. }
  319. //-----------------------------------------------------------------------------
  320. function setMetricsOption( %flag )
  321. {
  322. $pref::Sandbox::metricsOption = %flag;
  323. updateToolboxOptions();
  324. }
  325. //-----------------------------------------------------------------------------
  326. function setFPSMetricsOption( %flag )
  327. {
  328. $pref::Sandbox::fpsmetricsOption = %flag;
  329. updateToolboxOptions();
  330. }
  331. //-----------------------------------------------------------------------------
  332. function setMetricsOption( %flag )
  333. {
  334. $pref::Sandbox::metricsOption = %flag;
  335. updateToolboxOptions();
  336. }
  337. //-----------------------------------------------------------------------------
  338. function setJointsOption( %flag )
  339. {
  340. $pref::Sandbox::jointsOption = %flag;
  341. updateToolboxOptions();
  342. }
  343. //-----------------------------------------------------------------------------
  344. function setWireframeOption( %flag )
  345. {
  346. $pref::Sandbox::wireframeOption = %flag;
  347. updateToolboxOptions();
  348. }
  349. //-----------------------------------------------------------------------------
  350. function setAABBOption( %flag )
  351. {
  352. $pref::Sandbox::aabbOption = %flag;
  353. updateToolboxOptions();
  354. }
  355. //-----------------------------------------------------------------------------
  356. function setOOBBOption( %flag )
  357. {
  358. $pref::Sandbox::oobbOption = %flag;
  359. updateToolboxOptions();
  360. }
  361. //-----------------------------------------------------------------------------
  362. function setSleepOption( %flag )
  363. {
  364. $pref::Sandbox::sleepOption = %flag;
  365. updateToolboxOptions();
  366. }
  367. //-----------------------------------------------------------------------------
  368. function setCollisionOption( %flag )
  369. {
  370. $pref::Sandbox::collisionOption = %flag;
  371. updateToolboxOptions();
  372. }
  373. //-----------------------------------------------------------------------------
  374. function setPositionOption( %flag )
  375. {
  376. $pref::Sandbox::positionOption = %flag;
  377. updateToolboxOptions();
  378. }
  379. //-----------------------------------------------------------------------------
  380. function setSortOption( %flag )
  381. {
  382. $pref::Sandbox::sortOption = %flag;
  383. updateToolboxOptions();
  384. }
  385. //-----------------------------------------------------------------------------
  386. function ToyListScroller::scrollToNext(%this)
  387. {
  388. %currentScroll = %this.getScrollPositionY();
  389. %currentScroll += 85;
  390. %this.setScrollPosition(0, %currentScroll);
  391. }
  392. //-----------------------------------------------------------------------------
  393. function ToyListScroller::scrollToPrevious(%this)
  394. {
  395. %currentScroll = %this.getScrollPositionY();
  396. %currentScroll -= 85;
  397. %this.setScrollPosition(0, %currentScroll);
  398. }
  399. //-----------------------------------------------------------------------------
  400. function ToyListArray::initialize(%this, %index)
  401. {
  402. %this.clear();
  403. %currentExtent = %this.extent;
  404. %newExtent = getWord(%currentExtent, 0) SPC "20";
  405. %this.Extent = %newExtent;
  406. // Fetch the toy count.
  407. %toyCount = SandboxToys.getCount();
  408. // Populate toys in the selected category.
  409. for ( %toyIndex = 0; %toyIndex < %toyCount; %toyIndex++ )
  410. {
  411. // Fetch the toy module.
  412. %moduleDefinition = SandboxToys.getObject( %toyIndex );
  413. // Skip the toy module if the "all" category is not selected and if the toy is not in the selected category.
  414. if ( %index != $toyAllCategoryIndex && %moduleDefinition.ToyCategoryIndex != %index )
  415. continue;
  416. // Fetch the module version.
  417. %versionId = %moduleDefinition.versionId;
  418. // Format module title so that version#1 doesn't show version but all other versions do.
  419. if ( %versionId == 1 )
  420. %moduleTitle = %moduleDefinition.moduleId;
  421. else
  422. %moduleTitle = %moduleDefinition.moduleId SPC "(v" @ %moduleDefinition.versionId @ ")";
  423. // Add the toy GUI list.
  424. %this.addToyButton(%moduleTitle, %moduleDefinition);
  425. // Select the toy if it's the default and we've not selected a toy yet.
  426. if (!$defaultToySelected && %moduleDefinition.moduleId $= $pref::Sandbox::defaultToyId && %moduleDefinition.versionId == $pref::Sandbox::defaultToyVersionId )
  427. {
  428. $defaultToySelected = true;
  429. $defaultModuleID = %moduleDefinition.getId();
  430. }
  431. }
  432. }
  433. //-----------------------------------------------------------------------------
  434. function ToyListArray::addToyButton(%this, %moduleTitle, %moduleDefintion)
  435. {
  436. %button = new GuiButtonCtrl()
  437. {
  438. canSaveDynamicFields = "0";
  439. HorizSizing = "relative";
  440. class = "ToySelectButton";
  441. VertSizing = "relative";
  442. isContainer = "0";
  443. Profile = "BlueButtonProfile";
  444. toolTipProfile = "GuiToolTipProfile";
  445. toolTip = %moduleDefintion.Description;
  446. Position = "0 0";
  447. Extent = "160 80";
  448. Visible = "1";
  449. toy = %moduleDefintion.getId();
  450. isContainer = "0";
  451. Active = "1";
  452. text = %moduleTitle;
  453. groupNum = "-1";
  454. buttonType = "PushButton";
  455. useMouseEvents = "0";
  456. };
  457. %button.command = %button @ ".performSelect();";
  458. %this.add(%button);
  459. }
  460. //-----------------------------------------------------------------------------
  461. function ToySelectButton::performSelect(%this)
  462. {
  463. // Finish if already selected.
  464. if ( %this.toy == Sandbox.ActiveToy )
  465. return;
  466. // Load the selected toy.
  467. loadToy( %this.toy );
  468. }