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.

397 lines
8.8 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef BUYMOUSEOVERPANELBUTTON_H
  8. #define BUYMOUSEOVERPANELBUTTON_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include <KeyValues.h>
  13. #include <filesystem.h>
  14. #include "mouseoverpanelbutton.h"
  15. #include "hud.h"
  16. #include "c_cs_player.h"
  17. #include "cs_gamerules.h"
  18. #include "cstrike/bot/shared_util.h"
  19. #include <vgui/ISurface.h>
  20. #include <vgui/ILocalize.h>
  21. #include <vgui_controls/ImagePanel.h>
  22. using namespace vgui;
  23. //-----------------------------------------------------------------------------
  24. // Purpose: Triggers a new panel when the mouse goes over the button
  25. //-----------------------------------------------------------------------------
  26. class BuyMouseOverPanelButton : public MouseOverPanelButton
  27. {
  28. private:
  29. typedef MouseOverPanelButton BaseClass;
  30. public:
  31. BuyMouseOverPanelButton(vgui::Panel *parent, const char *panelName, vgui::EditablePanel *panel) :
  32. MouseOverPanelButton( parent, panelName, panel)
  33. {
  34. m_iPrice = 0;
  35. m_iPreviousPrice = 0;
  36. m_iASRestrict = 0;
  37. m_iDEUseOnly = 0;
  38. m_command = NULL;
  39. m_bIsBargain = false;
  40. m_pBlackMarketPrice = NULL;//new EditablePanel( parent, "BlackMarket_Labels" );
  41. if ( m_pBlackMarketPrice )
  42. {
  43. m_pBlackMarketPrice->LoadControlSettings( "Resource/UI/BlackMarket_Labels.res" );
  44. int x,y,wide,tall;
  45. GetClassPanel()->GetBounds( x, y, wide, tall );
  46. m_pBlackMarketPrice->SetBounds( x, y, wide, tall );
  47. int px, py;
  48. GetClassPanel()->GetPinOffset( px, py );
  49. int rx, ry;
  50. GetClassPanel()->GetResizeOffset( rx, ry );
  51. // Apply pin settings from template, too
  52. m_pBlackMarketPrice->SetAutoResize( GetClassPanel()->GetPinCorner(), GetClassPanel()->GetAutoResize(), px, py, rx, ry );
  53. }
  54. }
  55. virtual void ApplySettings( KeyValues *resourceData )
  56. {
  57. BaseClass::ApplySettings( resourceData );
  58. KeyValues *kv = resourceData->FindKey( "cost", false );
  59. if( kv ) // if this button has a cost defined for it
  60. {
  61. m_iPrice = kv->GetInt(); // save the price away
  62. }
  63. kv = resourceData->FindKey( "as_restrict", false );
  64. if( kv ) // if this button has a map limitation for it
  65. {
  66. m_iASRestrict = kv->GetInt(); // save the as_restrict away
  67. }
  68. kv = resourceData->FindKey( "de_useonly", false );
  69. if( kv ) // if this button has a map limitation for it
  70. {
  71. m_iDEUseOnly = kv->GetInt(); // save the de_useonly away
  72. }
  73. if ( m_command )
  74. {
  75. delete[] m_command;
  76. m_command = NULL;
  77. }
  78. kv = resourceData->FindKey( "command", false );
  79. if ( kv )
  80. {
  81. m_command = CloneString( kv->GetString() );
  82. }
  83. SetPriceState();
  84. SetMapTypeState();
  85. }
  86. int GetASRestrict() { return m_iASRestrict; }
  87. int GetDEUseOnly() { return m_iDEUseOnly; }
  88. virtual void PerformLayout()
  89. {
  90. BaseClass::PerformLayout();
  91. SetPriceState();
  92. SetMapTypeState();
  93. #ifndef CS_SHIELD_ENABLED
  94. if ( !Q_stricmp( GetName(), "shield" ) )
  95. {
  96. SetVisible( false );
  97. SetEnabled( false );
  98. }
  99. #endif
  100. }
  101. virtual void ApplySchemeSettings( vgui::IScheme *pScheme )
  102. {
  103. BaseClass::ApplySchemeSettings( pScheme );
  104. m_avaliableColor = pScheme->GetColor( "Label.TextColor", Color( 0, 0, 0, 0 ) );
  105. m_unavailableColor = pScheme->GetColor( "Label.DisabledFgColor2", Color( 0, 0, 0, 0 ) );
  106. m_bargainColor = Color( 0, 255, 0, 192 );
  107. SetPriceState();
  108. SetMapTypeState();
  109. }
  110. void SetPriceState()
  111. {
  112. if ( CSGameRules() && CSGameRules()->IsBlackMarket() )
  113. {
  114. SetMarketState();
  115. }
  116. else
  117. {
  118. if ( GetParent() )
  119. {
  120. Panel *pPanel = dynamic_cast< Panel * >(GetParent()->FindChildByName( "MarketSticker" ) );
  121. if ( pPanel )
  122. {
  123. pPanel->SetVisible( false );
  124. }
  125. }
  126. m_bIsBargain = false;
  127. }
  128. C_CSPlayer *pPlayer = C_CSPlayer::GetLocalCSPlayer();
  129. if ( m_iPrice && ( pPlayer && m_iPrice > pPlayer->GetAccount() ) )
  130. {
  131. SetFgColor( m_unavailableColor );
  132. SetCommand( "buy_unavailable" );
  133. }
  134. else
  135. {
  136. if ( m_bIsBargain == false )
  137. {
  138. SetFgColor( m_avaliableColor );
  139. }
  140. else
  141. {
  142. SetFgColor( m_bargainColor );
  143. }
  144. SetCommand( m_command );
  145. }
  146. }
  147. void SetMarketState( void )
  148. {
  149. Panel *pClassPanel = GetClassPanel();
  150. if ( pClassPanel )
  151. {
  152. pClassPanel->SetVisible( false );
  153. }
  154. if ( m_pBlackMarketPrice )
  155. {
  156. Label *pLabel = dynamic_cast< Label * >(m_pBlackMarketPrice->FindChildByName( "pricelabel" ) );
  157. if ( pLabel )
  158. {
  159. const int BufLen = 2048;
  160. wchar_t wbuf[BufLen] = L"";
  161. const wchar_t *formatStr = g_pVGuiLocalize->Find("#Cstrike_MarketPreviousPrice");
  162. if ( !formatStr )
  163. formatStr = L"%s1";
  164. char strPrice[16];
  165. wchar_t szPrice[64];
  166. Q_snprintf( strPrice, sizeof( strPrice ), "%d", m_iPreviousPrice );
  167. g_pVGuiLocalize->ConvertANSIToUnicode( strPrice, szPrice, sizeof(szPrice));
  168. g_pVGuiLocalize->ConstructString( wbuf, sizeof(wbuf), formatStr, 1, szPrice );
  169. pLabel->SetText( wbuf );
  170. pLabel->SetVisible( true );
  171. }
  172. pLabel = dynamic_cast< Label * >(m_pBlackMarketPrice->FindChildByName( "price" ) );
  173. if ( pLabel )
  174. {
  175. const int BufLen = 2048;
  176. wchar_t wbuf[BufLen] = L"";
  177. const wchar_t *formatStr = g_pVGuiLocalize->Find("#Cstrike_MarketCurrentPrice");
  178. if ( !formatStr )
  179. formatStr = L"%s1";
  180. char strPrice[16];
  181. wchar_t szPrice[64];
  182. Q_snprintf( strPrice, sizeof( strPrice ), "%d", m_iPrice );
  183. g_pVGuiLocalize->ConvertANSIToUnicode( strPrice, szPrice, sizeof(szPrice));
  184. g_pVGuiLocalize->ConstructString( wbuf, sizeof(wbuf), formatStr, 1, szPrice );
  185. pLabel->SetText( wbuf );
  186. pLabel->SetVisible( true );
  187. }
  188. pLabel = dynamic_cast< Label * >(m_pBlackMarketPrice->FindChildByName( "difference" ) );
  189. if ( pLabel )
  190. {
  191. const int BufLen = 2048;
  192. wchar_t wbuf[BufLen] = L"";
  193. const wchar_t *formatStr = g_pVGuiLocalize->Find("#Cstrike_MarketDeltaPrice");
  194. if ( !formatStr )
  195. formatStr = L"%s1";
  196. char strPrice[16];
  197. wchar_t szPrice[64];
  198. int iDifference = m_iPreviousPrice - m_iPrice;
  199. if ( iDifference >= 0 )
  200. {
  201. pLabel->SetFgColor( m_bargainColor );
  202. }
  203. else
  204. {
  205. pLabel->SetFgColor( Color( 192, 28, 0, 255 ) );
  206. }
  207. Q_snprintf( strPrice, sizeof( strPrice ), "%d", abs( iDifference ) );
  208. g_pVGuiLocalize->ConvertANSIToUnicode( strPrice, szPrice, sizeof(szPrice));
  209. g_pVGuiLocalize->ConstructString( wbuf, sizeof(wbuf), formatStr, 1, szPrice );
  210. pLabel->SetText( wbuf );
  211. pLabel->SetVisible( true );
  212. }
  213. ImagePanel *pImage = dynamic_cast< ImagePanel * >(m_pBlackMarketPrice->FindChildByName( "classimage" ) );
  214. if ( pImage )
  215. {
  216. ImagePanel *pClassImage = dynamic_cast< ImagePanel * >(GetClassPanel()->FindChildByName( "classimage" ) );
  217. if ( pClassImage )
  218. {
  219. pImage->SetSize( pClassImage->GetWide(), pClassImage->GetTall() );
  220. pImage->SetImage( pClassImage->GetImage() );
  221. }
  222. }
  223. if ( GetParent() )
  224. {
  225. Panel *pPanel = dynamic_cast< Panel * >(GetParent()->FindChildByName( "MarketSticker" ) );
  226. if ( pPanel )
  227. {
  228. if ( m_bIsBargain )
  229. {
  230. pPanel->SetVisible( true );
  231. }
  232. else
  233. {
  234. pPanel->SetVisible( false );
  235. }
  236. }
  237. }
  238. }
  239. }
  240. void SetMapTypeState()
  241. {
  242. CCSGameRules *pRules = CSGameRules();
  243. if ( pRules )
  244. {
  245. if( pRules->IsVIPMap() )
  246. {
  247. if ( m_iASRestrict )
  248. {
  249. SetFgColor( m_unavailableColor );
  250. SetCommand( "buy_unavailable" );
  251. }
  252. }
  253. if ( !pRules->IsBombDefuseMap() )
  254. {
  255. if ( m_iDEUseOnly )
  256. {
  257. SetFgColor( m_unavailableColor );
  258. SetCommand( "buy_unavailable" );
  259. }
  260. }
  261. }
  262. }
  263. void SetBargainButton( bool state )
  264. {
  265. m_bIsBargain = state;
  266. }
  267. void SetCurrentPrice( int iPrice )
  268. {
  269. m_iPrice = iPrice;
  270. }
  271. void SetPreviousPrice( int iPrice )
  272. {
  273. m_iPreviousPrice = iPrice;
  274. }
  275. const char *GetBuyCommand( void )
  276. {
  277. return m_command;
  278. }
  279. virtual void ShowPage()
  280. {
  281. if ( g_lastPanel )
  282. {
  283. for( int i = 0; i< g_lastPanel->GetParent()->GetChildCount(); i++ )
  284. {
  285. MouseOverPanelButton *buyButton = dynamic_cast<MouseOverPanelButton *>(g_lastPanel->GetParent()->GetChild(i));
  286. if ( buyButton )
  287. {
  288. buyButton->HidePage();
  289. }
  290. }
  291. }
  292. BaseClass::ShowPage();
  293. if ( !Q_stricmp( m_command, "vguicancel" ) )
  294. return;
  295. if ( CSGameRules() && CSGameRules()->IsBlackMarket() )
  296. {
  297. if ( m_pBlackMarketPrice && !m_pBlackMarketPrice->IsVisible() )
  298. {
  299. m_pBlackMarketPrice->SetVisible( true );
  300. }
  301. }
  302. }
  303. virtual void HidePage()
  304. {
  305. BaseClass::HidePage();
  306. if ( m_pBlackMarketPrice && m_pBlackMarketPrice->IsVisible() )
  307. {
  308. m_pBlackMarketPrice->SetVisible( false );
  309. }
  310. }
  311. private:
  312. int m_iPrice;
  313. int m_iPreviousPrice;
  314. int m_iASRestrict;
  315. int m_iDEUseOnly;
  316. bool m_bIsBargain;
  317. Color m_avaliableColor;
  318. Color m_unavailableColor;
  319. Color m_bargainColor;
  320. char *m_command;
  321. public:
  322. vgui::EditablePanel *m_pBlackMarketPrice;
  323. };
  324. #endif // BUYMOUSEOVERPANELBUTTON_H