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.

447 lines
12 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #include "cbase.h"
  7. #include "weapon_csbase.h"
  8. #include "cs_ammodef.h"
  9. #include <vgui/IVGui.h>
  10. #include <vgui/IScheme.h>
  11. #include <vgui/ISurface.h>
  12. #include <vgui_controls/Label.h>
  13. #include <vgui/ILocalize.h>
  14. #include "vgui_controls/BuildGroup.h"
  15. #include "vgui_controls/BitmapImagePanel.h"
  16. #include "vgui_controls/TextEntry.h"
  17. #include "vgui_controls/TextImage.h"
  18. #include "vgui_controls/RichText.h"
  19. #include "vgui_controls/QueryBox.h"
  20. #include "career_box.h"
  21. #include "buypreset_listbox.h"
  22. #include "buypreset_weaponsetlabel.h"
  23. #include "backgroundpanel.h"
  24. #include "cstrike/bot/shared_util.h"
  25. using namespace vgui;
  26. const float horizTitleRatio = 18.0f/68.0f;
  27. //--------------------------------------------------------------------------------------------------------------
  28. //--------------------------------------------------------------------------------------------------------------
  29. /*
  30. class PresetNameTextEntry : public TextEntry
  31. {
  32. public:
  33. PresetNameTextEntry(Panel *parent, CBuyPresetEditMainMenu *menu, const char *name ) : TextEntry( parent, name )
  34. {
  35. m_pMenu = menu;
  36. }
  37. virtual void FireActionSignal()
  38. {
  39. TextEntry::FireActionSignal();
  40. if ( m_pMenu )
  41. {
  42. m_pMenu->SetDirty();
  43. }
  44. }
  45. private:
  46. CBuyPresetEditMainMenu *m_pMenu;
  47. };
  48. */
  49. //--------------------------------------------------------------------------------------------------------------
  50. //--------------------------------------------------------------------------------------------------------------
  51. int GetScaledValue( HScheme hScheme, int unscaled )
  52. {
  53. int val = scheme()->GetProportionalScaledValueEx( hScheme, unscaled );
  54. return GetAlternateProportionalValueFromScaled( hScheme, val );
  55. }
  56. //--------------------------------------------------------------------------------------------------------------
  57. //--------------------------------------------------------------------------------------------------------------
  58. class PresetBackgroundPanel : public vgui::Panel
  59. {
  60. typedef vgui::Panel BaseClass;
  61. public:
  62. PresetBackgroundPanel( vgui::Panel *parent, const char *panelName ) : BaseClass( parent, panelName )
  63. {
  64. };
  65. virtual void ApplySchemeSettings( vgui::IScheme *pScheme )
  66. {
  67. BaseClass::ApplySchemeSettings( pScheme );
  68. SetBorder( pScheme->GetBorder("ButtonBorder") );
  69. m_lineColor = pScheme->GetColor( "Border.Bright", Color( 0, 0, 0, 0 ) );
  70. }
  71. virtual void ApplySettings( KeyValues *inResourceData )
  72. {
  73. BaseClass::ApplySettings( inResourceData );
  74. m_lines.RemoveAll();
  75. KeyValues *lines = inResourceData->FindKey( "lines", false );
  76. if ( lines )
  77. {
  78. KeyValues *line = lines->GetFirstValue();
  79. while ( line )
  80. {
  81. const char *str = line->GetString( NULL, "" );
  82. Vector4D p;
  83. int numPoints = sscanf( str, "%f %f %f %f", &p[0], &p[1], &p[2], &p[3] );
  84. if ( numPoints == 4 )
  85. {
  86. m_lines.AddToTail( p );
  87. }
  88. line = line->GetNextValue();
  89. }
  90. }
  91. }
  92. virtual void PaintBackground( void )
  93. {
  94. BaseClass::PaintBackground();
  95. vgui::surface()->DrawSetColor( m_lineColor );
  96. vgui::surface()->DrawSetTextColor( m_lineColor );
  97. for ( int i=0; i<m_scaledLines.Count(); ++i )
  98. {
  99. int x1, x2, y1, y2;
  100. x1 = m_scaledLines[i][0];
  101. y1 = m_scaledLines[i][1];
  102. x2 = m_scaledLines[i][2];
  103. y2 = m_scaledLines[i][3];
  104. vgui::surface()->DrawFilledRect( x1, y1, x2, y2 );
  105. }
  106. }
  107. virtual void PerformLayout( void )
  108. {
  109. m_scaledLines.RemoveAll();
  110. for ( int i=0; i<m_lines.Count(); ++i )
  111. {
  112. int x1, x2, y1, y2;
  113. x1 = GetScaledValue( GetScheme(), m_lines[i][0] );
  114. y1 = GetScaledValue( GetScheme(), m_lines[i][1] );
  115. x2 = GetScaledValue( GetScheme(), m_lines[i][2] );
  116. y2 = GetScaledValue( GetScheme(), m_lines[i][3] );
  117. if ( x1 == x2 )
  118. {
  119. ++x2;
  120. }
  121. if ( y1 == y2 )
  122. {
  123. ++y2;
  124. }
  125. m_scaledLines.AddToTail( Vector4D( x1, y1, x2, y2 ) );
  126. }
  127. }
  128. private:
  129. Color m_lineColor;
  130. CUtlVector< Vector4D > m_lines;
  131. CUtlVector< Vector4D > m_scaledLines;
  132. };
  133. //--------------------------------------------------------------------------------------------------------------
  134. //--------------------------------------------------------------------------------------------------------------
  135. BuyPresetEditPanel::BuyPresetEditPanel( Panel *parent, const char *panelName, const char *resourceFilename, int fallbackIndex, bool editableName ) : BaseClass( parent, panelName )
  136. {
  137. SetProportional( parent->IsProportional() );
  138. if ( IsProportional() )
  139. {
  140. m_baseWide = m_baseTall = scheme()->GetProportionalScaledValueEx( GetScheme(), 100 );
  141. }
  142. else
  143. {
  144. m_baseWide = m_baseTall = 100;
  145. }
  146. SetSize( m_baseWide, m_baseTall );
  147. m_fallbackIndex = fallbackIndex;
  148. m_pBgPanel = new PresetBackgroundPanel( this, "mainBackground" );
  149. m_pTitleEntry = NULL;
  150. m_pTitleLabel = NULL;
  151. m_pCostLabel = NULL;
  152. /*
  153. m_pTitleEntry = new PresetNameTextEntry( this, dynamic_cast<CBuyPresetEditMainMenu *>(parent), "titleEntry" );
  154. m_pTitleLabel = new Label( this, "title", "" );
  155. m_pCostLabel = new Label( this, "cost", "" );
  156. */
  157. m_pPrimaryWeapon = new WeaponLabel( this, "primary" );
  158. m_pSecondaryWeapon = new WeaponLabel( this, "secondary" );
  159. m_pHEGrenade = new EquipmentLabel( this, "hegrenade" );
  160. m_pSmokeGrenade = new EquipmentLabel( this, "smokegrenade" );
  161. m_pFlashbangs = new EquipmentLabel( this, "flashbang" );
  162. m_pDefuser = new EquipmentLabel( this, "defuser" );
  163. m_pNightvision = new EquipmentLabel( this, "nightvision" );
  164. m_pArmor = new EquipmentLabel( this, "armor" );
  165. if ( resourceFilename )
  166. {
  167. LoadControlSettings( resourceFilename );
  168. }
  169. int x, y, w, h;
  170. m_pBgPanel->GetBounds( x, y, w, h );
  171. m_baseWide = x + w;
  172. m_baseTall = y + h;
  173. SetSize( m_baseWide, m_baseTall );
  174. }
  175. //--------------------------------------------------------------------------------------------------------------
  176. BuyPresetEditPanel::~BuyPresetEditPanel()
  177. {
  178. }
  179. //--------------------------------------------------------------------------------------------------------------
  180. void BuyPresetEditPanel::SetWeaponSet( const WeaponSet *pWeaponSet, bool current )
  181. {
  182. // set to empty state
  183. Reset();
  184. // now fill in items
  185. if ( pWeaponSet )
  186. {
  187. if ( m_pTitleLabel )
  188. {
  189. m_pTitleLabel->SetText( SharedVarArgs( "#Cstrike_BuyPresetChoice%d", m_fallbackIndex ) );
  190. }
  191. if ( m_pTitleEntry )
  192. {
  193. m_pTitleEntry->SetText( SharedVarArgs( "#Cstrike_BuyPresetChoice%d", m_fallbackIndex ) );
  194. }
  195. if ( m_pCostLabel )
  196. {
  197. const int BufLen = 256;
  198. wchar_t wbuf[BufLen];
  199. g_pVGuiLocalize->ConstructString( wbuf, sizeof( wbuf ),
  200. g_pVGuiLocalize->Find( "#Cstrike_BuyPresetPlainCost" ),
  201. 1, NumAsWString( pWeaponSet->FullCost() ) );
  202. m_pCostLabel->SetText( wbuf );
  203. }
  204. m_pPrimaryWeapon->SetWeapon( &pWeaponSet->m_primaryWeapon, true, true );
  205. m_pSecondaryWeapon->SetWeapon( &pWeaponSet->m_secondaryWeapon, false, true );
  206. if ( pWeaponSet->m_HEGrenade )
  207. m_pHEGrenade->SetItem( "gfx/vgui/hegrenade_square", 1 );
  208. if ( pWeaponSet->m_smokeGrenade )
  209. m_pSmokeGrenade->SetItem( "gfx/vgui/smokegrenade_square", 1 );
  210. if ( pWeaponSet->m_flashbangs )
  211. m_pFlashbangs->SetItem( "gfx/vgui/flashbang_square", pWeaponSet->m_flashbangs );
  212. if ( pWeaponSet->m_defuser )
  213. m_pDefuser->SetItem( "gfx/vgui/defuser", 1 );
  214. if ( pWeaponSet->m_nightvision )
  215. m_pNightvision->SetItem( "gfx/vgui/nightvision", 1 );
  216. if ( pWeaponSet->m_armor )
  217. {
  218. if ( pWeaponSet->m_helmet )
  219. m_pArmor->SetItem( "gfx/vgui/kevlar_helmet", 1 );
  220. else
  221. m_pArmor->SetItem( "gfx/vgui/kevlar", 1 );
  222. }
  223. }
  224. }
  225. //--------------------------------------------------------------------------------------------------------------
  226. void BuyPresetEditPanel::SetText( const wchar_t *text )
  227. {
  228. if ( !text )
  229. text = L"";
  230. if ( m_pTitleLabel )
  231. {
  232. m_pTitleLabel->SetText( text );
  233. }
  234. if ( m_pTitleEntry )
  235. {
  236. m_pTitleEntry->SetText( text );
  237. }
  238. InvalidateLayout();
  239. }
  240. //--------------------------------------------------------------------------------------------------------------
  241. /**
  242. * Handle command callbacks
  243. */
  244. void BuyPresetEditPanel::OnCommand( const char *command )
  245. {
  246. if (stricmp(command, "close"))
  247. {
  248. PostActionSignal( new KeyValues("Command", "command", SharedVarArgs( "%s %d", command, m_fallbackIndex )) );
  249. }
  250. BaseClass::OnCommand(command);
  251. }
  252. //--------------------------------------------------------------------------------------------------------------
  253. void BuyPresetEditPanel::ApplySchemeSettings(IScheme *pScheme)
  254. {
  255. BaseClass::ApplySchemeSettings(pScheme);
  256. SetBgColor( Color( 0, 0, 0, 0 ) );
  257. IBorder *pBorder = NULL;
  258. int i;
  259. for (i = 0; i < GetChildCount(); i++)
  260. {
  261. // perform auto-layout on the child panel
  262. Panel *child = GetChild(i);
  263. if (!child)
  264. continue;
  265. if ( !stricmp( "button", child->GetClassName() ) )
  266. {
  267. Button *pButton = dynamic_cast<Button *>(child);
  268. if ( pButton )
  269. {
  270. pButton->SetDefaultBorder( pBorder );
  271. pButton->SetDepressedBorder( pBorder );
  272. pButton->SetKeyFocusBorder( pBorder );
  273. }
  274. }
  275. }
  276. pBorder = pScheme->GetBorder("BuyPresetButtonBorder");
  277. const int NumButtons = 4;
  278. const char * buttonNames[4] = { "editPrimary", "editSecondary", "editGrenades", "editEquipment" };
  279. for ( i=0; i<NumButtons; ++i )
  280. {
  281. Panel *pPanel = FindChildByName( buttonNames[i] );
  282. if ( pPanel )
  283. {
  284. pPanel->SetBorder( pBorder );
  285. if ( !stricmp( "button", pPanel->GetClassName() ) )
  286. {
  287. Button *pButton = dynamic_cast<Button *>(pPanel);
  288. if ( pButton )
  289. {
  290. pButton->SetDefaultBorder( pBorder );
  291. pButton->SetDepressedBorder( pBorder );
  292. pButton->SetKeyFocusBorder( pBorder );
  293. Color fgColor, bgColor;
  294. fgColor = GetSchemeColor("Label.TextDullColor", GetFgColor(), pScheme);
  295. bgColor = Color( 0, 0, 0, 0 );
  296. pButton->SetDefaultColor( fgColor, bgColor );
  297. }
  298. }
  299. }
  300. }
  301. }
  302. //--------------------------------------------------------------------------------------------------------------
  303. /**
  304. * Overrides EditablePanel's resizing of children to scale them proportionally to the main panel's change.
  305. */
  306. void BuyPresetEditPanel::OnSizeChanged( int wide, int tall )
  307. {
  308. if ( !m_baseWide )
  309. m_baseWide = 1;
  310. if ( !m_baseTall )
  311. m_baseTall = 1;
  312. Panel::OnSizeChanged(wide, tall);
  313. InvalidateLayout();
  314. if ( wide == m_baseWide && tall == m_baseTall )
  315. {
  316. Repaint();
  317. return;
  318. }
  319. float xScale = wide / (float) m_baseWide;
  320. float yScale = tall / (float) m_baseTall;
  321. for (int i = 0; i < GetChildCount(); i++)
  322. {
  323. // perform auto-layout on the child panel
  324. Panel *child = GetChild(i);
  325. if (!child)
  326. continue;
  327. int x, y, w, t;
  328. child->GetBounds(x, y, w, t);
  329. int newX = (int) x * xScale;
  330. int newY = (int) y * yScale;
  331. int newW = (int) (x+w) * xScale - newX;
  332. int newT = (int) t * yScale;
  333. // make sure the child isn't too big...
  334. if(newX+newW>wide)
  335. {
  336. continue;
  337. }
  338. if(newY+newT>tall)
  339. {
  340. continue;
  341. }
  342. child->SetBounds(newX, newY, newW, newT);
  343. child->InvalidateLayout();
  344. }
  345. Repaint();
  346. // update the baselines
  347. m_baseWide = wide;
  348. m_baseTall = tall;
  349. }
  350. //--------------------------------------------------------------------------------------------------------------
  351. void BuyPresetEditPanel::Reset()
  352. {
  353. if ( m_pTitleLabel )
  354. {
  355. m_pTitleLabel->SetText( "#Cstrike_BuyPresetNewChoice" );
  356. }
  357. if ( m_pTitleEntry )
  358. {
  359. m_pTitleEntry->SetText( "#Cstrike_BuyPresetNewChoice" );
  360. }
  361. if ( m_pCostLabel )
  362. {
  363. m_pCostLabel->SetText( "" );
  364. }
  365. BuyPresetWeapon weapon;
  366. m_pPrimaryWeapon->SetWeapon( &weapon, true, false );
  367. m_pSecondaryWeapon->SetWeapon( &weapon, false, false );
  368. m_pHEGrenade->SetItem( NULL, 1 );
  369. m_pSmokeGrenade->SetItem( NULL, 1 );
  370. m_pFlashbangs->SetItem( NULL, 1 );
  371. m_pDefuser->SetItem( NULL, 1 );
  372. m_pNightvision->SetItem( NULL, 1 );
  373. m_pArmor->SetItem( NULL, 1 );
  374. }
  375. //--------------------------------------------------------------------------------------------------------------