ChooseLevelMenu.tscript 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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. //----------------------------------------
  23. function ChooseLevelMenu::onAdd( %this )
  24. {
  25. if(!isObject(ChooseLevelAssetQuery))
  26. new AssetQuery(ChooseLevelAssetQuery);
  27. }
  28. function ChooseLevelMenu::onWake(%this)
  29. {
  30. LevelPreviewArray.clear();
  31. ChooseLevelAssetQuery.clear();
  32. AssetDatabase.findAssetType(ChooseLevelAssetQuery, "LevelAsset");
  33. %count = ChooseLevelAssetQuery.getCount();
  34. if(%count == 0 && !IsDirectory("tools"))
  35. {
  36. //We have no levels found. Prompt the user to open the editor to the default level if the tools are present
  37. MessageBoxOK("Error", "No levels were found in any modules. Please ensure you have modules loaded that contain gameplay code and level files.",
  38. "Canvas.popDialog(ChooseLevelMenu); if(isObject(ChooseLevelMenu.returnGui) && ChooseLevelMenu.returnGui.isMethod(\"onReturnTo\")) ChooseLevelMenu.returnGui.onReturnTo();");
  39. ChooseLevelAssetQuery.delete();
  40. return;
  41. }
  42. for(%i=0; %i < %count; %i++)
  43. {
  44. %assetId = ChooseLevelAssetQuery.getAsset(%i);
  45. if(AssetDatabase.getAssetModule(%assetId).ModuleId $= "ToolsModule")
  46. continue;
  47. %levelAsset = AssetDatabase.acquireAsset(%assetId);
  48. %file = %levelAsset.getLevelPath();
  49. if ( !isFile(%file) )
  50. continue;
  51. %levelPreviewImg = %levelAsset.getPreviewImagePath();
  52. if (!isFile(%levelPreviewImg))
  53. %levelPreviewImg = "UI:no_preview_image";
  54. %preview = new GuiContainer() {
  55. extent = "480 480";
  56. levelAssetId = %assetId;
  57. new GuiButtonCtrl() {
  58. position = "0 0";
  59. extent = "480 480";
  60. buttonType = "ToggleButton";
  61. profile = LevelPreviewButtonProfile;
  62. horizSizing = "width";
  63. vertSizing = "height";
  64. internalName = "button";
  65. class = "LevelPreviewButton";
  66. command = "$selectedLevelAsset = " @ %assetId @ ";";
  67. altCommand = "ChooseLevelBegin(1);"; //allow doubleclick to quick action it
  68. };
  69. new GuiBitmapCtrl() {
  70. position = "20 0";
  71. extent = "440 440";
  72. BitmapAsset = %levelPreviewImg;
  73. horizSizing = "width";
  74. vertSizing = "bottom";
  75. profile = GuiNonModalDefaultProfile;
  76. };
  77. new GuiTextCtrl() {
  78. position = "20 445";
  79. extent = "440 15";
  80. text = %levelAsset.levelName;
  81. profile = MenuSubHeaderText;
  82. internalName = "levelNameTxt";
  83. };
  84. new GuiMLTextCtrl() {
  85. position = "20 465";
  86. extent = "440 15";
  87. text = %levelAsset.levelDescription;
  88. profile = GuiMLTextProfile;
  89. internalName = "levelDescTxt";
  90. };
  91. };
  92. LevelPreviewArray.add(%preview);
  93. }
  94. // Also add the new level mission as defined in the world editor settings
  95. // if we are choosing a level to launch in the editor.
  96. if ( %this.launchInEditor )
  97. {
  98. %this.addMissionFile( "tools/levels/DefaultEditorLevel.mis" );
  99. }
  100. //LevelList.setSelected(0);
  101. if(!$pref::HostMultiPlayer)
  102. ChooseLevelTitleText.setText("SINGLE PLAYER");
  103. else
  104. ChooseLevelTitleText.setText("CREATE SERVER");
  105. $MenuList = LevelPreviewArray;
  106. $MenuList.listPosition = 0;
  107. $MenuList.syncGui();
  108. }
  109. if(!isObject( ChooseLevelActionMap ) )
  110. {
  111. new ActionMap(ChooseLevelActionMap){};
  112. //Null the up/down nav so we can swap in left/right nav
  113. ChooseLevelActionMap.bindCmd( keyboard, w, "" );
  114. ChooseLevelActionMap.bindCmd( keyboard, s, "" );
  115. ChooseLevelActionMap.bindCmd( gamepad, yaxis, "" );
  116. ChooseLevelActionMap.bindCmd( gamepad, upov, "" );
  117. ChooseLevelActionMap.bindCmd( gamepad, dpov, "" );
  118. ChooseLevelActionMap.bind( keyboard, a, BaseUINavigatePrev );
  119. ChooseLevelActionMap.bind( keyboard, d, BaseUINavigateNext );
  120. ChooseLevelActionMap.bind( gamepad, xaxis, "D", "-0.23 0.23", BaseUIStickNavigate );
  121. ChooseLevelActionMap.bind( gamepad, lpov, BaseUINavigatePrev );
  122. ChooseLevelActionMap.bind( gamepad, rpov, BaseUINavigateNext );
  123. ChooseLevelActionMap.bind( keyboard, Enter, ChooseLevelBegin );
  124. ChooseLevelActionMap.bind( gamepad, btn_a, ChooseLevelBegin );
  125. }
  126. function LevelPreviewArray::syncGUI(%this)
  127. {
  128. %this.callOnChildren("setHighlighted", false);
  129. %btn = %this.getObject(%this.listPosition);
  130. %btn-->button.setHighlighted(true);
  131. $selectedLevelAsset = %btn.levelAssetId;
  132. }
  133. function ChooseLevelBegin(%val)
  134. {
  135. if(%val)
  136. {
  137. // So we can't fire the button when loading is in progress.
  138. if ( isObject( ServerGroup ) )
  139. return;
  140. Canvas.popDialog();
  141. %entry = LevelPreviewArray.getObject(LevelPreviewArray.listPosition);
  142. if(!AssetDatabase.isDeclaredAsset(%entry.levelAssetId))
  143. {
  144. MessageBoxOK("Error", "Selected level preview does not have a valid level asset!");
  145. return;
  146. }
  147. // Launch the chosen level with the editor open?
  148. if ( ChooseLevelMenu.launchInEditor )
  149. {
  150. activatePackage( "BootEditor" );
  151. ChooseLevelMenu.launchInEditor = false;
  152. StartGame(%entry.levelAssetId, "SinglePlayer");
  153. }
  154. else
  155. {
  156. StartGame(%entry.levelAssetId);
  157. }
  158. }
  159. }
  160. function ChooseLevelMenu::onSleep( %this )
  161. {
  162. // This is set from the outside, only stays true for a single wake/sleep
  163. // cycle.
  164. %this.launchInEditor = false;
  165. }
  166. function LevelPreviewButton::onHighlighted(%this, %highlighted)
  167. {
  168. %container = %this.getParent();
  169. %container-->levelNameTxt.profile = %highlighted ? MenuSubHeaderTextHighlighted : MenuSubHeaderText;
  170. %container-->levelDescTxt.profile = %highlighted ? GuiMLTextProfileHighlighted : GuiMLTextProfile;
  171. LevelPreviewScroll.scrollToObject(%this);
  172. }