2
0

toolbox.cs 18 KB

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