toolbox.cs 19 KB

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