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.

339 lines
11 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #include "dmecommentarynodeentity.h"
  7. #include "datamodel/dmelementfactoryhelper.h"
  8. #include "toolframework/itoolentity.h"
  9. #include "materialsystem/imesh.h"
  10. #include "materialsystem/imaterial.h"
  11. #include "materialsystem/imaterialsystem.h"
  12. #include "engine/iclientleafsystem.h"
  13. #include "toolutils/enginetools_int.h"
  14. #include "commedittool.h"
  15. #include "KeyValues.h"
  16. // memdbgon must be the last include file in a .cpp file!!!
  17. #include "tier0/memdbgon.h"
  18. #define SPHERE_RADIUS 16
  19. //-----------------------------------------------------------------------------
  20. // Expose this class to the scene database
  21. //-----------------------------------------------------------------------------
  22. IMPLEMENT_ELEMENT_FACTORY( DmeCommentaryNodeEntity, CDmeCommentaryNodeEntity );
  23. //-----------------------------------------------------------------------------
  24. // Purpose:
  25. //-----------------------------------------------------------------------------
  26. void CDmeCommentaryNodeEntity::OnConstruction()
  27. {
  28. m_ClassName.InitAndSet( this, "classname", false, FATTRIB_HAS_CALLBACK );
  29. m_TargetName.Init( this, "targetname" );
  30. m_bIsPlaceholder.InitAndSet( this, "_placeholder", false, FATTRIB_DONTSAVE );
  31. m_vecLocalOrigin.Init( this, "origin" );
  32. m_vecLocalAngles.Init( this, "angles" );
  33. // Used to make sure these aren't saved if they aren't changed
  34. m_TargetName.GetAttribute()->AddFlag( FATTRIB_DONTSAVE | FATTRIB_HAS_CALLBACK );
  35. m_vecLocalAngles.GetAttribute()->AddFlag( FATTRIB_DONTSAVE | FATTRIB_HAS_CALLBACK );
  36. m_bInfoTarget = false;
  37. m_bIsDirty = false;
  38. m_hEngineEntity = HTOOLHANDLE_INVALID;
  39. KeyValues *pVMTKeyValues = new KeyValues( "UnlitGeneric" );
  40. pVMTKeyValues->SetString( "$basetexture", "editor/info_target" );
  41. pVMTKeyValues->SetInt( "$nocull", 1 );
  42. pVMTKeyValues->SetInt( "$vertexcolor", 1 );
  43. pVMTKeyValues->SetInt( "$vertexalpha", 1 );
  44. pVMTKeyValues->SetInt( "$no_fullbright", 1 );
  45. pVMTKeyValues->SetInt( "$translucent", 1 );
  46. m_InfoTargetSprite.Init( "__commentary_info_target", pVMTKeyValues );
  47. pVMTKeyValues = new KeyValues( "UnlitGeneric" );
  48. pVMTKeyValues->SetInt( "$nocull", 1 );
  49. pVMTKeyValues->SetString( "$color", "{255 0 0}" );
  50. pVMTKeyValues->SetInt( "$vertexalpha", 1 );
  51. pVMTKeyValues->SetInt( "$no_fullbright", 1 );
  52. pVMTKeyValues->SetInt( "$additive", 1 );
  53. m_SelectedInfoTarget.Init( "__selected_commentary_info_target", pVMTKeyValues );
  54. }
  55. void CDmeCommentaryNodeEntity::OnDestruction()
  56. {
  57. // Unhook it from the engine
  58. AttachToEngineEntity( HTOOLHANDLE_INVALID );
  59. m_SelectedInfoTarget.Shutdown();
  60. m_InfoTargetSprite.Shutdown();
  61. }
  62. //-----------------------------------------------------------------------------
  63. // Called whem attributes change
  64. //-----------------------------------------------------------------------------
  65. void CDmeCommentaryNodeEntity::OnAttributeChanged( CDmAttribute *pAttribute )
  66. {
  67. BaseClass::OnAttributeChanged( pAttribute );
  68. // Once these have changed, then save them out, and don't bother calling back
  69. if ( pAttribute == m_TargetName.GetAttribute() ||
  70. pAttribute == m_vecLocalAngles.GetAttribute() )
  71. {
  72. pAttribute->RemoveFlag( FATTRIB_DONTSAVE | FATTRIB_HAS_CALLBACK );
  73. return;
  74. }
  75. if ( pAttribute == m_ClassName.GetAttribute() )
  76. {
  77. m_bInfoTarget = !Q_strncmp( m_ClassName, "info_target", 11 );
  78. if ( !Q_stricmp( m_ClassName, "point_commentary_node" ) )
  79. {
  80. SetModelName( "models/extras/info_speech.mdl" );
  81. GetMDL()->m_flPlaybackRate = 0.0f;
  82. }
  83. else
  84. {
  85. SetModelName( NULL );
  86. }
  87. return;
  88. }
  89. }
  90. //-----------------------------------------------------------------------------
  91. // Returns the entity ID
  92. //-----------------------------------------------------------------------------
  93. int CDmeCommentaryNodeEntity::GetEntityId() const
  94. {
  95. return atoi( GetName() );
  96. }
  97. //-----------------------------------------------------------------------------
  98. // Mark the entity as being dirty
  99. //-----------------------------------------------------------------------------
  100. void CDmeCommentaryNodeEntity::MarkDirty( bool bDirty )
  101. {
  102. m_bIsDirty = bDirty;
  103. }
  104. //-----------------------------------------------------------------------------
  105. // Is the renderable transparent?
  106. //-----------------------------------------------------------------------------
  107. bool CDmeCommentaryNodeEntity::IsTransparent( void )
  108. {
  109. return m_bIsDirty || m_bInfoTarget || BaseClass::IsTransparent();
  110. }
  111. //-----------------------------------------------------------------------------
  112. // Entity Key iteration
  113. //-----------------------------------------------------------------------------
  114. bool CDmeCommentaryNodeEntity::IsEntityKey( CDmAttribute *pEntityKey )
  115. {
  116. return pEntityKey->IsFlagSet( FATTRIB_USERDEFINED );
  117. }
  118. //-----------------------------------------------------------------------------
  119. // Purpose:
  120. //-----------------------------------------------------------------------------
  121. CDmAttribute *CDmeCommentaryNodeEntity::FirstEntityKey()
  122. {
  123. for ( CDmAttribute *pAttribute = FirstAttribute(); pAttribute; pAttribute = pAttribute->NextAttribute() )
  124. {
  125. if ( IsEntityKey( pAttribute ) )
  126. return pAttribute;
  127. }
  128. return NULL;
  129. }
  130. //-----------------------------------------------------------------------------
  131. // Purpose:
  132. //-----------------------------------------------------------------------------
  133. CDmAttribute *CDmeCommentaryNodeEntity::NextEntityKey( CDmAttribute *pEntityKey )
  134. {
  135. if ( !pEntityKey )
  136. return NULL;
  137. for ( CDmAttribute *pAttribute = pEntityKey->NextAttribute(); pAttribute; pAttribute = pAttribute->NextAttribute() )
  138. {
  139. if ( IsEntityKey( pAttribute ) )
  140. return pAttribute;
  141. }
  142. return NULL;
  143. }
  144. //-----------------------------------------------------------------------------
  145. // Attach/detach from an engine entity with the same editor index
  146. //-----------------------------------------------------------------------------
  147. void CDmeCommentaryNodeEntity::AttachToEngineEntity( HTOOLHANDLE hToolHandle )
  148. {
  149. if ( m_hEngineEntity != HTOOLHANDLE_INVALID )
  150. {
  151. clienttools->SetEnabled( m_hEngineEntity, true );
  152. }
  153. m_hEngineEntity = hToolHandle;
  154. if ( m_hEngineEntity != HTOOLHANDLE_INVALID )
  155. {
  156. clienttools->SetEnabled( m_hEngineEntity, false );
  157. }
  158. }
  159. //-----------------------------------------------------------------------------
  160. // Position and bounds for the model
  161. //-----------------------------------------------------------------------------
  162. const Vector &CDmeCommentaryNodeEntity::GetRenderOrigin( void )
  163. {
  164. return m_vecLocalOrigin;
  165. }
  166. const QAngle &CDmeCommentaryNodeEntity::GetRenderAngles( void )
  167. {
  168. return *(QAngle*)(&m_vecLocalAngles.Get());
  169. }
  170. //-----------------------------------------------------------------------------
  171. // Draws the helper for the entity
  172. //-----------------------------------------------------------------------------
  173. void CDmeCommentaryNodeEntity::DrawSprite( IMaterial *pMaterial )
  174. {
  175. float t = 0.5f * sin( Plat_FloatTime() * M_PI / 1.0f ) + 0.5f;
  176. CMatRenderContextPtr pRenderContext( materials );
  177. pRenderContext->Bind( pMaterial );
  178. IMesh* pMesh = pRenderContext->GetDynamicMesh();
  179. CMeshBuilder meshBuilder;
  180. meshBuilder.Begin( pMesh, MATERIAL_TRIANGLE_STRIP, 4, 4 );
  181. unsigned char nBaseR = 255;
  182. unsigned char nBaseG = 255;
  183. unsigned char nBaseB = 255;
  184. unsigned char nAlpha = m_bIsDirty ? (unsigned char)(255 * t) : 255;
  185. meshBuilder.Position3f( -SPHERE_RADIUS, -SPHERE_RADIUS, 0.0f );
  186. meshBuilder.Color4ub( nBaseR, nBaseG, nBaseB, nAlpha );
  187. meshBuilder.TexCoord2f( 0, 0.0f, 1.0f );
  188. meshBuilder.AdvanceVertex();
  189. meshBuilder.Position3f( SPHERE_RADIUS, -SPHERE_RADIUS, 0.0f );
  190. meshBuilder.Color4ub( nBaseR, nBaseG, nBaseB, nAlpha );
  191. meshBuilder.TexCoord2f( 0, 1.0f, 1.0f );
  192. meshBuilder.AdvanceVertex();
  193. meshBuilder.Position3f( SPHERE_RADIUS, SPHERE_RADIUS, 0.0f );
  194. meshBuilder.Color4ub( nBaseR, nBaseG, nBaseB, nAlpha );
  195. meshBuilder.TexCoord2f( 0, 1.0f, 0.0f );
  196. meshBuilder.AdvanceVertex();
  197. meshBuilder.Position3f( -SPHERE_RADIUS, SPHERE_RADIUS, 0.0f );
  198. meshBuilder.Color4ub( nBaseR, nBaseG, nBaseB, nAlpha );
  199. meshBuilder.TexCoord2f( 0, 0.0f, 0.0f );
  200. meshBuilder.AdvanceVertex();
  201. meshBuilder.FastIndex( 0 );
  202. meshBuilder.FastIndex( 1 );
  203. meshBuilder.FastIndex( 3 );
  204. meshBuilder.FastIndex( 2 );
  205. meshBuilder.End();
  206. pMesh->Draw();
  207. }
  208. //-----------------------------------------------------------------------------
  209. // Draws the helper for the entity
  210. //-----------------------------------------------------------------------------
  211. int CDmeCommentaryNodeEntity::DrawModel( int flags )
  212. {
  213. bool bSelected = ( g_pCommEditTool->GetCurrentEntity().Get() == this );
  214. if ( !m_bInfoTarget )
  215. {
  216. // If we have a visible engine entity, we don't need to draw it here
  217. // info targets always draw though, because they have no visible model.
  218. CDisableUndoScopeGuard guard;
  219. float t = 0.5f * sin( Plat_FloatTime() * M_PI / 1.0f ) + 0.5f;
  220. unsigned char nAlpha = m_bIsDirty ? (unsigned char)(255 * t) : 255;
  221. if ( bSelected )
  222. {
  223. GetMDL()->m_Color.SetColor( 255, 64, 64, nAlpha );
  224. }
  225. else
  226. {
  227. GetMDL()->m_Color.SetColor( 255, 255, 255, nAlpha );
  228. }
  229. return BaseClass::DrawModel( flags );
  230. }
  231. Assert( IsDrawingInEngine() );
  232. CMatRenderContextPtr pRenderContext( materials );
  233. matrix3x4_t mat;
  234. VMatrix worldToCamera, cameraToWorld;
  235. pRenderContext->GetMatrix( MATERIAL_VIEW, &worldToCamera );
  236. MatrixInverseTR( worldToCamera, cameraToWorld );
  237. MatrixCopy( cameraToWorld.As3x4(), mat );
  238. MatrixSetColumn( m_vecLocalOrigin, 3, mat );
  239. pRenderContext->MatrixMode( MATERIAL_MODEL );
  240. pRenderContext->PushMatrix();
  241. pRenderContext->LoadMatrix( mat );
  242. pRenderContext->FogMode( MATERIAL_FOG_NONE );
  243. pRenderContext->SetNumBoneWeights( 0 );
  244. pRenderContext->CullMode( MATERIAL_CULLMODE_CW );
  245. DrawSprite( m_InfoTargetSprite );
  246. if ( bSelected )
  247. {
  248. DrawSprite( m_SelectedInfoTarget );
  249. }
  250. pRenderContext->CullMode( MATERIAL_CULLMODE_CCW );
  251. pRenderContext->MatrixMode( MATERIAL_MODEL );
  252. pRenderContext->PopMatrix();
  253. return 1;
  254. }
  255. //-----------------------------------------------------------------------------
  256. // Position and bounds for the model
  257. //-----------------------------------------------------------------------------
  258. void CDmeCommentaryNodeEntity::GetRenderBounds( Vector& mins, Vector& maxs )
  259. {
  260. if ( !m_bInfoTarget )
  261. {
  262. BaseClass::GetRenderBounds( mins, maxs );
  263. return;
  264. }
  265. mins.Init( -SPHERE_RADIUS, -SPHERE_RADIUS, -SPHERE_RADIUS );
  266. maxs.Init( SPHERE_RADIUS, SPHERE_RADIUS, SPHERE_RADIUS );
  267. }
  268. //-----------------------------------------------------------------------------
  269. // Update renderable position
  270. //-----------------------------------------------------------------------------
  271. void CDmeCommentaryNodeEntity::SetRenderOrigin( const Vector &vecOrigin )
  272. {
  273. m_vecLocalOrigin = vecOrigin;
  274. clienttools->MarkClientRenderableDirty( this );
  275. }
  276. void CDmeCommentaryNodeEntity::SetRenderAngles( const QAngle &angles )
  277. {
  278. m_vecLocalAngles = *(Vector*)&angles;
  279. clienttools->MarkClientRenderableDirty( this );
  280. }