Torque3D/Templates/BaseGame/game/data/UI/guis/ChooseLevelMenu.tscript
Areloch ae8eca48e1 Implementation of Subscenes, SceneGroups and Gamemodes
Standardizes Gamemodes to be an actual class with data and utility functions that can be parsed
Adds inspector field handling for selecting gamemodes
Updated Scene class to work with Gamemodes for the gamemode field
Updates editor suite elements to be able to create SubScenes, SceneGroups and Gamemodes
Adds ability to convert SimGroup to SubScene
Updates BaseUI's chooselevel menu to have gamemode selection and filters shown levels based on selected gamemode
2024-09-01 16:39:00 -05:00

427 lines
13 KiB
Plaintext

//-----------------------------------------------------------------------------
// 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);
%this.previewButtonSize = "445 120";
}
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();
LevelPreviewArray.clear();
refreshGameModesList();
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;
%this.schedule(32, openMenu, 0);
}
function refreshGameModesList()
{
GameModePreviewArray.clear();
$pref::Server::GameMode = "";
ChooseLevelMenuTabList-->LevelBtn.active = false;
//popilate the gamemodes list first
%gamemodeList = getGameModesList();
for(%i=0; %i < getTokenCount(%gamemodeList, ";"); %i++)
{
%gameModeObj = getToken(%gamemodeList, ";", %i);
if(isObject(%gameModeObj))
{
%gameModeName = %gameModeObj.gameModeName;
if(%gameModeName $= "")
%gameModeName = %gameModeObj.getName();
%preview = new GuiButtonCtrl() {
position = "0 0";
extent = ChooseLevelMenu.previewButtonSize;
buttonType = "PushButton";
profile = GuiMenuButtonLeftJustProfile;
horizSizing = "right";
vertSizing = "bottom";
internalName = "button";
class = "GameModePreviewButton";
//command = "ChooseGameModeBegin(" @ %i @ ");";
altCommand = "ChooseGameModeBegin(" @ %i @ ");"; //allow doubleclick to quick action it
text = %gameModeName;
gameModeObj = %gameModeObj;
gameModeDesc = %gameModeObj.description;
previewImage = %gameModeObj.previewImage;
textMargin = 120;
groupNum = 2;
cansave = false;
};
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;
}
//filter the levelAssets by the gamemode selected
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;
echo("LevelAsset gamemodes: " @ %levelGameModes);
%foundGameModeMatch = false;
for(%gm = 0; %gm < getTokenCount(%levelGameModes, ";"); %gm++)
{
%gameModeName = getToken(%levelGameModes, ";", %gm);
echo("testing gamemode: " @ %gameModeName);
echo("selected gamemode: " @ $pref::Server::GameMode.getName());
if(%gameModeName $= $pref::Server::GameMode.getName())
{
%foundGameModeMatch = true;
break;
}
}
if(!%foundGameModeMatch)
continue;
%levelPreviewImg = %levelAsset.getPreviewImagePath();
if (!isFile(%levelPreviewImg))
%levelPreviewImg = "UI:no_preview_image";
%preview = new GuiIconButtonCtrl() {
position = "0 0";
extent = ChooseLevelMenu.previewButtonSize;
buttonType = "PushButton";
profile = GuiMenuButtonLeftJustProfile;
horizSizing = "right";
vertSizing = "bottom";
internalName = "button";
class = "LevelPreviewButton";
//command = "$selectedLevelAsset = " @ %assetId @ ";";
altCommand = "ChooseLevelBegin(" @ %i @ ");"; //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);
}
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"));
ChooseLevelStartBtn.setBitmap(ChooseLevelActionMap.getCommandButtonBitmap(%device, "ChooseLevelBegin"));
ChooseLevelMenuPrevNavIcon.setBitmap(ChooseLevelActionMap.getCommandButtonBitmap(%device, "ChooseLevelMenuPrevMenu"));
ChooseLevelMenuNextNavIcon.setBitmap(ChooseLevelActionMap.getCommandButtonBitmap(%device, "ChooseLevelMenuNextMenu"));
}
function LevelPreviewArray::syncGUI(%this)
{
%btn = %this.getObject(%this.listPosition);
%btn.setHighlighted(true);
$selectedLevelAsset = %btn.levelAssetId;
}
function GameModePreviewArray::syncGUI(%this)
{
%btn = %this.getObject(%this.listPosition);
%btn.setHighlighted(true);
$pref::Server::GameMode = %btn.gameModeObject;
}
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);
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);
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 ChooseLevelMenuOption(%val)
{
if(%val)
{
if(ChooseLevelMenu.currentMenuIdx == 0)
ChooseGameModeBegin(ChooseLevelMenu.listPosition);
else if(ChooseLevelMenu.currentMenuIdx == 1)
ChooseLevelBegin(ChooseLevelMenu.listPosition);
}
}
function ChooseGameModeBegin(%index)
{
%entry = GameModePreviewArray.getObject(%index);
if(!isObject(%entry) || !isObject(%entry.gameModeObj))
{
MessageBoxOK("Error", "Selected game mode does not have a valid mode");
return;
}
$pref::Server::GameMode = %entry.gameModeObj;
ChooseLevelMenuTabList-->LevelBtn.active = true;
refreshLevelsList();
ChooseLevelMenu.openMenu(1);
}
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);
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);
}
}