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.

356 lines
8.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //===========================================================================//
  6. #include "cbase.h"
  7. #include "toolframework_client.h"
  8. #include "igamesystem.h"
  9. #include "tier1/KeyValues.h"
  10. #include "toolframework/iclientenginetools.h"
  11. #include "client_factorylist.h"
  12. #include "iviewrender.h"
  13. #include "materialsystem/imaterialvar.h"
  14. extern IViewRender *view;
  15. class CToolFrameworkClient : public CBaseGameSystemPerFrame
  16. {
  17. public:
  18. // Methods of IGameSystem
  19. virtual bool Init();
  20. virtual void LevelInitPreEntity();
  21. virtual void LevelInitPostEntity();
  22. virtual void LevelShutdownPreEntity();
  23. virtual void LevelShutdownPostEntity();
  24. virtual void PreRender();
  25. virtual void PostRender();
  26. public:
  27. // Other public methods
  28. void PostToolMessage( HTOOLHANDLE hEntity, KeyValues *msg );
  29. void AdjustEngineViewport( int& x, int& y, int& width, int& height );
  30. bool SetupEngineView( Vector &origin, QAngle &angles, float &fov );
  31. bool SetupAudioState( AudioState_t &audioState );
  32. bool IsThirdPersonCamera();
  33. IClientEngineTools *m_pTools;
  34. };
  35. //-----------------------------------------------------------------------------
  36. // Singleton
  37. //-----------------------------------------------------------------------------
  38. static CToolFrameworkClient g_ToolFrameworkClient;
  39. #ifndef NO_TOOLFRAMEWORK
  40. bool ToolsEnabled()
  41. {
  42. return g_ToolFrameworkClient.m_pTools && g_ToolFrameworkClient.m_pTools->InToolMode();
  43. }
  44. #endif
  45. IGameSystem *ToolFrameworkClientSystem()
  46. {
  47. return &g_ToolFrameworkClient;
  48. }
  49. bool CToolFrameworkClient::Init()
  50. {
  51. factorylist_t list;
  52. FactoryList_Retrieve( list );
  53. m_pTools = ( IClientEngineTools * )list.appSystemFactory( VCLIENTENGINETOOLS_INTERFACE_VERSION, NULL );
  54. return ( m_pTools != NULL );
  55. }
  56. void CToolFrameworkClient::LevelInitPreEntity()
  57. {
  58. if ( m_pTools )
  59. {
  60. m_pTools->LevelInitPreEntityAllTools();
  61. }
  62. }
  63. void CToolFrameworkClient::LevelInitPostEntity()
  64. {
  65. if ( m_pTools )
  66. {
  67. m_pTools->LevelInitPostEntityAllTools();
  68. }
  69. }
  70. void CToolFrameworkClient::LevelShutdownPreEntity()
  71. {
  72. if ( m_pTools )
  73. {
  74. m_pTools->LevelShutdownPreEntityAllTools();
  75. }
  76. }
  77. void CToolFrameworkClient::LevelShutdownPostEntity()
  78. {
  79. if ( m_pTools )
  80. {
  81. m_pTools->LevelShutdownPostEntityAllTools();
  82. }
  83. }
  84. void CToolFrameworkClient::PreRender()
  85. {
  86. if ( m_pTools )
  87. {
  88. m_pTools->PreRenderAllTools();
  89. }
  90. }
  91. void CToolFrameworkClient::PostRender()
  92. {
  93. if ( m_pTools )
  94. {
  95. m_pTools->PostRenderAllTools();
  96. }
  97. }
  98. //-----------------------------------------------------------------------------
  99. // Should we render with a 3rd person camera?
  100. //-----------------------------------------------------------------------------
  101. bool CToolFrameworkClient::IsThirdPersonCamera()
  102. {
  103. if ( !m_pTools )
  104. return false;
  105. return m_pTools->IsThirdPersonCamera( );
  106. }
  107. bool ToolFramework_IsThirdPersonCamera( )
  108. {
  109. return g_ToolFrameworkClient.IsThirdPersonCamera( );
  110. }
  111. //-----------------------------------------------------------------------------
  112. // Posts a message to all tools
  113. //-----------------------------------------------------------------------------
  114. void CToolFrameworkClient::PostToolMessage( HTOOLHANDLE hEntity, KeyValues *msg )
  115. {
  116. if ( m_pTools )
  117. {
  118. m_pTools->PostToolMessage( hEntity, msg );
  119. }
  120. }
  121. void ToolFramework_PostToolMessage( HTOOLHANDLE hEntity, KeyValues *msg )
  122. {
  123. g_ToolFrameworkClient.PostToolMessage( hEntity, msg );
  124. }
  125. //-----------------------------------------------------------------------------
  126. // View manipulation
  127. //-----------------------------------------------------------------------------
  128. void CToolFrameworkClient::AdjustEngineViewport( int& x, int& y, int& width, int& height )
  129. {
  130. if ( m_pTools )
  131. {
  132. m_pTools->AdjustEngineViewport( x, y, width, height );
  133. }
  134. }
  135. void ToolFramework_AdjustEngineViewport( int& x, int& y, int& width, int& height )
  136. {
  137. g_ToolFrameworkClient.AdjustEngineViewport( x, y, width, height );
  138. }
  139. //-----------------------------------------------------------------------------
  140. // View manipulation
  141. //-----------------------------------------------------------------------------
  142. bool CToolFrameworkClient::SetupEngineView( Vector &origin, QAngle &angles, float &fov )
  143. {
  144. if ( !m_pTools )
  145. return false;
  146. return m_pTools->SetupEngineView( origin, angles, fov );
  147. }
  148. bool ToolFramework_SetupEngineView( Vector &origin, QAngle &angles, float &fov )
  149. {
  150. return g_ToolFrameworkClient.SetupEngineView( origin, angles, fov );
  151. }
  152. //-----------------------------------------------------------------------------
  153. // microphone manipulation
  154. //-----------------------------------------------------------------------------
  155. bool CToolFrameworkClient::SetupAudioState( AudioState_t &audioState )
  156. {
  157. if ( !m_pTools )
  158. return false;
  159. return m_pTools->SetupAudioState( audioState );
  160. }
  161. bool ToolFramework_SetupAudioState( AudioState_t &audioState )
  162. {
  163. return g_ToolFrameworkClient.SetupAudioState( audioState );
  164. }
  165. //-----------------------------------------------------------------------------
  166. // Helper class to indicate ownership of effects
  167. //-----------------------------------------------------------------------------
  168. CRecordEffectOwner::CRecordEffectOwner( C_BaseEntity *pEntity, bool bIsViewModel )
  169. {
  170. m_bToolsEnabled = ToolsEnabled() && clienttools->IsInRecordingMode();
  171. if ( m_bToolsEnabled )
  172. {
  173. KeyValues *msg = new KeyValues( "EffectsOwner" );
  174. msg->SetInt( "viewModel", bIsViewModel );
  175. ToolFramework_PostToolMessage( pEntity ? pEntity->GetToolHandle() : HTOOLHANDLE_INVALID, msg );
  176. msg->deleteThis();
  177. }
  178. }
  179. CRecordEffectOwner::~CRecordEffectOwner()
  180. {
  181. if ( m_bToolsEnabled )
  182. {
  183. KeyValues *msg = new KeyValues( "EffectsOwner" );
  184. ToolFramework_PostToolMessage( HTOOLHANDLE_INVALID, msg );
  185. msg->deleteThis();
  186. }
  187. }
  188. //-----------------------------------------------------------------------------
  189. // material recording - primarily for proxy materials
  190. //-----------------------------------------------------------------------------
  191. void WriteFloat( char *&buf, float f)
  192. {
  193. *( float* )buf = f;
  194. buf += sizeof( float );
  195. }
  196. void WriteInt( char *&buf, int i )
  197. {
  198. *( int* )buf = i;
  199. buf += sizeof( int );
  200. }
  201. void WritePtr( char *&buf, void *p )
  202. {
  203. *( void** )buf = p;
  204. buf += sizeof( void* );
  205. }
  206. void ToolFramework_RecordMaterialParams( IMaterial *pMaterial )
  207. {
  208. Assert( pMaterial );
  209. if ( !pMaterial )
  210. return;
  211. if ( !clienttools->IsInRecordingMode() )
  212. return;
  213. C_BaseEntity *pEnt = view->GetCurrentlyDrawingEntity();
  214. if ( !pEnt || !pEnt->IsToolRecording() )
  215. return;
  216. KeyValues *msg = new KeyValues( "material_proxy_state" );
  217. msg->SetString( "mtlName", pMaterial->GetName() );
  218. msg->SetString( "groupName", pMaterial->GetTextureGroupName() );
  219. int nParams = pMaterial->ShaderParamCount();
  220. IMaterialVar **pParams = pMaterial->GetShaderParams();
  221. char str[ 256 ];
  222. for ( int i = 0; i < nParams; ++i )
  223. {
  224. IMaterialVar *pVar = pParams[ i ];
  225. const char *pVarName = pVar->GetName();
  226. MaterialVarType_t vartype = pVar->GetType();
  227. switch ( vartype )
  228. {
  229. case MATERIAL_VAR_TYPE_FLOAT:
  230. msg->SetFloat( pVarName, pVar->GetFloatValue() );
  231. break;
  232. case MATERIAL_VAR_TYPE_INT:
  233. msg->SetInt( pVarName, pVar->GetIntValue() );
  234. break;
  235. case MATERIAL_VAR_TYPE_STRING:
  236. msg->SetString( pVarName, pVar->GetStringValue() );
  237. break;
  238. case MATERIAL_VAR_TYPE_FOURCC:
  239. Assert( 0 ); // JDTODO
  240. break;
  241. case MATERIAL_VAR_TYPE_VECTOR:
  242. {
  243. const float *pVal = pVar->GetVecValue();
  244. int dim = pVar->VectorSize();
  245. switch ( dim )
  246. {
  247. case 2:
  248. V_snprintf( str, sizeof( str ), "vector2d: %f %f", pVal[ 0 ], pVal[ 1 ] );
  249. break;
  250. case 3:
  251. V_snprintf( str, sizeof( str ), "vector3d: %f %f %f", pVal[ 0 ], pVal[ 1 ], pVal[ 2 ] );
  252. break;
  253. case 4:
  254. V_snprintf( str, sizeof( str ), "vector4d: %f %f %f %f", pVal[ 0 ], pVal[ 1 ], pVal[ 2 ], pVal[ 3 ] );
  255. break;
  256. default:
  257. Assert( 0 );
  258. *str = 0;
  259. }
  260. msg->SetString( pVarName, str );
  261. }
  262. break;
  263. case MATERIAL_VAR_TYPE_MATRIX:
  264. {
  265. const VMatrix &matrix = pVar->GetMatrixValue();
  266. const float *pVal = matrix.Base();
  267. V_snprintf( str, sizeof( str ),
  268. "matrix: %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f",
  269. pVal[ 0 ], pVal[ 1 ], pVal[ 2 ], pVal[ 3 ],
  270. pVal[ 4 ], pVal[ 5 ], pVal[ 6 ], pVal[ 7 ],
  271. pVal[ 8 ], pVal[ 9 ], pVal[ 10 ], pVal[ 11 ],
  272. pVal[ 12 ], pVal[ 13 ], pVal[ 14 ], pVal[ 15 ] );
  273. msg->SetString( pVarName, str );
  274. }
  275. break;
  276. case MATERIAL_VAR_TYPE_TEXTURE:
  277. // V_snprintf( str, sizeof( str ), "texture: %x", pVar->GetTextureValue() );
  278. // msg->SetString( pVarName, str );
  279. break;
  280. case MATERIAL_VAR_TYPE_MATERIAL:
  281. // V_snprintf( str, sizeof( str ), "material: %x", pVar->GetMaterialValue() );
  282. // msg->SetString( pVarName, str );
  283. break;
  284. case MATERIAL_VAR_TYPE_UNDEFINED:
  285. // Assert( 0 ); // these appear to be (mostly? all?) textures, although I don't know why they're not caught by the texture case above...
  286. break; // JDTODO
  287. default:
  288. Assert( 0 );
  289. }
  290. }
  291. Assert( pEnt->GetToolHandle() );
  292. ToolFramework_PostToolMessage( pEnt->GetToolHandle(), msg );
  293. msg->deleteThis();
  294. }