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.

348 lines
8.7 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // The copyright to the contents herein is the property of Valve, L.L.C.
  4. // The contents may be used and/or copied only with the written permission of
  5. // Valve, L.L.C., or in accordance with the terms and conditions stipulated in
  6. // the agreement/contract under which the contents have been supplied.
  7. //
  8. // $Header: $
  9. // $NoKeywords: $
  10. //
  11. // Material editor
  12. //=============================================================================
  13. #include <windows.h>
  14. #include "vstdlib/cvar.h"
  15. #include "appframework/vguimatsysapp.h"
  16. #include "filesystem.h"
  17. #include "materialsystem/imaterialsystem.h"
  18. #include "vgui/IVGui.h"
  19. #include "vgui_controls/Panel.h"
  20. #include "vgui/ISurface.h"
  21. #include "vgui_controls/controls.h"
  22. #include "vgui/IScheme.h"
  23. #include "vgui/ILocalize.h"
  24. #include "vgui/IPanel.h"
  25. #include "tier0/dbg.h"
  26. #include "vgui_controls/Frame.h"
  27. #include "vgui_controls/AnimationController.h"
  28. #include "datamodel/dmelementfactoryhelper.h"
  29. #include "tier0/icommandline.h"
  30. #include "materialsystem/MaterialSystem_Config.h"
  31. #include "VGuiMatSurface/IMatSystemSurface.h"
  32. #include "datamodel/dmelement.h"
  33. #include "filesystem_init.h"
  34. #include "vstdlib/iprocessutils.h"
  35. #include "dmserializers/idmserializers.h"
  36. #include "dme_controls/dmecontrols.h"
  37. #include "p4lib/ip4.h"
  38. #include "tier3/tier3.h"
  39. //-----------------------------------------------------------------------------
  40. // Forward declarations
  41. //-----------------------------------------------------------------------------
  42. vgui::Panel *CreateElementViewerPanel();
  43. //-----------------------------------------------------------------------------
  44. // redirect spew to debug output window
  45. //-----------------------------------------------------------------------------
  46. SpewRetval_t SpewToODS( SpewType_t spewType, char const *pMsg )
  47. {
  48. OutputDebugString( pMsg );
  49. switch( spewType )
  50. {
  51. case SPEW_MESSAGE:
  52. case SPEW_WARNING:
  53. case SPEW_LOG:
  54. return SPEW_CONTINUE;
  55. case SPEW_ASSERT:
  56. case SPEW_ERROR:
  57. default:
  58. return SPEW_DEBUGGER;
  59. }
  60. }
  61. //-----------------------------------------------------------------------------
  62. // The application object
  63. //-----------------------------------------------------------------------------
  64. class CElementViewerApp : public CVguiMatSysApp
  65. {
  66. typedef CVguiMatSysApp BaseClass;
  67. public:
  68. // Methods of IApplication
  69. virtual bool Create();
  70. virtual bool PreInit( );
  71. virtual int Main();
  72. virtual void Destroy();
  73. private:
  74. virtual const char *GetAppName() { return "ElementViewer"; }
  75. };
  76. DEFINE_WINDOWED_STEAM_APPLICATION_OBJECT( CElementViewerApp );
  77. //-----------------------------------------------------------------------------
  78. // Create all singleton systems
  79. //-----------------------------------------------------------------------------
  80. bool CElementViewerApp::Create()
  81. {
  82. SpewOutputFunc( SpewToODS );
  83. // This is a little cheezy but fast...
  84. CommandLine()->AppendParm( "-resizing", NULL );
  85. if ( !BaseClass::Create() )
  86. return false;
  87. AppSystemInfo_t appSystems[] =
  88. {
  89. { "vstdlib.dll", PROCESS_UTILS_INTERFACE_VERSION },
  90. { "p4lib.dll", P4_INTERFACE_VERSION },
  91. { "", "" } // Required to terminate the list
  92. };
  93. AddSystem( g_pDataModel, VDATAMODEL_INTERFACE_VERSION );
  94. AddSystem( g_pDmElementFramework, VDMELEMENTFRAMEWORK_VERSION );
  95. AddSystem( g_pDmSerializers, DMSERIALIZERS_INTERFACE_VERSION );
  96. return AddSystems( appSystems );
  97. }
  98. void CElementViewerApp::Destroy()
  99. {
  100. BaseClass::Destroy();
  101. }
  102. //-----------------------------------------------------------------------------
  103. // Init, shutdown
  104. //-----------------------------------------------------------------------------
  105. bool CElementViewerApp::PreInit( )
  106. {
  107. if ( !BaseClass::PreInit() )
  108. return false;
  109. if ( !g_pFullFileSystem || !g_pMaterialSystem || !g_pVGui || !g_pVGuiSurface || !g_pDataModel || !g_pMatSystemSurface || !p4 )
  110. {
  111. Error( "Element viewer is missing required interfaces!\n" );
  112. return false;
  113. }
  114. return true;
  115. }
  116. //-----------------------------------------------------------------------------
  117. // Purpose:
  118. //-----------------------------------------------------------------------------
  119. void VGui_RecursivePrintTree( int depth, int start, int end, vgui::Panel *current, int& totaldrawn )
  120. {
  121. // No more room
  122. if ( totaldrawn >= 128 )
  123. return;
  124. if ( !current )
  125. return;
  126. int count = current->GetChildCount();
  127. for ( int i = 0; i < count ; i++ )
  128. {
  129. vgui::Panel *panel = current->GetChild( i );
  130. // Msg( "%i: %s : %p, %s %s\n",
  131. // i + 1,
  132. // panel->GetName(),
  133. // panel,
  134. // panel->IsVisible() ? "visible" : "hidden",
  135. // panel->IsPopup() ? "popup" : "" );
  136. int width = panel->GetWide();
  137. int height = panel->GetTall();
  138. int x, y;
  139. panel->GetPos( x, y );
  140. if ( depth >= start && depth <= end )
  141. {
  142. totaldrawn++;
  143. Msg(
  144. // Con_NPrintf( totaldrawn++,
  145. "%s (%i.%i): %p, %s %s x(%i) y(%i) w(%i) h(%i)\n",
  146. panel->GetName(),
  147. depth + 1,
  148. i + 1,
  149. panel,
  150. panel->IsVisible() ? "visible" : "hidden",
  151. panel->IsPopup() ? "popup" : "",
  152. x, y,
  153. width, height );
  154. }
  155. VGui_RecursivePrintTree( depth + 1, start, end, panel, totaldrawn );
  156. }
  157. }
  158. #define VGUI_DRAWPOPUPS 1
  159. #define VGUI_DRAWTREE 0
  160. #define VGUI_DRAWPANEL ""
  161. #define VGUI_TREESTART 0
  162. //-----------------------------------------------------------------------------
  163. // Purpose:
  164. //-----------------------------------------------------------------------------
  165. void VGui_DrawPopups( void )
  166. {
  167. if ( !VGUI_DRAWPOPUPS )
  168. return;
  169. int c = vgui::surface()->GetPopupCount();
  170. for ( int i = 0; i < c; i++ )
  171. {
  172. vgui::VPANEL popup = vgui::surface()->GetPopup( i );
  173. if ( !popup )
  174. continue;
  175. const char *p = vgui::ipanel()->GetName( popup );
  176. bool visible = vgui::ipanel()->IsVisible( popup );
  177. int width, height;
  178. int x, y;
  179. vgui::ipanel()->GetSize( popup, width, height );
  180. vgui::ipanel()->GetPos( popup, x, y );
  181. //Con_NPrintf( i,
  182. Msg(
  183. "%i: %s : %x, %s pos(%i,%i) w(%i) h(%i)\n",
  184. i,
  185. p,
  186. popup,
  187. visible ? "visible" : "hidden",
  188. x, y,
  189. width, height );
  190. }
  191. }
  192. //-----------------------------------------------------------------------------
  193. // Purpose:
  194. //-----------------------------------------------------------------------------
  195. void VGui_DrawHierarchy( void )
  196. {
  197. if ( VGUI_DRAWTREE <= 0 && Q_strlen( VGUI_DRAWPANEL ) <= 0 )
  198. return;
  199. Msg( "\n" );
  200. int startlevel = 0;
  201. int endlevel = 1000;
  202. bool wholetree = VGUI_DRAWTREE > 0 ? true : false;
  203. if ( wholetree )
  204. {
  205. startlevel = VGUI_TREESTART;
  206. endlevel = VGUI_DRAWTREE;
  207. }
  208. // Can't start after end
  209. startlevel = min( endlevel, startlevel );
  210. int drawn = 0;
  211. vgui::VPANEL root = vgui::surface()->GetEmbeddedPanel();
  212. if ( !root )
  213. return;
  214. vgui::Panel *p = vgui::ipanel()->GetPanel( root, "ElementViewer" );
  215. if ( !wholetree )
  216. {
  217. // Find named panel
  218. char const *name = VGUI_DRAWPANEL;
  219. p = p->FindChildByName( name, true );
  220. }
  221. if ( !p )
  222. return;
  223. VGui_RecursivePrintTree( 0, startlevel, endlevel, p, drawn );
  224. }
  225. //-----------------------------------------------------------------------------
  226. // main application
  227. //-----------------------------------------------------------------------------
  228. int CElementViewerApp::Main()
  229. {
  230. g_pMaterialSystem->ModInit();
  231. if (!SetVideoMode())
  232. return 0;
  233. // load scheme
  234. if (!vgui::scheme()->LoadSchemeFromFile("resource/BoxRocket.res", "ElementViewer" ))
  235. {
  236. Assert( 0 );
  237. }
  238. // load the boxrocket localization file
  239. g_pVGuiLocalize->AddFile( "resource/boxrocket_%language%.txt" );
  240. // load the base localization file
  241. g_pVGuiLocalize->AddFile( "Resource/valve_%language%.txt" );
  242. g_pFullFileSystem->AddSearchPath( "platform", "PLATFORM" );
  243. g_pVGuiLocalize->AddFile( "Resource/vgui_%language%.txt");
  244. g_pVGuiLocalize->AddFile( "Resource/dmecontrols_%language%.txt");
  245. // start vgui
  246. g_pVGui->Start();
  247. // add our main window
  248. vgui::Panel *mainPanel = CreateElementViewerPanel();
  249. // run app frame loop
  250. vgui::VPANEL root = vgui::surface()->GetEmbeddedPanel();
  251. vgui::surface()->Invalidate( root );
  252. CMatRenderContextPtr pRenderContext( g_pMaterialSystem );
  253. int nLastTime = Plat_MSTime();
  254. while (g_pVGui->IsRunning())
  255. {
  256. // Give other applications a chance to run
  257. Sleep( 1 );
  258. int nTime = Plat_MSTime();
  259. if ( ( nTime - nLastTime ) < 16 )
  260. continue;
  261. nLastTime = nTime;
  262. g_pDmElementFramework->BeginEdit();
  263. AppPumpMessages();
  264. pRenderContext->Viewport( 0, 0, GetWindowWidth(), GetWindowHeight() );
  265. vgui::GetAnimationController()->UpdateAnimations( Sys_FloatTime() );
  266. g_pMaterialSystem->BeginFrame( 0 );
  267. pRenderContext->ClearColor4ub( 76, 88, 68, 255 );
  268. pRenderContext->ClearBuffers( true, true );
  269. g_pVGui->RunFrame();
  270. g_pVGuiSurface->PaintTraverseEx( root, true );
  271. g_pMaterialSystem->EndFrame();
  272. g_pMaterialSystem->SwapBuffers();
  273. }
  274. delete mainPanel;
  275. g_pMaterialSystem->ModShutdown();
  276. return 1;
  277. }