| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539 |
- //-----------------------------------------------------------------------------
- // Copyright (c) 2012 GarageGames, LLC
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to
- // deal in the Software without restriction, including without limitation the
- // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
- // sell copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- // IN THE SOFTWARE.
- //-----------------------------------------------------------------------------
- //----------------------------------------
- function ChooseLevelMenu::onAdd( %this )
- {
- if(!isObject(ChooseLevelAssetQuery))
- new AssetQuery(ChooseLevelAssetQuery);
-
- $LevelPreviewButtonSize = "445 60";
- }
- function ChooseLevelMenu::fillPrefEntries( %this )
- {
- playerNameCTRL.setText($Pref::Player::Name);
- serverNameCTRL.setText($Pref::Server::Name);
- serverPassCTRL.setText($Pref::Server::Password);
- serverInfoCTRL.setText($Pref::Server::Info);
- serverMaxPlayersCTRL.setText($Pref::Server::MaxPlayers);
- }
- function ChooseLevelMenu::onWake(%this)
- {
- %this.fillPrefEntries();
-
- refreshGameModesList();
- refreshLevelsList();
- if(!$pref::HostMultiPlayer)
- ChooseLevelTitleText.setText("SINGLE PLAYER");
- else
- ChooseLevelTitleText.setText("CREATE SERVER");
-
- ChooseLevelMenuTabList-->ConfigBtn.visible = $pref::HostMultiPlayer;
-
- ChooseLevelMenuTabList.visible = true;//$pref::HostMultiPlayer;
- ChooseLevelMenuNavButtonOverlay.visible = true;//$pref::HostMultiPlayer;
-
- //If we've only got one gamemode, just force it to use that one, as we can
- //assume that is THE game's gamemode
- %gamemodeList = getGameModesList();
- if(%gamemodeList.count() == 1)
- {
- %gameModeObj = %gamemodeList.getKey(0);
- if(isObject(%gameModeObj))
- {
- %gameModeObj.active = true;
-
- ChooseLevelMenuTabList-->LevelBtn.active = true;
- ChooseLevelMenuTabList-->GameModeBtn.active = false;
-
- ChooseLevelMenu.openMenu(1);
-
- return;
- }
- }
- else
- {
- //Otherwise open the gamemode list as normal
- %this.schedule(32, openMenu, 0);
- }
- }
- function refreshGameModesList()
- {
- GameModePreviewArray.clear();
-
- $pref::Server::GameMode = "";
- ChooseLevelMenuTabList-->LevelBtn.active = false;
-
- //popilate the gamemodes list first
- %gamemodeList = getGameModesList();
- %gameModeCount = %gamemodeList.count();
-
- for (%i=0; %i<%gameModeCount; %i++)
- {
- %gameModeObj = %gamemodeList.getKey(%i);
- if(isObject(%gameModeObj))
- {
- %gameModeName = %gameModeObj.gameModeName;
- if(%gameModeName $= "")
- %gameModeName = %gameModeObj.getName();
-
- %preview = new GuiContainer() {
- position = "0 0";
- extent = $LevelPreviewButtonSize;
- horizSizing = "right";
- vertSizing = "bottom";
- gameModeObj = %gameModeObj;
- gameModeDesc = %gameModeObj.description;
- previewImage = %gameModeObj.previewImage;
- cansave = false;
-
- new GuiToggleButtonCtrl() {
- position = $LevelPreviewButtonSize.y SPC 0;
- extent = $LevelPreviewButtonSize.x - $LevelPreviewButtonSize.y - 5 SPC $LevelPreviewButtonSize.y;
- profile = GuiMenuButtonProfile;
- horizSizing = "right";
- vertSizing = "bottom";
- internalName = "button";
- class = "GameModePreviewButton";
- text = %gameModeName;
- command = "ToggleGameMode(" @ %i @ ");"; //allow doubleclick to quick action it
- textMargin = 120;
- };
-
- new GuiBitmapCtrl() {
- position = "0 0";
- extent = $LevelPreviewButtonSize.y SPC $LevelPreviewButtonSize.y;
- internalName = "checkbox";
- bitmapAsset = "UI:toggleMarker_image";
- };
- };
-
-
- GameModePreviewArray.add(%preview);
- }
- }
- }
- function refreshLevelsList()
- {
- LevelPreviewArray.clear();
-
- //fetch the levelAssets
- ChooseLevelAssetQuery.clear();
- AssetDatabase.findAssetType(ChooseLevelAssetQuery, "LevelAsset");
-
- %count = ChooseLevelAssetQuery.getCount();
-
- if(%count == 0 && !IsDirectory("tools"))
- {
- //We have no levels found. Prompt the user to open the editor to the default level if the tools are present
- MessageBoxOK("Error", "No levels were found in any modules. Please ensure you have modules loaded that contain gameplay code and level files.",
- "Canvas.popDialog(ChooseLevelMenu); if(isObject(ChooseLevelMenu.returnGui) && ChooseLevelMenu.returnGui.isMethod(\"onReturnTo\")) ChooseLevelMenu.returnGui.onReturnTo();");
-
- ChooseLevelAssetQuery.clear();
- return;
- }
-
- %gameModesList = getGameModesList();
-
- //filter the levelAssets by the gamemode selected
- %filteredIndex = 0;
- for(%i=0; %i < %count; %i++)
- {
- %assetId = ChooseLevelAssetQuery.getAsset(%i);
-
- if(AssetDatabase.getAssetModule(%assetId).ModuleId $= "ToolsModule")
- continue;
-
- %levelAsset = AssetDatabase.acquireAsset(%assetId);
-
- %file = %levelAsset.getLevelPath();
-
- if ( !isFile(%file) )
- continue;
-
- if( %levelAsset.isSubScene )
- continue;
-
- //filter for selected gamemode
- %levelGameModes = %levelAsset.gameModesNames;
-
- //If the level has no gamemodes defined, we just assume the default case of it being a wildcard to whatever gamemode
- //is available
- if(%levelGameModes $= "")
- {
- %foundGameModeMatch = true;
- }
- else
- {
- %foundGameModeMatch = false;
- for(%gm = 0; %gm < getTokenCount(%levelGameModes, ";"); %gm++)
- {
- %gameModeName = getToken(%levelGameModes, ";", %gm);
- for(%g=0; %g < %gameModesList.count(); %g++)
- {
- %gameModeObj = %gameModesList.getKey(%g);
-
- if(!isObject(%gameModeObj) || (!%gameModeObj.active && !%gameModeObj.alwaysActive && %gameModesList.count() > 1))
- continue;
-
- if(%gameModeName $= %gameModeObj.getName())
- {
- %foundGameModeMatch = true;
- break;
- }
- }
-
- if(%foundGameModeMatch)
- break;
- }
- }
-
- if(!%foundGameModeMatch)
- continue;
-
- %levelPreviewImg = %levelAsset.getPreviewImagePath();
-
- if (!isFile(%levelPreviewImg))
- %levelPreviewImg = "UI:no_preview_image";
-
- %preview = new GuiIconButtonCtrl() {
- position = "0 0";
- extent = $LevelPreviewButtonSize;
- buttonType = "PushButton";
- profile = GuiMenuButtonLeftJustProfile;
- horizSizing = "right";
- vertSizing = "bottom";
- internalName = "button";
- class = "LevelPreviewButton";
- //command = "$selectedLevelAsset = " @ %assetId @ ";";
- altCommand = "ChooseLevelBegin(" @ %filteredIndex @ ");"; //allow doubleclick to quick action it
- text = %levelAsset.levelName;
- bitmapAsset = %levelPreviewImg;
- levelAsset = %levelAsset;
- levelAssetId = %assetId;
- iconLocation = "left";
- sizeIconToButton = true;
- makeIconSquare = true;
- textLocation = "left";
- textMargin = 120;
- groupNum = 2;
- cansave = false;
- };
-
- LevelPreviewArray.add(%preview);
-
- %filteredIndex++;
- }
- GameModePreviewArray.listPosition = 0;
- LevelPreviewArray.listPosition = 0;
-
- // Also add the new level mission as defined in the world editor settings
- // if we are choosing a level to launch in the editor.
- if ( ChooseLevelMenu.launchInEditor )
- {
- ChooseLevelMenu.addMissionFile( "tools/levels/DefaultEditorLevel.mis" );
- }
- }
- if(!isObject( ChooseLevelActionMap ) )
- {
- new ActionMap(ChooseLevelActionMap){};
-
- ChooseLevelActionMap.bind( keyboard, q, ChooseLevelMenuPrevMenu);
- ChooseLevelActionMap.bind( gamepad, btn_l, ChooseLevelMenuPrevMenu);
-
- ChooseLevelActionMap.bind( keyboard, e, ChooseLevelMenuNextMenu);
- ChooseLevelActionMap.bind( gamepad, btn_r, ChooseLevelMenuNextMenu);
-
- ChooseLevelActionMap.bind( keyboard, Space, ChooseLevelMenuOption );
- ChooseLevelActionMap.bind( gamepad, btn_a, ChooseLevelMenuOption );
- }
- function ChooseLevelMenu::syncGUI(%this)
- {
- //Update the button imagery to comply to the last input device we'd used
- %device = Canvas.getLastInputDevice();
- if(%device $= "mouse")
- %device = "keyboard";
-
- //Category handling
- %btn = ChooseLevelMenuTabList.getObject(%this.currentMenuIdx);
- %btn.setHighlighted(true);
-
- %buttonPosX = %btn.position.x + ChooseLevelMenuTabList.position.x;
-
- ChooseLevelMenuPrevNavIcon.position.x = %buttonPosX;
- ChooseLevelMenuNextNavIcon.position.x = %buttonPosX + %btn.extent.x - 40;
-
- ChooseLevelBackBtn.setBitmap(BaseUIActionMap.getCommandButtonBitmap(%device, "BaseUIBackOut"));
-
- if(ChooseLevelMenu.currentMenuIdx == 0)
- ChooseLevelNextBtn.text = "Toggle Mode";
- else
- ChooseLevelNextBtn.text = "Start Game";
-
- ChooseLevelNextBtn.setBitmap(ChooseLevelActionMap.getCommandButtonBitmap(%device, "ChooseLevelMenuOption"));
-
- ChooseLevelMenuPrevNavIcon.setBitmap(ChooseLevelActionMap.getCommandButtonBitmap(%device, "ChooseLevelMenuPrevMenu"));
- ChooseLevelMenuNextNavIcon.setBitmap(ChooseLevelActionMap.getCommandButtonBitmap(%device, "ChooseLevelMenuNextMenu"));
-
- %hasActiveGameMode = false;
- for(%i=0; %i < GameModePreviewArray.getCount(); %i++)
- {
- %btn = GameModePreviewArray.getObject(%i);
- if(!isObject(%btn.gameModeObj))
- continue;
-
- if((%btn.gameModeObj.isActive() || %btn.gameModeObj.isAlwaysActive()) == true)
- {
- %hasActiveGameMode = true;
- break;
- }
- }
-
- ChooseLevelMenuTabList-->LevelBtn.active = %hasActiveGameMode;
- }
- function LevelPreviewArray::syncGUI(%this)
- {
- %btn = %this.getObject(%this.listPosition);
- %btn.setHighlighted(true);
-
- $selectedLevelAsset = %btn.levelAssetId;
- }
- function GameModePreviewArray::syncGUI(%this)
- {
- for(%i=0; %i < %this.getCount(); %i++)
- {
- %btn = %this.getObject(%i);
- %btn-->button.setHighlighted(false);
-
- %bitmapAst = (%btn.gameModeObj.active || %btn.gameModeObj.alwaysActive) ? "UI:toggleMarker_h_image" : "UI:toggleMarker_image";
- %btn-->checkbox.setBitmap(%bitmapAst);
- }
-
- %btn = %this.getObject(%this.listPosition);
- %btn-->button.setHighlighted(true);
- }
- function ChooseLevelMenuPrevMenu(%val)
- {
- if(%val)
- {
- %currentIdx = ChooseLevelMenu.currentMenuIdx;
- ChooseLevelMenu.currentMenuIdx -= 1;
-
- %endIndex = 1;
- if($pref::HostMultiPlayer)
- %endIndex = 2;
-
- ChooseLevelMenu.currentMenuIdx = mClamp(ChooseLevelMenu.currentMenuIdx, 0, %endIndex);
-
- //bail if we're trying to step into a hidden or disabled menu
- if(!ChooseLevelMenu.isMenuAvailable(ChooseLevelMenu.currentMenuIdx))
- {
- ChooseLevelMenu.currentMenuIdx = %currentIdx;
- return;
- }
-
- if(%currentIdx == ChooseLevelMenu.currentMenuIdx)
- return;
-
- ChooseLevelMenu.openMenu(ChooseLevelMenu.currentMenuIdx);
- }
- }
- function ChooseLevelMenuNextMenu(%val)
- {
- if(%val)
- {
- %currentIdx = ChooseLevelMenu.currentMenuIdx;
- ChooseLevelMenu.currentMenuIdx += 1;
-
- %endIndex = 1;
- if($pref::HostMultiPlayer)
- %endIndex = 2;
-
- ChooseLevelMenu.currentMenuIdx = mClamp(ChooseLevelMenu.currentMenuIdx, 0, %endIndex);
-
- //bail if we're trying to step into a hidden or disabled menu
- if(!ChooseLevelMenu.isMenuAvailable(ChooseLevelMenu.currentMenuIdx))
- {
- ChooseLevelMenu.currentMenuIdx = %currentIdx;
- return;
- }
-
- if(%currentIdx == ChooseLevelMenu.currentMenuIdx)
- return;
-
- ChooseLevelMenu.openMenu(ChooseLevelMenu.currentMenuIdx);
- }
- }
- function ChooseLevelMenu::openMenu(%this, %menuIdx)
- {
- GameModeSelectContainer.setVisible(%menuIdx == 0);
- LevelSelectContainer.setVisible(%menuIdx == 1);
- ServerConfigContainer.setVisible(%menuIdx == 2);
- if(%menuIdx == 0)
- $MenuList = GameModePreviewArray;
- else if(%menuIdx == 1)
- $MenuList = LevelPreviewArray;
- else if(%menuIdx == 2)
- $MenuList = ServerConfigList;
-
- %this.currentMenuIdx = %menuIdx;
- if($MenuList.isMethod("syncGui"))
- $MenuList.syncGui();
-
- %this.syncGui();
- }
- function ChooseLevelMenu::isMenuAvailable(%this, %menuIdx)
- {
- if(%menuIdx == 0)
- %btn = %this-->GameModeBtn;
- else if(%menuIdx == 1)
- %btn = %this-->LevelBtn;
- else if(%menuIdx == 2)
- %btn = %this-->ConfigBtn;
-
- return %btn.isActive() && %btn.isVisible();
- }
- function ChooseLevelMenuOption(%val)
- {
- if(%val)
- {
- if(ChooseLevelMenu.currentMenuIdx == 0)
- ToggleGameMode(ChooseLevelMenu.listPosition);
- else if(ChooseLevelMenu.currentMenuIdx == 1)
- ChooseLevelBegin(ChooseLevelMenu.listPosition);
- }
- }
- function ToggleGameMode(%index)
- {
- if(%index $= "")
- %index = $MenuList.listPosition;
-
- %entry = GameModePreviewArray.getObject(%index);
- if(!isObject(%entry) || !isObject(%entry.gameModeObj))
- {
- MessageBoxOK("Error", "Selected game mode does not have a valid mode");
- return;
- }
-
- %entry.gameModeObj.active = !%entry.gameModeObj.active;
-
- refreshLevelsList();
-
- $MenuList.syncGui();
- ChooseLevelMenu.syncGui();
-
- }
- function ChooseLevelBegin(%index)
- {
- // So we can't fire the button when loading is in progress.
- if ( isObject( ServerGroup ) )
- return;
-
- Canvas.popDialog();
-
- %entry = LevelPreviewArray.getObject(%index);
-
- if(!AssetDatabase.isDeclaredAsset(%entry.levelAssetId))
- {
- MessageBoxOK("Error", "Selected level preview does not have a valid level asset!");
- return;
- }
-
- $selectedLevelAsset = %entry.levelAssetId;
- // Launch the chosen level with the editor open?
- if ( ChooseLevelMenu.launchInEditor )
- {
- activatePackage( "BootEditor" );
- ChooseLevelMenu.launchInEditor = false;
- StartGame(%entry.levelAssetId, "SinglePlayer");
- }
- else
- {
- StartGame(%entry.levelAssetId);
- }
- }
- function ChooseLevelMenu::onSleep( %this )
- {
- // This is set from the outside, only stays true for a single wake/sleep
- // cycle.
- %this.launchInEditor = false;
-
- //Ensure any changes we made to our server configs is saved out
- if($pref::HostMultiPlayer)
- {
- echo("Exporting server prefs");
- %prefPath = getPrefpath();
- export("$Pref::Server::*", %prefPath @ "/serverPrefs." @ $TorqueScriptFileExtension, false);
- BanList::Export(%prefPath @ "/banlist." @ $TorqueScriptFileExtension);
- }
- }
- function GameModePreviewButton::onHighlighted(%this, %highlighted)
- {
- if(%highlighted)
- {
- $MenuList.listPosition = $MenuList.getObjectIndex(%this.getParent());
-
- GameModePreviewBitmap.bitmapAsset = %this.previewImage;
-
- GameModePreviewBitmap.visible = (%this.previewImage !$= "");
-
- GameModeNameText.text = %this.text;
- GameModeDescriptionText.setText(%this.gameModeDesc);
-
- GameModePreviewScroll.scrollToObject(%this);
- }
- }
- function LevelPreviewButton::onHighlighted(%this, %highlighted)
- {
- if(%highlighted)
- {
- $MenuList.listPosition = $MenuList.getObjectIndex(%this);
-
- LevelPreviewBitmap.bitmapAsset = %this.bitmapAsset;
- LevelNameText.text = %this.levelAsset.levelName;
- LevelDescriptionText.setText(%this.levelAsset.levelDescription);
-
- LevelPreviewScroll.scrollToObject(%this);
- }
- }
|