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.

388 lines
11 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #if defined( INCLUDE_SCALEFORM )
  9. #include "sfhudcallvotepanel.h"
  10. #include "hud_macros.h"
  11. #include <vgui/ILocalize.h>
  12. #include "c_cs_player.h"
  13. #include "c_cs_playerresource.h"
  14. #include "basepanel.h"
  15. #include "gametypes.h"
  16. #include "cs_gamerules.h"
  17. #include "c_user_message_register.h"
  18. // memdbgon must be the last include file in a .cpp file!!!
  19. #include "tier0/memdbgon.h"
  20. ConVar cl_test_vote_scroll( "cl_test_vote_scroll", "0", FCVAR_CLIENTDLL | FCVAR_DEVELOPMENTONLY );
  21. SFHudCallVotePanel* SFHudCallVotePanel::m_pInstance = NULL;
  22. SFUI_BEGIN_GAME_API_DEF
  23. SFUI_DECL_METHOD( PopulatePlayerTargets ),
  24. SFUI_DECL_METHOD( PopulateMapTargets ),
  25. SFUI_DECL_METHOD( PopulateBackupFilenames ),
  26. SFUI_DECL_METHOD( GetNumberOfValidMapsInGroup ),
  27. SFUI_DECL_METHOD( GetNumberOfValidKickTargets ),
  28. SFUI_DECL_METHOD( IsQueuedMatchmaking ),
  29. SFUI_DECL_METHOD( IsEndMatchMapVoteEnabled ),
  30. SFUI_DECL_METHOD( IsPlayingClassicCompetitive ),
  31. SFUI_END_GAME_API_DEF( SFHudCallVotePanel, CallVotePanel );
  32. //-----------------------------------------------------------------------------
  33. // Purpose: Constructor
  34. //-----------------------------------------------------------------------------
  35. SFHudCallVotePanel::SFHudCallVotePanel() : ScaleformFlashInterface()
  36. {
  37. }
  38. void SFHudCallVotePanel::LoadDialog( void )
  39. {
  40. if ( m_pInstance )
  41. {
  42. m_pInstance->Show();
  43. }
  44. else
  45. {
  46. m_pInstance = new SFHudCallVotePanel();
  47. SFUI_REQUEST_ELEMENT( SF_FULL_SCREEN_SLOT, g_pScaleformUI, SFHudCallVotePanel, m_pInstance, CallVotePanel );
  48. }
  49. }
  50. void SFHudCallVotePanel::UnloadDialog( void )
  51. {
  52. if ( m_pInstance )
  53. {
  54. m_pInstance->RemoveFlashElement();
  55. m_pInstance = NULL;
  56. }
  57. }
  58. void SFHudCallVotePanel::PostUnloadFlash( void )
  59. {
  60. BasePanel()->DismissPauseMenu();
  61. m_pInstance = NULL;
  62. delete this;
  63. }
  64. void SFHudCallVotePanel::LevelShutdown( void )
  65. {
  66. UnloadDialog();
  67. }
  68. void SFHudCallVotePanel::FlashReady( void )
  69. {
  70. if ( !m_FlashAPI )
  71. {
  72. return;
  73. }
  74. Show();
  75. }
  76. bool SFHudCallVotePanel::PreUnloadFlash( void )
  77. {
  78. return true;
  79. }
  80. void SFHudCallVotePanel::Show()
  81. {
  82. WITH_SLOT_LOCKED
  83. {
  84. if ( m_pScaleformUI )
  85. {
  86. m_pScaleformUI->Value_InvokeWithoutReturn( m_FlashAPI, "showPanel", NULL, 0 );
  87. }
  88. }
  89. }
  90. void SFHudCallVotePanel::Hide( void )
  91. {
  92. WITH_SLOT_LOCKED
  93. {
  94. if ( m_pScaleformUI )
  95. {
  96. m_pScaleformUI->Value_InvokeWithoutReturn( m_FlashAPI, "hidePanel", NULL, 0 );
  97. }
  98. }
  99. }
  100. void SFHudCallVotePanel::PopulatePlayerTargets( SCALEFORM_CALLBACK_ARGS_DECL )
  101. {
  102. if ( g_PR )
  103. {
  104. int slot = 0;
  105. SFVALUE result = CreateFlashObject();
  106. SFVALUE names = CreateFlashArray( MAX_PLAYERS );
  107. SFVALUE ids = CreateFlashArray( MAX_PLAYERS );
  108. for ( int player = 1 ; player < MAX_PLAYERS; ++player )
  109. {
  110. if ( g_PR->IsConnected( player ) &&
  111. ( cl_test_vote_scroll.GetBool() || !g_PR->IsFakePlayer( player ) ) &&
  112. GetLocalPlayerIndex() != player &&
  113. g_PR->GetTeam( player ) == g_PR->GetTeam( GetLocalPlayerIndex() ) )
  114. {
  115. player_info_t playerInfo;
  116. if ( engine->GetPlayerInfo( player, &playerInfo ) )
  117. {
  118. wchar_t szName[MAX_DECORATED_PLAYER_NAME_LENGTH];
  119. wchar_t szSafeName[MAX_DECORATED_PLAYER_NAME_LENGTH];
  120. g_pVGuiLocalize->ConvertANSIToUnicode( g_PR->GetPlayerName( player ), szName, sizeof(szName) );
  121. g_pScaleformUI->MakeStringSafe( szName, szSafeName, sizeof( szSafeName ) );
  122. TruncatePlayerName( szSafeName, ARRAYSIZE( szSafeName ), CALLVOTE_NAME_TRUNCATE_AT );
  123. m_pScaleformUI->Value_SetArrayElement( names, slot, szSafeName );
  124. m_pScaleformUI->Value_SetArrayElement( ids, slot, playerInfo.userID );
  125. slot++;
  126. }
  127. }
  128. }
  129. m_pScaleformUI->Value_SetMember( result, "names", names );
  130. m_pScaleformUI->Value_SetMember( result, "ids", ids );
  131. m_pScaleformUI->Value_SetMember( result, "count", slot );
  132. m_pScaleformUI->Params_SetResult( obj, result );
  133. SafeReleaseSFVALUE( names );
  134. SafeReleaseSFVALUE( ids );
  135. SafeReleaseSFVALUE( result );
  136. }
  137. }
  138. void SFHudCallVotePanel::GetNumberOfValidKickTargets( SCALEFORM_CALLBACK_ARGS_DECL )
  139. {
  140. if ( g_PR )
  141. {
  142. int slot = 0;
  143. SFVALUE result = CreateFlashObject();
  144. for ( int player = 1; player < MAX_PLAYERS; ++player )
  145. {
  146. if ( g_PR->IsConnected( player ) &&
  147. ( cl_test_vote_scroll.GetBool() || !g_PR->IsFakePlayer( player ) ) &&
  148. GetLocalPlayerIndex() != player &&
  149. g_PR->GetTeam( player ) == g_PR->GetTeam( GetLocalPlayerIndex() ) )
  150. {
  151. player_info_t playerInfo;
  152. if ( engine->GetPlayerInfo( player, &playerInfo ) )
  153. {
  154. slot++;
  155. }
  156. }
  157. }
  158. m_pScaleformUI->Value_SetMember( result, "count", slot );
  159. m_pScaleformUI->Params_SetResult( obj, result );
  160. SafeReleaseSFVALUE( result );
  161. }
  162. }
  163. void SFHudCallVotePanel::PopulateMapTargets( SCALEFORM_CALLBACK_ARGS_DECL )
  164. {
  165. bool bIncludeCurrentMap = false;
  166. if ( pui->Params_GetNumArgs( obj ) >= 1 )
  167. {
  168. bIncludeCurrentMap = pui->Params_GetArgAsBool( obj, 0 );
  169. }
  170. SFVALUE result = CreateFlashObject();
  171. SFVALUE names = CreateFlashArray( MAX_TARGET_COUNT );
  172. SFVALUE ids = CreateFlashArray( MAX_TARGET_COUNT );
  173. int numMaps = 0; //The number of maps in this cycle group
  174. int actualNumMaps = 0; //The number of maps we display (in case we have any errors)
  175. if ( g_pGameTypes )
  176. {
  177. const char* mapGroupName = engine->GetMapGroupName();
  178. const CUtlStringList* mapsInGroup = g_pGameTypes->GetMapGroupMapList( mapGroupName );
  179. if ( mapsInGroup )
  180. {
  181. numMaps = mapsInGroup->Count();
  182. char szCurLevel[MAX_PATH];
  183. V_strcpy_safe( szCurLevel, engine->GetLevelName() );
  184. V_FixSlashes(szCurLevel, '/' ); // use consistent slashes.
  185. V_StripExtension( szCurLevel, szCurLevel, sizeof( szCurLevel ) );
  186. for( int i = 0 ; i < numMaps ; ++i )
  187. {
  188. const char* internalMapName = (*mapsInGroup)[i];
  189. if ( !bIncludeCurrentMap && V_strstr( szCurLevel, V_GetFileName( internalMapName ) ) )
  190. {
  191. // don't populate the list with the same map that we're on
  192. continue;
  193. }
  194. if ( internalMapName && V_strcmp( "undefined", internalMapName ) != 0 && V_strlen( internalMapName ) > 0 )
  195. {
  196. const wchar_t* friendlyMapName = CSGameRules()->GetFriendlyMapName(internalMapName);
  197. if ( friendlyMapName )
  198. {
  199. m_pScaleformUI->Value_SetArrayElement( names, actualNumMaps, friendlyMapName );
  200. }
  201. else
  202. {
  203. m_pScaleformUI->Value_SetArrayElement( names, actualNumMaps, internalMapName );
  204. }
  205. m_pScaleformUI->Value_SetArrayElement( ids, actualNumMaps, internalMapName );
  206. actualNumMaps++;
  207. }
  208. }
  209. }
  210. }
  211. m_pScaleformUI->Value_SetMember( result, "friendlyNames", names );
  212. m_pScaleformUI->Value_SetMember( result, "names", ids );
  213. m_pScaleformUI->Value_SetMember( result, "count", actualNumMaps );
  214. m_pScaleformUI->Params_SetResult( obj, result );
  215. SafeReleaseSFVALUE( names );
  216. SafeReleaseSFVALUE( ids );
  217. SafeReleaseSFVALUE( result );
  218. }
  219. void SFHudCallVotePanel::PopulateBackupFilenames( SCALEFORM_CALLBACK_ARGS_DECL )
  220. {
  221. engine->ServerCmd( "send_round_backup_file_list" );
  222. }
  223. bool __MsgFunc_RoundBackupFilenames( const CCSUsrMsg_RoundBackupFilenames &msg )
  224. {
  225. if ( SFHudCallVotePanel::m_pInstance )
  226. {
  227. SFHudCallVotePanel::m_pInstance->PopulateBackupFilenames_Callback( msg );
  228. }
  229. return true;
  230. }
  231. USER_MESSAGE_REGISTER( RoundBackupFilenames );
  232. void SFHudCallVotePanel::PopulateBackupFilenames_Callback( const CCSUsrMsg_RoundBackupFilenames &msg )
  233. {
  234. if ( !FlashAPIIsValid() )
  235. return;
  236. static CCSUsrMsg_RoundBackupFilenames files[ 10 ];
  237. files[ clamp( msg.index(), 0, 9 ) ] = msg;
  238. // don't do anything until we get the last file msg.
  239. if ( msg.index() < ( msg.count() - 1 ) )
  240. return;
  241. char szCommaDelimitedFilenames[1024] = { 0 };
  242. char szCommaDelimitedNicenames[1024] = { 0 };
  243. for ( int i = 0; i < min ( msg.count(), 10 ); i++ )
  244. {
  245. if ( *szCommaDelimitedFilenames )
  246. V_strcat_safe( szCommaDelimitedFilenames, "," );
  247. V_strcat_safe( szCommaDelimitedFilenames, files[ i ].filename().c_str() );
  248. if ( *szCommaDelimitedNicenames )
  249. V_strcat_safe( szCommaDelimitedNicenames, "," );
  250. V_strcat_safe( szCommaDelimitedNicenames, files[ i ].nicename().c_str() );
  251. }
  252. WITH_SFVALUEARRAY( data, 2 )
  253. {
  254. m_pScaleformUI->ValueArray_SetElement( data, 0, szCommaDelimitedFilenames );
  255. m_pScaleformUI->ValueArray_SetElement( data, 1, szCommaDelimitedNicenames );
  256. m_pScaleformUI->Value_InvokeWithoutReturn( m_FlashAPI, "PopulateBackupFilenames_Callback", data, 2 );
  257. }
  258. }
  259. void SFHudCallVotePanel::GetNumberOfValidMapsInGroup( SCALEFORM_CALLBACK_ARGS_DECL )
  260. {
  261. bool bIncludeCurrentMap = false;
  262. if ( pui->Params_GetNumArgs( obj ) >= 1 )
  263. {
  264. bIncludeCurrentMap = pui->Params_GetArgAsBool( obj, 0 );
  265. }
  266. SFVALUE result = CreateFlashObject();
  267. int numMaps = 0; //The number of maps in this cycle group
  268. int actualNumMaps = 0; //The number of maps we display (in case we have any errors)
  269. static ConVarRef sv_vote_to_changelevel_before_match_point( "sv_vote_to_changelevel_before_match_point" );
  270. if ( g_pGameTypes && CSGameRules()
  271. && !( sv_vote_to_changelevel_before_match_point.GetInt() > 0 && ( CSGameRules()->IsMatchPoint() || CSGameRules()->IsIntermission() ) ) )
  272. {
  273. const char* mapGroupName = engine->GetMapGroupName();
  274. const CUtlStringList* mapsInGroup = g_pGameTypes->GetMapGroupMapList( mapGroupName );
  275. if ( mapsInGroup )
  276. {
  277. numMaps = mapsInGroup->Count();
  278. for( int i = 0 ; i < numMaps ; ++i )
  279. {
  280. const char* internalMapName = ( *mapsInGroup )[i];
  281. if ( !bIncludeCurrentMap && 0 == V_strcmp( engine->GetLevelNameShort(), internalMapName ) )
  282. {
  283. // don't populate the list with the same map that we're on
  284. continue;
  285. }
  286. else if ( internalMapName && V_strlen( internalMapName ) > 0 )
  287. {
  288. actualNumMaps++;
  289. }
  290. }
  291. }
  292. }
  293. m_pScaleformUI->Value_SetMember( result, "count", actualNumMaps );
  294. m_pScaleformUI->Params_SetResult( obj, result );
  295. SafeReleaseSFVALUE( result );
  296. }
  297. void SFHudCallVotePanel::IsQueuedMatchmaking( SCALEFORM_CALLBACK_ARGS_DECL )
  298. {
  299. bool bQ = CSGameRules() && CSGameRules()->IsQueuedMatchmaking();
  300. m_pScaleformUI->Params_SetResult( obj, bQ );
  301. }
  302. void SFHudCallVotePanel::IsEndMatchMapVoteEnabled( SCALEFORM_CALLBACK_ARGS_DECL )
  303. {
  304. bool bY = false;
  305. C_CS_PlayerResource *cs_PR = dynamic_cast<C_CS_PlayerResource *>( g_PR );
  306. if ( cs_PR && CSGameRules()->IsEndMatchVotingForNextMapEnabled() )
  307. bY = true;
  308. m_pScaleformUI->Params_SetResult( obj, bY );
  309. }
  310. void SFHudCallVotePanel::IsPlayingClassicCompetitive( SCALEFORM_CALLBACK_ARGS_DECL )
  311. {
  312. m_pScaleformUI->Params_SetResult( obj, ( CSGameRules() && CSGameRules()->IsPlayingAnyCompetitiveStrictRuleset() ) );
  313. }
  314. #endif // INCLUDE_SCALEFORM