summaryrefslogtreecommitdiff
path: root/game/client/tf/vgui/crate_detail_panels.cpp
blob: dcde85d5c4c21d0c4b382124f25e3dbe1b8927c6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=============================================================================//


#include "cbase.h"
#include "crate_detail_panels.h"
#include "vgui_controls/TextImage.h"
#include "econ_gcmessages.h"
#include "gc_clientsystem.h"
#include "econ_ui.h"
#include <vgui/ISurface.h>
#include "econ_item_inventory.h"
#include "econ/tool_items/tool_items.h"

#define SHUFFLE_TIME 5.f

float CInputStringForItemBackpackOverlayDialog::m_sflNextShuffleTime = 0.f;
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CInputStringForItemBackpackOverlayDialog::CInputStringForItemBackpackOverlayDialog( vgui::Panel *pParent, CEconItemView *pItem, CEconItemView *pChosenKey )
	: vgui::EditablePanel( pParent, "InputStringForItemBackpackOverlayDialog" )
	, m_Item( *pItem )
	, m_pPreviewModelPanel( NULL )
	, m_pTextEntry( NULL )
	, m_pItemModelPanelKVs( NULL )
	, m_bUpdateRecieved( false )
{
	if ( pChosenKey )
	{
		m_UseableKey = *pChosenKey;
	}
	m_pPreviewModelPanel = new CItemModelPanel( this, "preview_model" );
	m_pTextEntry = new vgui::TextEntry( this, "TextEntryControl" );
	m_pShuffleButton = new CExButton( this, "ShuffleButton", "Shuffle" );
	m_pRareLootLabel = new CExLabel( this, "RareLootLabel", "#Econ_Revolving_Loot_List_Rare_Item" );
	m_pProgressBar = new vgui::ProgressBar( this, "ShuffleProgress" );
	m_pGetKeyButton = new CExButton( this, "GetKeyButton", "getkey" );
	m_pUseKeyButton = new CExButton( this, "UseKeyButton", "usekey" );

	m_pMouseOverItemPanel = vgui::SETUP_PANEL( new CItemModelPanel( this, "mouseoveritempanel" ) );
	m_pMouseOverTooltip = new CItemModelPanelToolTip( this );
	m_pMouseOverTooltip->SetupPanels( this, m_pMouseOverItemPanel );

	ListenForGameEvent( "inventory_updated" );
}

CInputStringForItemBackpackOverlayDialog::~CInputStringForItemBackpackOverlayDialog()
{
	if ( m_pItemModelPanelKVs )
	{
		m_pItemModelPanelKVs->deleteThis();
		m_pItemModelPanelKVs = NULL;
	}

	m_vecContentsPanels.PurgeAndDeleteElements();
}

void CInputStringForItemBackpackOverlayDialog::FireGameEvent( IGameEvent *event )
{
	// If we're not visible, ignore all events
	if ( !IsVisible() )
		return;

	// Something caused our inventory to update.  Assuming it was from our shuffle
	// then we need to update ourselves.
	const char *type = event->GetName();
	if ( Q_strcmp( "inventory_updated", type ) == 0 )
	{
		m_bUpdateRecieved = true;
	}
}

void CInputStringForItemBackpackOverlayDialog::ApplySchemeSettings( vgui::IScheme *pScheme )
{
	BaseClass::ApplySchemeSettings( pScheme );

	LoadControlSettings( "Resource/UI/econ/InputStringForItemBackpackOverlayDialog.res" );

	CCrateLootListWrapper itemWrapper( &m_Item );
	const IEconLootList *pLootList = itemWrapper.GetEconLootList();
	// Set the crate footer text.  The crate itself specifies what to use.
	if ( pLootList->GetLootListFooterLocalizationKey() )
	{
		m_pRareLootLabel->SetText( pLootList->GetLootListFooterLocalizationKey() );
	}
	else
	{
		const char *pszRareLootListFooterLocalizationKey = m_Item.GetItemDefinition()->GetDefinitionString( "loot_list_rare_item_footer", "#Econ_Revolving_Loot_List_Rare_Item" );
		m_pRareLootLabel->SetText( pszRareLootListFooterLocalizationKey );
	}

	// Use the gradient border for the tooltip
	m_pMouseOverItemPanel->SetBorder( pScheme->GetBorder("LoadoutItemPopupBorder") );

	m_pPreviewModelPanel->SetItem( &m_Item );
	m_pPreviewModelPanel->SetActAsButton( false, false ); // Dont mess around with the mouse

	m_pTextEntry->RequestFocus();	
}

void CInputStringForItemBackpackOverlayDialog::ApplySettings( KeyValues *inResourceData )
{
	BaseClass::ApplySettings( inResourceData );

	// Pull out the model panel KVs for this panel
	KeyValues *pItemKV = inResourceData->FindKey( "modelpanels_kv" );
	if ( pItemKV )
	{
		if ( m_pItemModelPanelKVs )
		{
			m_pItemModelPanelKVs->deleteThis();
		}
		m_pItemModelPanelKVs = new KeyValues( "modelpanels_kv" );
		pItemKV->CopySubkeys( m_pItemModelPanelKVs );
	}

	CreateItemPanels();
}

void CInputStringForItemBackpackOverlayDialog::CreateItemPanels()
{
	CCrateLootListWrapper itemWrapper( &m_Item );
	const IEconLootList *pLootList = itemWrapper.GetEconLootList();

	class CItemDefLootListIterator : public IEconLootList::IEconLootListIterator
	{
	public:
		CItemDefLootListIterator( CUtlVector< item_definition_index_t > *pVecItemDefs  )
			: m_pVecItemDefs( pVecItemDefs )
		{}

		virtual void OnIterate( item_definition_index_t unItemDefIndex ) OVERRIDE
		{
			const CEconItemDefinition *pItemDef = GetItemSchema()->GetItemDefinition( unItemDefIndex );
			if ( pItemDef && pItemDef->BValidForShuffle() )
			{
				m_pVecItemDefs->AddToTail( unItemDefIndex );
			}
		}

	private:
		CUtlVector< item_definition_index_t > * const m_pVecItemDefs;
	};

	// Get the drops from the item
	CUtlVector< item_definition_index_t > vecItemDefs;
	CItemDefLootListIterator it( &vecItemDefs );
	pLootList->EnumerateUserFacingPotentialDrops( &it );

	if ( !m_pItemModelPanelKVs )
		return;

	if ( m_vecContentsPanels.Count() != vecItemDefs.Count() )
	{
		m_vecContentsPanels.PurgeAndDeleteElements();

		FOR_EACH_VEC( vecItemDefs, i )
		{
			// Create new panel
			CItemModelPanel* pItemPanel = m_vecContentsPanels[ m_vecContentsPanels.AddToTail( new CItemModelPanel( this, CFmtStr( "item_preview_%d", i ) ) ) ];
			pItemPanel->ApplySettings( m_pItemModelPanelKVs );
			pItemPanel->InvalidateLayout( true );
			pItemPanel->SetActAsButton( false, true );	// Lets us get mouse enter/exit evens for tooltips
			pItemPanel->SetTooltip( m_pMouseOverTooltip, "" );	// Tooltip panel to use
		}
	}

	// Create the panels and set the items into them
	FOR_EACH_VEC( vecItemDefs, i )
	{
		const item_definition_index_t &itemDef = vecItemDefs[i];
			
		CItemModelPanel* pItemPanel = m_vecContentsPanels[i];

		CEconItemView item;
		item.SetItemDefIndex( itemDef );
		item.SetItemQuality( AE_UNIQUE );	// Unique by default
		item.SetItemLevel( 0 ); // Hide this?
		item.SetInitialized( true );
		item.SetItemOriginOverride( kEconItemOrigin_Invalid );

		pItemPanel->SetItem( &item );
	}
}

void CInputStringForItemBackpackOverlayDialog::PerformLayout( void )
{
	BaseClass::PerformLayout();

	// Find out how wide these panels will be side by side
	const int nBuffer = 5;
	int nTotalWide = 0;
	const int nCount = m_vecContentsPanels.Count();
	if ( nCount )
	{
		const int nWide = m_vecContentsPanels.Head()->GetWide();
		nTotalWide = (nCount * nWide) + ( (nCount - 1) * nBuffer );
	}

	// Find out how much space the panels take up within the parent
	int nParentWide = GetWide();
	int nDiff = nParentWide - nTotalWide;
	// How far we need to offset from the left edge
	int nStartOffset = nDiff / 2;

	// Place all the panels side by side
	FOR_EACH_VEC( m_vecContentsPanels, i )
	{
		CItemModelPanel* pItemPanel = m_vecContentsPanels[ i ];

		const int nWide = pItemPanel->GetWide();
		
		pItemPanel->SetPos( nStartOffset + i * (nWide + nBuffer), YRES(150) );
		pItemPanel->SetVisible( true );
	}

	// Which button to show
	m_pUseKeyButton->SetVisible( m_UseableKey.IsValid() );
	m_pGetKeyButton->SetVisible( !m_UseableKey.IsValid() );
}

void CInputStringForItemBackpackOverlayDialog::OnCommand( const char *command )
{
	if ( !Q_strnicmp( command, "cancel", 6 ) )
	{
		TFModalStack()->PopModal( this );

		SetVisible( false );
		MarkForDeletion();
	}
	else if ( !Q_strnicmp( command, "shuffle", 7 ) )
	{
		// let the GC know
		if ( m_pTextEntry && Plat_FloatTime() >= m_sflNextShuffleTime )
		{
			// Set the next time they can send a request to shuffle
			m_sflNextShuffleTime = Plat_FloatTime() + SHUFFLE_TIME;

			enum { kMaxCodeStringSize = 32 };
			char szText[ kMaxCodeStringSize ] = { 0 };
			m_pTextEntry->GetText( &szText[0], sizeof( szText )  );

			GCSDK::CProtoBufMsg<CMsgGCShuffleCrateContents> msg( k_EMsgGCShuffleCrateContents );

			msg.Body().set_crate_item_id( m_Item.GetID() );
			msg.Body().set_user_code_string( szText );

			GCClientSystem()->BSendMessage( msg );

			m_pProgressBar->SetProgress( 0.f );
			m_pProgressBar->SetVisible( true );
			m_pTextEntry->SetVisible( false );

			vgui::surface()->PlaySound( "ui/itemcrate_shuffle.wav" );
		}
	}
	else if ( !Q_strnicmp( command, "getkey", 6 ) )
	{
		static CSchemaAttributeDefHandle pAttrDef_DecodedBy( "decoded by itemdefindex" );
			
		uint32 iDecodableItemDef = 0;
		if ( m_Item.FindAttribute( pAttrDef_DecodedBy, &iDecodableItemDef ) )
		{
			// casting to the proper type since our econ system is dumb
			const float& value_as_float = (float&)iDecodableItemDef;
			EconUI()->CloseEconUI();
			EconUI()->OpenStorePanel( (int)value_as_float, false );

			// close ourselves
			TFModalStack()->PopModal( this );
			SetVisible( false );
			MarkForDeletion();
		}
	}
	else if ( !Q_strnicmp( command, "usekey", 6 ) )
	{
		if ( m_UseableKey.IsValid() )
		{
			// Use the key
			ApplyTool( GetParent(), &m_UseableKey, &m_Item );
			// close ourselves
			TFModalStack()->PopModal( this );
			SetVisible( false );
			MarkForDeletion();
		}
	}
}

void CInputStringForItemBackpackOverlayDialog::FindUsableKey()
{
	static CSchemaAttributeDefHandle pAttrDef_DecodedBy( "decoded by itemdefindex" );
			
	uint32 iDecodableItemDef = 0;
	if ( m_Item.FindAttribute( pAttrDef_DecodedBy, &iDecodableItemDef ) )
	{
		const float& value_as_float = (float&)iDecodableItemDef;
		iDecodableItemDef = (float)value_as_float;
		CPlayerInventory *pInventory = InventoryManager()->GetLocalInventory();
		if ( !pInventory )
			return;

		for ( int i = 0; i < pInventory->GetItemCount(); i++ )
		{
			CEconItemView *pItem = pInventory->GetItem(i);
			if ( pItem->GetItemDefIndex() == iDecodableItemDef )
			{
				m_UseableKey = *pItem;
			}
		}
	}
}

void CInputStringForItemBackpackOverlayDialog::OnThink()
{
	float flDelta = m_sflNextShuffleTime - Plat_FloatTime();

	// If we're ready, show "Shuffle"
	if ( flDelta < 0 )
	{
		// Show the text entry, show the progress bar
		m_pProgressBar->SetVisible( false );
		m_pTextEntry->SetVisible( true );

		// Re-enable the shuffle/use buttons
		m_pShuffleButton->SetEnabled( m_pTextEntry->GetTextLength() != 0 );
		m_pUseKeyButton->SetEnabled( true );
		// Say "Shuffle"
		m_pShuffleButton->SetText( "#ShuffleContents" );

		// We got a inventory update message, update
		if ( m_bUpdateRecieved )
		{
			CreateItemPanels();
			m_bUpdateRecieved = false;
		}
	}
	else
	{
		// Show the progress bar, hide the text field
		m_pProgressBar->SetVisible( true );
		m_pTextEntry->SetVisible( false );

		// Dont allow clicking the shuffle or use key button
		m_pShuffleButton->SetEnabled( false );
		m_pUseKeyButton->SetEnabled( false );
		// Say "Shuffling..."
		m_pShuffleButton->SetText( "#ShufflingContents" );

		// Set progress
		float flProgress = ( SHUFFLE_TIME - flDelta ) / SHUFFLE_TIME;
		m_pProgressBar->SetProgress( flProgress );
	}
}

void CInputStringForItemBackpackOverlayDialog::Show()
{
	SetVisible( true );
	MakePopup();
	MoveToFront();
	SetKeyBoardInputEnabled( true );
	SetMouseInputEnabled( true );
	TFModalStack()->PushModal( this );

	// If a key wasnt passed in, find the first one in the 
	// player's inventory
	if ( !m_UseableKey.IsValid() )
	{
		FindUsableKey();
	}

	// Which button to show
	m_pUseKeyButton->SetVisible( m_UseableKey.IsValid() );
	m_pGetKeyButton->SetVisible( !m_UseableKey.IsValid() );

	// Put the current gen code of the crate into the text field
	static CSchemaAttributeDefHandle pAttrDef_DecodedBy( "crate generation code" );
	const char *pszAttrGenCode;
	if ( FindAttribute_UnsafeBitwiseCast<CAttribute_String>( &m_Item, pAttrDef_DecodedBy, &pszAttrGenCode ) )
	{
		m_pTextEntry->SetText( pszAttrGenCode );
	}
}