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.

446 lines
9.7 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: [jpaquin] The "Player Two press start" widget
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #if defined( INCLUDE_SCALEFORM )
  8. #include "basepanel.h"
  9. #include "splitscreensignon.h"
  10. #include "../gameui/cstrike15/cstrike15basepanel.h"
  11. #include "../engine/filesystem_engine.h"
  12. #if defined( _X360 )
  13. #include "xbox/xbox_launch.h"
  14. #else
  15. #include "xbox/xboxstubs.h"
  16. #endif
  17. #if defined ( _PS3 )
  18. #include <sysutil/sysutil_userinfo.h>
  19. #endif
  20. #include "engineinterface.h"
  21. #include "modinfo.h"
  22. #include "gameui_interface.h"
  23. #include "tier1/utlbuffer.h"
  24. #include "filesystem.h"
  25. #include <vgui/ILocalize.h>
  26. #include "inputsystem/iinputsystem.h"
  27. using namespace vgui;
  28. // for SRC
  29. #include <vstdlib/random.h>
  30. // memdbgon must be the last include file in a .cpp file!!!
  31. #include <tier0/memdbgon.h>
  32. SFUI_BEGIN_GAME_API_DEF
  33. SFUI_END_GAME_API_DEF( SplitScreenSignonWidget, SplitScreenSignon );
  34. #if defined ( _PS3 )
  35. static CellUserInfoTypeSet s_CellTypeSet;
  36. static bool s_bUserSelectFinished = true;
  37. static CellUserInfoUserStat s_CellUserSelected;
  38. static int s_iUserSelectResult;
  39. static void UserSelectFinishCallback(int result, CellUserInfoUserStat* pSelectUser, void* userdata)
  40. {
  41. s_iUserSelectResult = result;
  42. if(result == CELL_USERINFO_RET_OK)
  43. {
  44. memcpy(&s_CellUserSelected, pSelectUser, sizeof(s_CellUserSelected));
  45. }
  46. s_bUserSelectFinished = true;
  47. }
  48. #endif
  49. SplitScreenSignonWidget::SplitScreenSignonWidget() :
  50. m_bVisible( false ),
  51. m_bConditionsAreValid( false ),
  52. m_bLoading( false ),
  53. m_bWantShown( false ),
  54. m_pPlayer2Name( NULL ),
  55. m_bWaitingForSignon( false ),
  56. m_iSecondPlayerId( -1 ),
  57. m_iControllerThatPressedStart( -1 ),
  58. m_bCurrentlyProcessingSignin( false ),
  59. m_bDropSecondPlayer( false )
  60. {
  61. ListenForGameEvent( "sfuievent" );
  62. #ifdef _PS3
  63. s_CellTypeSet.title = "Select Player 2 user";
  64. s_CellTypeSet.focus = CELL_USERINFO_FOCUS_LISTHEAD;
  65. s_CellTypeSet.type = CELL_USERINFO_LISTTYPE_NOCURRENT;
  66. #endif
  67. }
  68. void SplitScreenSignonWidget::FlashReady( void )
  69. {
  70. m_bLoading = false;
  71. // Setup subscription so we are notified when the user signs in
  72. g_pMatchFramework->GetEventsSubscription()->Subscribe( this );
  73. ( m_bVisible ) ? OnShow() : OnHide();
  74. }
  75. bool SplitScreenSignonWidget::PreUnloadFlash( void )
  76. {
  77. // Remember to unsubscribe so we don't crash later!
  78. StopListeningForAllEvents();
  79. g_pMatchFramework->GetEventsSubscription()->Unsubscribe( this );
  80. return true;
  81. }
  82. void SplitScreenSignonWidget::OnShow( void )
  83. {
  84. if ( FlashAPIIsValid() )
  85. {
  86. WITH_SLOT_LOCKED
  87. {
  88. ScaleformUI()->Value_InvokeWithoutReturn( m_FlashAPI, "showPanel", 0, NULL );
  89. }
  90. }
  91. else if ( !m_bLoading )
  92. {
  93. m_bLoading = true;
  94. SFUI_REQUEST_ELEMENT( SF_FULL_SCREEN_SLOT, g_pScaleformUI, SplitScreenSignonWidget, this, SplitScreenSignon );
  95. }
  96. m_bVisible = true;
  97. }
  98. void SplitScreenSignonWidget::OnHide( void )
  99. {
  100. if ( FlashAPIIsValid() )
  101. {
  102. WITH_SLOT_LOCKED
  103. {
  104. ScaleformUI()->Value_InvokeWithoutReturn( m_FlashAPI, "hidePanel", 0, NULL );
  105. }
  106. }
  107. m_bVisible = false;
  108. }
  109. void SplitScreenSignonWidget::UpdateState( void )
  110. {
  111. bool showit = ( m_bConditionsAreValid && m_bWantShown );
  112. if ( showit != m_bVisible )
  113. showit ? OnShow() : OnHide();
  114. }
  115. void SplitScreenSignonWidget::Show( bool showit )
  116. {
  117. if ( showit != m_bWantShown )
  118. {
  119. m_bWantShown = showit;
  120. UpdateState();
  121. }
  122. }
  123. void SplitScreenSignonWidget::SplitScreenConditionsAreValid( bool value )
  124. {
  125. if ( value != m_bConditionsAreValid )
  126. {
  127. m_bConditionsAreValid = value;
  128. UpdateState();
  129. }
  130. }
  131. void SplitScreenSignonWidget::Update( void )
  132. {
  133. #if defined( _GAMECONSOLE )
  134. SplitScreenConditionsAreValid( g_pInputSystem->GetJoystickCount() > 1 );
  135. if ( m_bVisible )
  136. {
  137. #ifdef _PS3
  138. g_pInputSystem->SetPS3StartButtonIdentificationMode();
  139. #endif
  140. int iUserPressingStart = -1;
  141. int iPrimary = XBX_GetUserId( 0 );
  142. if ( XBX_GetNumGameUsers() == 1 )
  143. {
  144. if ( m_iControllerThatPressedStart == -1 )
  145. {
  146. for ( int i = 0; i < XUSER_MAX_COUNT && ( iUserPressingStart == -1 ) ; i++ )
  147. {
  148. if ( i != iPrimary )
  149. {
  150. if ( g_pInputSystem->IsButtonDown( ButtonCodeToJoystickButtonCode( KEY_XBUTTON_INACTIVE_START, i ) ) )
  151. {
  152. iUserPressingStart = i;
  153. }
  154. }
  155. }
  156. if ( iUserPressingStart != -1 )
  157. {
  158. m_iControllerThatPressedStart = iUserPressingStart;
  159. m_bWaitingForSignon = true;
  160. #if defined( _X360 )
  161. xboxsystem->ShowSigninUI( 2, XSSUI_FLAGS_LOCALSIGNINONLY ); // Two user, no special flags
  162. #elif defined ( _PS3 )
  163. if(s_bUserSelectFinished) // Prevent cellUserInfoSelectUser_ListType being called more than once
  164. {
  165. s_bUserSelectFinished = false;
  166. cellUserInfoEnableOverlay(1); // Dim background while showing user selection dialog
  167. cellUserInfoSelectUser_ListType(&s_CellTypeSet, UserSelectFinishCallback, SYS_MEMORY_CONTAINER_ID_INVALID, NULL);
  168. }
  169. #endif
  170. }
  171. }
  172. #ifdef _PS3
  173. if(m_bWaitingForSignon && s_bUserSelectFinished)
  174. {
  175. m_bWaitingForSignon = false;
  176. if(s_iUserSelectResult == CELL_USERINFO_RET_OK)
  177. {
  178. int userID = s_CellUserSelected.id;
  179. if ( userID != -1 )
  180. {
  181. SetPlayerSignedIn();
  182. SetPlayer2Name( s_CellUserSelected.name );
  183. }
  184. else
  185. {
  186. m_iControllerThatPressedStart = -1;
  187. }
  188. }
  189. else
  190. {
  191. m_iControllerThatPressedStart = -1;
  192. }
  193. }
  194. #endif
  195. }
  196. else if ( ( XBX_GetNumGameUsers() == 2 ) )
  197. {
  198. if(g_pInputSystem->IsButtonDown( ButtonCodeToJoystickButtonCode( KEY_XBUTTON_START, 1 ) ))
  199. {
  200. m_bDropSecondPlayer = true;
  201. }
  202. else if(m_bDropSecondPlayer)
  203. {
  204. // Wait for start button to be released before dropping
  205. // otherwise release event gets converted to KEY_XBUTTON_INACTIVE_START on PS3
  206. m_bDropSecondPlayer = false;
  207. DropSecondPlayer();
  208. }
  209. }
  210. }
  211. #endif
  212. }
  213. void SplitScreenSignonWidget::SetPlayerSignedIn( void )
  214. {
  215. if ( m_iControllerThatPressedStart == -1 )
  216. {
  217. return;
  218. }
  219. #if defined( _GAMECONSOLE )
  220. XBX_SetUserId( 1, m_iControllerThatPressedStart );
  221. XBX_SetUserIsGuest ( 1, 0 );
  222. XBX_SetNumGameUsers ( 2 );
  223. #endif
  224. m_iSecondPlayerId = m_iControllerThatPressedStart;
  225. m_iControllerThatPressedStart = -1;
  226. g_pMatchFramework->GetEventsSubscription()->BroadcastEvent( new KeyValues( "OnProfilesChanged", "numProfiles", ( int ) XBX_GetNumGameUsers() ) );
  227. BasePanel()->UpdateRichPresenceInfo();
  228. ConVarRef ss_enable( "ss_enable" );
  229. ss_enable.SetValue( 1 );
  230. ConVarRef ss_pipsplit( "ss_pipsplit" );
  231. ss_pipsplit.SetValue( 0 );
  232. }
  233. void SplitScreenSignonWidget::SetPlayer2Name( const char* name )
  234. {
  235. #if defined( _GAMECONSOLE )
  236. if ( FlashAPIIsValid() )
  237. {
  238. WITH_SLOT_LOCKED
  239. {
  240. SafeReleaseSFVALUE( m_pPlayer2Name );
  241. if ( name && *name )
  242. {
  243. m_pPlayer2Name = CreateFlashString( name );
  244. m_pScaleformUI->Value_InvokeWithoutReturn( m_FlashAPI, "setPlayer2Name", m_pPlayer2Name, 1 );
  245. }
  246. else
  247. {
  248. m_pScaleformUI->Value_InvokeWithoutReturn( m_FlashAPI, "clearPlayer2Name", NULL, 0 );
  249. }
  250. }
  251. }
  252. #endif
  253. }
  254. void SplitScreenSignonWidget::DropSecondPlayer( void )
  255. {
  256. #if defined( _GAMECONSOLE )
  257. XBX_ClearSlot( 1 );
  258. XBX_SetNumGameUsers ( 1 );
  259. RevertUIToOnePlayerMode();
  260. if ( !m_bCurrentlyProcessingSignin )
  261. {
  262. g_pMatchFramework->GetEventsSubscription()->BroadcastEvent( new KeyValues( "OnProfilesChanged", "numProfiles", ( int ) XBX_GetNumGameUsers() ) );
  263. BasePanel()->UpdateRichPresenceInfo();
  264. }
  265. #endif
  266. }
  267. void SplitScreenSignonWidget::RevertUIToOnePlayerMode( void )
  268. {
  269. if ( FlashAPIIsValid() )
  270. SetPlayer2Name( NULL );
  271. m_iSecondPlayerId = -1;
  272. m_bWaitingForSignon = false;
  273. ConVarRef ss_enable( "ss_enable" );
  274. ss_enable.SetValue( 0 );
  275. ConVarRef ss_pipsplit( "ss_pipsplit" );
  276. ss_pipsplit.SetValue( 0 );
  277. }
  278. void SplitScreenSignonWidget::FireGameEvent( IGameEvent* pEvent )
  279. {
  280. char const *szName = pEvent->GetName();
  281. // Notify that sign-in has completed
  282. if ( !V_stricmp( szName, "sfuievent" ) )
  283. {
  284. const char* action = pEvent->GetString( "action" );
  285. const char* data = pEvent->GetString( "data" );
  286. if ( action && *action )
  287. {
  288. if ( data && *data )
  289. {
  290. if ( !V_stricmp( data, "mainmenu" ) || !V_stricmp( data, "creategamedialog" ) )
  291. {
  292. if ( !V_stricmp( action, "show" ) )
  293. {
  294. Show( true );
  295. }
  296. else
  297. {
  298. Show( false );
  299. }
  300. }
  301. }
  302. }
  303. }
  304. }
  305. void SplitScreenSignonWidget::OnEvent( KeyValues *pEvent )
  306. {
  307. #if defined( _X360 )
  308. if ( !m_bCurrentlyProcessingSignin )
  309. {
  310. m_bCurrentlyProcessingSignin = true;
  311. char const *szName = pEvent->GetName();
  312. // Notify that sign-in has completed
  313. if ( m_bWaitingForSignon )
  314. {
  315. if ( !V_stricmp( szName, "OnSysSigninChange" ) &&
  316. !V_stricmp( "signin", pEvent->GetString( "action", "" ) ) )
  317. {
  318. m_bWaitingForSignon = false;
  319. int userID = pEvent->GetInt( "user1", -1 );
  320. if ( userID != -1 )
  321. {
  322. SetPlayerSignedIn();
  323. }
  324. else
  325. {
  326. m_iControllerThatPressedStart = -1;
  327. }
  328. }
  329. else
  330. if ( !V_stricmp( szName, "OnSysXUIEvent" ) &&
  331. !V_stricmp( "closed", pEvent->GetString( "action", "" ) ) )
  332. {
  333. m_bWaitingForSignon = false;
  334. m_iControllerThatPressedStart = -1;
  335. }
  336. else if ( !V_stricmp( szName, "OnProfilesChanged" ) )
  337. {
  338. m_bWaitingForSignon = false;
  339. m_iControllerThatPressedStart = -1;
  340. }
  341. }
  342. else if ( !V_stricmp( szName, "OnProfilesChanged" ) )
  343. {
  344. if ( m_iSecondPlayerId != -1)
  345. {
  346. IPlayerLocal *pProfile = g_pMatchFramework->GetMatchSystem()->GetPlayerManager()->GetLocalPlayer( m_iSecondPlayerId );
  347. uint state = XUserGetSigninState( m_iSecondPlayerId );
  348. if ( state != eXUserSigninState_NotSignedIn )
  349. {
  350. if ( pProfile )
  351. {
  352. SetPlayer2Name( pProfile->GetName() );
  353. }
  354. else
  355. {
  356. SetPlayer2Name( "Player2" );
  357. }
  358. }
  359. else
  360. {
  361. DropSecondPlayer();
  362. }
  363. }
  364. }
  365. m_bCurrentlyProcessingSignin = false;
  366. }
  367. #endif
  368. }
  369. #endif