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.

208 lines
6.4 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "vgui_controls/EditablePanel.h"
  8. #include "vgui_controls/ComboBox.h"
  9. #include "econ_item_system.h"
  10. #include "econ_item_constants.h"
  11. #include "item_style_select_dialog.h"
  12. #include "econ_gcmessages.h"
  13. #include "backpack_panel.h"
  14. #include "econ_ui.h"
  15. #include "gc_clientsystem.h"
  16. // memdbgon must be the last include file in a .cpp file!!!
  17. #include <tier0/memdbgon.h>
  18. //-----------------------------------------------------------------------------
  19. // Purpose:
  20. //-----------------------------------------------------------------------------
  21. CComboBoxBackpackOverlayDialogBase::CComboBoxBackpackOverlayDialogBase( vgui::Panel *parent, CEconItemView *pItem )
  22. : vgui::EditablePanel( parent, "ComboBoxBackpackOverlayDialogBase" )
  23. , m_pPreviewModelPanel( NULL )
  24. , m_pItem( pItem )
  25. {
  26. if ( m_pItem )
  27. {
  28. m_pPreviewModelPanel = new CItemModelPanel( this, "preview_model" );
  29. m_pPreviewModelPanel->SetItem( m_pItem );
  30. }
  31. m_pComboBox = new vgui::ComboBox( this, "ComboBox", 5, false );
  32. }
  33. //-----------------------------------------------------------------------------
  34. // Purpose:
  35. //-----------------------------------------------------------------------------
  36. void CComboBoxBackpackOverlayDialogBase::ApplySchemeSettings( vgui::IScheme *pScheme )
  37. {
  38. LoadControlSettings( "Resource/UI/econ/ComboBoxBackpackOverlayDialog.res" );
  39. BaseClass::ApplySchemeSettings( pScheme );
  40. if ( m_pPreviewModelPanel )
  41. {
  42. if ( m_pPreviewModelPanel && m_pItem )
  43. {
  44. m_pPreviewModelPanel->SetItem( m_pItem );
  45. }
  46. m_pPreviewModelPanel->SetActAsButton( true, false );
  47. }
  48. if ( m_pComboBox )
  49. {
  50. m_pComboBox->RemoveAll();
  51. vgui::HFont hFont = pScheme->GetFont( "HudFontSmallestBold", true );
  52. m_pComboBox->SetFont( hFont );
  53. PopulateComboBoxOptions();
  54. m_pComboBox->AddActionSignalTarget( this );
  55. }
  56. CExLabel *pTitleLabel = dynamic_cast<CExLabel *>( FindChildByName( "TitleLabel" ) );
  57. if ( pTitleLabel )
  58. {
  59. pTitleLabel->SetText( GetTitleLabelLocalizationToken() );
  60. }
  61. }
  62. //-----------------------------------------------------------------------------
  63. // Purpose:
  64. //-----------------------------------------------------------------------------
  65. void CComboBoxBackpackOverlayDialogBase::OnCommand( const char *command )
  66. {
  67. if ( !Q_strnicmp( command, "cancel", 6 ) )
  68. {
  69. TFModalStack()->PopModal( this );
  70. SetVisible( false );
  71. MarkForDeletion();
  72. }
  73. else if ( !Q_strnicmp( command, "apply", 5 ) )
  74. {
  75. OnComboBoxApplication();
  76. TFModalStack()->PopModal( this );
  77. SetVisible( false );
  78. MarkForDeletion();
  79. }
  80. }
  81. //-----------------------------------------------------------------------------
  82. // Purpose:
  83. //-----------------------------------------------------------------------------
  84. void CComboBoxBackpackOverlayDialogBase::Show()
  85. {
  86. SetVisible( true );
  87. MakePopup();
  88. MoveToFront();
  89. SetKeyBoardInputEnabled( true );
  90. SetMouseInputEnabled( true );
  91. TFModalStack()->PushModal( this );
  92. // Special-case the path where we only wind up with one option in the combo box. If
  93. // this happens we just pretend the user selected it and move on to the next step. For
  94. // restoration, this will bring up a confirmation dialog, and for setting styles... well,
  95. // we'd expect it to never happen because the option to bring up the UI wouldn't be enabled
  96. // if there was only a single style.
  97. if ( m_pComboBox && m_pComboBox->GetItemCount() == 1 )
  98. {
  99. OnCommand( "apply" );
  100. }
  101. }
  102. //-----------------------------------------------------------------------------
  103. // Purpose: Called when text changes in combo box
  104. //-----------------------------------------------------------------------------
  105. void CComboBoxBackpackOverlayDialogBase::OnTextChanged( KeyValues *data )
  106. {
  107. if ( !m_pComboBox )
  108. return;
  109. Panel *pPanel = reinterpret_cast<vgui::Panel *>( data->GetPtr("panel") );
  110. vgui::ComboBox *pComboBox = dynamic_cast<vgui::ComboBox *>( pPanel );
  111. if ( pComboBox == m_pComboBox )
  112. {
  113. OnComboBoxChanged( m_pComboBox->GetActiveItem() );
  114. }
  115. }
  116. //-----------------------------------------------------------------------------
  117. // Purpose:
  118. //-----------------------------------------------------------------------------
  119. void CStyleSelectDialog::PopulateComboBoxOptions()
  120. {
  121. CEconItemView* pItem = GetPreviewModelPanel()->GetItem();
  122. Assert( pItem );
  123. if ( pItem->GetStaticData()->GetNumStyles() )
  124. {
  125. KeyValues *pKeyValues = new KeyValues( "data" );
  126. for ( style_index_t i=0; i<pItem->GetStaticData()->GetNumStyles(); i++ )
  127. {
  128. const CEconStyleInfo *pStyle = pItem->GetStaticData()->GetStyleInfo( i );
  129. if ( pStyle && pStyle->IsSelectable() )
  130. {
  131. pKeyValues->SetInt( "style_index", i );
  132. GetComboBox()->AddItem( pItem->GetStaticData()->GetStyleInfo( i )->GetName(), pKeyValues );
  133. }
  134. }
  135. pKeyValues->deleteThis();
  136. GetComboBox()->ActivateItemByRow( pItem->GetItemStyle() );
  137. }
  138. else
  139. {
  140. GetComboBox()->ActivateItemByRow( 0 );
  141. }
  142. }
  143. //-----------------------------------------------------------------------------
  144. // Purpose:
  145. //-----------------------------------------------------------------------------
  146. void CStyleSelectDialog::OnComboBoxApplication()
  147. {
  148. KeyValues *pKV = GetComboBox()->GetActiveItemUserData();
  149. int iNewStyle = pKV->GetInt( "style_index", 0 );
  150. CEconItemView* pItem = GetPreviewModelPanel()->GetItem();
  151. if ( pItem )
  152. {
  153. const char* pszStyleName = pItem->GetStaticData()->GetStyleInfo( iNewStyle )->GetName();
  154. // Tell the GC to update the style.
  155. GCSDK::CGCMsg< MsgGCSetItemStyle_t > msg( k_EMsgGCSetItemStyle );
  156. msg.Body().m_unItemID = pItem->GetItemID();
  157. msg.Body().m_iStyle = iNewStyle;
  158. GCClientSystem()->BSendMessage( msg );
  159. EconUI()->Gamestats_ItemTransaction( IE_ITEM_CHANGED_STYLE, pItem, pszStyleName /* stored unlocalized here intentionally */, iNewStyle );
  160. // Tell our parent about the change
  161. if ( pItem && pItem->IsValid() )
  162. {
  163. KeyValues *pKey = new KeyValues( "SelectionReturned" );
  164. pKey->SetUint64( "itemindex", pItem->GetItemID() );
  165. PostMessage( GetParent(), pKey );
  166. }
  167. }
  168. }
  169. //-----------------------------------------------------------------------------
  170. // Purpose:
  171. //-----------------------------------------------------------------------------
  172. void CStyleSelectDialog::OnComboBoxChanged( int iNewSelection )
  173. {
  174. GetPreviewModelPanel()->SetItemStyle( GetComboBox()->GetActiveItem() );
  175. GetPreviewModelPanel()->UpdatePanels();
  176. }