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.

332 lines
7.8 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. //=============================================================================
  4. #define WIN32_LEAN_AND_MEAN
  5. #include <Windows.h> // HINSTANCE
  6. // Valve includes
  7. #include "inputsystem/iinputsystem.h"
  8. #include "itemtest/itemtest.h"
  9. #include "itemtest/itemtest_controls.h"
  10. #include "p4lib/ip4.h"
  11. #include "tier0/icommandline.h"
  12. #include "vgui/ILocalize.h"
  13. #include "vgui/ISurface.h"
  14. #include "vgui/IVGui.h"
  15. #include "vgui_controls/consoledialog.h"
  16. #include "vgui_controls/MessageBox.h"
  17. #include "vgui_controls/Panel.h"
  18. // Local includes
  19. #include "itemtestapp.h"
  20. // Last include
  21. #include <tier0/memdbgon.h>
  22. //-----------------------------------------------------------------------------
  23. // Forward declarations
  24. //-----------------------------------------------------------------------------
  25. class CConsoleDialogNew;
  26. SpewRetval_t ConsoleDialogSpewFunc( SpewType_t spewType, const tchar *pMsg );
  27. //-----------------------------------------------------------------------------
  28. //
  29. //-----------------------------------------------------------------------------
  30. static vgui::Panel *g_pTopPanel = NULL;
  31. static vgui::DHANDLE< CConsoleDialogNew > g_hConsoleDialog;
  32. static vgui::DHANDLE< vgui::Frame > g_hMainFrame;
  33. //=============================================================================
  34. //
  35. //=============================================================================
  36. class CItemTestVGUIApp : public CItemTestApp
  37. {
  38. typedef CItemTestApp BaseClass;
  39. public:
  40. virtual bool Create();
  41. virtual int Main();
  42. // Methods of IApplication
  43. virtual bool PreInit();
  44. protected:
  45. static vgui::Panel *InitializeVGUI();
  46. static void ShutdownVGUI();
  47. };
  48. //-----------------------------------------------------------------------------
  49. //
  50. //-----------------------------------------------------------------------------
  51. DEFINE_WINDOWED_STEAM_APPLICATION_OBJECT( CItemTestVGUIApp );
  52. //=============================================================================
  53. //
  54. //=============================================================================
  55. class CConsoleDialogNew : public vgui::CConsoleDialog
  56. {
  57. DECLARE_CLASS_SIMPLE( CConsoleDialogNew, vgui::CConsoleDialog );
  58. public:
  59. CConsoleDialogNew( vgui::Panel *pPanel, const char *pszName )
  60. : CConsoleDialog( pPanel, pszName, false )
  61. {
  62. }
  63. virtual void OnCommandSubmitted( const char *pszCommand )
  64. {
  65. if ( !V_stricmp( pszCommand, "quit" ) )
  66. {
  67. if ( g_hMainFrame )
  68. g_hMainFrame->Close();
  69. Close();
  70. }
  71. else if ( !V_stricmp( pszCommand, "help" ) )
  72. {
  73. __s_ApplicationObject.PrintHelp();
  74. }
  75. else
  76. {
  77. Warning( "Error! Unknown command \"%s\"\n", pszCommand );
  78. }
  79. }
  80. };
  81. //-----------------------------------------------------------------------------
  82. //
  83. //-----------------------------------------------------------------------------
  84. bool CItemTestVGUIApp::Create()
  85. {
  86. AppSystemInfo_t appSystems[] =
  87. {
  88. { "inputsystem.dll", INPUTSYSTEM_INTERFACE_VERSION },
  89. { "vgui2.dll", VGUI_IVGUI_INTERFACE_VERSION },
  90. { "", "" } // Required to terminate the list
  91. };
  92. if ( FindParam( kDev ) && !FindParam( kNoP4 ) )
  93. {
  94. AppModule_t p4Module = LoadModule( "p4lib.dll" );
  95. AddSystem( p4Module, P4_INTERFACE_VERSION );
  96. }
  97. return AddSystems( appSystems );
  98. }
  99. //-----------------------------------------------------------------------------
  100. //
  101. //-----------------------------------------------------------------------------
  102. bool CItemTestVGUIApp::PreInit()
  103. {
  104. if ( !BaseClass::PreInit() )
  105. return false;
  106. CreateInterfaceFn factory = GetFactory();
  107. return vgui::VGui_InitInterfacesList( "CVguiSteamApp", &factory, 1 );
  108. }
  109. //-----------------------------------------------------------------------------
  110. //
  111. //-----------------------------------------------------------------------------
  112. int CItemTestVGUIApp::Main()
  113. {
  114. g_pTopPanel = InitializeVGUI();
  115. if ( !g_pTopPanel )
  116. return 1;
  117. SpewOutputFunc( ConsoleDialogSpewFunc );
  118. if ( !g_hMainFrame )
  119. {
  120. // add our main window
  121. CItemUploadWizard *pItemUploadWizard = new CItemUploadWizard( g_pTopPanel, "Item Upload Wizard" );
  122. if ( pItemUploadWizard )
  123. {
  124. g_hMainFrame = pItemUploadWizard;
  125. CAssetTF &assetTF = pItemUploadWizard->Asset();
  126. ProcessCommandLine( &assetTF, false );
  127. pItemUploadWizard->UpdateGUI();
  128. }
  129. }
  130. if ( !g_hConsoleDialog )
  131. {
  132. if ( g_pTopPanel )
  133. {
  134. CConsoleDialogNew *pConsoleDialog = g_hMainFrame ?
  135. new CConsoleDialogNew( g_hMainFrame, "console" ) :
  136. new CConsoleDialogNew( g_pTopPanel, "console" );
  137. if ( pConsoleDialog )
  138. {
  139. g_hConsoleDialog = pConsoleDialog;
  140. if ( !g_hMainFrame )
  141. {
  142. g_hMainFrame = g_hConsoleDialog;
  143. g_hConsoleDialog->SetSize( 640, 480 );
  144. g_hConsoleDialog->MoveToCenterOfScreen();
  145. }
  146. else
  147. {
  148. int nWide = 0;
  149. int nTall = 0;
  150. int nX = 0;
  151. int nY = 0;
  152. g_hMainFrame->GetBounds( nX, nY, nWide, nTall );
  153. g_hConsoleDialog->SetSize( nWide, MAX( nTall / 3, 120 ) );
  154. g_hConsoleDialog->SetPos( nX, nY + nTall + 3 );
  155. }
  156. g_hConsoleDialog->Activate();
  157. g_hConsoleDialog->SetVisible( true );
  158. g_hConsoleDialog->SetDeleteSelfOnClose( true );
  159. g_hConsoleDialog->ColorPrint( Color( 255, 192, 0, 255 ), "\nNOTE" );
  160. g_hConsoleDialog->Print( ": The only commands available are 'quit' and 'help'\n" );
  161. }
  162. }
  163. }
  164. if ( g_hMainFrame )
  165. {
  166. // show main window
  167. g_hMainFrame->SetSizeable( true );
  168. g_hMainFrame->SetMenuButtonVisible( true );
  169. g_hMainFrame->MoveToCenterOfScreen();
  170. g_hMainFrame->Activate();
  171. // Run the app
  172. while ( vgui::ivgui()->IsRunning() && g_hMainFrame )
  173. {
  174. vgui::ivgui()->RunFrame();
  175. }
  176. }
  177. ShutdownVGUI();
  178. return 0;
  179. }
  180. //-----------------------------------------------------------------------------
  181. //
  182. //-----------------------------------------------------------------------------
  183. vgui::Panel *CItemTestVGUIApp::InitializeVGUI()
  184. {
  185. // Init the surface
  186. vgui::Panel *pTopPanel = new vgui::Panel( NULL, "TopPanel" );
  187. if ( !pTopPanel )
  188. return NULL;
  189. pTopPanel->SetVisible( true );
  190. vgui::surface()->SetEmbeddedPanel( pTopPanel->GetVPanel() );
  191. // load the scheme
  192. vgui::scheme()->LoadSchemeFromFile( "resource/itemtest_scheme.res", NULL );
  193. // localization
  194. g_pVGuiLocalize->AddFile( "resource/platform_%language%.txt");
  195. g_pVGuiLocalize->AddFile( "resource/vgui_%language%.txt" );
  196. g_pVGuiLocalize->AddFile( "resource/itemtest_%language%.txt");
  197. g_pVGuiLocalize->AddFile( "resource/itemtest_english.txt");
  198. // Start vgui
  199. vgui::ivgui()->Start();
  200. return pTopPanel;
  201. }
  202. //-----------------------------------------------------------------------------
  203. //
  204. //-----------------------------------------------------------------------------
  205. void CItemTestVGUIApp::ShutdownVGUI()
  206. {
  207. if ( g_hConsoleDialog )
  208. g_hConsoleDialog->Close();
  209. if ( g_hMainFrame )
  210. g_hMainFrame->Close();
  211. if ( !g_pTopPanel )
  212. return;
  213. delete g_pTopPanel;
  214. g_pTopPanel = NULL;
  215. }
  216. //-----------------------------------------------------------------------------
  217. // Spew func
  218. //-----------------------------------------------------------------------------
  219. SpewRetval_t ConsoleDialogSpewFunc( SpewType_t spewType, const tchar *pMsg )
  220. {
  221. vgui::CConsoleDialog *pConsole = g_hConsoleDialog;
  222. if ( !pConsole )
  223. return SPEW_CONTINUE;
  224. OutputDebugString( pMsg );
  225. switch( spewType )
  226. {
  227. case SPEW_ASSERT:
  228. pConsole->ColorPrint( Color( 255, 192, 0, 255 ), pMsg );
  229. #ifdef _DEBUG
  230. return SPEW_DEBUGGER;
  231. #else
  232. return SPEW_CONTINUE;
  233. #endif
  234. case SPEW_ERROR:
  235. pConsole->ColorPrint( Color( 255, 0, 0, 255 ), pMsg );
  236. break;
  237. case SPEW_WARNING:
  238. pConsole->ColorPrint( Color( 192, 192, 0, 255 ), pMsg );
  239. break;
  240. case SPEW_MESSAGE:
  241. {
  242. Color c = *GetSpewOutputColor();
  243. if ( !V_stricmp( GetSpewOutputGroup(), "developer" ) )
  244. {
  245. pConsole->Print( pMsg );
  246. }
  247. else
  248. {
  249. pConsole->ColorPrint( c, pMsg );
  250. }
  251. }
  252. break;
  253. }
  254. if ( vgui::ivgui()->IsRunning() && g_hMainFrame )
  255. {
  256. vgui::ivgui()->RunFrame();
  257. }
  258. return SPEW_CONTINUE;
  259. }