Creating a Team-Menu

From Valve Developer Community
Jump to: navigation, search
Warning.png Warning: There is no vgui_TeamFortressViewport.pre file in latest sdk anymore, all of its functions now in Baseviewport.cpp.

The Source SDK already has most of the code you will need to create a team menu, but not everything. Valve must want us to learn by filling in the gaps.

The goal in this first step is to enable a team-selection menu, using the available code as much as possible.

Creating the viewport

First you should take a look at the VGUI2 Overview(broken link). You will learn that the base for all VGUI panels (not class-based, but where VGUI-objects are attached) is a viewport.

This viewport is created in cl_dll/clientmode_shared.cpp in the method ClientModeSharedl::InitViewport:

void ClientModeShared::InitViewport()
{
  m_pViewport = new CBaseViewport();
  m_pViewport->Start( gameuifuncs, gameeventmanager );
}

Creating the panel

Let's take a look at the CBaseViewport, which handles all the different Panels used by the mod. It is defined in cl_dll/game_controls/baseviewport.cpp.

This function creates all panels known to this viewport:

void CBaseViewport::CreateDefaultPanels( void )
{
  AddNewPanel( CreatePanelByName( PANEL_SCOREBOARD ) );
  AddNewPanel( CreatePanelByName( PANEL_INFO ) );
  AddNewPanel( CreatePanelByName( PANEL_SPECGUI ) );
  AddNewPanel( CreatePanelByName( PANEL_SPECMENU ) );
  // AddNewPanel( CreatePanelByName( PANEL_TEAM ) );
  // AddNewPanel( CreatePanelByName( PANEL_CLASS ) );
  // AddNewPanel( CreatePanelByName( PANEL_BUY ) );
}

Since you want to get the team menu, remove the comment before PANEL_TEAM.

The "real" creation is done in CBaseViewport::CreatePanelByName, a function which takes the panel name as string and creates a corresponding class. The panel names are referenced in game_shared/viewport_panel_names.h, e.g.

#define PANEL_TEAM "team"

PANEL_TEAM creates the class CTeamMenu, which contains all the functionality of that panel and is implemented in game_controls/teammenu.cpp. So uncomment that, too.

  else if ( Q_strcmp(PANEL_TEAM, szPanelName) == 0 )
  {
    newpanel = new CTeamMenu( this );
  }

Implementing the menu

The class CTeamMenu is nearly functional; the only thing missing is the function which handles the messages created by the panel. Open game_controls/teammenu.h to add the declaration of the new function OnCommand to the class. Find

// command callbacks

and add

 void OnCommand( const char *command );

Then add the implementation in game_controls/teammenu.cpp:

void CTeamMenu::OnCommand( const char *command )
{
  if ( Q_stricmp( command, "vguicancel" ) )
  {
    engine->ClientCmd( const_cast<char *>( command ) );
  }
  Close();
  gViewPortInterface->ShowBackGround( false );
  BaseClass::OnCommand(command);
}

You don't see anything done here with the commands. They are passed to the base class where hopefully they will be employed in a useful manner, but that is beyond the scope of this tutorial.

Another thing you need to modify: Remove (comment) any lines containing "m_pMapInfo", since you don't need nor create it for that panel. Also remove the line "LoadMapPage( m_szMapName );", because there's no need to get the mapinfo, if it is not displayed.

Defining the layout

The layout and contents of your panel can be defined solely by a resource file. If you don't need to do customized things (e.g. displaying gamemode-dependent buttons or map information), you won't require any more to complete your menu.

Most resources are missing in the Source SDK; the team menu resource is no exception. You need to create one yourself (or copy from any cache-file), the standard path would be <modname>/multimod/resource/ui/Teammenu.res.

It defines one panel (team), one label (joinTeam) and five buttons (jointeam1, jointeam2, jointeam3, autojoin, CancelButton) The interesting part in the button-definition is "Command", because this defines which command the client executes if he presses that button.

You don't need to edit the file by hand. Valve included a nice tool with which you may change it ingame, as mentioned in VGUI2 Overview (again). Just press Shift+Ctrl+ALT+B to open the VGUI Build-Mode editor:

"Resource/UI/TeamMenu.res"
{
 "team"
{
 "ControlName"  "CTeamMenu"
 "fieldName"  "team"
 "xpos"  "100"
 "ypos"  "60"
 "wide"  "240"
 "tall"  "290"
 "autoResize"  "0"
 "pinCorner"  "0"
 "visible"  "1"
 "enabled"  "1"
 "tabPosition"  "0"
 "settitlebarvisible"  "0"
 "title"  "#Frame_Untitled"
}
"joinTeam"
{
 "ControlName"  "Label"
 "fieldName"  "joinTeam"
 "xpos"  "20"
 "ypos"  "20"
 "wide"  "200"
 "tall"  "30"
 "autoResize"  "0"
 "pinCorner"  "0"
 "visible"  "1"
 "enabled"  "1"
 "tabPosition"  "0"
 "labelText"  "#TM_Join_Team"
 "textAlignment"  "center"
 "dulltext"  "0"
 "brighttext"  "0"
 "font"  "MenuTitle"
 "wrap"  "0"
}
"jointeam1"
{
 "ControlName"  "Button"
 "fieldName"  "jointeam1"
 "xpos"  "20"
 "ypos"  "60"
 "wide"  "200"
 "tall"  "30"
 "autoResize"  "0"
 "pinCorner"  "2"
 "visible"  "1"
 "enabled"  "1"
 "tabPosition"  "3"
 "labelText"  "#TM_Join_Team_1"
 "textAlignment"  "west"
 "dulltext"  "0"
 "brighttext"  "0"
 "wrap"  "0"
 "Command"  "jointeam 1"
 "Default"  "0"
}
"jointeam2"
{
 "ControlName"  "Button"
 "fieldName"  "jointeam2"
 "xpos"  "20"
 "ypos"  "100"
 "wide"  "200"
 "tall"  "30"
 "autoResize"  "0"
 "pinCorner"  "2"
 "visible"  "1"
 "enabled"  "1"
 "tabPosition"  "4"
 "labelText"  "#TM_Join_Team_2"
 "textAlignment"  "west"
 "dulltext"  "0"
 "brighttext"  "0"
 "wrap"  "0"
 "Command"  "jointeam 2"
 "Default"  "0"
}
"jointeam3"
{
 "ControlName"  "Button"
 "fieldName"  "jointeam3"
 "xpos"  "20"
 "ypos"  "140"
 "wide"  "200"
 "tall"  "30"
 "autoResize"  "0"
 "pinCorner"  "2"
 "visible"  "1"
 "enabled"  "1"
 "tabPosition"  "5"
 "labelText"  "#TM_Join_Team_3"
 "textAlignment"  "west"
 "dulltext"  "0"
 "brighttext"  "0"
 "wrap"  "0"
 "Command"  "jointeam 3"
 "Default"  "0"
}
"autojoin"
{
 "ControlName"  "Button"
 "fieldName"  "autojoin"
 "xpos"  "20"
 "ypos"  "180"
 "wide"  "200"
 "tall"  "30"
 "autoResize"  "0"
 "pinCorner"  "2"
 "visible"  "1"
 "enabled"  "1"
 "tabPosition"  "1"
 "labelText"  "#TM_Auto_Join"
 "textAlignment"  "west"
 "dulltext"  "0"
 "brighttext"  "0"
 "wrap"  "0"
 "Command"  "jointeam 0"
 "Default"  "0"
}
"CancelButton"
{
 "ControlName"  "Button"
 "fieldName"  "CancelButton"
 "xpos"  "20"
 "ypos"  "240"
 "wide"  "200"
 "tall"  "30"
 "autoResize"  "0"
 "pinCorner"  "2"
 "visible"  "1"
 "enabled"  "1"
 "tabPosition"  "0"
 "labelText"  "#TM_Cancel"
 "textAlignment"  "center"
 "dulltext"  "0"
 "brighttext"  "0"
 "wrap"  "0"
 "Command"  "vguicancel"
 "Default"  "1"
}
}

Displaying the menu

The only thing missing to show the menu is a command, which invokes it. cl_dll/baseviewport.cpp already implements a function to show any panel (e.g. "showpanel team" in the console)

CON_COMMAND( showpanel, "Shows a viewport panel <name>" )

but a teamselection-command is no big deal:

CON_COMMAND( chooseteam, "Opens a menu for teamchoose" )
{
 if ( !gViewPortInterface )
  return;
 gViewPortInterface->ShowPanel( "team", true );
}

If you use use the command "chooseteam" in the console, this code will be executed and if the viewport was created successfully, it will show the menu.

But maybe you will need to show this menu after player connected to the server (like in CSS). So open hl2mp_client.cpp and find

pPlayer->ShowViewPortPanel( PANEL_INFO, true, data );

and add before or after it

pPlayer->ShowViewPortPanel( PANEL_TEAM, true, data );


Now open hl2mp_player.cpp, find

void CHL2MP_Player::PickDefaultSpawnTeam( void )

and replace it with this:

void CHL2MP_Player::PickDefaultSpawnTeam( void )
{
    if ( GetTeamNumber() == 0 )
    {
        if ( HL2MPRules()->IsTeamplay() == false )
        {
            if ( GetModelPtr() == NULL )
            {
                ChangeTeam( TEAM_UNASSIGNED );
            }
        }
        else
        {
            ChangeTeam( TEAM_SPECTATOR );
        }
    }
}

Localization

Unlike written in the VGUI2 Overview, the translation file is not loaded automatically. Here's a statement about that from the coding-list:

If you are loading the game via the parameters:

-game "c:\absolutepathto\moddir"

The localization file will not load.  This is because the '<moddir>_' prefix
is loaded from the -game parameter.  This is a bug in the engine, and I have
reported it.  I am told that this is being fixed.

If you wish to temporarilly get around this, copy your mod into
SteamApps/<username>/half-life 2/<moddir> and run HL2 with

-game moddir

That is - run it without any quotes, and use the relative folder name.. the
same way as you would have loaded a HL1 mod. 

For now you need to load the file yourself, for example in the viewport-constructor. Look into cl_dll\game_controls\vgui_TeamFortressViewport.pre and find "CBaseViewport::CBaseViewport". Then add the AddFile function after "SetProportional":

CBaseViewport::CBaseViewport() : vgui::EditablePanel( NULL, "CBaseViewport")
{
 gViewPortInterface = this;
 ...
 ...
 SetProportional( true );

 vgui::localize()->AddFile( vgui::filesystem(), "resource/<modname>_%language%.txt" );
 ...
}

Finally the translation file itself, resource/<modname>_%language%.txt. Be sure you save it as Unicode.

"lang"
{
 "Language" "English"
 "Tokens"
 {
  "TM_Join_Team"     "Choose a team"
  "TM_Join_Team_1"   "Join team 1"
  "TM_Join_Team_2"   "Join team 2"
  "TM_Join_Team_3"   "Join team 3"
  "TM_Auto_Join"     "Auto-select"
  "TM_Cancel"        "Cancel teamchoice"
 }
}

If you take a look back at TeamMenu.res, you'll find labeltexts beginning with a "#". Such labels are replaced by their counterpart in the translation file, only without the "#". For example #TM_Join_Team will be replaced with "Choose a team", if your steamlanguage is set to english. For other languages you need to create additional files, like <modname>_german, <modname>_french.txt, etc.