ChooseLevelMenu.tscript 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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. $LevelPreviewButtonSize = "445 60";
  28. }
  29. function ChooseLevelMenu::fillPrefEntries( %this )
  30. {
  31. playerNameCTRL.setText($Pref::Player::Name);
  32. serverNameCTRL.setText($Pref::Server::Name);
  33. serverPassCTRL.setText($Pref::Server::Password);
  34. serverInfoCTRL.setText($Pref::Server::Info);
  35. serverMaxPlayersCTRL.setText($Pref::Server::MaxPlayers);
  36. }
  37. function ChooseLevelMenu::onWake(%this)
  38. {
  39. %this.fillPrefEntries();
  40. refreshGameModesList();
  41. refreshLevelsList();
  42. if(!$pref::HostMultiPlayer)
  43. ChooseLevelTitleText.setText("SINGLE PLAYER");
  44. else
  45. ChooseLevelTitleText.setText("CREATE SERVER");
  46. ChooseLevelMenuTabList-->ConfigBtn.visible = $pref::HostMultiPlayer;
  47. ChooseLevelMenuTabList.visible = true;//$pref::HostMultiPlayer;
  48. ChooseLevelMenuNavButtonOverlay.visible = true;//$pref::HostMultiPlayer;
  49. //If we've only got one gamemode, just force it to use that one, as we can
  50. //assume that is THE game's gamemode
  51. %gamemodeList = getGameModesList();
  52. if(%gamemodeList.count() == 1)
  53. {
  54. %gameModeObj = %gamemodeList.getKey(0);
  55. if(isObject(%gameModeObj))
  56. {
  57. %gameModeObj.active = true;
  58. ChooseLevelMenuTabList-->LevelBtn.active = true;
  59. ChooseLevelMenuTabList-->GameModeBtn.active = false;
  60. ChooseLevelMenu.openMenu(1);
  61. return;
  62. }
  63. }
  64. else
  65. {
  66. //Otherwise open the gamemode list as normal
  67. %this.schedule(32, openMenu, 0);
  68. }
  69. }
  70. function refreshGameModesList()
  71. {
  72. GameModePreviewArray.clear();
  73. $pref::Server::GameMode = "";
  74. ChooseLevelMenuTabList-->LevelBtn.active = false;
  75. //popilate the gamemodes list first
  76. %gamemodeList = getGameModesList();
  77. %gameModeCount = %gamemodeList.count();
  78. for (%i=0; %i<%gameModeCount; %i++)
  79. {
  80. %gameModeObj = %gamemodeList.getKey(%i);
  81. if(isObject(%gameModeObj))
  82. {
  83. %gameModeName = %gameModeObj.gameModeName;
  84. if(%gameModeName $= "")
  85. %gameModeName = %gameModeObj.getName();
  86. %preview = new GuiContainer() {
  87. position = "0 0";
  88. extent = $LevelPreviewButtonSize;
  89. horizSizing = "right";
  90. vertSizing = "bottom";
  91. gameModeObj = %gameModeObj;
  92. gameModeDesc = %gameModeObj.description;
  93. previewImage = %gameModeObj.previewImage;
  94. cansave = false;
  95. new GuiToggleButtonCtrl() {
  96. position = $LevelPreviewButtonSize.y SPC 0;
  97. extent = $LevelPreviewButtonSize.x - $LevelPreviewButtonSize.y - 5 SPC $LevelPreviewButtonSize.y;
  98. profile = GuiMenuButtonProfile;
  99. horizSizing = "right";
  100. vertSizing = "bottom";
  101. internalName = "button";
  102. class = "GameModePreviewButton";
  103. text = %gameModeName;
  104. command = "ToggleGameMode(" @ %i @ ");"; //allow doubleclick to quick action it
  105. textMargin = 120;
  106. };
  107. new GuiBitmapCtrl() {
  108. position = "0 0";
  109. extent = $LevelPreviewButtonSize.y SPC $LevelPreviewButtonSize.y;
  110. internalName = "checkbox";
  111. bitmapAsset = "UI:toggleMarker_image";
  112. };
  113. };
  114. GameModePreviewArray.add(%preview);
  115. }
  116. }
  117. }
  118. function refreshLevelsList()
  119. {
  120. LevelPreviewArray.clear();
  121. //fetch the levelAssets
  122. ChooseLevelAssetQuery.clear();
  123. AssetDatabase.findAssetType(ChooseLevelAssetQuery, "LevelAsset");
  124. %count = ChooseLevelAssetQuery.getCount();
  125. if(%count == 0 && !IsDirectory("tools"))
  126. {
  127. //We have no levels found. Prompt the user to open the editor to the default level if the tools are present
  128. MessageBoxOK("Error", "No levels were found in any modules. Please ensure you have modules loaded that contain gameplay code and level files.",
  129. "Canvas.popDialog(ChooseLevelMenu); if(isObject(ChooseLevelMenu.returnGui) && ChooseLevelMenu.returnGui.isMethod(\"onReturnTo\")) ChooseLevelMenu.returnGui.onReturnTo();");
  130. ChooseLevelAssetQuery.clear();
  131. return;
  132. }
  133. %gameModesList = getGameModesList();
  134. //filter the levelAssets by the gamemode selected
  135. %filteredIndex = 0;
  136. for(%i=0; %i < %count; %i++)
  137. {
  138. %assetId = ChooseLevelAssetQuery.getAsset(%i);
  139. if(AssetDatabase.getAssetModule(%assetId).ModuleId $= "ToolsModule")
  140. continue;
  141. %levelAsset = AssetDatabase.acquireAsset(%assetId);
  142. %file = %levelAsset.getLevelPath();
  143. if ( !isFile(%file) )
  144. continue;
  145. if( %levelAsset.isSubScene )
  146. continue;
  147. //filter for selected gamemode
  148. %levelGameModes = %levelAsset.gameModesNames;
  149. //If the level has no gamemodes defined, we just assume the default case of it being a wildcard to whatever gamemode
  150. //is available
  151. if(%levelGameModes $= "")
  152. {
  153. %foundGameModeMatch = true;
  154. }
  155. else
  156. {
  157. %foundGameModeMatch = false;
  158. for(%gm = 0; %gm < getTokenCount(%levelGameModes, ";"); %gm++)
  159. {
  160. %gameModeName = getToken(%levelGameModes, ";", %gm);
  161. for(%g=0; %g < %gameModesList.count(); %g++)
  162. {
  163. %gameModeObj = %gameModesList.getKey(%g);
  164. if(!isObject(%gameModeObj) || (!%gameModeObj.active && !%gameModeObj.alwaysActive && %gameModesList.count() > 1))
  165. continue;
  166. if(%gameModeName $= %gameModeObj.getName())
  167. {
  168. %foundGameModeMatch = true;
  169. break;
  170. }
  171. }
  172. if(%foundGameModeMatch)
  173. break;
  174. }
  175. }
  176. if(!%foundGameModeMatch)
  177. continue;
  178. %levelPreviewImg = %levelAsset.getPreviewImagePath();
  179. if (!isFile(%levelPreviewImg))
  180. %levelPreviewImg = "UI:no_preview_image";
  181. %preview = new GuiIconButtonCtrl() {
  182. position = "0 0";
  183. extent = $LevelPreviewButtonSize;
  184. buttonType = "PushButton";
  185. profile = GuiMenuButtonLeftJustProfile;
  186. horizSizing = "right";
  187. vertSizing = "bottom";
  188. internalName = "button";
  189. class = "LevelPreviewButton";
  190. //command = "$selectedLevelAsset = " @ %assetId @ ";";
  191. altCommand = "ChooseLevelBegin(" @ %filteredIndex @ ");"; //allow doubleclick to quick action it
  192. text = %levelAsset.levelName;
  193. bitmapAsset = %levelPreviewImg;
  194. levelAsset = %levelAsset;
  195. levelAssetId = %assetId;
  196. iconLocation = "left";
  197. sizeIconToButton = true;
  198. makeIconSquare = true;
  199. textLocation = "left";
  200. textMargin = 120;
  201. groupNum = 2;
  202. cansave = false;
  203. };
  204. LevelPreviewArray.add(%preview);
  205. %filteredIndex++;
  206. }
  207. GameModePreviewArray.listPosition = 0;
  208. LevelPreviewArray.listPosition = 0;
  209. // Also add the new level mission as defined in the world editor settings
  210. // if we are choosing a level to launch in the editor.
  211. if ( ChooseLevelMenu.launchInEditor )
  212. {
  213. ChooseLevelMenu.addMissionFile( "tools/levels/DefaultEditorLevel.mis" );
  214. }
  215. }
  216. if(!isObject( ChooseLevelActionMap ) )
  217. {
  218. new ActionMap(ChooseLevelActionMap){};
  219. ChooseLevelActionMap.bind( keyboard, q, ChooseLevelMenuPrevMenu);
  220. ChooseLevelActionMap.bind( gamepad, btn_l, ChooseLevelMenuPrevMenu);
  221. ChooseLevelActionMap.bind( keyboard, e, ChooseLevelMenuNextMenu);
  222. ChooseLevelActionMap.bind( gamepad, btn_r, ChooseLevelMenuNextMenu);
  223. ChooseLevelActionMap.bind( keyboard, Space, ChooseLevelMenuOption );
  224. ChooseLevelActionMap.bind( gamepad, btn_a, ChooseLevelMenuOption );
  225. }
  226. function ChooseLevelMenu::syncGUI(%this)
  227. {
  228. //Update the button imagery to comply to the last input device we'd used
  229. %device = Canvas.getLastInputDevice();
  230. if(%device $= "mouse")
  231. %device = "keyboard";
  232. //Category handling
  233. %btn = ChooseLevelMenuTabList.getObject(%this.currentMenuIdx);
  234. %btn.setHighlighted(true);
  235. %buttonPosX = %btn.position.x + ChooseLevelMenuTabList.position.x;
  236. ChooseLevelMenuPrevNavIcon.position.x = %buttonPosX;
  237. ChooseLevelMenuNextNavIcon.position.x = %buttonPosX + %btn.extent.x - 40;
  238. ChooseLevelBackBtn.setBitmap(BaseUIActionMap.getCommandButtonBitmap(%device, "BaseUIBackOut"));
  239. if(ChooseLevelMenu.currentMenuIdx == 0)
  240. ChooseLevelNextBtn.text = "Toggle Mode";
  241. else
  242. ChooseLevelNextBtn.text = "Start Game";
  243. ChooseLevelNextBtn.setBitmap(ChooseLevelActionMap.getCommandButtonBitmap(%device, "ChooseLevelMenuOption"));
  244. ChooseLevelMenuPrevNavIcon.setBitmap(ChooseLevelActionMap.getCommandButtonBitmap(%device, "ChooseLevelMenuPrevMenu"));
  245. ChooseLevelMenuNextNavIcon.setBitmap(ChooseLevelActionMap.getCommandButtonBitmap(%device, "ChooseLevelMenuNextMenu"));
  246. %hasActiveGameMode = false;
  247. for(%i=0; %i < GameModePreviewArray.getCount(); %i++)
  248. {
  249. %btn = GameModePreviewArray.getObject(%i);
  250. if(!isObject(%btn.gameModeObj))
  251. continue;
  252. if((%btn.gameModeObj.isActive() || %btn.gameModeObj.isAlwaysActive()) == true)
  253. {
  254. %hasActiveGameMode = true;
  255. break;
  256. }
  257. }
  258. ChooseLevelMenuTabList-->LevelBtn.active = %hasActiveGameMode;
  259. }
  260. function LevelPreviewArray::syncGUI(%this)
  261. {
  262. %btn = %this.getObject(%this.listPosition);
  263. %btn.setHighlighted(true);
  264. $selectedLevelAsset = %btn.levelAssetId;
  265. }
  266. function GameModePreviewArray::syncGUI(%this)
  267. {
  268. for(%i=0; %i < %this.getCount(); %i++)
  269. {
  270. %btn = %this.getObject(%i);
  271. %btn-->button.setHighlighted(false);
  272. %bitmapAst = (%btn.gameModeObj.active || %btn.gameModeObj.alwaysActive) ? "UI:toggleMarker_h_image" : "UI:toggleMarker_image";
  273. %btn-->checkbox.setBitmap(%bitmapAst);
  274. }
  275. %btn = %this.getObject(%this.listPosition);
  276. %btn-->button.setHighlighted(true);
  277. }
  278. function ChooseLevelMenuPrevMenu(%val)
  279. {
  280. if(%val)
  281. {
  282. %currentIdx = ChooseLevelMenu.currentMenuIdx;
  283. ChooseLevelMenu.currentMenuIdx -= 1;
  284. %endIndex = 1;
  285. if($pref::HostMultiPlayer)
  286. %endIndex = 2;
  287. ChooseLevelMenu.currentMenuIdx = mClamp(ChooseLevelMenu.currentMenuIdx, 0, %endIndex);
  288. //bail if we're trying to step into a hidden or disabled menu
  289. if(!ChooseLevelMenu.isMenuAvailable(ChooseLevelMenu.currentMenuIdx))
  290. {
  291. ChooseLevelMenu.currentMenuIdx = %currentIdx;
  292. return;
  293. }
  294. if(%currentIdx == ChooseLevelMenu.currentMenuIdx)
  295. return;
  296. ChooseLevelMenu.openMenu(ChooseLevelMenu.currentMenuIdx);
  297. }
  298. }
  299. function ChooseLevelMenuNextMenu(%val)
  300. {
  301. if(%val)
  302. {
  303. %currentIdx = ChooseLevelMenu.currentMenuIdx;
  304. ChooseLevelMenu.currentMenuIdx += 1;
  305. %endIndex = 1;
  306. if($pref::HostMultiPlayer)
  307. %endIndex = 2;
  308. ChooseLevelMenu.currentMenuIdx = mClamp(ChooseLevelMenu.currentMenuIdx, 0, %endIndex);
  309. //bail if we're trying to step into a hidden or disabled menu
  310. if(!ChooseLevelMenu.isMenuAvailable(ChooseLevelMenu.currentMenuIdx))
  311. {
  312. ChooseLevelMenu.currentMenuIdx = %currentIdx;
  313. return;
  314. }
  315. if(%currentIdx == ChooseLevelMenu.currentMenuIdx)
  316. return;
  317. ChooseLevelMenu.openMenu(ChooseLevelMenu.currentMenuIdx);
  318. }
  319. }
  320. function ChooseLevelMenu::openMenu(%this, %menuIdx)
  321. {
  322. GameModeSelectContainer.setVisible(%menuIdx == 0);
  323. LevelSelectContainer.setVisible(%menuIdx == 1);
  324. ServerConfigContainer.setVisible(%menuIdx == 2);
  325. if(%menuIdx == 0)
  326. $MenuList = GameModePreviewArray;
  327. else if(%menuIdx == 1)
  328. $MenuList = LevelPreviewArray;
  329. else if(%menuIdx == 2)
  330. $MenuList = ServerConfigList;
  331. %this.currentMenuIdx = %menuIdx;
  332. if($MenuList.isMethod("syncGui"))
  333. $MenuList.syncGui();
  334. %this.syncGui();
  335. }
  336. function ChooseLevelMenu::isMenuAvailable(%this, %menuIdx)
  337. {
  338. if(%menuIdx == 0)
  339. %btn = %this-->GameModeBtn;
  340. else if(%menuIdx == 1)
  341. %btn = %this-->LevelBtn;
  342. else if(%menuIdx == 2)
  343. %btn = %this-->ConfigBtn;
  344. return %btn.isActive() && %btn.isVisible();
  345. }
  346. function ChooseLevelMenuOption(%val)
  347. {
  348. if(%val)
  349. {
  350. if(ChooseLevelMenu.currentMenuIdx == 0)
  351. ToggleGameMode(ChooseLevelMenu.listPosition);
  352. else if(ChooseLevelMenu.currentMenuIdx == 1)
  353. ChooseLevelBegin(ChooseLevelMenu.listPosition);
  354. }
  355. }
  356. function ToggleGameMode(%index)
  357. {
  358. if(%index $= "")
  359. %index = $MenuList.listPosition;
  360. %entry = GameModePreviewArray.getObject(%index);
  361. if(!isObject(%entry) || !isObject(%entry.gameModeObj))
  362. {
  363. MessageBoxOK("Error", "Selected game mode does not have a valid mode");
  364. return;
  365. }
  366. %entry.gameModeObj.active = !%entry.gameModeObj.active;
  367. refreshLevelsList();
  368. $MenuList.syncGui();
  369. ChooseLevelMenu.syncGui();
  370. }
  371. function ChooseLevelBegin(%index)
  372. {
  373. // So we can't fire the button when loading is in progress.
  374. if ( isObject( ServerGroup ) )
  375. return;
  376. Canvas.popDialog();
  377. %entry = LevelPreviewArray.getObject(%index);
  378. if(!AssetDatabase.isDeclaredAsset(%entry.levelAssetId))
  379. {
  380. MessageBoxOK("Error", "Selected level preview does not have a valid level asset!");
  381. return;
  382. }
  383. $selectedLevelAsset = %entry.levelAssetId;
  384. // Launch the chosen level with the editor open?
  385. if ( ChooseLevelMenu.launchInEditor )
  386. {
  387. activatePackage( "BootEditor" );
  388. ChooseLevelMenu.launchInEditor = false;
  389. StartGame(%entry.levelAssetId, "SinglePlayer");
  390. }
  391. else
  392. {
  393. StartGame(%entry.levelAssetId);
  394. }
  395. }
  396. function ChooseLevelMenu::onSleep( %this )
  397. {
  398. // This is set from the outside, only stays true for a single wake/sleep
  399. // cycle.
  400. %this.launchInEditor = false;
  401. //Ensure any changes we made to our server configs is saved out
  402. if($pref::HostMultiPlayer)
  403. {
  404. echo("Exporting server prefs");
  405. %prefPath = getPrefpath();
  406. export("$Pref::Server::*", %prefPath @ "/serverPrefs." @ $TorqueScriptFileExtension, false);
  407. BanList::Export(%prefPath @ "/banlist." @ $TorqueScriptFileExtension);
  408. }
  409. }
  410. function GameModePreviewButton::onHighlighted(%this, %highlighted)
  411. {
  412. if(%highlighted)
  413. {
  414. $MenuList.listPosition = $MenuList.getObjectIndex(%this.getParent());
  415. GameModePreviewBitmap.bitmapAsset = %this.previewImage;
  416. GameModePreviewBitmap.visible = (%this.previewImage !$= "");
  417. GameModeNameText.text = %this.text;
  418. GameModeDescriptionText.setText(%this.gameModeDesc);
  419. GameModePreviewScroll.scrollToObject(%this);
  420. }
  421. }
  422. function LevelPreviewButton::onHighlighted(%this, %highlighted)
  423. {
  424. if(%highlighted)
  425. {
  426. $MenuList.listPosition = $MenuList.getObjectIndex(%this);
  427. LevelPreviewBitmap.bitmapAsset = %this.bitmapAsset;
  428. LevelNameText.text = %this.levelAsset.levelName;
  429. LevelDescriptionText.setText(%this.levelAsset.levelDescription);
  430. LevelPreviewScroll.scrollToObject(%this);
  431. }
  432. }