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.

273 lines
9.6 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "rtime.h"
  8. #include "vgui_controls/EditablePanel.h"
  9. #include "vgui_controls/TextEntry.h"
  10. #include "vgui/IInput.h"
  11. #include "econ_item_system.h"
  12. #include "econ_item_constants.h"
  13. #include "econ_gcmessages.h"
  14. #include "econ_item_inventory.h"
  15. #include "item_rental_ui.h"
  16. #ifdef TF_CLIENT_DLL
  17. #include "c_tf_gamestats.h"
  18. #endif
  19. // memdbgon must be the last include file in a .cpp file!!!
  20. #include <tier0/memdbgon.h>
  21. //-----------------------------------------------------------------------------
  22. // Purpose: Confirm item preview.
  23. //-----------------------------------------------------------------------------
  24. class CConfirmItemPreviewDialog : public CBaseToolUsageDialog
  25. {
  26. DECLARE_CLASS_SIMPLE( CConfirmItemPreviewDialog, CBaseToolUsageDialog );
  27. public:
  28. CConfirmItemPreviewDialog( vgui::Panel *pParent, CEconItemView *pPreviewItem );
  29. ~CConfirmItemPreviewDialog();
  30. virtual void ApplySchemeSettings( vgui::IScheme *scheme );
  31. virtual void Apply( void );
  32. private:
  33. CEconItemView* m_pPreviewItem;
  34. };
  35. //-----------------------------------------------------------------------------
  36. // Purpose:
  37. //-----------------------------------------------------------------------------
  38. CConfirmItemPreviewDialog::CConfirmItemPreviewDialog( vgui::Panel *parent, CEconItemView *pPreviewItem ) : CBaseToolUsageDialog( parent, "ConfirmItemPreviewDialog", pPreviewItem, pPreviewItem )
  39. {
  40. m_pPreviewItem = pPreviewItem;
  41. }
  42. //-----------------------------------------------------------------------------
  43. // Purpose:
  44. //-----------------------------------------------------------------------------
  45. CConfirmItemPreviewDialog::~CConfirmItemPreviewDialog()
  46. {
  47. delete m_pPreviewItem;
  48. }
  49. //-----------------------------------------------------------------------------
  50. // Purpose:
  51. //-----------------------------------------------------------------------------
  52. void CConfirmItemPreviewDialog::ApplySchemeSettings( vgui::IScheme *pScheme )
  53. {
  54. LoadControlSettings( "Resource/UI/econ/ConfirmItemPreviewDialog.res" );
  55. BaseClass::ApplySchemeSettings( pScheme );
  56. m_pTitleLabel = dynamic_cast<vgui::Label*>( FindChildByName("TitleLabel") );
  57. if ( m_pTitleLabel )
  58. {
  59. wchar_t *pszBaseString = g_pVGuiLocalize->Find( "ItemPreviewDialogTitle" );
  60. if ( pszBaseString )
  61. {
  62. wchar_t wTemp[256];
  63. g_pVGuiLocalize->ConstructString_safe( wTemp, pszBaseString, 1, m_pToolModelPanel->GetItem()->GetItemName() );
  64. m_pTitleLabel->SetText( wTemp );
  65. m_pTitleLabel->GetTextImage()->ClearColorChangeStream();
  66. }
  67. }
  68. m_pSubjectModelPanel->SetVisible( false );
  69. }
  70. //-----------------------------------------------------------------------------
  71. // Purpose:
  72. //-----------------------------------------------------------------------------
  73. void CConfirmItemPreviewDialog::Apply( void )
  74. {
  75. // Notify the GC that the player wants to preview this item.
  76. GCSDK::CGCMsg< MsgGCItemPreviewRequest_t > msg( k_EMsgGCItemPreviewRequest );
  77. msg.Body().m_unItemDefIndex = m_pToolModelPanel->GetItem()->GetItemDefIndex();
  78. // OGS LOGGING HERE
  79. GCClientSystem()->BSendMessage( msg );
  80. EconUI()->SetPreventClosure( false );
  81. }
  82. //-----------------------------------------------------------------------------
  83. // Purpose: GC Msg handler to receive the item preview query response.
  84. //-----------------------------------------------------------------------------
  85. class CGCItemPreviewStatusResponse : public GCSDK::CGCClientJob
  86. {
  87. public:
  88. CGCItemPreviewStatusResponse( GCSDK::CGCClient *pClient ) : GCSDK::CGCClientJob( pClient ) {}
  89. virtual bool BYieldingRunGCJob( GCSDK::IMsgNetPacket *pNetPacket )
  90. {
  91. GCSDK::CGCMsg<MsgGCItemPreviewCheckStatusResponse_t> msg( pNetPacket );
  92. CStorePanel *pStorePanel = EconUI()->GetStorePanel();
  93. if ( !pStorePanel )
  94. return true;
  95. if ( msg.Body().m_eResponse == k_EGCMsgResponseOK )
  96. {
  97. // We can preview the item.
  98. CEconItemView *pPreviewItem = new CEconItemView();
  99. pPreviewItem->Init( msg.Body().m_unItemDefIndex, AE_UNIQUE, AE_USE_SCRIPT_VALUE, true );
  100. CConfirmItemPreviewDialog *dialog = vgui::SETUP_PANEL( new CConfirmItemPreviewDialog( pStorePanel->GetPropertySheet()->GetActivePage(), pPreviewItem ) );
  101. MakeModalAndBringToFront( dialog );
  102. }
  103. else
  104. {
  105. #ifdef TF_CLIENT_DLL
  106. C_CTFGameStats::ImmediateWriteInterfaceEvent( "store_preview_item_denied", CFmtStr( "%i", msg.Body().m_unItemDefIndex ).Access() );
  107. #endif
  108. // We aren't allowed to preview an item right now.
  109. CTFMessageBoxDialog* pDialog = ShowMessageBox( "#ItemPreview_PreviewStartFailedTitle", "#ItemPreview_PreviewStartFailedText", "#GameUI_OK" );
  110. RTime32 nextTime = msg.Body().m_timePreviewTime + EconUI()->GetStorePanel()->GetPriceSheet()->GetPreviewPeriod();
  111. locchar_t wzValue[64];
  112. char time_buf[k_RTimeRenderBufferSize];
  113. GLocalizationProvider()->ConvertUTF8ToLocchar( CRTime::Render( nextTime, time_buf ), wzValue, sizeof( wzValue ) );
  114. pDialog->AddStringToken( "date_time", wzValue );
  115. }
  116. return true;
  117. }
  118. };
  119. GC_REG_JOB( GCSDK::CGCClient, CGCItemPreviewStatusResponse, "CGCItemPreviewStatusResponse", k_EMsgGCItemPreviewStatusResponse, GCSDK::k_EServerTypeGCClient );
  120. //-----------------------------------------------------------------------------
  121. // Purpose: The GC is telling us our request has been granted.
  122. //-----------------------------------------------------------------------------
  123. class CGCItemPreviewRequestResponse : public GCSDK::CGCClientJob
  124. {
  125. public:
  126. CGCItemPreviewRequestResponse( GCSDK::CGCClient *pClient ) : GCSDK::CGCClientJob( pClient ) {}
  127. static void OnPreviewItemConfirm( bool bConfirmed, void *pContext )
  128. {
  129. InventoryManager()->ShowItemsPickedUp( true, false );
  130. CStorePage* pStorePage = dynamic_cast<CStorePage*>( EconUI()->GetStorePanel()->GetActivePage() );
  131. if ( pStorePage )
  132. {
  133. pStorePage->UpdateModelPanels();
  134. }
  135. }
  136. virtual bool BYieldingRunGCJob( GCSDK::IMsgNetPacket *pNetPacket )
  137. {
  138. GCSDK::CGCMsg<MsgGCItemPreviewRequestResponse_t> msg( pNetPacket );
  139. CStorePanel *pStorePanel = EconUI()->GetStorePanel();
  140. if ( !pStorePanel )
  141. return true;
  142. if ( msg.Body().m_eResponse == k_EGCMsgResponseOK )
  143. {
  144. // The preview has started.
  145. ShowMessageBox( "#ItemPreview_PreviewStartedTitle", "#ItemPreview_PreviewStartedText", "#GameUI_OK", OnPreviewItemConfirm );
  146. }
  147. else
  148. {
  149. // The preview cannot start right now for some reason.
  150. }
  151. return true;
  152. }
  153. };
  154. GC_REG_JOB( GCSDK::CGCClient, CGCItemPreviewRequestResponse, "CGCItemPreviewRequestResponse", k_EMsgGCItemPreviewRequestResponse, GCSDK::k_EServerTypeGCClient );
  155. void OpenStoreToItem( bool bConfirmed, void *pContext )
  156. {
  157. CEconPreviewExpiredNotification* pNotification = (CEconPreviewExpiredNotification*) pContext;
  158. if ( pNotification )
  159. {
  160. pNotification->SetIsInUse( false );
  161. pNotification->MarkForDeletion();
  162. if ( bConfirmed )
  163. {
  164. EconUI()->OpenStorePanel( pNotification->GetItemDefIndex(), true );
  165. }
  166. }
  167. }
  168. CEconPreviewNotification::CEconPreviewNotification( uint64 ulSteamID, uint32 iItemDef )
  169. : CEconNotification()
  170. {
  171. SetSteamID( ulSteamID );
  172. SetLifetime( 20.0f );
  173. m_pItemDef = GetItemSchema()->GetItemDefinition( iItemDef );
  174. if ( !m_pItemDef )
  175. return;
  176. AddStringToken( "item_name", g_pVGuiLocalize->Find(m_pItemDef->GetItemBaseName()) );
  177. }
  178. void CEconPreviewExpiredNotification::Trigger()
  179. {
  180. CTFGenericConfirmDialog *pDialog = ShowConfirmDialog( "#TF_PreviewItem_Expired_Title", "#TF_PreviewItem_Expired_Text", "#TF_PreviewItem_BuyIt", "#TF_PreviewItem_NotNow", &OpenStoreToItem );
  181. pDialog->SetContext( this );
  182. pDialog->AddStringToken( "item_name", g_pVGuiLocalize->Find(m_pItemDef->GetItemBaseName()) );
  183. SetIsInUse( true );
  184. }
  185. //-----------------------------------------------------------------------------
  186. // Purpose: The GC is telling us our preview item has expired.
  187. //-----------------------------------------------------------------------------
  188. class CGCItemPreviewExpireNotification : public GCSDK::CGCClientJob
  189. {
  190. public:
  191. CGCItemPreviewExpireNotification( GCSDK::CGCClient *pClient ) : GCSDK::CGCClientJob( pClient ) {}
  192. virtual bool BYieldingRunGCJob( GCSDK::IMsgNetPacket *pNetPacket )
  193. {
  194. GCSDK::CGCMsg<MsgGCItemPreviewExpireNotification_t> msg( pNetPacket );
  195. CEconPreviewExpiredNotification *pNotification = new CEconPreviewExpiredNotification( msg.Hdr().m_ulSteamID, msg.Body().m_unItemDefIndex );
  196. pNotification->SetText( "#TF_PreviewItem_Expired" );
  197. NotificationQueue_Add( pNotification );
  198. return true;
  199. }
  200. };
  201. GC_REG_JOB( GCSDK::CGCClient, CGCItemPreviewExpireNotification, "CGCItemPreviewExpireNotification", k_EMsgGCItemPreviewExpireNotification, GCSDK::k_EServerTypeGCClient );
  202. //-----------------------------------------------------------------------------
  203. // Purpose: The GC is telling us we bought our preview item!
  204. //-----------------------------------------------------------------------------
  205. class CGCItemPreviewItemBoughtNotification : public GCSDK::CGCClientJob
  206. {
  207. public:
  208. CGCItemPreviewItemBoughtNotification( GCSDK::CGCClient *pClient ) : GCSDK::CGCClientJob( pClient ) {}
  209. virtual bool BYieldingRunGCJob( GCSDK::IMsgNetPacket *pNetPacket )
  210. {
  211. GCSDK::CProtoBufMsg<CMsgGCItemPreviewItemBoughtNotification> msg( pNetPacket );
  212. CEconPreviewItemBoughtNotification *pNotification = new CEconPreviewItemBoughtNotification( msg.Hdr().client_steam_id(), msg.Body().item_def_index() );
  213. pNotification->SetText( "#TF_PreviewItem_ItemBought" );
  214. NotificationQueue_Add( pNotification );
  215. return true;
  216. }
  217. };
  218. GC_REG_JOB( GCSDK::CGCClient, CGCItemPreviewItemBoughtNotification, "CGCItemPreviewItemBoughtNotification", k_EMsgGCItemPreviewItemBoughtNotification, GCSDK::k_EServerTypeGCClient );