Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

276 lines
6.8 KiB

  1. //====== Copyright � 1996-2005, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #include "vgui_baseui_interface.h"
  7. #include "vgui/IVGui.h"
  8. #include "vgui_controls/Frame.h"
  9. #include "vgui_controls/Label.h"
  10. #include "vgui_controls/AnimationController.h"
  11. #include "vgui/ILocalize.h"
  12. #include "mathlib/mathlib.h"
  13. #include "inputsystem/ButtonCode.h"
  14. #include "vgui_askconnectpanel.h"
  15. #include "keys.h"
  16. #include "cl_pluginhelpers.h"
  17. // NOTE: This has to be the last file included!
  18. #include "tier0/memdbgon.h"
  19. using namespace vgui;
  20. class CAskConnectPanel : public EditablePanel
  21. {
  22. DECLARE_CLASS_SIMPLE( CAskConnectPanel, vgui::EditablePanel );
  23. public:
  24. CAskConnectPanel( VPANEL parent );
  25. ~CAskConnectPanel();
  26. void GetHostName( char *pOut, int maxOutBytes );
  27. void SetHostName( const char *pHostName );
  28. void StartSlideAnimation( float flDuration );
  29. void UpdateCurrentPosition();
  30. void Hide();
  31. virtual void ApplySchemeSettings( vgui::IScheme *pScheme );
  32. virtual void ApplySettings(KeyValues *inResourceData);
  33. virtual void OnTick();
  34. public:
  35. static CAskConnectPanel *s_pAskConnectPanel;
  36. private:
  37. char m_HostName[256];
  38. Color m_bgColor;
  39. int m_OriginalWidth; // Don't get less than this wide.
  40. double m_flAnimationEndTime; // -1 if not playing
  41. Label *m_pInfoLabel;
  42. Label *m_pHostNameLabel;
  43. int m_HostNameLabelRightSidePadding; // Grow the whole panel to make sure there's this much padding on the right of the hostname label.
  44. Label *m_pAcceptLabel;
  45. AnimationController *m_pAnimationController;
  46. };
  47. CAskConnectPanel *CAskConnectPanel::s_pAskConnectPanel = NULL;
  48. CAskConnectPanel::CAskConnectPanel( VPANEL parent )
  49. : BaseClass( NULL, "AskConnectPanel" ), m_bgColor( 0, 0, 0, 192 )
  50. {
  51. SetParent( parent );
  52. Assert( s_pAskConnectPanel == NULL );
  53. s_pAskConnectPanel = this;
  54. m_flAnimationEndTime = -1;
  55. SetKeyBoardInputEnabled( false );
  56. SetMouseInputEnabled( false );
  57. SetVisible( false );
  58. m_pHostNameLabel = new Label( this, "HostNameLabel", "" );
  59. m_pAcceptLabel = new Label( this, "AcceptLabel", "" );
  60. m_pInfoLabel = new Label( this, "InfoLabel", "" );
  61. m_HostName[0] = 0;
  62. vgui::ivgui()->AddTickSignal( GetVPanel() );
  63. SetAutoDelete( true );
  64. m_pAnimationController = new vgui::AnimationController( NULL );
  65. m_pAnimationController->SetParent( parent );
  66. m_pAnimationController->SetScriptFile( parent, "scripts/plugin_animations.txt" );
  67. m_pAnimationController->SetProportional( false );
  68. LoadControlSettings( "resource\\askconnectpanel.res" );
  69. InvalidateLayout( true );
  70. m_OriginalWidth = GetWide();
  71. int x, y, wide, tall;
  72. m_pHostNameLabel->GetBounds( x, y, wide, tall );
  73. m_HostNameLabelRightSidePadding = GetWide() - (x+wide);
  74. }
  75. CAskConnectPanel::~CAskConnectPanel()
  76. {
  77. s_pAskConnectPanel = NULL;
  78. }
  79. void CAskConnectPanel::GetHostName( char *pOut, int maxOutBytes )
  80. {
  81. V_strncpy( pOut, m_HostName, maxOutBytes );
  82. }
  83. void CAskConnectPanel::SetHostName( const char *pHostName )
  84. {
  85. V_strncpy( m_HostName, pHostName, sizeof( m_HostName ) );
  86. m_pHostNameLabel->SetText( pHostName );
  87. // Update our width.
  88. int x, y, wide, tall;
  89. m_pHostNameLabel->SizeToContents();
  90. m_pHostNameLabel->GetBounds( x, y, wide, tall );
  91. int x2, y2, wide2, tall2;
  92. wchar_t wcMessage[512];
  93. g_pVGuiLocalize->ConstructString( wcMessage, sizeof( wcMessage ), g_pVGuiLocalize->Find("#Valve_ServerOfferingToConnect"), 0 );
  94. m_pInfoLabel->SetText( wcMessage );
  95. m_pInfoLabel->SizeToContents();
  96. m_pInfoLabel->GetBounds( x2, y2, wide2, tall2 );
  97. int desiredWidth = MAX(x+wide,x2+wide2) + m_HostNameLabelRightSidePadding;
  98. if ( desiredWidth < m_OriginalWidth )
  99. desiredWidth = m_OriginalWidth;
  100. SetWide( desiredWidth );
  101. }
  102. void CAskConnectPanel::ApplySettings(KeyValues *inResourceData)
  103. {
  104. BaseClass::ApplySettings(inResourceData);
  105. const char *pStr = inResourceData->GetString( "BgColor", NULL );
  106. if ( pStr )
  107. {
  108. int r, g, b, a;
  109. if ( sscanf( pStr, "%d %d %d %d", &r, &g, &b, &a ) == 4 )
  110. {
  111. m_bgColor = Color( r, g, b, a );
  112. SetBgColor( m_bgColor );
  113. }
  114. }
  115. }
  116. void CAskConnectPanel::StartSlideAnimation( float flDuration )
  117. {
  118. m_flAnimationEndTime = Plat_FloatTime() + flDuration;
  119. // Figure out what key they have bound...
  120. const char *pKeyName = Key_NameForBinding( "askconnect_accept" );
  121. if ( pKeyName )
  122. {
  123. wchar_t wcKeyName[64], wcMessage[512];
  124. g_pVGuiLocalize->ConvertANSIToUnicode( pKeyName, wcKeyName, sizeof( wcKeyName ) );
  125. g_pVGuiLocalize->ConstructString( wcMessage, sizeof( wcMessage ), g_pVGuiLocalize->Find("#Valve_PressKeyToAccept"), 1, wcKeyName );
  126. m_pAcceptLabel->SetText( wcMessage );
  127. }
  128. else
  129. {
  130. m_pAcceptLabel->SetText( "#Valve_BindKeyToAccept" );
  131. }
  132. m_pAnimationController->StartAnimationSequence( "AskConnectShow" );
  133. SetVisible( true );
  134. InvalidateLayout();
  135. UpdateCurrentPosition();
  136. }
  137. void CAskConnectPanel::Hide()
  138. {
  139. m_flAnimationEndTime = -1;
  140. SetVisible( false );
  141. }
  142. void CAskConnectPanel::OnTick()
  143. {
  144. // Do the hide animation?
  145. if ( m_flAnimationEndTime != -1 )
  146. {
  147. if ( Plat_FloatTime() > m_flAnimationEndTime )
  148. {
  149. m_flAnimationEndTime = -1;
  150. m_pAnimationController->StartAnimationSequence( "AskConnectHide" );
  151. }
  152. }
  153. m_pAnimationController->UpdateAnimations( Sys_FloatTime() );
  154. // Make sure vgui doesn't call Paint() on us after we're hidden.
  155. if ( GetAlpha() == 0 )
  156. SetVisible( false );
  157. if ( IsVisible() )
  158. {
  159. UpdateCurrentPosition();
  160. }
  161. BaseClass::OnTick();
  162. }
  163. void CAskConnectPanel::UpdateCurrentPosition()
  164. {
  165. int x=0, y=0, wide=0, tall=0;
  166. if ( g_PluginManager )
  167. g_PluginManager->GetHudMessagePosition( x, y, wide, tall );
  168. SetPos( x, y+tall );
  169. }
  170. void CAskConnectPanel::ApplySchemeSettings( vgui::IScheme *pScheme )
  171. {
  172. BaseClass::ApplySchemeSettings( pScheme );
  173. SetBgColor( m_bgColor );
  174. SetPaintBackgroundType( 2 );
  175. }
  176. void SetupDefaultAskConnectAcceptKey()
  177. {
  178. // If they don't have a binding for askconnect_accept, set one up.
  179. if ( !Key_NameForBinding( "askconnect_accept" ) )
  180. {
  181. // .. but only if they don't already have something setup for F3.
  182. if ( !Key_BindingForKey( KEY_F3 ) )
  183. {
  184. Key_SetBinding( KEY_F3, "askconnect_accept" );
  185. }
  186. }
  187. }
  188. vgui::Panel* CreateAskConnectPanel( VPANEL parent )
  189. {
  190. return new CAskConnectPanel( parent );
  191. }
  192. void ShowAskConnectPanel( const char *pHostName, float flDuration )
  193. {
  194. CAskConnectPanel *pPanel = CAskConnectPanel::s_pAskConnectPanel;
  195. if ( pPanel )
  196. {
  197. pPanel->SetHostName( pHostName );
  198. pPanel->StartSlideAnimation( flDuration );
  199. pPanel->MoveToFront();
  200. }
  201. }
  202. void HideAskConnectPanel()
  203. {
  204. CAskConnectPanel *pPanel = CAskConnectPanel::s_pAskConnectPanel;
  205. if ( pPanel )
  206. pPanel->Hide();
  207. }
  208. bool IsAskConnectPanelActive( char *pHostName, int maxHostNameBytes )
  209. {
  210. CAskConnectPanel *pPanel = CAskConnectPanel::s_pAskConnectPanel;
  211. if ( pPanel && pPanel->IsVisible() && pPanel->GetAlpha() > 0 )
  212. {
  213. pPanel->GetHostName( pHostName, maxHostNameBytes );
  214. return true;
  215. }
  216. else
  217. {
  218. return false;
  219. }
  220. }