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.

889 lines
25 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "vgui/IInput.h"
  8. #include <vgui/IVGui.h>
  9. #include <vgui/IScheme.h>
  10. #include <vgui/ILocalize.h>
  11. #include "vgui_controls/TextImage.h"
  12. #include "ienginevgui.h"
  13. #include "iclientmode.h"
  14. #include "baseviewport.h"
  15. #include "item_pickup_panel.h"
  16. #include "econ_ui.h"
  17. #include "econ_item_inventory.h"
  18. #include "econ_item_constants.h"
  19. #include "item_confirm_delete_dialog.h"
  20. #include "backpack_panel.h"
  21. #ifdef TF_CLIENT_DLL
  22. #include "c_tf_freeaccount.h"
  23. #include "clientmode_tf.h"
  24. #include "quest_log_panel.h"
  25. #endif
  26. //-----------------------------------------------------------------------------
  27. // Purpose:
  28. //-----------------------------------------------------------------------------
  29. CItemPickupPanel::CItemPickupPanel( Panel *parent ) : Frame( parent, "item_pickup" )
  30. {
  31. vgui::VPANEL gameuiPanel = enginevgui->GetPanel( PANEL_GAMEUIDLL );
  32. SetParent( gameuiPanel );
  33. // We don't want the gameui to delete us, or things get messy
  34. SetAutoDelete( false );
  35. SetMoveable( false );
  36. SetSizeable( false );
  37. vgui::HScheme scheme = vgui::scheme()->LoadSchemeFromFileEx( enginevgui->GetPanel( PANEL_CLIENTDLL ), "resource/ClientScheme.res", "ClientScheme");
  38. SetScheme(scheme);
  39. SetProportional( true );
  40. m_pNextButton = new vgui::Button( this, "NextButton", "#Next" );
  41. m_pPrevButton = new vgui::Button( this, "PrevButton", "#Prev" );
  42. m_pDiscardButton = NULL;
  43. m_pDiscardedLabel = NULL;
  44. m_pOpenLoadoutButton = NULL;
  45. m_pConfirmDeleteDialog = NULL;
  46. m_pModelPanelsKV = NULL;
  47. m_bRandomizePickupMethods = false;
  48. m_pItemFoundMethod = NULL;
  49. m_pCloseButton = NULL;
  50. for ( int i = 0; i < ITEMPICKUP_NUM_MODELPANELS; i++ )
  51. {
  52. m_aModelPanels[i] = new CItemModelPanel( this, VarArgs("modelpanel%d", i) );
  53. }
  54. ListenForGameEvent( "gameui_hidden" );
  55. vgui::EditablePanel *pToolTipPanel = new vgui::EditablePanel( this, "DiscardButtonTooltip" );
  56. m_pToolTip = new CSimplePanelToolTip( this );
  57. m_pToolTip->SetControlledPanel( pToolTipPanel );
  58. }
  59. //-----------------------------------------------------------------------------
  60. // Purpose:
  61. //-----------------------------------------------------------------------------
  62. CItemPickupPanel::~CItemPickupPanel()
  63. {
  64. if ( m_pModelPanelsKV )
  65. {
  66. m_pModelPanelsKV->deleteThis();
  67. m_pModelPanelsKV = NULL;
  68. }
  69. }
  70. //-----------------------------------------------------------------------------
  71. // Purpose:
  72. //-----------------------------------------------------------------------------
  73. void CItemPickupPanel::ApplySchemeSettings( vgui::IScheme *pScheme )
  74. {
  75. BaseClass::ApplySchemeSettings( pScheme );
  76. LoadControlSettings( "Resource/UI/econ/ItemPickupPanel.res" );
  77. m_pItemsFoundLabel = dynamic_cast<vgui::Label*>(FindChildByName( "ItemsFoundLabel" ));
  78. m_pDiscardedLabel = dynamic_cast<vgui::Label*>( FindChildByName("DiscardedLabel") );
  79. m_pItemFoundMethod = dynamic_cast<vgui::Label*>(FindChildByName( "SelectedItemFoundMethodLabel" ));
  80. for ( int i = 0; i < ITEMPICKUP_NUM_MODELPANELS; i++ )
  81. {
  82. m_aModelPanels[i]->SetPaintBackgroundType( 2 );
  83. }
  84. m_pDiscardButton = dynamic_cast<CExImageButton*>( FindChildByName("DiscardButton") );
  85. if ( m_pDiscardButton )
  86. {
  87. m_pDiscardButton->SetTooltip( m_pToolTip, "" );
  88. }
  89. m_pOpenLoadoutButton = dynamic_cast<vgui::Button*>( FindChildByName("OpenLoadoutButton") );
  90. m_pCloseButton = dynamic_cast<CExButton*>( FindChildByName("CloseButton") );
  91. }
  92. //-----------------------------------------------------------------------------
  93. // Purpose:
  94. //-----------------------------------------------------------------------------
  95. void CItemPickupPanel::ApplySettings( KeyValues *inResourceData )
  96. {
  97. BaseClass::ApplySettings( inResourceData );
  98. KeyValues *pModelKV = inResourceData->FindKey( "modelpanelskv" );
  99. if ( pModelKV )
  100. {
  101. if ( m_pModelPanelsKV )
  102. {
  103. m_pModelPanelsKV->deleteThis();
  104. }
  105. m_pModelPanelsKV = new KeyValues("modelpanelkv");
  106. pModelKV->CopySubkeys( m_pModelPanelsKV );
  107. for ( int i = 0; i < ITEMPICKUP_NUM_MODELPANELS; i++ )
  108. {
  109. m_aModelPanels[i]->ApplySettings( m_pModelPanelsKV );
  110. }
  111. }
  112. }
  113. //-----------------------------------------------------------------------------
  114. // Purpose:
  115. //-----------------------------------------------------------------------------
  116. void CItemPickupPanel::PerformLayout( void )
  117. {
  118. if ( GetVParent() )
  119. {
  120. int w,h;
  121. vgui::ipanel()->GetSize( GetVParent(), w, h );
  122. SetBounds(0,0,w,h);
  123. }
  124. BaseClass::PerformLayout();
  125. // Now lay out our model panels
  126. for ( int i = 0; i < ITEMPICKUP_NUM_MODELPANELS; i++ )
  127. {
  128. int iPos = (i - ITEMPICKUP_MODELPANEL_CENTER);
  129. int iXPos = (GetWide() * 0.5) + (iPos * m_iModelPanelSpacing) + (iPos * m_iModelPanelW) - (m_iModelPanelW * 0.5);
  130. m_aModelPanels[i]->SetBounds( iXPos, m_iModelPanelY, m_iModelPanelW, m_iModelPanelH );
  131. m_aModelPanels[i]->SetVisible( m_aModelPanels[i]->HasItem() );
  132. }
  133. }
  134. //-----------------------------------------------------------------------------
  135. // Purpose:
  136. //-----------------------------------------------------------------------------
  137. void CItemPickupPanel::ShowPanel(bool bShow)
  138. {
  139. if ( bShow )
  140. {
  141. // Start with the first item centered, and the rest off to the right.
  142. m_iSelectedItem = 0;
  143. UpdateModelPanels();
  144. MoveToFront(); // Any open dialogs from the main menu (options, servers, etc.) showing up sometimes - this should fix.
  145. }
  146. else
  147. {
  148. for ( int i = 0; i < m_aItems.Count(); i++ )
  149. {
  150. if ( !m_aItems[i].bDiscarded )
  151. {
  152. // if not a real item, ignore
  153. if ( m_aItems[i].pItem.GetSOCData() == NULL )
  154. {
  155. continue;
  156. }
  157. // ignore items that are purchased or are store promotional items
  158. CEconItem *pEconItem = m_aItems[i].pItem.GetSOCData();
  159. switch ( pEconItem->GetOrigin() )
  160. {
  161. case kEconItemOrigin_PreviewItem:
  162. case kEconItemOrigin_Purchased:
  163. case kEconItemOrigin_StorePromotion:
  164. continue;
  165. }
  166. }
  167. }
  168. if ( m_pConfirmDeleteDialog )
  169. {
  170. m_pConfirmDeleteDialog->MarkForDeletion();
  171. m_pConfirmDeleteDialog = NULL;
  172. }
  173. m_aItems.Purge();
  174. }
  175. SetMouseInputEnabled( bShow );
  176. SetVisible( bShow );
  177. }
  178. //-----------------------------------------------------------------------------
  179. // Purpose:
  180. //-----------------------------------------------------------------------------
  181. void CItemPickupPanel::FireGameEvent( IGameEvent *event )
  182. {
  183. if ( !IsVisible() )
  184. return;
  185. const char * type = event->GetName();
  186. if ( Q_strcmp(type, "gameui_hidden") == 0 )
  187. {
  188. ShowPanel( false );
  189. }
  190. }
  191. //-----------------------------------------------------------------------------
  192. // Purpose:
  193. //-----------------------------------------------------------------------------
  194. void CItemPickupPanel::OnCommand( const char *command )
  195. {
  196. if ( !Q_stricmp( command, "vguicancel" ) )
  197. {
  198. AcknowledgeItems();
  199. ShowPanel( false );
  200. }
  201. else if ( !Q_stricmp( command, "changeloadout" ) )
  202. {
  203. AcknowledgeItems();
  204. ShowPanel( false );
  205. EconUI()->OpenEconUI( ECONUI_LOADOUT, true );
  206. }
  207. else if ( !Q_stricmp( command, "nextitem" ) )
  208. {
  209. m_iSelectedItem = clamp( m_iSelectedItem+1, 0, m_aItems.Count()-1 );
  210. UpdateModelPanels();
  211. }
  212. else if ( !Q_stricmp( command, "previtem" ) )
  213. {
  214. m_iSelectedItem = clamp( m_iSelectedItem-1, 0, m_aItems.Count()-1 );
  215. UpdateModelPanels();
  216. }
  217. else if ( !Q_stricmp( command, "discarditem" ) )
  218. {
  219. // Bring up confirm dialog
  220. CConfirmDeleteItemDialog *pConfirm = vgui::SETUP_PANEL( new CConfirmDeleteItemDialog( this ) );
  221. if ( pConfirm )
  222. {
  223. pConfirm->Show();
  224. m_pConfirmDeleteDialog = pConfirm;
  225. }
  226. }
  227. else
  228. {
  229. engine->ClientCmd( const_cast<char *>( command ) );
  230. }
  231. BaseClass::OnCommand( command );
  232. }
  233. //-----------------------------------------------------------------------------
  234. // Purpose:
  235. //-----------------------------------------------------------------------------
  236. void CItemPickupPanel::UpdateModelPanels( void )
  237. {
  238. for ( int i = 0; i < ITEMPICKUP_NUM_MODELPANELS; i++ )
  239. {
  240. int iPos = (i - ITEMPICKUP_MODELPANEL_CENTER);
  241. int iItem = m_iSelectedItem + iPos;
  242. if ( iItem < 0 || iItem >= m_aItems.Count() )
  243. {
  244. m_aModelPanels[i]->SetItem( NULL );
  245. m_aModelPanels[i]->SetVisible( false );
  246. continue;
  247. }
  248. m_aModelPanels[i]->SetItem( &m_aItems[iItem].pItem );
  249. m_aModelPanels[i]->SetVisible( true );
  250. }
  251. // Enable next & prev buttons if there are other items to the sides
  252. bool bCanScrollRight = (m_iSelectedItem + 1) < m_aItems.Count();
  253. m_pNextButton->SetVisible( bCanScrollRight );
  254. m_pNextButton->SetEnabled( bCanScrollRight );
  255. bool bCanScrollLeft = ((m_iSelectedItem - 1) >= 0) && m_aItems.Count();
  256. m_pPrevButton->SetVisible( bCanScrollLeft );
  257. m_pPrevButton->SetEnabled( bCanScrollLeft );
  258. bool bDiscarded = false;
  259. if ( m_iSelectedItem >= 0 && m_iSelectedItem < m_aItems.Count() )
  260. {
  261. bDiscarded = m_aItems[m_iSelectedItem].bDiscarded;
  262. }
  263. bool bAllowDiscard = true;
  264. int iFoundMethod = 0;
  265. if ( m_bRandomizePickupMethods )
  266. {
  267. iFoundMethod = RandomInt( 0, ARRAYSIZE(g_pszItemPickupMethodStrings)-1 );
  268. }
  269. else if ( m_iSelectedItem >= 0 && m_iSelectedItem < m_aItems.Count() )
  270. {
  271. iFoundMethod = GetUnacknowledgedReason( m_aItems[m_iSelectedItem].pItem.GetInventoryPosition() );
  272. bAllowDiscard = ( iFoundMethod <= UNACK_ITEM_DROPPED );
  273. iFoundMethod--;
  274. if ( iFoundMethod < 0 || iFoundMethod >= ARRAYSIZE(g_pszItemPickupMethodStrings) )
  275. {
  276. iFoundMethod = 0;
  277. }
  278. }
  279. // Hide the discard button if it's not a random find
  280. m_pDiscardButton->SetVisible( !bDiscarded && bAllowDiscard );
  281. m_pOpenLoadoutButton->SetVisible( !bDiscarded );
  282. m_pDiscardedLabel->SetVisible( bDiscarded );
  283. if ( m_pItemFoundMethod )
  284. {
  285. enum
  286. {
  287. COLOR_NORMAL = 1,
  288. COLOR_HIGHLIGHTED = 2,
  289. };
  290. m_pItemFoundMethod->GetTextImage()->ClearColorChangeStream();
  291. wchar_t wszTmp[1024];
  292. wchar_t *pLocText = g_pVGuiLocalize->Find( g_pszItemPickupMethodStrings[iFoundMethod] );
  293. if ( pLocText )
  294. {
  295. // Loop through and replace the color changes
  296. Color baseColor = m_pItemFoundMethod->GetFgColor();
  297. wchar_t *wpszChar = pLocText;
  298. int iOutPos = 0;
  299. while ( *wpszChar )
  300. {
  301. if ( *wpszChar == COLOR_NORMAL )
  302. {
  303. m_pItemFoundMethod->GetTextImage()->AddColorChange( baseColor, iOutPos );
  304. }
  305. else if ( *wpszChar == COLOR_HIGHLIGHTED )
  306. {
  307. m_pItemFoundMethod->GetTextImage()->AddColorChange( Color(200,80,60,255), iOutPos );
  308. }
  309. else
  310. {
  311. wszTmp[iOutPos] = *wpszChar;
  312. iOutPos++;
  313. }
  314. wpszChar++;
  315. if ( iOutPos >= (ARRAYSIZE(wszTmp) - 1) )
  316. {
  317. wszTmp[iOutPos] = L'\0';
  318. break;
  319. }
  320. }
  321. if ( iOutPos < (ARRAYSIZE(wszTmp) - 1) )
  322. {
  323. wszTmp[iOutPos] = L'\0';
  324. }
  325. m_pItemFoundMethod->SetText( wszTmp );
  326. }
  327. }
  328. if ( m_pItemsFoundLabel )
  329. {
  330. if ( m_aItems.Count() > 1 )
  331. {
  332. m_pItemsFoundLabel->SetText( "#NewItemsAcquired" );
  333. SetDialogVariable( "numitems", m_aItems.Count() );
  334. }
  335. else
  336. {
  337. m_pItemsFoundLabel->SetText( "#NewItemAcquired" );
  338. }
  339. }
  340. SetDialogVariable( "selecteditem", m_iSelectedItem+1 );
  341. // Update the loadout button as appropriate
  342. if ( m_iSelectedItem >= 0 && m_iSelectedItem < m_aItems.Count() )
  343. {
  344. if ( m_aItems[m_iSelectedItem].pItem.IsValid() && !bDiscarded )
  345. {
  346. SetDialogVariable("loadouttext", g_pVGuiLocalize->Find( "#OpenGeneralLoadout" ) );
  347. }
  348. }
  349. if ( m_pCloseButton )
  350. {
  351. const char *pszCloseString = (m_bReturnToGame && engine->IsInGame()) ? "#CloseItemPanel" : "#GameUI_OK";
  352. m_pCloseButton->SetText( g_pVGuiLocalize->Find( pszCloseString ) );
  353. }
  354. m_pCloseButton->RequestFocus();
  355. }
  356. //-----------------------------------------------------------------------------
  357. void CItemPickupPanel::AcknowledgeItems( void )
  358. {
  359. // On command, AckKnowledge all these items
  360. for ( int i = 0; i < m_aItems.Count(); i++ )
  361. {
  362. InventoryManager()->AcknowledgeItem( &m_aItems[i].pItem, false );
  363. }
  364. InventoryManager()->SaveAckFile();
  365. // If we were crafting, and the craft panel is up, we return to that instead.
  366. if ( EconUI()->IsUIPanelVisible( ECONUI_CRAFTING ) )
  367. return;
  368. // Check to make sure the player has room for all his items. If not, bring up the discard panel. Otherwise, go away.
  369. if ( !InventoryManager()->CheckForRoomAndForceDiscard( ) )
  370. {
  371. #ifdef TF_CLIENT_DLL
  372. // If the quest log is up, we just go back to that
  373. if ( GetQuestLog() && GetQuestLog()->IsVisible() )
  374. return;
  375. #endif
  376. // If we're connected to a game server, we also close the game UI.
  377. if ( m_bReturnToGame && engine->IsInGame() )
  378. {
  379. engine->ClientCmd_Unrestricted( "gameui_hide" );
  380. }
  381. }
  382. }
  383. //-----------------------------------------------------------------------------
  384. // Purpose: Handles the escape key
  385. //-----------------------------------------------------------------------------
  386. void CItemPickupPanel::OnKeyCodeTyped( vgui::KeyCode code )
  387. {
  388. if( code == KEY_ESCAPE )
  389. {
  390. OnCommand( "vguicancel" );
  391. }
  392. else
  393. {
  394. BaseClass::OnKeyCodeTyped( code );
  395. }
  396. }
  397. //-----------------------------------------------------------------------------
  398. // Purpose: Handles button press code for controllers and enter
  399. //-----------------------------------------------------------------------------
  400. void CItemPickupPanel::OnKeyCodePressed( vgui::KeyCode code )
  401. {
  402. ButtonCode_t nButtonCode = GetBaseButtonCode( code );
  403. if( code == KEY_ENTER || nButtonCode == KEY_XBUTTON_A || nButtonCode == KEY_XBUTTON_B || nButtonCode == STEAMCONTROLLER_A || nButtonCode == STEAMCONTROLLER_B )
  404. {
  405. OnCommand( "vguicancel" );
  406. }
  407. else if( code == KEY_HOME || nButtonCode == KEY_XBUTTON_Y || nButtonCode == STEAMCONTROLLER_Y )
  408. {
  409. OnCommand( "changeloadout" );
  410. }
  411. else if ( nButtonCode == KEY_XBUTTON_RIGHT ||
  412. nButtonCode == KEY_XSTICK1_RIGHT ||
  413. nButtonCode == KEY_XSTICK2_RIGHT ||
  414. nButtonCode == KEY_RIGHT )
  415. {
  416. OnCommand( "nextitem" );
  417. }
  418. else if ( nButtonCode == KEY_XBUTTON_LEFT ||
  419. nButtonCode == KEY_XSTICK1_LEFT ||
  420. nButtonCode == KEY_XSTICK2_LEFT ||
  421. nButtonCode == KEY_LEFT )
  422. {
  423. OnCommand( "previtem" );
  424. }
  425. else
  426. {
  427. BaseClass::OnKeyCodePressed( code );
  428. }
  429. }
  430. //-----------------------------------------------------------------------------
  431. // Purpose:
  432. //-----------------------------------------------------------------------------
  433. void CItemPickupPanel::AddItem( CEconItemView *pItem )
  434. {
  435. // Guard against duplicates
  436. FOR_EACH_VEC( m_aItems, i )
  437. {
  438. if ( m_aItems[i].pItem.GetItemID() == pItem->GetItemID() )
  439. return;
  440. }
  441. int iIdx = m_aItems.AddToTail();
  442. m_aItems[iIdx].pItem = *pItem;
  443. m_aItems[iIdx].bDiscarded = false;
  444. if ( IsVisible() )
  445. {
  446. UpdateModelPanels();
  447. InvalidateLayout();
  448. }
  449. }
  450. //-----------------------------------------------------------------------------
  451. // Purpose:
  452. //-----------------------------------------------------------------------------
  453. void CItemPickupPanel::OnConfirmDelete( KeyValues *data )
  454. {
  455. m_pConfirmDeleteDialog = NULL;
  456. m_pCloseButton->RequestFocus();
  457. int iConfirmed = data->GetInt( "confirmed", 0 );
  458. if ( !iConfirmed )
  459. return;
  460. if ( m_iSelectedItem >= 0 && m_iSelectedItem < m_aItems.Count() )
  461. {
  462. if ( m_aItems[m_iSelectedItem].pItem.IsValid() )
  463. {
  464. CPlayerInventory *pInventory = InventoryManager()->GetLocalInventory();
  465. if ( pInventory )
  466. {
  467. EconUI()->Gamestats_ItemTransaction( IE_ITEM_DISCARDED, &m_aItems[m_iSelectedItem].pItem );
  468. InventoryManager()->DropItem( m_aItems[m_iSelectedItem].pItem.GetItemID() );
  469. InvalidateLayout();
  470. m_aItems[m_iSelectedItem].bDiscarded = true;
  471. m_pDiscardButton->SetVisible( false );
  472. m_pOpenLoadoutButton->SetVisible( false );
  473. m_pDiscardedLabel->SetVisible( true );
  474. // If we've discarded all our items, we exit immediately
  475. bool bFoundUndiscarded = false;
  476. for ( int i = 0; i < m_aItems.Count(); i++ )
  477. {
  478. if ( !m_aItems[i].bDiscarded )
  479. {
  480. bFoundUndiscarded = true;
  481. break;
  482. }
  483. }
  484. if ( !bFoundUndiscarded )
  485. {
  486. OnCommand( "vguicancel" );
  487. }
  488. }
  489. }
  490. }
  491. }
  492. static vgui::DHANDLE<CItemPickupPanel> g_ItemPickupPanel;
  493. //-----------------------------------------------------------------------------
  494. // Purpose:
  495. //-----------------------------------------------------------------------------
  496. CItemPickupPanel *OpenItemPickupPanel( void )
  497. {
  498. if (!g_ItemPickupPanel.Get())
  499. {
  500. g_ItemPickupPanel = vgui::SETUP_PANEL( new CItemPickupPanel( NULL ) );
  501. g_ItemPickupPanel->InvalidateLayout( false, true );
  502. }
  503. engine->ClientCmd_Unrestricted( "gameui_activate" );
  504. g_ItemPickupPanel->ShowPanel( true );
  505. return g_ItemPickupPanel;
  506. }
  507. //-----------------------------------------------------------------------------
  508. // Purpose:
  509. //-----------------------------------------------------------------------------
  510. CItemPickupPanel *GetItemPickupPanel( void )
  511. {
  512. return g_ItemPickupPanel.Get();
  513. }
  514. //=======================================================================================================================================================
  515. // ITEM DISCARD PANEL
  516. //=======================================================================================================================================================
  517. //-----------------------------------------------------------------------------
  518. // Purpose:
  519. //-----------------------------------------------------------------------------
  520. CItemDiscardPanel::CItemDiscardPanel( Panel *parent ) : Frame( parent, "item_discard" )
  521. {
  522. vgui::VPANEL gameuiPanel = enginevgui->GetPanel( PANEL_GAMEUIDLL );
  523. SetParent( gameuiPanel );
  524. // We don't want the gameui to delete us, or things get messy
  525. SetAutoDelete( false );
  526. SetMoveable( false );
  527. SetSizeable( false );
  528. vgui::HScheme scheme = vgui::scheme()->LoadSchemeFromFileEx( enginevgui->GetPanel( PANEL_CLIENTDLL ), "resource/ClientScheme.res", "ClientScheme");
  529. SetScheme(scheme);
  530. SetProportional( true );
  531. m_pModelPanel = new CItemModelPanel( this, "modelpanel" );
  532. m_pBackpackPanel = new CBackpackPanel( this, "backpack_panel" );
  533. m_pConfirmDeleteDialog = NULL;
  534. m_bDiscardedNewItem = false;
  535. m_bMadeRoom = false;
  536. m_pDiscardedLabelCarat = NULL;
  537. m_pDiscardedLabel = NULL;
  538. m_pDiscardButton = NULL;
  539. m_pCloseButton = NULL;
  540. m_pItemMouseOverPanel = new CItemModelPanel( this, "ItemMouseOverItemPanel" );
  541. m_pItemToolTip = new CSimplePanelToolTip( this );
  542. m_pItemToolTip->SetControlledPanel( m_pItemMouseOverPanel );
  543. m_pModelPanel->SetTooltip( m_pItemToolTip, "" );
  544. m_pModelPanel->SetActAsButton( false, true );
  545. ListenForGameEvent( "gameui_hidden" );
  546. }
  547. //-----------------------------------------------------------------------------
  548. // Purpose:
  549. //-----------------------------------------------------------------------------
  550. void CItemDiscardPanel::ApplySchemeSettings( vgui::IScheme *pScheme )
  551. {
  552. BaseClass::ApplySchemeSettings( pScheme );
  553. m_pItemMouseOverPanel->InvalidateLayout( true, true );
  554. m_pModelPanel->InvalidateLayout( true, true );
  555. LoadControlSettings( "Resource/UI/econ/ItemDiscardPanel.res" );
  556. // Highlight the X on the discard button
  557. m_pDiscardButton = dynamic_cast<CExButton*>( FindChildByName("DiscardButton") );
  558. if ( m_pDiscardButton )
  559. {
  560. SetXToRed( m_pDiscardButton );
  561. }
  562. m_pCloseButton = dynamic_cast<CExButton*>( FindChildByName("CloseButton") );
  563. m_pDiscardedLabel = dynamic_cast<vgui::Label*>( FindChildByName("DiscardedLabel") );
  564. m_pDiscardedLabelCarat = dynamic_cast<vgui::Label*>( FindChildByName("DiscardedCaratLabel") );
  565. }
  566. //-----------------------------------------------------------------------------
  567. // Purpose:
  568. //-----------------------------------------------------------------------------
  569. void CItemDiscardPanel::PerformLayout( void )
  570. {
  571. if ( GetVParent() )
  572. {
  573. int w,h;
  574. vgui::ipanel()->GetSize( GetVParent(), w, h );
  575. SetBounds(0,0,w,h);
  576. }
  577. BaseClass::PerformLayout();
  578. m_pDiscardedLabelCarat->SetVisible( m_bDiscardedNewItem );
  579. m_pDiscardedLabel->SetVisible( m_bDiscardedNewItem );
  580. m_pDiscardButton->SetVisible( !m_bDiscardedNewItem && !m_bMadeRoom );
  581. m_pCloseButton->SetVisible( m_bDiscardedNewItem || m_bMadeRoom );
  582. if ( m_pItemMouseOverPanel->IsVisible() )
  583. {
  584. m_pItemMouseOverPanel->SetVisible( false );
  585. }
  586. }
  587. //-----------------------------------------------------------------------------
  588. // Purpose:
  589. //-----------------------------------------------------------------------------
  590. void CItemDiscardPanel::ShowPanel(bool bShow)
  591. {
  592. if ( bShow )
  593. {
  594. m_bDiscardedNewItem = false;
  595. m_bMadeRoom = false;
  596. m_pBackpackPanel->ShowPanel( 0, true );
  597. InvalidateLayout();
  598. }
  599. else
  600. {
  601. if ( m_pConfirmDeleteDialog )
  602. {
  603. m_pConfirmDeleteDialog->MarkForDeletion();
  604. m_pConfirmDeleteDialog = NULL;
  605. }
  606. }
  607. SetMouseInputEnabled( bShow );
  608. SetVisible( bShow );
  609. #ifdef TF_CLIENT_DLL
  610. // If the player made room and is a trial account, suggest that they upgrade to get more space.
  611. if ( bShow && IsFreeTrialAccount() )
  612. {
  613. ShowUpgradeMessageBox( "#TF_TrialNeedSpace_Title", "#TF_TrialNeedSpace_Text" );
  614. }
  615. #endif
  616. }
  617. //-----------------------------------------------------------------------------
  618. // Purpose:
  619. //-----------------------------------------------------------------------------
  620. void CItemDiscardPanel::FireGameEvent( IGameEvent *event )
  621. {
  622. if ( !IsVisible() )
  623. return;
  624. const char * type = event->GetName();
  625. if ( Q_strcmp(type, "gameui_hidden") == 0 )
  626. {
  627. // If they haven't discarded down to <MAX items, bring us right back up again
  628. if ( InventoryManager()->CheckForRoomAndForceDiscard() )
  629. {
  630. engine->ClientCmd_Unrestricted( "gameui_activate" );
  631. }
  632. else
  633. {
  634. ShowPanel( false );
  635. }
  636. }
  637. }
  638. //-----------------------------------------------------------------------------
  639. // Purpose:
  640. //-----------------------------------------------------------------------------
  641. void CItemDiscardPanel::OnCommand( const char *command )
  642. {
  643. if ( !Q_stricmp( command, "vguicancel" ) )
  644. {
  645. ShowPanel( false );
  646. // Check to make sure the player has room for all his items. If not, bring up the discard panel. Otherwise, go away.
  647. if ( !InventoryManager()->CheckForRoomAndForceDiscard() )
  648. {
  649. // If we're connected to a game server, we also close the game UI.
  650. if ( engine->IsInGame() )
  651. {
  652. engine->ClientCmd_Unrestricted( "gameui_hide" );
  653. }
  654. }
  655. }
  656. else if ( !Q_stricmp( command, "discarditem" ) )
  657. {
  658. // Bring up confirm dialog
  659. CConfirmDeleteItemDialog *pConfirm = vgui::SETUP_PANEL( new CConfirmDeleteItemDialog( this ) );
  660. if ( pConfirm )
  661. {
  662. pConfirm->Show();
  663. m_pConfirmDeleteDialog = pConfirm;
  664. }
  665. }
  666. else
  667. {
  668. engine->ClientCmd( const_cast<char *>( command ) );
  669. }
  670. BaseClass::OnCommand( command );
  671. }
  672. //-----------------------------------------------------------------------------
  673. // Purpose:
  674. //-----------------------------------------------------------------------------
  675. void CItemDiscardPanel::SetItem( CEconItemView *pItem )
  676. {
  677. if ( m_pModelPanel )
  678. {
  679. m_pModelPanel->SetItem( pItem );
  680. }
  681. if ( m_pItemMouseOverPanel )
  682. {
  683. m_pItemMouseOverPanel->SetItem( pItem );
  684. IScheme *pScheme = scheme()->GetIScheme( GetScheme() );
  685. m_pItemMouseOverPanel->InvalidateLayout(true);
  686. m_pItemMouseOverPanel->SetBorder( pScheme->GetBorder("MainMenuBGBorder") );
  687. }
  688. }
  689. //-----------------------------------------------------------------------------
  690. // Purpose:
  691. //-----------------------------------------------------------------------------
  692. void CItemDiscardPanel::OnConfirmDelete( KeyValues *data )
  693. {
  694. if ( !m_bDiscardedNewItem && !m_bMadeRoom && data && m_pModelPanel->HasItem() )
  695. {
  696. int iConfirmed = data->GetInt( "confirmed", 0 );
  697. if ( iConfirmed == 2 )
  698. {
  699. // Player discarded an item from the backpack to make room
  700. m_bMadeRoom = true;
  701. InvalidateLayout();
  702. }
  703. else if ( iConfirmed )
  704. {
  705. CPlayerInventory *pInventory = InventoryManager()->GetLocalInventory();
  706. if ( !pInventory )
  707. return;
  708. InventoryManager()->DropItem( m_pModelPanel->GetItem()->GetItemID() );
  709. m_bDiscardedNewItem = true;
  710. InvalidateLayout();
  711. }
  712. m_pBackpackPanel->RequestFocus();
  713. }
  714. m_pConfirmDeleteDialog = NULL;
  715. }
  716. //-----------------------------------------------------------------------------
  717. // Purpose: Handles the escape key
  718. //-----------------------------------------------------------------------------
  719. void CItemDiscardPanel::OnKeyCodeTyped( vgui::KeyCode code )
  720. {
  721. if( code == KEY_ESCAPE )
  722. {
  723. OnCommand( "vguicancel" );
  724. }
  725. else
  726. {
  727. BaseClass::OnKeyCodeTyped( code );
  728. }
  729. }
  730. //-----------------------------------------------------------------------------
  731. // Purpose: Handles button press code for controllers and enter
  732. //-----------------------------------------------------------------------------
  733. void CItemDiscardPanel::OnKeyCodePressed( vgui::KeyCode code )
  734. {
  735. ButtonCode_t nButtonCode = GetBaseButtonCode( code );
  736. if( nButtonCode == KEY_XBUTTON_B )
  737. {
  738. OnCommand( "vguicancel" );
  739. }
  740. else
  741. {
  742. BaseClass::OnKeyCodePressed( code );
  743. }
  744. }
  745. static vgui::DHANDLE<CItemDiscardPanel> g_ItemDiscardPanel;
  746. //-----------------------------------------------------------------------------
  747. // Purpose:
  748. //-----------------------------------------------------------------------------
  749. CItemDiscardPanel *OpenItemDiscardPanel( void )
  750. {
  751. if (!g_ItemDiscardPanel.Get())
  752. {
  753. g_ItemDiscardPanel = vgui::SETUP_PANEL( new CItemDiscardPanel( NULL ) );
  754. g_ItemDiscardPanel->InvalidateLayout( false, true );
  755. }
  756. engine->ClientCmd_Unrestricted( "gameui_activate" );
  757. g_ItemDiscardPanel->ShowPanel( true );
  758. return g_ItemDiscardPanel;
  759. }
  760. //-----------------------------------------------------------------------------
  761. // Purpose:
  762. //-----------------------------------------------------------------------------
  763. CItemDiscardPanel *GetItemDiscardPanel( void )
  764. {
  765. return g_ItemDiscardPanel.Get();
  766. }