toolbox.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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 ReloadToyOverlayButton::onClick(%this)
  200. {
  201. // Finish if no toy is loaded.
  202. if ( !isObject(Sandbox.ActiveToy) )
  203. return;
  204. // Reset custom controls.
  205. resetCustomControls();
  206. // Reload the toy.
  207. loadToy( Sandbox.ActiveToy );
  208. }
  209. //-----------------------------------------------------------------------------
  210. function RestartToyOverlayButton::onClick(%this)
  211. {
  212. // Finish if no toy is loaded.
  213. if ( !isObject(Sandbox.ActiveToy) )
  214. return;
  215. // Reset the toy.
  216. if ( Sandbox.ActiveToy.ScopeSet.isMethod("reset") )
  217. Sandbox.ActiveToy.ScopeSet.reset();
  218. }
  219. //-----------------------------------------------------------------------------
  220. function updateToolboxOptions()
  221. {
  222. // Finish if the sandbox scene is not available.
  223. if ( !isObject(SandboxScene) )
  224. return;
  225. // Set the scene color.
  226. SandboxScene.BackgroundColor = getStockColorName($activeSceneColor);
  227. SandboxScene.UseBackgroundColor = true;
  228. // Set option.
  229. if ( $pref::Sandbox::metricsOption )
  230. SandboxScene.setDebugOn( "metrics" );
  231. else
  232. SandboxScene.setDebugOff( "metrics" );
  233. // Set option.
  234. if ( $pref::Sandbox::fpsmetricsOption )
  235. SandboxScene.setDebugOn( "fps" );
  236. else
  237. SandboxScene.setDebugOff( "fps" );
  238. // Set option.
  239. if ( $pref::Sandbox::jointsOption )
  240. SandboxScene.setDebugOn( "joints" );
  241. else
  242. SandboxScene.setDebugOff( "joints" );
  243. // Set option.
  244. if ( $pref::Sandbox::wireframeOption )
  245. SandboxScene.setDebugOn( "wireframe" );
  246. else
  247. SandboxScene.setDebugOff( "wireframe" );
  248. // Set option.
  249. if ( $pref::Sandbox::aabbOption )
  250. SandboxScene.setDebugOn( "aabb" );
  251. else
  252. SandboxScene.setDebugOff( "aabb" );
  253. // Set option.
  254. if ( $pref::Sandbox::oobbOption )
  255. SandboxScene.setDebugOn( "oobb" );
  256. else
  257. SandboxScene.setDebugOff( "oobb" );
  258. // Set option.
  259. if ( $pref::Sandbox::sleepOption )
  260. SandboxScene.setDebugOn( "sleep" );
  261. else
  262. SandboxScene.setDebugOff( "sleep" );
  263. // Set option.
  264. if ( $pref::Sandbox::collisionOption )
  265. SandboxScene.setDebugOn( "collision" );
  266. else
  267. SandboxScene.setDebugOff( "collision" );
  268. // Set option.
  269. if ( $pref::Sandbox::positionOption )
  270. SandboxScene.setDebugOn( "position" );
  271. else
  272. SandboxScene.setDebugOff( "position" );
  273. // Set option.
  274. if ( $pref::Sandbox::sortOption )
  275. SandboxScene.setDebugOn( "sort" );
  276. else
  277. SandboxScene.setDebugOff( "sort" );
  278. // Set the options check-boxe.
  279. MetricsOptionCheckBox.setStateOn( $pref::Sandbox::metricsOption );
  280. FpsMetricsOptionCheckBox.setStateOn( $pref::Sandbox::fpsmetricsOption );
  281. JointsOptionCheckBox.setStateOn( $pref::Sandbox::jointsOption );
  282. WireframeOptionCheckBox.setStateOn( $pref::Sandbox::wireframeOption );
  283. AABBOptionCheckBox.setStateOn( $pref::Sandbox::aabbOption );
  284. OOBBOptionCheckBox.setStateOn( $pref::Sandbox::oobbOption );
  285. SleepOptionCheckBox.setStateOn( $pref::Sandbox::sleepOption );
  286. CollisionOptionCheckBox.setStateOn( $pref::Sandbox::collisionOption );
  287. PositionOptionCheckBox.setStateOn( $pref::Sandbox::positionOption );
  288. SortOptionCheckBox.setStateOn( $pref::Sandbox::sortOption );
  289. BatchOptionCheckBox.setStateOn( SandboxScene.getBatchingEnabled() );
  290. // Is this on the desktop?
  291. //if ( $platform $= "windows" || $platform $= "macos" )
  292. if ( $platform !$= "iOS" )
  293. {
  294. // Set the fullscreen check-box.
  295. FullscreenOptionButton.setStateOn( $pref::Video::fullScreen );
  296. // Set the full-screen mode appropriately.
  297. if ( isFullScreen() != $pref::Video::fullScreen )
  298. toggleFullScreen();
  299. }
  300. }
  301. //-----------------------------------------------------------------------------
  302. function setFullscreenOption( %flag )
  303. {
  304. $pref::Video::fullScreen = %flag;
  305. updateToolboxOptions();
  306. }
  307. //-----------------------------------------------------------------------------
  308. function setMetricsOption( %flag )
  309. {
  310. $pref::Sandbox::metricsOption = %flag;
  311. updateToolboxOptions();
  312. }
  313. //-----------------------------------------------------------------------------
  314. function setFPSMetricsOption( %flag )
  315. {
  316. $pref::Sandbox::fpsmetricsOption = %flag;
  317. updateToolboxOptions();
  318. }
  319. //-----------------------------------------------------------------------------
  320. function setMetricsOption( %flag )
  321. {
  322. $pref::Sandbox::metricsOption = %flag;
  323. updateToolboxOptions();
  324. }
  325. //-----------------------------------------------------------------------------
  326. function setJointsOption( %flag )
  327. {
  328. $pref::Sandbox::jointsOption = %flag;
  329. updateToolboxOptions();
  330. }
  331. //-----------------------------------------------------------------------------
  332. function setWireframeOption( %flag )
  333. {
  334. $pref::Sandbox::wireframeOption = %flag;
  335. updateToolboxOptions();
  336. }
  337. //-----------------------------------------------------------------------------
  338. function setAABBOption( %flag )
  339. {
  340. $pref::Sandbox::aabbOption = %flag;
  341. updateToolboxOptions();
  342. }
  343. //-----------------------------------------------------------------------------
  344. function setOOBBOption( %flag )
  345. {
  346. $pref::Sandbox::oobbOption = %flag;
  347. updateToolboxOptions();
  348. }
  349. //-----------------------------------------------------------------------------
  350. function setSleepOption( %flag )
  351. {
  352. $pref::Sandbox::sleepOption = %flag;
  353. updateToolboxOptions();
  354. }
  355. //-----------------------------------------------------------------------------
  356. function setCollisionOption( %flag )
  357. {
  358. $pref::Sandbox::collisionOption = %flag;
  359. updateToolboxOptions();
  360. }
  361. //-----------------------------------------------------------------------------
  362. function setPositionOption( %flag )
  363. {
  364. $pref::Sandbox::positionOption = %flag;
  365. updateToolboxOptions();
  366. }
  367. //-----------------------------------------------------------------------------
  368. function setSortOption( %flag )
  369. {
  370. $pref::Sandbox::sortOption = %flag;
  371. updateToolboxOptions();
  372. }
  373. //-----------------------------------------------------------------------------
  374. function ToyListScroller::scrollToNext(%this)
  375. {
  376. %currentScroll = %this.getScrollPositionY();
  377. %currentScroll += 85;
  378. %this.setScrollPosition(0, %currentScroll);
  379. }
  380. //-----------------------------------------------------------------------------
  381. function ToyListScroller::scrollToPrevious(%this)
  382. {
  383. %currentScroll = %this.getScrollPositionY();
  384. %currentScroll -= 85;
  385. %this.setScrollPosition(0, %currentScroll);
  386. }
  387. //-----------------------------------------------------------------------------
  388. function ToyListArray::initialize(%this, %index)
  389. {
  390. %this.clear();
  391. %currentExtent = %this.extent;
  392. %newExtent = getWord(%currentExtent, 0) SPC "20";
  393. %this.Extent = %newExtent;
  394. // Fetch the toy count.
  395. %toyCount = SandboxToys.getCount();
  396. // Populate toys in the selected category.
  397. for ( %toyIndex = 0; %toyIndex < %toyCount; %toyIndex++ )
  398. {
  399. // Fetch the toy module.
  400. %moduleDefinition = SandboxToys.getObject( %toyIndex );
  401. // Skip the toy module if the "all" category is not selected and if the toy is not in the selected category.
  402. if ( %index != $toyAllCategoryIndex && %moduleDefinition.ToyCategoryIndex != %index )
  403. continue;
  404. // Fetch the module version.
  405. %versionId = %moduleDefinition.versionId;
  406. // Format module title so that version#1 doesn't show version but all other versions do.
  407. if ( %versionId == 1 )
  408. %moduleTitle = %moduleDefinition.moduleId;
  409. else
  410. %moduleTitle = %moduleDefinition.moduleId SPC "(v" @ %moduleDefinition.versionId @ ")";
  411. // Add the toy GUI list.
  412. %this.addToyButton(%moduleTitle, %moduleDefinition);
  413. // Select the toy if it's the default and we've not selected a toy yet.
  414. if (!$defaultToySelected && %moduleDefinition.moduleId $= $pref::Sandbox::defaultToyId && %moduleDefinition.versionId == $pref::Sandbox::defaultToyVersionId )
  415. {
  416. $defaultToySelected = true;
  417. $defaultModuleID = %moduleDefinition.getId();
  418. }
  419. }
  420. }
  421. //-----------------------------------------------------------------------------
  422. function ToyListArray::addToyButton(%this, %moduleTitle, %moduleDefintion)
  423. {
  424. %button = new GuiButtonCtrl()
  425. {
  426. canSaveDynamicFields = "0";
  427. HorizSizing = "relative";
  428. class = "ToySelectButton";
  429. VertSizing = "relative";
  430. isContainer = "0";
  431. Profile = "BlueButtonProfile";
  432. toolTipProfile = "GuiToolTipProfile";
  433. toolTip = %moduleDefintion.Description;
  434. Position = "0 0";
  435. Extent = "160 80";
  436. Visible = "1";
  437. toy = %moduleDefintion.getId();
  438. isContainer = "0";
  439. Active = "1";
  440. text = %moduleTitle;
  441. groupNum = "-1";
  442. buttonType = "PushButton";
  443. useMouseEvents = "0";
  444. };
  445. %button.command = %button @ ".performSelect();";
  446. %this.add(%button);
  447. }
  448. //-----------------------------------------------------------------------------
  449. function ToySelectButton::performSelect(%this)
  450. {
  451. // Finish if already selected.
  452. if ( %this.toy == Sandbox.ActiveToy )
  453. return;
  454. // Load the selected toy.
  455. loadToy( %this.toy );
  456. }