//========= Copyright © 1996-2005, Valve Corporation, All rights reserved. ============// // // Purpose: // // $NoKeywords: $ //=============================================================================// #include "cbase.h" #include #include #include #include #include "spectatorgui.h" #include #include #include #include #include #include #include // _snprintf define #include #include "commandmenu.h" #include "hltvcamera.h" #include #include #include #include #include "IGameUIFuncs.h" // for key bindings #include #include #include // memdbgon must be the last include file in a .cpp file!!! #include "tier0/memdbgon.h" #ifndef _XBOX extern IGameUIFuncs *gameuifuncs; // for key binding details #endif // void DuckMessage(const char *str); // from vgui_teamfortressviewport.cpp ConVar spec_scoreboard( "spec_scoreboard", "0", FCVAR_CLIENTDLL | FCVAR_ARCHIVE ); CSpectatorGUI *g_pSpectatorGUI = NULL; static char *s_SpectatorModes[] = { "#Spec_Mode0", "#Spec_Mode1", "#Spec_Mode2", "#Spec_Mode3", "#Spec_Mode4", "#Spec_Mode5", "" }; using namespace vgui; ConVar cl_spec_mode( "cl_spec_mode", "1", FCVAR_ARCHIVE | FCVAR_USERINFO, "spectator mode" ); //----------------------------------------------------------------------------- // Purpose: left and right buttons pointing buttons //----------------------------------------------------------------------------- class CSpecButton : public Button { public: CSpecButton(Panel *parent, const char *panelName): Button(parent, panelName, "") {} private: void ApplySchemeSettings(vgui::IScheme *pScheme) { Button::ApplySchemeSettings(pScheme); SetFont(pScheme->GetFont("Marlett", IsProportional()) ); } }; //----------------------------------------------------------------------------- // Purpose: Constructor //----------------------------------------------------------------------------- CSpectatorMenu::CSpectatorMenu( IViewPort *pViewPort ) : Frame( NULL, PANEL_SPECMENU ) { m_iDuckKey = -1; m_pViewPort = pViewPort; SetMouseInputEnabled( true ); SetKeyBoardInputEnabled( true ); SetTitleBarVisible( false ); // don't draw a title bar SetMoveable( false ); SetSizeable( false ); SetProportional(true); SetScheme("ClientScheme"); m_pPlayerList = new ComboBox(this, "playercombo", 10 , false); m_pViewOptions = new ComboBox(this, "viewcombo", 10 , false ); m_pConfigSettings = new ComboBox(this, "settingscombo", 10 , false ); m_pLeftButton = new CSpecButton( this, "specprev"); m_pLeftButton->SetText("3"); m_pRightButton = new CSpecButton( this, "specnext"); m_pRightButton->SetText("4"); m_pPlayerList->SetText(""); m_pViewOptions->SetText("#Spec_Modes"); m_pConfigSettings->SetText("#Spec_Options"); m_pPlayerList->SetOpenDirection( Menu::UP ); m_pViewOptions->SetOpenDirection( Menu::UP ); m_pConfigSettings->SetOpenDirection( Menu::UP ); // create view config menu CommandMenu * menu = new CommandMenu(m_pViewOptions, "spectatormenu", gViewPortInterface); menu->LoadFromFile( "Resource/spectatormenu.res" ); m_pConfigSettings->SetMenu( menu ); // attach menu to combo box // create view mode menu menu = new CommandMenu(m_pViewOptions, "spectatormodes", gViewPortInterface); menu->LoadFromFile("Resource/spectatormodes.res"); m_pViewOptions->SetMenu( menu ); // attach menu to combo box LoadControlSettings("Resource/UI/BottomSpectator.res"); } void CSpectatorMenu::ApplySchemeSettings(IScheme *pScheme) { BaseClass::ApplySchemeSettings(pScheme); // need to MakeReadyForUse() on the menus so we can set their bg color before they are displayed m_pConfigSettings->GetMenu()->MakeReadyForUse(); m_pViewOptions->GetMenu()->MakeReadyForUse(); m_pPlayerList->GetMenu()->MakeReadyForUse(); m_pConfigSettings->GetMenu()->SetBgColor( BLACK_BAR_COLOR ); m_pViewOptions->GetMenu()->SetBgColor( BLACK_BAR_COLOR ); m_pPlayerList->GetMenu()->SetBgColor( BLACK_BAR_COLOR ); } //----------------------------------------------------------------------------- // Purpose: makes the GUI fill the screen //----------------------------------------------------------------------------- void CSpectatorMenu::PerformLayout() { int w,h; GetHudSize(w, h); // fill the screen SetSize(w,GetTall()); } //----------------------------------------------------------------------------- // Purpose: Handles changes to combo boxes //----------------------------------------------------------------------------- void CSpectatorMenu::OnTextChanged(KeyValues *data) { Panel *panel = reinterpret_cast( data->GetPtr("panel") ); vgui::ComboBox *box = dynamic_cast( panel ); if( box == m_pConfigSettings) // don't change the text in the config setting combo { m_pConfigSettings->SetText("#Spec_Options"); } else if ( box == m_pPlayerList ) { KeyValues *kv = box->GetActiveItemUserData(); if ( kv && GameResources() ) { const char *player = kv->GetString("player"); int currentPlayerNum = GetSpectatorTarget(); const char *currentPlayerName = GameResources()->GetPlayerName( currentPlayerNum ); if ( !FStrEq( currentPlayerName, player ) ) { char command[128]; Q_snprintf( command, sizeof(command), "spec_player \"%s\"", player ); engine->ClientCmd( command ); } } } } void CSpectatorMenu::OnCommand( const char *command ) { if (!stricmp(command, "specnext") ) { engine->ClientCmd("spec_next"); } else if (!stricmp(command, "specprev") ) { engine->ClientCmd("spec_prev"); } } //----------------------------------------------------------------------------- // Purpose: when duck is pressed it hides the active part of the GUI //----------------------------------------------------------------------------- void CSpectatorMenu::OnKeyCodePressed(KeyCode code) { // we can't compare the keycode to a known code, because translation from bound keys // to vgui key codes is not 1:1. Get the engine version of the key for the binding // and the actual pressed key, and compare those.. int iLastTrappedKey = engine->GetLastPressedEngineKey(); // the enginekey version of the code param if( iLastTrappedKey == m_iDuckKey ) { // hide if DUCK is pressed again m_pViewPort->ShowPanel( this, false ); } } void CSpectatorMenu::ShowPanel(bool bShow) { if ( BaseClass::IsVisible() == bShow ) return; if ( bShow ) { Activate(); SetMouseInputEnabled( true ); SetKeyBoardInputEnabled( true ); } else { SetVisible( false ); SetMouseInputEnabled( false ); SetKeyBoardInputEnabled( false ); } bool bIsEnabled = true; if ( engine->IsHLTV() && HLTVCamera()->IsPVSLocked() ) { // when wattching HLTV with a locked PVS, some elements are disabled bIsEnabled = false; } m_pLeftButton->SetVisible( bIsEnabled ); m_pRightButton->SetVisible( bIsEnabled ); m_pPlayerList->SetVisible( bIsEnabled ); m_pViewOptions->SetVisible( bIsEnabled ); } int CSpectatorMenu::PlayerAddItem( int itemID, wchar_t *name, KeyValues *data ) { if ( m_pPlayerList->IsItemIDValid( itemID ) ) { m_pPlayerList->UpdateItem( itemID, name, data ); return itemID + 1; } else { return m_pPlayerList->AddItem( name, data ) + 1; } } void CSpectatorMenu::SetPlayerNameText(const wchar_t *text ) { char *ansiText = (char *) _alloca( (wcslen( text ) + 1) * sizeof( char ) ); if ( ansiText ) { localize()->ConvertUnicodeToANSI( text, ansiText, (wcslen( text ) + 1) * sizeof( char ) ); m_pPlayerList->SetText( ansiText ); } } //----------------------------------------------------------------------------- // main spectator panel //----------------------------------------------------------------------------- // Purpose: Constructor //----------------------------------------------------------------------------- CSpectatorGUI::CSpectatorGUI(IViewPort *pViewPort) : Frame( NULL, PANEL_SPECGUI ) { // m_bHelpShown = false; // m_bInsetVisible = false; // m_iDuckKey = KEY_NONE; m_bSpecScoreboard = false; m_pViewPort = pViewPort; g_pSpectatorGUI = this; // initialize dialog SetVisible(false); SetTitle("SpectatorGUI", true); SetProportional(true); // load the new scheme early!! SetScheme("ClientScheme"); SetMoveable(false); SetSizeable(false); SetMouseInputEnabled( false ); SetKeyBoardInputEnabled( false ); // hide the system buttons SetTitleBarVisible( false ); m_pTopBar = new Panel( this, "topbar" ); m_pBottomBarBlank = new Panel( this, "bottombarblank" ); // m_pBannerImage = new ImagePanel( m_pTopBar, NULL ); m_pPlayerLabel = new Label( this, "playerlabel", "" ); m_pPlayerLabel->SetVisible( false ); LoadControlSettings("Resource/UI/Spectator.res"); SetPaintBorderEnabled(false); SetPaintBackgroundEnabled(false); m_pBottomBarBlank->SetVisible( true ); m_pTopBar->SetVisible( true ); // m_pBannerImage->SetVisible(false); InvalidateLayout(); } //----------------------------------------------------------------------------- // Purpose: Destructor //----------------------------------------------------------------------------- CSpectatorGUI::~CSpectatorGUI() { g_pSpectatorGUI = NULL; } //----------------------------------------------------------------------------- // Purpose: Sets the colour of the top and bottom bars //----------------------------------------------------------------------------- void CSpectatorGUI::ApplySchemeSettings(IScheme *pScheme) { BaseClass::ApplySchemeSettings( pScheme ); SetBgColor(Color( 0,0,0,0 ) ); // make the background transparent m_pTopBar->SetBgColor(BLACK_BAR_COLOR); m_pBottomBarBlank->SetBgColor(BLACK_BAR_COLOR); // m_pBottomBar->SetBgColor(Color( 0,0,0,0 )); SetPaintBorderEnabled(false); SetBorder( NULL ); } //----------------------------------------------------------------------------- // Purpose: makes the GUI fill the screen //----------------------------------------------------------------------------- void CSpectatorGUI::PerformLayout() { int w,h,x,y; GetHudSize(w, h); // fill the screen SetBounds(0,0,w,h); // stretch the bottom bar across the screen m_pBottomBarBlank->GetPos(x,y); m_pBottomBarBlank->SetSize( w, h - y ); } //----------------------------------------------------------------------------- // Purpose: checks spec_scoreboard cvar to see if the scoreboard should be displayed //----------------------------------------------------------------------------- void CSpectatorGUI::OnThink() { BaseClass::OnThink(); if ( IsVisible() ) { if ( m_bSpecScoreboard != spec_scoreboard.GetBool() ) { if ( !spec_scoreboard.GetBool() || !gViewPortInterface->GetActivePanel() ) { m_bSpecScoreboard = spec_scoreboard.GetBool(); gViewPortInterface->ShowPanel( PANEL_SCOREBOARD, m_bSpecScoreboard ); } } } } //----------------------------------------------------------------------------- // Purpose: sets the image to display for the banner in the top right corner //----------------------------------------------------------------------------- void CSpectatorGUI::SetLogoImage(const char *image) { if ( m_pBannerImage ) { m_pBannerImage->SetImage( scheme()->GetImage(image, false) ); } } //----------------------------------------------------------------------------- // Purpose: Sets the text of a control by name //----------------------------------------------------------------------------- void CSpectatorGUI::SetLabelText(const char *textEntryName, const char *text) { Label *entry = dynamic_cast