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.

301 lines
8.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "item_slot_panel.h"
  9. #include "tf_item_inventory.h"
  10. #include "item_selection_panel.h"
  11. #include "tf_gcmessages.h"
  12. #include "gc_clientsystem.h"
  13. #define NUM_MAX_SLOTS 1
  14. //-----------------------------------------------------------------------------
  15. // Purpose:
  16. //-----------------------------------------------------------------------------
  17. CItemSlotPanel::CItemSlotPanel( vgui::Panel *parent )
  18. : CBaseLoadoutPanel( parent, "item_slot_panel" )
  19. {
  20. m_pItem = NULL;
  21. m_pSelectionPanel = NULL;
  22. m_iCurrentSlotIndex = 0;
  23. }
  24. //-----------------------------------------------------------------------------
  25. // Purpose:
  26. //-----------------------------------------------------------------------------
  27. CItemSlotPanel::~CItemSlotPanel()
  28. {
  29. }
  30. //-----------------------------------------------------------------------------
  31. // Purpose:
  32. //-----------------------------------------------------------------------------
  33. void CItemSlotPanel::ApplySchemeSettings( vgui::IScheme *pScheme )
  34. {
  35. LoadControlSettings( "Resource/UI/ItemSlotPanel.res" );
  36. BaseClass::ApplySchemeSettings( pScheme );
  37. }
  38. //-----------------------------------------------------------------------------
  39. // Purpose:
  40. //-----------------------------------------------------------------------------
  41. void CItemSlotPanel::PerformLayout( void )
  42. {
  43. BaseClass::PerformLayout();
  44. for ( int i = 0; i < m_pItemModelPanels.Count(); i++ )
  45. {
  46. if ( !m_itemSlots[i].m_bHasSlot )
  47. {
  48. m_pItemModelPanels[i]->SetVisible( false );
  49. continue;
  50. }
  51. int iCenter = GetWide() * 0.5;
  52. int iButtonX = (i % GetNumColumns());
  53. int iButtonY = (i / GetNumColumns());
  54. int iXPos = (iCenter + m_iItemBackpackOffcenterX) + (iButtonX * m_pItemModelPanels[i]->GetWide()) + (m_iItemBackpackXDelta * iButtonX);
  55. int iYPos = m_iItemYPos + (iButtonY * m_pItemModelPanels[i]->GetTall() ) + (m_iItemBackpackYDelta * iButtonY);
  56. m_pItemModelPanels[i]->SetPos( iXPos, iYPos );
  57. m_pItemModelPanels[i]->SetVisible( true );
  58. }
  59. }
  60. //-----------------------------------------------------------------------------
  61. // Purpose:
  62. //-----------------------------------------------------------------------------
  63. void CItemSlotPanel::OnItemPanelMouseReleased( vgui::Panel *panel )
  64. {
  65. CItemModelPanel *pItemPanel = dynamic_cast < CItemModelPanel * > ( panel );
  66. if ( pItemPanel && IsVisible() )
  67. {
  68. for ( int i = 0; i < m_pItemModelPanels.Count(); i++ )
  69. {
  70. if ( m_pItemModelPanels[i] == pItemPanel )
  71. {
  72. OnCommand( VarArgs("change%d", i) );
  73. return;
  74. }
  75. }
  76. }
  77. }
  78. //-----------------------------------------------------------------------------
  79. // Purpose:
  80. //-----------------------------------------------------------------------------
  81. void CItemSlotPanel::OnSelectionReturned( KeyValues *data )
  82. {
  83. if ( data )
  84. {
  85. uint64 ulIndex = data->GetUint64( "itemindex", INVALID_ITEM_ID );
  86. if ( ulIndex != INVALID_ITEM_ID )
  87. {
  88. CEconItemView *pItemData = TFInventoryManager()->GetLocalTFInventory()->GetInventoryItemByItemID( ulIndex );
  89. if ( pItemData )
  90. {
  91. m_pItemModelPanels[ m_iCurrentSlotIndex ]->SetItem( pItemData );
  92. itemid_t ulOriginalID = pItemData->GetSOCData()->GetOriginalID();
  93. m_itemSlots[ m_iCurrentSlotIndex ].m_ulOriginalID = ulOriginalID;
  94. // tell GC to update the slot attribute
  95. GCSDK::CProtoBufMsg<CMsgSetItemSlotAttribute> msg( k_EMsgGC_ClientSetItemSlotAttribute );
  96. msg.Body().set_item_id( m_pItem->GetItemID() );
  97. msg.Body().set_slot_item_original_id( ulOriginalID );
  98. msg.Body().set_slot_index( m_iCurrentSlotIndex + 1 );
  99. //EconUI()->Gamestats_ItemTransaction( IE_ITEM_USED_TOOL, m_pToolModelPanel->GetItem(), "applied_upgrade_card", m_pToolModelPanel->GetItem()->GetItemDefIndex() );
  100. GCClientSystem()->BSendMessage( msg );
  101. }
  102. }
  103. }
  104. PostMessage( GetParent(), new KeyValues("SelectionEnded") );
  105. // It'll have deleted itself, so we don't need to clean it up
  106. m_pSelectionPanel = NULL;
  107. OnCancelSelection();
  108. // find the selected item and give it the focus
  109. CItemModelPanel *pSelection = GetFirstSelectedItemModelPanel( true );
  110. if( !pSelection )
  111. {
  112. m_pItemModelPanels[0]->SetSelected( true );
  113. pSelection = m_pItemModelPanels[0];
  114. }
  115. pSelection->RequestFocus();
  116. }
  117. //-----------------------------------------------------------------------------
  118. // Purpose:
  119. //-----------------------------------------------------------------------------
  120. void CItemSlotPanel::OnCancelSelection( void )
  121. {
  122. if ( m_pSelectionPanel )
  123. {
  124. m_pSelectionPanel->SetVisible( false );
  125. m_pSelectionPanel->MarkForDeletion();
  126. m_pSelectionPanel = NULL;
  127. }
  128. }
  129. //-----------------------------------------------------------------------------
  130. // Purpose:
  131. //-----------------------------------------------------------------------------
  132. void CItemSlotPanel::OnCommand( const char *command )
  133. {
  134. if ( !V_stricmp( command, "ok" ) )
  135. {
  136. SetVisible( false );
  137. return;
  138. }
  139. else if ( V_stristr( command, "change" ) )
  140. {
  141. const char *pszNum = command+6;
  142. if ( pszNum && pszNum[0] )
  143. {
  144. int iSlot = atoi(pszNum);
  145. if ( iSlot >= 0 && iSlot < m_itemSlots.Count() )
  146. {
  147. if ( m_iCurrentSlotIndex != iSlot )
  148. {
  149. m_iCurrentSlotIndex = iSlot;
  150. }
  151. m_selectionCriteria = CItemSelectionCriteria();
  152. m_selectionCriteria.SetTags( m_itemSlots[m_iCurrentSlotIndex].m_slotCriteriaAttribute.tags().c_str() );
  153. m_selectionCriteria.SetIgnoreEnabledFlag( true );
  154. // Create the selection screen. It removes itself on close.
  155. m_pSelectionPanel = new CItemCriteriaSelectionPanel( this, &m_selectionCriteria );
  156. m_pSelectionPanel->InvalidateLayout( false, true ); // need to ApplySchemeSettings now so it doesn't override our SetDialogVariable below later
  157. m_pSelectionPanel->ShowPanel( 0, true );
  158. m_pSelectionPanel->SetDialogVariable( "loadoutclass", g_pVGuiLocalize->Find( "#EditSlots_SelectItemPanel" ) );
  159. PostMessage( GetParent(), new KeyValues("SelectionStarted") );
  160. }
  161. }
  162. return;
  163. }
  164. BaseClass::OnCommand( command );
  165. }
  166. //-----------------------------------------------------------------------------
  167. // Purpose:
  168. //-----------------------------------------------------------------------------
  169. void CItemSlotPanel::UpdateModelPanels( void )
  170. {
  171. // For now, fill them out with the local player's currently wielded items
  172. for ( int i = 0; i < m_pItemModelPanels.Count(); i++ )
  173. {
  174. CEconItemView *pItemData = TFInventoryManager()->GetLocalTFInventory()->GetInventoryItemByOriginalID( m_itemSlots[i].m_ulOriginalID );
  175. m_pItemModelPanels[i]->SetItem( pItemData );
  176. m_pItemModelPanels[i]->SetShowQuantity( true );
  177. m_pItemModelPanels[i]->SetSelected( false );
  178. SetBorderForItem( m_pItemModelPanels[i], false );
  179. }
  180. // Now layout again to position our item buttons
  181. InvalidateLayout();
  182. }
  183. //-----------------------------------------------------------------------------
  184. // Purpose:
  185. //-----------------------------------------------------------------------------
  186. int CItemSlotPanel::GetNumItemPanels( void )
  187. {
  188. return NUM_MAX_SLOTS;
  189. }
  190. //-----------------------------------------------------------------------------
  191. // Purpose:
  192. //-----------------------------------------------------------------------------
  193. void CItemSlotPanel::OnShowPanel( bool bVisible, bool bReturningFromArmory )
  194. {
  195. if ( bVisible )
  196. {
  197. if ( m_pSelectionPanel )
  198. {
  199. m_pSelectionPanel->SetVisible( false );
  200. m_pSelectionPanel->MarkForDeletion();
  201. m_pSelectionPanel = NULL;
  202. }
  203. }
  204. }
  205. //-----------------------------------------------------------------------------
  206. // Purpose:
  207. //-----------------------------------------------------------------------------
  208. void CItemSlotPanel::AddNewItemPanel( int iPanelIndex )
  209. {
  210. BaseClass::AddNewItemPanel( iPanelIndex );
  211. m_itemSlots.AddToTail();
  212. }
  213. //-----------------------------------------------------------------------------
  214. // Purpose:
  215. //-----------------------------------------------------------------------------
  216. void CItemSlotPanel::SetItem( CEconItem* pItem )
  217. {
  218. if ( !pItem )
  219. {
  220. SetVisible( false );
  221. return;
  222. }
  223. OnCancelSelection();
  224. m_pItem = pItem;
  225. static CSchemaAttributeDefHandle s_itemSlotCriteriaAttributes[] =
  226. {
  227. CSchemaAttributeDefHandle( "item slot criteria 1" ),
  228. };
  229. COMPILE_TIME_ASSERT( ARRAYSIZE( s_itemSlotCriteriaAttributes ) == NUM_MAX_SLOTS );
  230. static CSchemaAttributeDefHandle s_itemInSlotAttributes[] =
  231. {
  232. CSchemaAttributeDefHandle( "item in slot 1" ),
  233. };
  234. COMPILE_TIME_ASSERT( ARRAYSIZE( s_itemInSlotAttributes ) == NUM_MAX_SLOTS );
  235. for ( int i=0; i<ARRAYSIZE( s_itemSlotCriteriaAttributes ); ++i )
  236. {
  237. m_itemSlots[i].m_bHasSlot = false;
  238. if ( m_pItem->FindAttribute( s_itemSlotCriteriaAttributes[i], &m_itemSlots[i].m_slotCriteriaAttribute ) )
  239. {
  240. m_itemSlots[i].m_bHasSlot = true;
  241. m_itemSlots[i].m_ulOriginalID = INVALID_ITEM_ID;
  242. m_pItem->FindAttribute( s_itemInSlotAttributes[i], &m_itemSlots[i].m_ulOriginalID );
  243. }
  244. }
  245. UpdateModelPanels();
  246. }