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.

250 lines
9.5 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. //=======================================================================================//
  4. #include "cbase.h"
  5. #include "loadout_preset_panel.h"
  6. #include "tf_item_inventory.h"
  7. #include "econ/econ_item_preset.h"
  8. #include "econ/econ_item_system.h"
  9. // memdbgon must be the last include file in a .cpp file!!!
  10. #include <tier0/memdbgon.h>
  11. //-----------------------------------------------------------------------------
  12. using namespace vgui;
  13. //-----------------------------------------------------------------------------
  14. DECLARE_BUILD_FACTORY( CLoadoutPresetPanel );
  15. //-----------------------------------------------------------------------------
  16. // Purpose:
  17. //-----------------------------------------------------------------------------
  18. CLoadoutPresetPanel::CLoadoutPresetPanel( vgui::Panel *pParent, const char *pName )
  19. : EditablePanel( pParent, "loadout_preset_panel" )
  20. {
  21. V_memset( m_pPresetButtons, 0, sizeof( m_pPresetButtons ) );
  22. m_iClass = TF_CLASS_UNDEFINED;
  23. m_pPresetButtonKv = NULL;
  24. m_bDisplayVertical = false;
  25. // Create all buttons
  26. for ( int i = 0; i < MAX_PRESETS; ++i )
  27. {
  28. CFmtStr fmtTokenName( "TF_ItemPresetName%i", i );
  29. CFmtStr fmtButtonName( "LoadPresetButton%i", i );
  30. wchar_t *pwszPresetName = g_pVGuiLocalize->Find( fmtTokenName.Access() );
  31. m_pPresetButtons[i] = new CExButton( this, fmtButtonName.Access(), pwszPresetName, this );
  32. }
  33. }
  34. //-----------------------------------------------------------------------------
  35. // Purpose:
  36. //-----------------------------------------------------------------------------
  37. void CLoadoutPresetPanel::ApplySchemeSettings( vgui::IScheme *pScheme )
  38. {
  39. BaseClass::ApplySchemeSettings( pScheme );
  40. LoadControlSettings( "Resource/UI/LoadoutPresetPanel.res" );
  41. m_aDefaultColors[LOADED][FG][DEFAULT] = vgui::scheme()->GetIScheme( GetScheme() )->GetColor( "Econ.Button.PresetDefaultColorFg", Color( 255, 255, 255, 255 ) );
  42. m_aDefaultColors[LOADED][FG][ARMED] = vgui::scheme()->GetIScheme( GetScheme() )->GetColor( "Econ.Button.PresetArmedColorFg", Color( 255, 255, 255, 255 ) );
  43. m_aDefaultColors[LOADED][FG][DEPRESSED] = vgui::scheme()->GetIScheme( GetScheme() )->GetColor( "Econ.Button.PresetDepressedColorFg", Color( 255, 255, 255, 255 ) );
  44. m_aDefaultColors[LOADED][BG][DEFAULT] = vgui::scheme()->GetIScheme( GetScheme() )->GetColor( "Econ.Button.PresetDefaultColorBg", Color( 255, 255, 255, 255 ) );
  45. m_aDefaultColors[LOADED][BG][ARMED] = vgui::scheme()->GetIScheme( GetScheme() )->GetColor( "Econ.Button.PresetArmedColorBg", Color( 255, 255, 255, 255 ) );
  46. m_aDefaultColors[LOADED][BG][DEPRESSED] = vgui::scheme()->GetIScheme( GetScheme() )->GetColor( "Econ.Button.PresetDepressedColorBg", Color( 255, 255, 255, 255 ) );
  47. m_aDefaultColors[NOTLOADED][FG][DEFAULT] = vgui::scheme()->GetIScheme( GetScheme() )->GetColor( "Button.TextColor", Color( 255, 255, 255, 255 ) );
  48. m_aDefaultColors[NOTLOADED][FG][ARMED] = vgui::scheme()->GetIScheme( GetScheme() )->GetColor( "Button.ArmedTextColor", Color( 255, 255, 255, 255 ) );
  49. m_aDefaultColors[NOTLOADED][FG][DEPRESSED] = vgui::scheme()->GetIScheme( GetScheme() )->GetColor( "Button.DepressedTextColor", Color( 255, 255, 255, 255 ) );
  50. m_aDefaultColors[NOTLOADED][BG][DEFAULT] = vgui::scheme()->GetIScheme( GetScheme() )->GetColor( "Button.BgColor", Color( 255, 255, 255, 255 ) );
  51. m_aDefaultColors[NOTLOADED][BG][ARMED] = vgui::scheme()->GetIScheme( GetScheme() )->GetColor( "Button.ArmedBgColor", Color( 255, 255, 255, 255 ) );
  52. m_aDefaultColors[NOTLOADED][BG][DEPRESSED] = vgui::scheme()->GetIScheme( GetScheme() )->GetColor( "Button.DepressedBgColor", Color( 255, 255, 255, 255 ) );
  53. }
  54. //-----------------------------------------------------------------------------
  55. // Purpose:
  56. //-----------------------------------------------------------------------------
  57. void CLoadoutPresetPanel::ApplySettings( KeyValues *pInResourceData )
  58. {
  59. BaseClass::ApplySettings( pInResourceData );
  60. KeyValues *pPresetButtonKv = pInResourceData->FindKey( "presetbutton_kv" );
  61. if ( pPresetButtonKv && !m_pPresetButtonKv )
  62. {
  63. m_pPresetButtonKv = new KeyValues( "presetbutton_kv" );
  64. pPresetButtonKv->CopySubkeys( m_pPresetButtonKv );
  65. }
  66. }
  67. //-----------------------------------------------------------------------------
  68. // Purpose:
  69. //-----------------------------------------------------------------------------
  70. void CLoadoutPresetPanel::PerformLayout()
  71. {
  72. BaseClass::PerformLayout();
  73. if ( !m_pPresetButtons[0] )
  74. return;
  75. const int nBuffer = XRES( 2 );
  76. for ( int i = 0; i < MAX_PRESETS; ++i )
  77. {
  78. if ( m_pPresetButtonKv )
  79. {
  80. m_pPresetButtons[i]->ApplySettings( m_pPresetButtonKv );
  81. }
  82. // Display buttons vertically or horizontally?
  83. // NOTE: Button width and height will be valid here, since we've just applied settings
  84. if ( m_bDisplayVertical )
  85. {
  86. const int nButtonHeight = m_pPresetButtons[0]->GetTall();
  87. m_pPresetButtons[i]->SetPos( 0, i * ( nButtonHeight + nBuffer ) );
  88. }
  89. else
  90. {
  91. const int nButtonWidth = m_pPresetButtons[0]->GetWide();
  92. const int nStartX = 0.5f * ( GetWide() - MAX_PRESETS * ( nButtonWidth + nBuffer ) );
  93. m_pPresetButtons[i]->SetPos( nStartX + i * ( nButtonWidth + nBuffer ), 0 );
  94. }
  95. m_pPresetButtons[i]->SetVisible( true );
  96. }
  97. vgui::ivgui()->AddTickSignal( GetVPanel(), 150 );
  98. UpdatePresetButtonStates();
  99. }
  100. //-----------------------------------------------------------------------------
  101. // Purpose:
  102. //-----------------------------------------------------------------------------
  103. void CLoadoutPresetPanel::SetClass( int iClass )
  104. {
  105. m_iClass = iClass;
  106. if ( iClass != TF_CLASS_UNDEFINED )
  107. {
  108. UpdatePresetButtonStates();
  109. }
  110. }
  111. //-----------------------------------------------------------------------------
  112. // Purpose:
  113. //-----------------------------------------------------------------------------
  114. void CLoadoutPresetPanel::EnableVerticalDisplay( bool bVertical )
  115. {
  116. m_bDisplayVertical = bVertical;
  117. }
  118. //-----------------------------------------------------------------------------
  119. // Purpose:
  120. //-----------------------------------------------------------------------------
  121. void CLoadoutPresetPanel::LoadPreset( int iPresetIndex )
  122. {
  123. TFInventoryManager()->LoadPreset( m_iClass, iPresetIndex );
  124. }
  125. //-----------------------------------------------------------------------------
  126. // Purpose:
  127. //-----------------------------------------------------------------------------
  128. void CLoadoutPresetPanel::OnCommand( const char *command )
  129. {
  130. if ( !V_strnicmp( command, "loadpreset_", 11 ) )
  131. {
  132. const int iPresetIndex = atoi( command + 11 );
  133. LoadPreset( iPresetIndex );
  134. }
  135. else
  136. {
  137. BaseClass::OnCommand( command );
  138. }
  139. }
  140. //-----------------------------------------------------------------------------
  141. // Purpose:
  142. //-----------------------------------------------------------------------------
  143. void CLoadoutPresetPanel::OnTick()
  144. {
  145. UpdatePresetButtonStates();
  146. }
  147. //-----------------------------------------------------------------------------
  148. // Purpose: Processes some keypresses for the loadout panel
  149. //-----------------------------------------------------------------------------
  150. bool CLoadoutPresetPanel::HandlePresetKeyPressed( vgui::KeyCode code )
  151. {
  152. ButtonCode_t nButtonCode = GetBaseButtonCode( code );
  153. if( nButtonCode == KEY_XBUTTON_LEFT_SHOULDER )
  154. {
  155. if( GetSelectedPresetID() > 0 )
  156. LoadPreset( GetSelectedPresetID() - 1 );
  157. return true;
  158. }
  159. else if( nButtonCode == KEY_XBUTTON_RIGHT_SHOULDER )
  160. {
  161. if( GetSelectedPresetID() < MAX_PRESETS - 1 )
  162. LoadPreset( GetSelectedPresetID() + 1 );
  163. return true;
  164. }
  165. else
  166. {
  167. return false;
  168. }
  169. }
  170. //-----------------------------------------------------------------------------
  171. // Purpose:
  172. //-----------------------------------------------------------------------------
  173. equipped_preset_t CLoadoutPresetPanel::GetSelectedPresetID() const
  174. {
  175. if ( !InventoryManager()->GetLocalInventory() )
  176. return INVALID_PRESET_INDEX;
  177. const uint32 unAccountID = InventoryManager()->GetLocalInventory()->GetOwner().GetAccountID();
  178. const CEconItemPerClassPresetData soSearch( unAccountID, m_iClass );
  179. GCSDK::CSharedObjectCache *pSOCache = InventoryManager()->GetLocalInventory()->GetSOC();
  180. if ( !pSOCache )
  181. return INVALID_PRESET_INDEX;
  182. const CEconItemPerClassPresetData *pExistingPerClassData = assert_cast<CEconItemPerClassPresetData *>( pSOCache->FindSharedObject( soSearch ) );
  183. if ( !pExistingPerClassData )
  184. return INVALID_PRESET_INDEX;
  185. return pExistingPerClassData->GetActivePreset();
  186. }
  187. //-----------------------------------------------------------------------------
  188. // Purpose:
  189. //-----------------------------------------------------------------------------
  190. void CLoadoutPresetPanel::UpdatePresetButtonStates()
  191. {
  192. const equipped_preset_t unEquippedPresetID = GetSelectedPresetID();
  193. for ( int i = 0; i < MAX_PRESETS; ++i )
  194. {
  195. if ( i == unEquippedPresetID )
  196. {
  197. m_pPresetButtons[i]->SetDefaultColor( m_aDefaultColors[LOADED][FG][DEFAULT], m_aDefaultColors[LOADED][BG][DEFAULT] );
  198. m_pPresetButtons[i]->SetArmedColor( m_aDefaultColors[LOADED][FG][ARMED], m_aDefaultColors[LOADED][BG][ARMED] );
  199. m_pPresetButtons[i]->SetDepressedColor( m_aDefaultColors[LOADED][FG][DEPRESSED], m_aDefaultColors[LOADED][BG][DEPRESSED] );
  200. }
  201. else
  202. {
  203. m_pPresetButtons[i]->SetDefaultColor( m_aDefaultColors[NOTLOADED][FG][DEFAULT], m_aDefaultColors[NOTLOADED][BG][DEFAULT] );
  204. m_pPresetButtons[i]->SetArmedColor( m_aDefaultColors[NOTLOADED][FG][ARMED], m_aDefaultColors[NOTLOADED][BG][ARMED] );
  205. m_pPresetButtons[i]->SetDepressedColor( m_aDefaultColors[NOTLOADED][FG][DEPRESSED], m_aDefaultColors[NOTLOADED][BG][DEPRESSED] );
  206. }
  207. CFmtStr fmtCmd( "loadpreset_%i", i );
  208. m_pPresetButtons[i]->SetCommand( fmtCmd.Access() );
  209. }
  210. }