Team Fortress 2 Source Code as on 22/4/2020
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.

286 lines
7.0 KiB

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