toolbox.cs 19 KB

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