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.

544 lines
18 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #ifndef DMECLIP_H
  7. #define DMECLIP_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "datamodel/dmelement.h"
  12. #include "datamodel/dmattribute.h"
  13. #include "datamodel/dmattributevar.h"
  14. #include "datamodel/dmehandle.h"
  15. #include "video/ivideoservices.h"
  16. #include "materialsystem/MaterialSystemUtil.h"
  17. #include "tier1/utlmap.h"
  18. #include "movieobjects/timeutils.h"
  19. //-----------------------------------------------------------------------------
  20. // Forward declarations
  21. //-----------------------------------------------------------------------------
  22. class CDmeClip;
  23. class CDmeTimeFrame;
  24. class CDmeBookmark;
  25. class CDmeSound;
  26. class CDmeChannel;
  27. class CDmeCamera;
  28. class CDmeLight;
  29. class CDmeDag;
  30. class CDmeInput;
  31. class CDmeOperator;
  32. class CDmeMaterial;
  33. class CDmeTrack;
  34. class CDmeTrackGroup;
  35. class IMaterial;
  36. class CDmeChannelsClip;
  37. class CDmeAnimationSet;
  38. class CDmeMaterialOverlayFXClip;
  39. class DmeLog_TimeSelection_t;
  40. struct Rect_t;
  41. enum DmeClipSkipFlag_t
  42. {
  43. DMESKIP_NONE = 0,
  44. DMESKIP_MUTED = 1,
  45. DMESKIP_INVISIBLE = 2,
  46. };
  47. DEFINE_ENUM_BITWISE_OPERATORS( DmeClipSkipFlag_t )
  48. //-----------------------------------------------------------------------------
  49. // Clip types
  50. //-----------------------------------------------------------------------------
  51. enum DmeClipType_t
  52. {
  53. DMECLIP_UNKNOWN = -1,
  54. DMECLIP_FIRST = 0,
  55. DMECLIP_CHANNEL = 0,
  56. DMECLIP_SOUND,
  57. DMECLIP_FX,
  58. DMECLIP_FILM,
  59. DMECLIP_LAST = DMECLIP_FILM,
  60. DMECLIP_TYPE_COUNT
  61. };
  62. DEFINE_ENUM_INCREMENT_OPERATORS( DmeClipType_t )
  63. typedef CUtlVector< CDmeHandle< CDmeClip > > DmeClipStack_t;
  64. //-----------------------------------------------------------------------------
  65. // Is a particular clip type non-overlapping?
  66. //-----------------------------------------------------------------------------
  67. inline bool IsNonoverlapping( DmeClipType_t type )
  68. {
  69. return ( type == DMECLIP_FILM );
  70. }
  71. //-----------------------------------------------------------------------------
  72. // String to clip type + back
  73. //-----------------------------------------------------------------------------
  74. DmeClipType_t ClipTypeFromString( const char *pName );
  75. const char *ClipTypeToString( DmeClipType_t type );
  76. //-----------------------------------------------------------------------------
  77. // Used to move clips in non-film track groups with film clips
  78. //-----------------------------------------------------------------------------
  79. struct ClipAssociation_t
  80. {
  81. enum AssociationType_t
  82. {
  83. HAS_CLIP = 0,
  84. BEFORE_START,
  85. AFTER_END,
  86. NO_MOVEMENT,
  87. };
  88. AssociationType_t m_nType;
  89. CDmeHandle< CDmeClip > m_hClip;
  90. CDmeHandle< CDmeClip > m_hAssociation;
  91. DmeTime_t m_offset;
  92. };
  93. //-----------------------------------------------------------------------------
  94. // Constructor, destructor
  95. //-----------------------------------------------------------------------------
  96. class CDmeClip : public CDmElement
  97. {
  98. DEFINE_ELEMENT( CDmeClip, CDmElement );
  99. public:
  100. // Inherited from IDmElement
  101. virtual void OnAttributeArrayElementAdded( CDmAttribute *pAttribute, int nFirstElem, int nLastElem );
  102. virtual void OnAttributeArrayElementRemoved( CDmAttribute *pAttribute, int nFirstElem, int nLastElem );
  103. // Returns the time frame
  104. CDmeTimeFrame *GetTimeFrame() const;
  105. DmeTime_t ToChildMediaTime ( DmeTime_t t, bool bClamp = true ) const;
  106. DmeTime_t FromChildMediaTime( DmeTime_t t, bool bClamp = true ) const;
  107. DmeTime_t ToChildMediaDuration ( DmeTime_t dt ) const;
  108. DmeTime_t FromChildMediaDuration( DmeTime_t dt ) const;
  109. DmeTime_t GetStartTime() const;
  110. DmeTime_t GetEndTime() const;
  111. DmeTime_t GetDuration() const;
  112. DmeTime_t GetTimeOffset() const;
  113. DmeTime_t GetStartInChildMediaTime() const;
  114. DmeTime_t GetEndInChildMediaTime() const;
  115. float GetTimeScale() const;
  116. void SetStartTime ( DmeTime_t t );
  117. void SetDuration ( DmeTime_t t );
  118. void SetTimeOffset( DmeTime_t t );
  119. void SetTimeScale ( float s );
  120. // Given a root clip and a child (or grandchild) clip, builds the stack
  121. // from root on down to the destination clip. If shot is specified, then it
  122. // must build a clip stack that passes through the shot
  123. bool BuildClipStack( DmeClipStack_t* pStack, CDmeClip *pRoot, CDmeClip *pShot = NULL );
  124. // Clip stack versions of time conversion
  125. static DmeTime_t ToChildMediaTime ( const DmeClipStack_t& stack, DmeTime_t globalTime, bool bClamp = true );
  126. static DmeTime_t FromChildMediaTime ( const DmeClipStack_t& stack, DmeTime_t localTime, bool bClamp = true );
  127. static DmeTime_t ToChildMediaDuration ( const DmeClipStack_t& stack, DmeTime_t globalDuration );
  128. static DmeTime_t FromChildMediaDuration( const DmeClipStack_t& stack, DmeTime_t localDuration );
  129. static void ToChildMediaTime( DmeLog_TimeSelection_t &params, const DmeClipStack_t& stack );
  130. void SetClipColor( const Color& clr );
  131. Color GetClipColor() const;
  132. void SetClipText( const char *pText );
  133. const char* GetClipText() const;
  134. // Clip type
  135. virtual DmeClipType_t GetClipType() { return DMECLIP_UNKNOWN; }
  136. // Track group iteration methods
  137. int GetTrackGroupCount() const;
  138. CDmeTrackGroup *GetTrackGroup( int nIndex ) const;
  139. const CUtlVector< DmElementHandle_t > &GetTrackGroups( ) const;
  140. // Track group addition/removal
  141. void AddTrackGroup( CDmeTrackGroup *pTrackGroup );
  142. void AddTrackGroupBefore( CDmeTrackGroup *pTrackGroup, CDmeTrackGroup *pBefore );
  143. CDmeTrackGroup *AddTrackGroup( const char *pTrackGroupName );
  144. void RemoveTrackGroup( int nIndex );
  145. void RemoveTrackGroup( CDmeTrackGroup *pTrackGroup );
  146. void RemoveTrackGroup( const char *pTrackGroupName );
  147. // Track group finding
  148. CDmeTrackGroup *FindTrackGroup( const char *pTrackGroupName ) const;
  149. int GetTrackGroupIndex( CDmeTrackGroup *pTrack ) const;
  150. CDmeTrackGroup *FindOrAddTrackGroup( const char *pTrackGroupName );
  151. // Swap track groups
  152. void SwapOrder( CDmeTrackGroup *pTrackGroup1, CDmeTrackGroup *pTrackGroup2 );
  153. // Clip finding
  154. virtual CDmeTrack *FindTrackForClip( CDmeClip *pClip, CDmeTrackGroup **ppTrackGroup = NULL ) const;
  155. bool FindMultiTrackGroupForClip( CDmeClip *pClip, int *pTrackGroupIndex, int *pTrackIndex = NULL, int *pClipIndex = NULL ) const;
  156. // Finding clips in tracks by time
  157. virtual void FindClipsAtTime( DmeClipType_t clipType, DmeTime_t time, DmeClipSkipFlag_t flags, CUtlVector< CDmeClip * >& clips ) const;
  158. virtual void FindClipsWithinTime( DmeClipType_t clipType, DmeTime_t startTime, DmeTime_t endTime, DmeClipSkipFlag_t flags, CUtlVector< CDmeClip * >& clips ) const;
  159. // Is a particular clip typed able to be added?
  160. bool IsSubClipTypeAllowed( DmeClipType_t type ) const;
  161. // Returns the special film track group
  162. virtual CDmeTrackGroup *GetFilmTrackGroup() const { return NULL; }
  163. virtual CDmeTrack *GetFilmTrack() const { return NULL; }
  164. // Checks for muteness
  165. void SetMute( bool state );
  166. bool IsMute( ) const;
  167. protected:
  168. virtual int AllowedClipTypes() const { return 1 << DMECLIP_CHANNEL; }
  169. // Is a track group valid to add?
  170. bool IsTrackGroupValid( CDmeTrackGroup *pTrackGroup );
  171. CDmaElementArray< CDmeTrackGroup > m_TrackGroups;
  172. CDmaElement< CDmeTimeFrame > m_TimeFrame;
  173. CDmaVar< Color > m_ClipColor;
  174. CDmaVar< bool > m_bMute;
  175. CDmaString m_ClipText;
  176. };
  177. inline bool CDmeClip::IsSubClipTypeAllowed( DmeClipType_t type ) const
  178. {
  179. return ( AllowedClipTypes() & ( 1 << type ) ) != 0;
  180. }
  181. inline void CDmeClip::SetMute( bool state )
  182. {
  183. m_bMute = state;
  184. }
  185. inline bool CDmeClip::IsMute( ) const
  186. {
  187. return m_bMute;
  188. }
  189. //-----------------------------------------------------------------------------
  190. // Sound clip
  191. //-----------------------------------------------------------------------------
  192. class CDmeSoundClip : public CDmeClip
  193. {
  194. DEFINE_ELEMENT( CDmeSoundClip, CDmeClip );
  195. public:
  196. virtual DmeClipType_t GetClipType() { return DMECLIP_SOUND; }
  197. void SetShowWave( bool state );
  198. bool ShouldShowWave( ) const;
  199. CDmaElement< CDmeSound > m_Sound;
  200. CDmaVar< bool > m_bShowWave;
  201. };
  202. //-----------------------------------------------------------------------------
  203. // Clip containing recorded data from the game
  204. //-----------------------------------------------------------------------------
  205. class CDmeChannelsClip : public CDmeClip
  206. {
  207. DEFINE_ELEMENT( CDmeChannelsClip, CDmeClip );
  208. public:
  209. virtual DmeClipType_t GetClipType() { return DMECLIP_CHANNEL; }
  210. CDmeChannel *CreatePassThruConnection
  211. (
  212. char const *passThruName,
  213. CDmElement *pFrom,
  214. char const *pFromAttribute,
  215. CDmElement *pTo,
  216. char const *pToAttribute,
  217. int index = 0
  218. );
  219. void RemoveChannel( CDmeChannel *pChannel );
  220. CDmaElementArray< CDmeChannel > m_Channels;
  221. };
  222. //-----------------------------------------------------------------------------
  223. // An effect clip
  224. //-----------------------------------------------------------------------------
  225. class CDmeFXClip : public CDmeClip
  226. {
  227. DEFINE_ELEMENT( CDmeFXClip, CDmeClip );
  228. public:
  229. virtual DmeClipType_t GetClipType() { return DMECLIP_FX; }
  230. enum
  231. {
  232. MAX_FX_INPUT_TEXTURES = 2
  233. };
  234. // All effects must be able to apply their effect
  235. virtual void ApplyEffect( DmeTime_t time, Rect_t &currentRect, Rect_t &totalRect, ITexture *pTextures[MAX_FX_INPUT_TEXTURES] ) {}
  236. // Global list of FX clip types
  237. static void InstallFXClipType( const char *pElementType, const char *pDescription );
  238. static int FXClipTypeCount();
  239. static const char *FXClipType( int nIndex );
  240. static const char *FXClipDescription( int nIndex );
  241. private:
  242. enum
  243. {
  244. MAX_FXCLIP_TYPES = 16
  245. };
  246. static const char *s_pFXClipTypes[MAX_FXCLIP_TYPES];
  247. static const char *s_pFXClipDescriptions[MAX_FXCLIP_TYPES];
  248. static int s_nFXClipTypeCount;
  249. };
  250. //-----------------------------------------------------------------------------
  251. // Helper Template factory for simple creation of factories
  252. //-----------------------------------------------------------------------------
  253. template <class T>
  254. class CDmFXClipFactory : public CDmElementFactory<T>
  255. {
  256. public:
  257. CDmFXClipFactory( const char *pLookupName, const char *pDescription ) : CDmElementFactory<T>( pLookupName )
  258. {
  259. CDmeFXClip::InstallFXClipType( pLookupName, pDescription );
  260. }
  261. };
  262. //-----------------------------------------------------------------------------
  263. // All effects must use IMPLEMENT_FX_CLIP_ELEMENT_FACTORY instead of IMPLEMENT_ELEMENT_FACTORY
  264. //-----------------------------------------------------------------------------
  265. #if defined( MOVIEOBJECTS_LIB ) || defined ( DATAMODEL_LIB ) || defined ( DMECONTROLS_LIB )
  266. #define IMPLEMENT_FX_CLIP_ELEMENT_FACTORY( lookupName, className, description ) \
  267. IMPLEMENT_ELEMENT( className ) \
  268. CDmFXClipFactory< className > g_##className##_Factory( #lookupName, description ); \
  269. CDmElementFactoryHelper g_##className##_Helper( #lookupName, &g_##className##_Factory, true ); \
  270. className *g_##className##LinkerHack = NULL;
  271. #else
  272. #define IMPLEMENT_FX_CLIP_ELEMENT_FACTORY( lookupName, className, description ) \
  273. IMPLEMENT_ELEMENT( className ) \
  274. CDmFXClipFactory< className > g_##className##_Factory( #lookupName, description ); \
  275. CDmElementFactoryHelper g_##className##_Helper( #lookupName, &g_##className##_Factory, false ); \
  276. className *g_##className##LinkerHack = NULL;
  277. #endif
  278. //-----------------------------------------------------------------------------
  279. // Film clip
  280. //-----------------------------------------------------------------------------
  281. class CDmeFilmClip : public CDmeClip
  282. {
  283. DEFINE_ELEMENT( CDmeFilmClip, CDmeClip );
  284. public:
  285. virtual DmeClipType_t GetClipType() { return DMECLIP_FILM; }
  286. // Attribute changed
  287. virtual void OnElementUnserialized( );
  288. virtual void PreAttributeChanged( CDmAttribute *pAttribute );
  289. virtual void OnAttributeChanged( CDmAttribute *pAttribute );
  290. // Resolve
  291. virtual void Resolve();
  292. // Returns the special film track group
  293. virtual CDmeTrackGroup *GetFilmTrackGroup() const;
  294. virtual CDmeTrack *GetFilmTrack() const;
  295. CDmeTrackGroup *FindOrCreateFilmTrackGroup();
  296. CDmeTrack *FindOrCreateFilmTrack();
  297. // Clip finding
  298. virtual CDmeTrack *FindTrackForClip( CDmeClip *pClip, CDmeTrackGroup **ppTrackGroup = NULL ) const;
  299. // Finding clips in tracks by time
  300. virtual void FindClipsAtTime( DmeClipType_t clipType, DmeTime_t time, DmeClipSkipFlag_t flags, CUtlVector< CDmeClip * >& clips ) const;
  301. virtual void FindClipsWithinTime( DmeClipType_t clipType, DmeTime_t startTime, DmeTime_t endTime, DmeClipSkipFlag_t flags, CUtlVector< CDmeClip * >& clips ) const;
  302. // mapname helper methods
  303. const char *GetMapName();
  304. void SetMapName( const char *pMapName );
  305. // Returns the camera associated with the clip
  306. CDmeCamera *GetCamera();
  307. void SetCamera( CDmeCamera *pCamera );
  308. // Audio volume
  309. void SetVolume( float state );
  310. float GetVolume() const;
  311. // Returns the monitor camera associated with the clip (for now, only 1 supported)
  312. CDmeCamera *GetMonitorCamera();
  313. void AddMonitorCamera( CDmeCamera *pCamera );
  314. void RemoveMonitorCamera( CDmeCamera *pCamera );
  315. void SelectMonitorCamera( CDmeCamera *pCamera );
  316. int FindMonitorCamera( CDmeCamera *pCamera );
  317. // Light helper methods
  318. int GetLightCount();
  319. CDmeLight *GetLight( int nIndex );
  320. void AddLight( CDmeLight *pLight );
  321. // Scene / Dag helper methods
  322. void SetScene( CDmeDag *pDag );
  323. CDmeDag *GetScene();
  324. // helper for inputs and operators
  325. int GetInputCount();
  326. CDmeInput *GetInput( int nIndex );
  327. void AddInput( CDmeInput *pInput );
  328. void RemoveAllInputs();
  329. void AddOperator( CDmeOperator *pOperator );
  330. void CollectOperators( CUtlVector< DmElementHandle_t > &operators );
  331. // Helper for overlays
  332. // FIXME: Change this to use CDmeMaterials
  333. IMaterial *GetOverlayMaterial();
  334. void SetOverlay( const char *pMaterialName );
  335. float GetOverlayAlpha();
  336. void SetOverlayAlpha( float alpha );
  337. void DrawOverlay( DmeTime_t time, Rect_t &currentRect, Rect_t &totalRect );
  338. bool HasOpaqueOverlay();
  339. // AVI tape out
  340. void UseCachedVersion( bool bUseCachedVersion );
  341. bool IsUsingCachedVersion() const;
  342. IVideoMaterial *GetCachedVideoMaterial();
  343. void SetCachedAVI( const char *pAVIFile );
  344. int GetAnimationSetCount();
  345. CDmeAnimationSet *GetAnimationSet( int idx );
  346. void AddAnimationSet( CDmeAnimationSet *element );
  347. void RemoveAllAnimationSets();
  348. CDmaElementArray< CDmElement > &GetAnimationSets(); // raw access to the array
  349. const CDmaElementArray< CDmElement > &GetAnimationSets() const;
  350. const CDmaElementArray< CDmeBookmark > &GetBookmarks() const;
  351. CDmaElementArray< CDmeBookmark > &GetBookmarks();
  352. void SetFadeTimes( DmeTime_t fadeIn, DmeTime_t fadeOut ) { m_fadeInDuration = fadeIn.GetTenthsOfMS(); m_fadeOutDuration = fadeOut.GetTenthsOfMS(); }
  353. void SetFadeInTime( DmeTime_t t ) { m_fadeInDuration = t.GetTenthsOfMS(); }
  354. void SetFadeOutTime( DmeTime_t t ) { m_fadeOutDuration = t.GetTenthsOfMS(); }
  355. DmeTime_t GetFadeInTime() const { return DmeTime_t( m_fadeInDuration.Get() ); }
  356. DmeTime_t GetFadeOutTime() const { return DmeTime_t( m_fadeOutDuration.Get() ); }
  357. // Used to move clips in non-film track groups with film clips
  358. // Call BuildClipAssociations before modifying the film track,
  359. // then UpdateAssociatedClips after modifying it.
  360. void BuildClipAssociations( CUtlVector< ClipAssociation_t > &association, bool bHandleGaps = true );
  361. void UpdateAssociatedClips( CUtlVector< ClipAssociation_t > &association );
  362. // Rolls associated clips so they remain in the same relative time
  363. void RollAssociatedClips( CDmeClip *pClip, CUtlVector< ClipAssociation_t > &association, DmeTime_t dt );
  364. // Shifts associated clips so they remain in the same relative time when pClip is scaled
  365. void ScaleAssociatedClips( CDmeClip *pClip, CUtlVector< ClipAssociation_t > &association, float ratio, DmeTime_t oldOffset );
  366. private:
  367. virtual int AllowedClipTypes() const { return (1 << DMECLIP_CHANNEL) | (1 << DMECLIP_SOUND) | (1 << DMECLIP_FX) | (1 << DMECLIP_FILM); }
  368. CDmaElement< CDmeTrackGroup > m_FilmTrackGroup;
  369. CDmaString m_MapName;
  370. CDmaElement < CDmeCamera > m_Camera;
  371. CDmaElementArray< CDmeCamera > m_MonitorCameras;
  372. CDmaVar< int > m_nActiveMonitor;
  373. CDmaElement < CDmeDag > m_Scene;
  374. CDmaElementArray< CDmeLight > m_Lights;
  375. CDmaElementArray< CDmeInput > m_Inputs;
  376. CDmaElementArray< CDmeOperator > m_Operators;
  377. CDmaString m_AVIFile;
  378. CDmaVar< int > m_fadeInDuration;
  379. CDmaVar< int > m_fadeOutDuration;
  380. CDmaElement< CDmeMaterialOverlayFXClip >m_MaterialOverlayEffect;
  381. CDmaVar< bool > m_bIsUsingCachedVersion;
  382. CDmaElementArray< CDmElement > m_AnimationSets; // "animationSets"
  383. CDmaElementArray< CDmeBookmark > m_Bookmarks;
  384. CDmaVar< float > m_Volume;
  385. IVideoMaterial *m_pCachedVersion;
  386. bool m_bReloadCachedVersion;
  387. CMaterialReference m_FadeMaterial;
  388. };
  389. //-----------------------------------------------------------------------------
  390. // Fast type conversions
  391. //-----------------------------------------------------------------------------
  392. inline bool IsFilmClip( CDmeClip *pClip )
  393. {
  394. return pClip && pClip->IsA( CDmeFilmClip::GetStaticTypeSymbol() );
  395. }
  396. //-----------------------------------------------------------------------------
  397. // Creates a slug clip
  398. //-----------------------------------------------------------------------------
  399. CDmeFilmClip *CreateSlugClip( const char *pClipName, DmeTime_t startTime, DmeTime_t endTime, DmFileId_t fileid );
  400. //-----------------------------------------------------------------------------
  401. // For use in template functions
  402. //-----------------------------------------------------------------------------
  403. template <class T>
  404. class CDmeClipInfo
  405. {
  406. public:
  407. static DmeClipType_t ClipType( ) { return DMECLIP_UNKNOWN; }
  408. };
  409. #define DECLARE_DMECLIP_TYPE( _className, _dmeClipType ) \
  410. template< > class CDmeClipInfo< _className > \
  411. { \
  412. public: \
  413. static DmeClipType_t ClipType() { return _dmeClipType; } \
  414. };
  415. DECLARE_DMECLIP_TYPE( CDmeSoundClip, DMECLIP_SOUND )
  416. DECLARE_DMECLIP_TYPE( CDmeChannelsClip, DMECLIP_CHANNEL )
  417. DECLARE_DMECLIP_TYPE( CDmeFXClip, DMECLIP_FX )
  418. DECLARE_DMECLIP_TYPE( CDmeFilmClip, DMECLIP_FILM )
  419. #define DMECLIP_TYPE( _className ) CDmeClipInfo<T>::ClipType()
  420. //-----------------------------------------------------------------------------
  421. // helper methods
  422. //-----------------------------------------------------------------------------
  423. CDmeTrack *GetParentTrack( CDmeClip *pClip );
  424. CDmeChannel *FindChannelTargetingElement( CDmeChannelsClip *pChannelsClip, CDmElement *pElement, const char *pAttributeName = NULL );
  425. CDmeChannel *FindChannelTargetingElement( CDmeFilmClip *pClip, CDmElement *pElement, const char *pAttributeName, CDmeChannelsClip **ppChannelsClip, CDmeTrack **ppTrack = NULL, CDmeTrackGroup **ppTrackGroup = NULL );
  426. #endif // DMECLIP_H