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.

883 lines
23 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "cstrikebuysubmenu.h"
  9. #include "cstrikebuymenu.h"
  10. #include "cs_shareddefs.h"
  11. #include "backgroundpanel.h"
  12. #include "buy_presets/buy_presets.h"
  13. #include "cstrike/bot/shared_util.h"
  14. #include <vgui/ISurface.h>
  15. #include <vgui/ILocalize.h>
  16. #include "buypreset_weaponsetlabel.h"
  17. #include "career_box.h"
  18. #include "cs_gamerules.h"
  19. #include "vgui_controls/RichText.h"
  20. #include "cs_weapon_parse.h"
  21. #include "c_cs_player.h"
  22. #include "cs_ammodef.h"
  23. using namespace vgui;
  24. //-----------------------------------------------------------------------------
  25. /**
  26. * This button resizes any images to fit in the width/height constraints
  27. */
  28. class BuyPresetButton : public vgui::Button
  29. {
  30. typedef vgui::Button BaseClass;
  31. public:
  32. BuyPresetButton(vgui::Panel *parent, const char *buttonName, const char *buttonText );
  33. virtual ~BuyPresetButton();
  34. virtual void PerformLayout( void );
  35. virtual void ClearImages( void );
  36. virtual void SetFgColor( Color c )
  37. {
  38. BaseClass::SetFgColor( c );
  39. }
  40. void SetAvailable( bool available )
  41. {
  42. m_available = available;
  43. }
  44. virtual int AddImage( vgui::IImage *image, int offset )
  45. {
  46. if ( image )
  47. {
  48. if ( !m_available )
  49. {
  50. image->SetColor( Color( 128, 128, 128, 255 ) );
  51. }
  52. }
  53. return BaseClass::AddImage( image, offset );
  54. }
  55. virtual void ApplySchemeSettings( vgui::IScheme *pScheme )
  56. {
  57. BaseClass::ApplySchemeSettings( pScheme );
  58. m_availableColor = pScheme->GetColor( "Label.TextColor", Color( 0, 0, 0, 0 ) );
  59. m_unavailableColor = pScheme->GetColor( "Label.DisabledFgColor2", Color( 0, 0, 0, 0 ) );
  60. }
  61. virtual Color GetButtonFgColor( void )
  62. {
  63. return ( m_available ) ? m_availableColor : m_unavailableColor;
  64. }
  65. private:
  66. bool m_available;
  67. Color m_availableColor;
  68. Color m_unavailableColor;
  69. };
  70. //-----------------------------------------------------------------------------
  71. BuyPresetButton::BuyPresetButton(vgui::Panel *parent, const char *buttonName, const char *buttonText ) : Button( parent, buttonName, buttonText )
  72. {
  73. m_available = false;
  74. }
  75. //-----------------------------------------------------------------------------
  76. BuyPresetButton::~BuyPresetButton()
  77. {
  78. ClearImages();
  79. }
  80. //-----------------------------------------------------------------------------
  81. void BuyPresetButton::ClearImages( void )
  82. {
  83. int imageCount = GetImageCount();
  84. for ( int i=0; i<imageCount; ++i )
  85. {
  86. BuyPresetImage *image = dynamic_cast< BuyPresetImage * >(GetImageAtIndex( i ));
  87. if ( image )
  88. {
  89. delete image;
  90. }
  91. }
  92. Button::ClearImages();
  93. }
  94. //-----------------------------------------------------------------------------
  95. void BuyPresetButton::PerformLayout( void )
  96. {
  97. // resize images
  98. int imageCount = GetImageCount();
  99. if ( imageCount > 1 )
  100. {
  101. int wide, tall;
  102. GetSize( wide, tall );
  103. for ( int i=1; i<imageCount; ++i )
  104. {
  105. IImage *image = GetImageAtIndex( i );
  106. if ( image )
  107. {
  108. int imageWide, imageTall;
  109. image->GetSize( imageWide, imageTall );
  110. float scaleX = 1.0f, scaleY = 1.0f;
  111. float widthPercent = 0.2f;
  112. if ( i == 1 )
  113. {
  114. widthPercent = 0.6f;
  115. }
  116. if ( imageWide > wide * widthPercent )
  117. {
  118. scaleX = (float)wide * widthPercent / (float)imageWide;
  119. }
  120. if ( imageTall > tall )
  121. {
  122. scaleY = (float)tall / (float)imageTall;
  123. }
  124. float scale = MIN( scaleX, scaleY );
  125. if ( scale < 1.0f )
  126. {
  127. imageWide *= scale;
  128. imageTall *= scale;
  129. image->SetSize( imageWide, imageTall );
  130. }
  131. }
  132. }
  133. }
  134. Button::PerformLayout();
  135. }
  136. //-----------------------------------------------------------------------------
  137. // Purpose: Constructor
  138. //-----------------------------------------------------------------------------
  139. CCSBuyMenu_CT::CCSBuyMenu_CT(IViewPort *pViewPort) : CCSBaseBuyMenu( pViewPort, "BuySubMenu_CT" )
  140. {
  141. m_pMainMenu->LoadControlSettings( "Resource/UI/BuyMenu_CT.res" );
  142. m_pMainMenu->SetVisible( false );
  143. m_iTeam = TEAM_CT;
  144. CreateBackground( this );
  145. m_backgroundLayoutFinished = false;
  146. }
  147. //-----------------------------------------------------------------------------
  148. // Purpose: Constructor
  149. //-----------------------------------------------------------------------------
  150. CCSBuyMenu_TER::CCSBuyMenu_TER(IViewPort *pViewPort) : CCSBaseBuyMenu( pViewPort, "BuySubMenu_TER" )
  151. {
  152. m_pMainMenu->LoadControlSettings( "Resource/UI/BuyMenu_TER.res" );
  153. m_pMainMenu->SetVisible( false );
  154. m_iTeam = TEAM_TERRORIST;
  155. CreateBackground( this );
  156. m_backgroundLayoutFinished = false;
  157. }
  158. //-----------------------------------------------------------------------------
  159. // Purpose: Constructor
  160. //-----------------------------------------------------------------------------
  161. CCSBaseBuyMenu::CCSBaseBuyMenu(IViewPort *pViewPort, const char *subPanelName) : CBuyMenu( pViewPort )
  162. {
  163. SetTitle( "#Cstrike_Buy_Menu", true);
  164. SetProportional( true );
  165. m_pMainMenu = new CCSBuySubMenu( this, subPanelName );
  166. m_pMainMenu->SetSize( 10, 10 ); // Quiet "parent not sized yet" spew
  167. #if USE_BUY_PRESETS
  168. for ( int i=0; i<NUM_BUY_PRESET_BUTTONS; ++i )
  169. {
  170. m_pBuyPresetButtons[i] = new BuyPresetButton( m_pMainMenu, VarArgs( "BuyPresetButton%c", 'A' + i ), "" );
  171. }
  172. m_pMoney = new Label( m_pMainMenu, "money", "" );
  173. //=============================================================================
  174. // HPE_BEGIN:
  175. // [pfreese] mainBackground was the little orange box outside that buy window
  176. // that shouldn't have been there. Maybe this was left over from some
  177. // copied code.
  178. //=============================================================================
  179. m_pMainBackground = NULL;
  180. // m_pMainBackground = new Panel( m_pMainMenu, "mainBackground" );
  181. //=============================================================================
  182. // HPE_END
  183. //=============================================================================
  184. m_pLoadout = new BuyPresetEditPanel( m_pMainMenu, "loadoutPanel", "Resource/UI/Loadout.res", 0, false );
  185. #else
  186. for ( int i=0; i<NUM_BUY_PRESET_BUTTONS; ++i )
  187. {
  188. m_pBuyPresetButtons[i] = NULL;
  189. }
  190. m_pMoney = NULL;
  191. m_pMainBackground = NULL;
  192. #endif // USE_BUY_PRESETS
  193. m_lastMoney = -1;
  194. m_pBlackMarket = new EditablePanel( m_pMainMenu, "BlackMarket_Bargains" );
  195. m_pBlackMarket->LoadControlSettings( "Resource/UI/BlackMarket_Bargains.res" );
  196. }
  197. //-----------------------------------------------------------------------------
  198. // Purpose:
  199. //-----------------------------------------------------------------------------
  200. void CCSBaseBuyMenu::SetVisible(bool state)
  201. {
  202. BaseClass::SetVisible(state);
  203. if ( state )
  204. {
  205. Panel *defaultButton = FindChildByName( "CancelButton" );
  206. if ( defaultButton )
  207. {
  208. defaultButton->RequestFocus();
  209. }
  210. SetMouseInputEnabled( true );
  211. m_pMainMenu->SetMouseInputEnabled( true );
  212. }
  213. }
  214. //-----------------------------------------------------------------------------
  215. // Purpose: shows/hides the buy menu
  216. //-----------------------------------------------------------------------------
  217. void CCSBaseBuyMenu::ShowPanel(bool bShow)
  218. {
  219. CBuyMenu::ShowPanel( bShow );
  220. #if USE_BUY_PRESETS
  221. if ( bShow )
  222. {
  223. UpdateBuyPresets( true );
  224. }
  225. #endif // USE_BUY_PRESETS
  226. }
  227. //-----------------------------------------------------------------------------
  228. static void GetPanelBounds( Panel *pPanel, wrect_t& bounds )
  229. {
  230. if ( !pPanel )
  231. {
  232. bounds.bottom = bounds.left = bounds.right = bounds.top = 0;
  233. }
  234. else
  235. {
  236. pPanel->GetBounds( bounds.left, bounds.top, bounds.right, bounds.bottom );
  237. bounds.right += bounds.left;
  238. bounds.bottom += bounds.top;
  239. }
  240. }
  241. //-----------------------------------------------------------------------------
  242. void CCSBaseBuyMenu::Paint()
  243. {
  244. #if USE_BUY_PRESETS
  245. C_CSPlayer *pPlayer = C_CSPlayer::GetLocalCSPlayer();
  246. int account = (pPlayer) ? pPlayer->GetAccount() : 0;
  247. if ( m_pMoney && m_lastMoney != account )
  248. {
  249. m_lastMoney = account;
  250. const int BufLen = 128;
  251. wchar_t wbuf[BufLen] = L"";
  252. const wchar_t *formatStr = g_pVGuiLocalize->Find("#Cstrike_Current_Money");
  253. if ( !formatStr )
  254. formatStr = L"%s1";
  255. g_pVGuiLocalize->ConstructString( wbuf, sizeof(wbuf), formatStr, 1, NumAsWString( m_lastMoney ) );
  256. m_pMoney->SetText( wbuf );
  257. }
  258. #endif // USE_BUY_PRESETS
  259. CBuyMenu::Paint();
  260. }
  261. //-----------------------------------------------------------------------------
  262. void CCSBaseBuyMenu::UpdateBuyPresets( bool showDefaultPanel )
  263. {
  264. bool setPanelVisible = false;
  265. if ( !showDefaultPanel )
  266. {
  267. setPanelVisible = true;
  268. }
  269. if ( !TheBuyPresets )
  270. TheBuyPresets = new BuyPresetManager();
  271. int i;
  272. // show buy preset buttons
  273. int numPresets = MIN( TheBuyPresets->GetNumPresets(), NUM_BUY_PRESET_BUTTONS );
  274. for ( i=0; i<numPresets; ++i )
  275. {
  276. if ( !m_pBuyPresetButtons[i] )
  277. continue;
  278. const BuyPreset *preset = TheBuyPresets->GetPreset(i);
  279. int setIndex;
  280. int currentCost = -1;
  281. WeaponSet currentSet;
  282. const WeaponSet *fullSet = NULL;
  283. for ( setIndex = 0; setIndex < preset->GetNumSets(); ++setIndex )
  284. {
  285. const WeaponSet *itemSet = preset->GetSet( setIndex );
  286. if ( itemSet )
  287. {
  288. itemSet->GetCurrent( currentCost, currentSet );
  289. if ( currentCost >= 0 )
  290. {
  291. fullSet = itemSet;
  292. break;
  293. }
  294. }
  295. }
  296. if ( !fullSet && preset->GetNumSets() )
  297. {
  298. fullSet = preset->GetSet( 0 );
  299. }
  300. // set the button's images
  301. m_pBuyPresetButtons[i]->ClearImages();
  302. m_pBuyPresetButtons[i]->SetTextImageIndex( 0 );
  303. m_pBuyPresetButtons[i]->SetText( "" );
  304. m_pBuyPresetButtons[i]->SetAvailable( currentCost >= 0 );
  305. const char *imageName = "";
  306. if ( fullSet )
  307. {
  308. if ( fullSet->GetPrimaryWeapon().GetWeaponID() != WEAPON_NONE )
  309. {
  310. imageName = ImageFnameFromWeaponID( fullSet->GetPrimaryWeapon().GetWeaponID(), true );
  311. BuyPresetImage * image = new BuyPresetImage( scheme()->GetImage(imageName, true) );
  312. m_pBuyPresetButtons[i]->AddImage( image, 0 );
  313. }
  314. if ( fullSet->GetSecondaryWeapon().GetWeaponID() != WEAPON_NONE )
  315. {
  316. imageName = ImageFnameFromWeaponID( fullSet->GetSecondaryWeapon().GetWeaponID(), false );
  317. BuyPresetImage * image = new BuyPresetImage( scheme()->GetImage(imageName, true) );
  318. m_pBuyPresetButtons[i]->AddImage( image, 0 );
  319. }
  320. }
  321. int displayCost = currentCost;
  322. if ( displayCost < 0 )
  323. displayCost = 0;
  324. const int BufLen = 1024;
  325. char aBuf[BufLen];
  326. Q_snprintf(aBuf, BufLen, "#Cstrike_BuyMenuPreset%d", i + 1);
  327. m_pBuyPresetButtons[i]->SetText( g_pVGuiLocalize->Find(aBuf) );
  328. Q_snprintf(aBuf, BufLen, "cl_buy_favorite %d", i + 1);
  329. m_pBuyPresetButtons[i]->SetCommand( aBuf );
  330. m_pBuyPresetButtons[i]->SetVisible( true );
  331. m_pBuyPresetButtons[i]->SetEnabled( true );
  332. }
  333. // hide unused buy preset buttons
  334. for ( i=numPresets+1; i<NUM_BUY_PRESET_BUTTONS; ++i )
  335. {
  336. if ( m_pBuyPresetButtons[i] )
  337. {
  338. m_pBuyPresetButtons[i]->SetVisible( false );
  339. m_pBuyPresetButtons[i]->SetEnabled( true );
  340. }
  341. }
  342. HandleBlackMarket();
  343. }
  344. const char *g_pWeaponNames[] =
  345. {
  346. " ",
  347. "#Cstrike_TitlesTXT_P228",
  348. "#Cstrike_TitlesTXT_Glock18",
  349. "#Cstrike_TitlesTXT_Scout",
  350. "#Cstrike_TitlesTXT_HE_Grenade",
  351. "#Cstrike_TitlesTXT_XM1014",
  352. " ",
  353. "#Cstrike_TitlesTXT_Mac10",
  354. "#Cstrike_TitlesTXT_Aug",
  355. "#Cstrike_TitlesTXT_Smoke_Grenade",
  356. "#Cstrike_TitlesTXT_Dual40",
  357. "#Cstrike_TitlesTXT_FiveSeven",
  358. "#Cstrike_TitlesTXT_UMP45",
  359. "#Cstrike_TitlesTXT_SG550",
  360. "#Cstrike_TitlesTXT_Galil",
  361. "#Cstrike_TitlesTXT_Famas",
  362. "#Cstrike_TitlesTXT_USP45",
  363. "#Cstrike_TitlesTXT_Magnum",
  364. "#Cstrike_TitlesTXT_mp5navy",
  365. "#Cstrike_TitlesTXT_ESM249",
  366. "#Cstrike_TitlesTXT_Leone12",
  367. "#Cstrike_TitlesTXT_M4A1",
  368. "#Cstrike_TitlesTXT_tmp",
  369. "#Cstrike_TitlesTXT_G3SG1",
  370. "#Cstrike_TitlesTXT_Flashbang",
  371. "#Cstrike_TitlesTXT_DesertEagle",
  372. "#Cstrike_TitlesTXT_SG552",
  373. "#Cstrike_TitlesTXT_AK47",
  374. " ",
  375. "#Cstrike_TitlesTXT_FNP90",
  376. " ",
  377. "#Cstrike_TitlesTXT_Kevlar_Vest",
  378. "#Cstrike_TitlesTXT_Kevlar_Vest_Ballistic_Helmet",
  379. "#Cstrike_TitlesTXT_Nightvision_Goggles"
  380. };
  381. int GetWeeklyBargain( void )
  382. {
  383. if ( CSGameRules() == NULL || CSGameRules()->m_pPrices == NULL )
  384. return 0;
  385. C_CSPlayer *pPlayer = C_CSPlayer::GetLocalCSPlayer();
  386. if ( pPlayer == NULL )
  387. return 0;
  388. int iBestIndex = 0;
  389. int iBestBargain = 99999;
  390. for ( int i = 1; i < WEAPON_MAX; i++ )
  391. {
  392. if ( i == WEAPON_SHIELDGUN )
  393. continue;
  394. CCSWeaponInfo *info = GetWeaponInfo( (CSWeaponID)i );
  395. if ( info == NULL )
  396. continue;
  397. if ( info->m_iTeam == TEAM_UNASSIGNED || info->m_iTeam == pPlayer->m_iTeamNum )
  398. {
  399. int iBargain = info->GetWeaponPrice() - info->GetPrevousPrice();
  400. if ( iBargain < iBestBargain )
  401. {
  402. iBestIndex = i;
  403. iBestBargain = iBargain;
  404. }
  405. }
  406. }
  407. return iBestIndex;
  408. }
  409. #ifdef _DEBUG
  410. ConVar cs_testbargain( "cs_testbargain", "1" );
  411. #endif
  412. void CCSBaseBuyMenu::HandleBlackMarket( void )
  413. {
  414. if ( CSGameRules() == NULL )
  415. return;
  416. if ( m_pLoadout )
  417. {
  418. if ( CSGameRules()->IsBlackMarket() )
  419. {
  420. if ( CSGameRules()->m_pPrices == NULL )
  421. return;
  422. if ( m_pBlackMarket == NULL )
  423. return;
  424. int iBargain = GetWeeklyBargain();
  425. CCSWeaponInfo *info = GetWeaponInfo( (CSWeaponID)iBargain );
  426. wchar_t *wszWeaponName = g_pVGuiLocalize->Find( g_pWeaponNames[iBargain]);
  427. if ( wszWeaponName == NULL )
  428. return;
  429. if ( info == NULL )
  430. return;
  431. m_pLoadout->SetVisible( false );
  432. Label *pLabel = dynamic_cast< Label * >(m_pMainMenu->FindChildByName( "loadoutLabel" ));
  433. if ( pLabel )
  434. {
  435. pLabel->SetVisible( false );
  436. }
  437. pLabel = dynamic_cast< Label * >(m_pBlackMarket->FindChildByName( "MarketHeadline" ));
  438. if ( pLabel )
  439. {
  440. const int BufLen = 2048;
  441. wchar_t wbuf[BufLen] = L"";
  442. const wchar_t *formatStr = g_pVGuiLocalize->Find("#Cstrike_MarketHeadline");
  443. if ( !formatStr )
  444. formatStr = L"%s1";
  445. g_pVGuiLocalize->ConstructString( wbuf, sizeof(wbuf), formatStr, 1, wszWeaponName );
  446. pLabel->SetText( wbuf );
  447. }
  448. pLabel = dynamic_cast< Label * >(m_pBlackMarket->FindChildByName( "MarketBargain" ));
  449. if ( pLabel )
  450. {
  451. const int BufLen = 2048;
  452. wchar_t wbuf[BufLen] = L"";
  453. const wchar_t *formatStr = g_pVGuiLocalize->Find("#Cstrike_MarketBargain");
  454. if ( !formatStr )
  455. formatStr = L"%s1";
  456. g_pVGuiLocalize->ConstructString( wbuf, sizeof(wbuf), formatStr, 1, wszWeaponName );
  457. pLabel->SetText( wbuf );
  458. }
  459. pLabel = dynamic_cast< Label * >(m_pBlackMarket->FindChildByName( "MarketStickerPrice" ));
  460. if ( pLabel )
  461. {
  462. char wbuf[16];
  463. Q_snprintf( wbuf, 16, "%d", CSGameRules()->m_pPrices->iCurrentPrice[iBargain] );
  464. pLabel->SetText( wbuf );
  465. }
  466. RichText *pText = dynamic_cast< RichText * >(m_pBlackMarket->FindChildByName( "MarketDescription" ));
  467. if ( pText )
  468. {
  469. char wbuf[2048];
  470. g_pVGuiLocalize->ConvertUnicodeToANSI( g_pVGuiLocalize->Find("#Cstrike_MarketDescription"), wbuf, 2048 );
  471. pText->SetText( "" );
  472. pText->InsertPossibleURLString( wbuf, Color( 255, 255, 255, 255 ), Color( 255, 176, 0, 255 ) );
  473. pText->SetVerticalScrollbar( false );
  474. pText->SetPaintBorderEnabled( false );
  475. pText->SetUnderlineFont( m_hUnderlineFont );
  476. }
  477. pLabel = dynamic_cast< Label * >(m_pBlackMarket->FindChildByName( "MarketBargainIcon" ));
  478. if ( pLabel )
  479. {
  480. char wbuff[12];
  481. Q_snprintf( wbuff, 12, "%c", info->iconActive->cCharacterInFont );
  482. pLabel->SetText( wbuff );
  483. }
  484. Button *pButton = dynamic_cast< Button * >(m_pMainMenu->FindChildByName( "BargainbuyButton" ));
  485. if ( pButton )
  486. {
  487. char command[512];
  488. char *pWeaponName = Q_stristr( info->szClassName, "_" );
  489. if ( pWeaponName )
  490. {
  491. pWeaponName++;
  492. Q_snprintf( command, 512, "buy %s", pWeaponName );
  493. }
  494. pButton->SetCommand( command );
  495. pButton->SetVisible( true );
  496. }
  497. m_pBlackMarket->SetVisible( true );
  498. m_pBlackMarket->SetZPos( -2 );
  499. }
  500. else
  501. {
  502. WeaponSet ws;
  503. TheBuyPresets->GetCurrentLoadout( &ws );
  504. m_pLoadout->SetWeaponSet( &ws, true );
  505. m_pLoadout->SetVisible( true );
  506. Panel *pLabel = dynamic_cast< Label * >(m_pMainMenu->FindChildByName( "loadoutLabel" ));
  507. if ( pLabel )
  508. {
  509. pLabel->SetVisible( true );
  510. }
  511. if ( m_pBlackMarket )
  512. {
  513. m_pBlackMarket->SetVisible( false );
  514. Button *pButton = dynamic_cast< Button * >(m_pMainMenu->FindChildByName( "BargainbuyButton" ));
  515. if ( pButton )
  516. {
  517. pButton->SetVisible( false );
  518. }
  519. }
  520. }
  521. }
  522. }
  523. //-----------------------------------------------------------------------------
  524. // Purpose: The CS background is painted by image panels, so we should do nothing
  525. //-----------------------------------------------------------------------------
  526. void CCSBaseBuyMenu::PaintBackground()
  527. {
  528. }
  529. //-----------------------------------------------------------------------------
  530. // Purpose: Scale / center the window
  531. //-----------------------------------------------------------------------------
  532. void CCSBaseBuyMenu::PerformLayout()
  533. {
  534. BaseClass::PerformLayout();
  535. // stretch the window to fullscreen
  536. if ( !m_backgroundLayoutFinished )
  537. LayoutBackgroundPanel( this );
  538. m_backgroundLayoutFinished = true;
  539. }
  540. //-----------------------------------------------------------------------------
  541. // Purpose:
  542. //-----------------------------------------------------------------------------
  543. void CCSBaseBuyMenu::ApplySchemeSettings( vgui::IScheme *pScheme )
  544. {
  545. BaseClass::ApplySchemeSettings( pScheme );
  546. ApplyBackgroundSchemeSettings( this, pScheme );
  547. if ( m_pMainBackground )
  548. {
  549. m_pMainBackground->SetBorder(pScheme->GetBorder("ButtonDepressedBorder"));
  550. m_pMainBackground->SetBgColor( GetSchemeColor( "Button.BgColor", GetBgColor(), pScheme ) );
  551. }
  552. m_hUnderlineFont = pScheme->GetFont( "CSUnderline", IsProportional() );
  553. #if USE_BUY_PRESETS
  554. UpdateBuyPresets( true );
  555. #endif // USE_BUY_PRESETS
  556. }
  557. //-----------------------------------------------------------------------------
  558. // Purpose:
  559. //-----------------------------------------------------------------------------
  560. static bool IsWeaponInvalid( CSWeaponID weaponID )
  561. {
  562. if ( weaponID == WEAPON_NONE )
  563. return false;
  564. return !CanBuyWeapon( WEAPON_NONE, WEAPON_NONE, weaponID );
  565. }
  566. //-----------------------------------------------------------------------------
  567. // Purpose:
  568. //-----------------------------------------------------------------------------
  569. void CCSBuySubMenu::OnThink()
  570. {
  571. UpdateVestHelmPrice();
  572. BaseClass::OnThink();
  573. }
  574. //-----------------------------------------------------------------------------
  575. // Purpose: When buying vest+helmet, if you already have a vest with no damage
  576. // then the price is reduced to just the helmet. Because this can change during
  577. // the game, we need to update the enable/disable state of the menu item dynamically.
  578. //-----------------------------------------------------------------------------
  579. void CCSBuySubMenu::UpdateVestHelmPrice()
  580. {
  581. C_CSPlayer *pPlayer = C_CSPlayer::GetLocalCSPlayer();
  582. if ( pPlayer == NULL )
  583. return;
  584. BuyMouseOverPanelButton *pButton = dynamic_cast< BuyMouseOverPanelButton * > ( FindChildByName( "kevlar_helmet", false ) );
  585. if ( pButton )
  586. {
  587. // Set its price to the current value from the player.
  588. pButton->SetCurrentPrice( pPlayer->GetCurrentAssaultSuitPrice() );
  589. }
  590. }
  591. //-----------------------------------------------------------------------------
  592. // Purpose:
  593. //-----------------------------------------------------------------------------
  594. void CCSBuySubMenu::OnCommand( const char *command )
  595. {
  596. #if USE_BUY_PRESETS
  597. const char *buyPresetSetString = "cl_buy_favorite_query_set ";
  598. if ( !strnicmp( command, buyPresetSetString, strlen( buyPresetSetString ) ) )
  599. {
  600. bool invalid = IsWeaponInvalid( GetClientWeaponID( true ) ) || IsWeaponInvalid( GetClientWeaponID( false ) );
  601. if ( invalid )
  602. {
  603. // can't save the favorite because it has an invalid weapon (colt for a T, etc)
  604. C_CSPlayer *pPlayer = C_CSPlayer::GetLocalCSPlayer();
  605. if ( pPlayer )
  606. {
  607. pPlayer->EmitSound( "BuyPreset.CantBuy" );
  608. }
  609. if ( cl_buy_favorite_nowarn.GetBool() )
  610. {
  611. BaseClass::OnCommand( "vguicancel" );
  612. }
  613. else
  614. {
  615. CCareerQueryBox *pBox = new CCareerQueryBox( this, "SetLoadoutError", "Resource/UI/SetLoadoutError.res" );
  616. pBox->AddActionSignalTarget( this );
  617. pBox->DoModal();
  618. }
  619. }
  620. else
  621. {
  622. // can save
  623. if ( cl_buy_favorite_quiet.GetBool() )
  624. {
  625. BaseClass::OnCommand( VarArgs( "cl_buy_favorite_set %d", atoi( command + strlen( buyPresetSetString ) ) ) );
  626. }
  627. else
  628. {
  629. CCareerQueryBox *pBox = new CCareerQueryBox( this, "SetLoadoutQuery", "Resource/UI/SetLoadoutQuery.res" );
  630. pBox->SetCancelButtonAsDefault();
  631. if ( pBox->GetOkButton() )
  632. {
  633. pBox->GetOkButton()->SetCommand( VarArgs( "cl_buy_favorite_set %d", atoi( command + strlen( buyPresetSetString ) ) ) );
  634. }
  635. pBox->AddActionSignalTarget( this );
  636. pBox->DoModal();
  637. }
  638. }
  639. return;
  640. }
  641. #endif // USE_BUY_PRESETS
  642. if ( FStrEq( command, "buy_unavailable" ) )
  643. {
  644. C_CSPlayer *pPlayer = C_CSPlayer::GetLocalCSPlayer();
  645. if ( pPlayer )
  646. {
  647. pPlayer->EmitSound( "BuyPreset.CantBuy" );
  648. }
  649. BaseClass::OnCommand( "vguicancel" );
  650. return;
  651. }
  652. BaseClass::OnCommand( command );
  653. }
  654. void CCSBuySubMenu::OnSizeChanged(int newWide, int newTall)
  655. {
  656. m_backgroundLayoutFinished = false;
  657. BaseClass::OnSizeChanged( newWide, newTall );
  658. }
  659. //-----------------------------------------------------------------------------
  660. // Purpose:
  661. //-----------------------------------------------------------------------------
  662. void CCSBuySubMenu::PerformLayout()
  663. {
  664. BaseClass::PerformLayout();
  665. // Buy submenus need to be shoved over for widescreen
  666. int screenW, screenH;
  667. GetHudSize( screenW, screenH );
  668. int fullW, fullH;
  669. fullW = scheme()->GetProportionalScaledValueEx( GetScheme(), 640 );
  670. fullH = scheme()->GetProportionalScaledValueEx( GetScheme(), 480 );
  671. fullW = GetAlternateProportionalValueFromScaled( GetScheme(), fullW );
  672. fullH = GetAlternateProportionalValueFromScaled( GetScheme(), fullH );
  673. int offsetX = (screenW - fullW)/2;
  674. int offsetY = (screenH - fullH)/2;
  675. if ( !m_backgroundLayoutFinished )
  676. ResizeWindowControls( this, GetWide(), GetTall(), offsetX, offsetY );
  677. m_backgroundLayoutFinished = true;
  678. HandleBlackMarket();
  679. }
  680. void CCSBuySubMenu::HandleBlackMarket( void )
  681. {
  682. if ( CSGameRules() == NULL )
  683. return;
  684. int iBestBargain = 99999;
  685. BuyMouseOverPanelButton *pButtonBargain = NULL;
  686. for (int i = 0; i < GetChildCount(); i++)
  687. {
  688. BuyMouseOverPanelButton *pButton = dynamic_cast< BuyMouseOverPanelButton * > ( GetChild(i) );
  689. if (!pButton)
  690. continue;
  691. pButton->SetBargainButton( false );
  692. const char *pWeaponName = Q_stristr( pButton->GetBuyCommand(), " " );
  693. if ( pWeaponName )
  694. {
  695. pWeaponName++;
  696. int iWeaponID = AliasToWeaponID(GetTranslatedWeaponAlias(pWeaponName));
  697. if ( iWeaponID == 0 )
  698. continue;
  699. CCSWeaponInfo *info = GetWeaponInfo( (CSWeaponID)iWeaponID );
  700. if ( info == NULL )
  701. continue;
  702. if ( CSGameRules()->IsBlackMarket() == false )
  703. {
  704. //=============================================================================
  705. // HPE_BEGIN:
  706. // [dwenger] Removed to avoid clearing of default price when not in black market mode
  707. //=============================================================================
  708. // pButton->SetCurrentPrice( info->GetDefaultPrice() );
  709. //=============================================================================
  710. // HPE_END
  711. //=============================================================================
  712. }
  713. else
  714. {
  715. int iBargain = info->GetWeaponPrice() - info->GetPrevousPrice();
  716. pButton->SetCurrentPrice( info->GetWeaponPrice() );
  717. pButton->SetPreviousPrice( info->GetPrevousPrice() );
  718. if ( iBargain < iBestBargain )
  719. {
  720. iBestBargain = iBargain;
  721. pButtonBargain = pButton;
  722. }
  723. }
  724. }
  725. }
  726. if ( pButtonBargain )
  727. {
  728. pButtonBargain->SetBargainButton( true );
  729. }
  730. }