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.

283 lines
8.0 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // The channel class - connects elements together, and allows for logging of data
  4. //
  5. //=============================================================================
  6. #ifndef DMECHANNEL_H
  7. #define DMECHANNEL_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "movieobjects/dmeoperator.h"
  12. #include "movieobjects/dmelog.h"
  13. #include "movieobjects/dmeclip.h"
  14. #include "movieobjects/proceduralpresets.h"
  15. #include "datamodel/idatamodel.h"
  16. #include "datamodel/dmehandle.h"
  17. //-----------------------------------------------------------------------------
  18. // Forward declarations
  19. //-----------------------------------------------------------------------------
  20. class CDmeClip;
  21. class CDmeChannel;
  22. //-----------------------------------------------------------------------------
  23. // different channel modes of operation
  24. //-----------------------------------------------------------------------------
  25. enum ChannelMode_t
  26. {
  27. CM_OFF,
  28. CM_PASS,
  29. CM_RECORD,
  30. CM_PLAY,
  31. };
  32. enum PlayMode_t
  33. {
  34. PM_HOLD,
  35. PM_LOOP,
  36. };
  37. //-----------------------------------------------------------------------------
  38. // A class managing channel recording
  39. //-----------------------------------------------------------------------------
  40. class CDmeChannelRecordingMgr
  41. {
  42. public:
  43. // constructor
  44. CDmeChannelRecordingMgr();
  45. // Activates, deactivates layer recording.
  46. void StartLayerRecording( const char *pUndoRedoDesc, const DmeLog_TimeSelection_t *pTimeSelection = NULL );
  47. void FinishLayerRecording( float flThreshold, bool bFlattenLayers = true );
  48. // Adds a channel to the recording layer
  49. void AddChannelToRecordingLayer( CDmeChannel *pChannel, CDmeClip *pRoot = NULL, CDmeClip *pShot = NULL );
  50. // Used to iterate over all channels currently being recorded
  51. // NOTE: Use CDmeChannel::AddToRecordingLayer to add a channel to the recording layer
  52. int GetLayerRecordingChannelCount();
  53. CDmeChannel* GetLayerRecordingChannel( int nIndex );
  54. // Computes time selection info in log time for a particular recorded channel
  55. // NOTE: Only valid if IsUsingTimeSelection() returns true
  56. void GetLocalTimeSelection( DmeLog_TimeSelection_t& selection, int nIndex );
  57. // Methods which control various aspects of recording
  58. void UpdateTimeAdvancing( bool bPaused, DmeTime_t tCurTime );
  59. void UpdateRecordingTimeSelectionTimes( const DmeLog_TimeSelection_t& timeSelection );
  60. void SetIntensityOnAllLayers( float flIntensity );
  61. void SetRecordingMode( RecordingMode_t mode );
  62. void SetPresetValue( CDmeChannel* pChannel, CDmAttribute *pPresetValue );
  63. void SetProceduralTarget( int nProceduralMode, const CDmAttribute *pTarget );
  64. void SetProceduralTarget( int nProceduralMode, const CUtlVector< KeyValues * >& list );
  65. int GetProceduralType() const;
  66. const CDmAttribute *GetProceduralTarget() const;
  67. const CUtlVector< KeyValues * > &GetPasteTarget() const;
  68. // Methods to query aspects of recording
  69. bool IsTimeAdvancing() const;
  70. bool IsUsingDetachedTimeSelection() const;
  71. bool IsUsingTimeSelection() const;
  72. private:
  73. struct LayerChannelInfo_t
  74. {
  75. LayerChannelInfo_t() : m_pPresetValue( 0 ) {}
  76. CDmeHandle< CDmeChannel > m_Channel;
  77. DmeClipStack_t m_ClipStack;
  78. CDmAttribute* m_pPresetValue;
  79. };
  80. // Methods available for CDmeChannel
  81. bool ShouldRecordUsingTimeSelection() const;
  82. // Internal methods
  83. void FlattenLayers( float flThreshhold );
  84. void RemoveAllChannelsFromRecordingLayer( );
  85. bool m_bActive : 1;
  86. bool m_bSavedUndoState : 1;
  87. bool m_bUseTimeSelection : 1;
  88. CUtlVector< LayerChannelInfo_t > m_LayerChannels;
  89. DmeLog_TimeSelection_t m_TimeSelection;
  90. int m_nRevealType;
  91. const CDmAttribute *m_pRevealTarget;
  92. CUtlVector< KeyValues * > m_PasteTarget;
  93. friend CDmeChannel;
  94. };
  95. // Singleton
  96. extern CDmeChannelRecordingMgr *g_pChannelRecordingMgr;
  97. //-----------------------------------------------------------------------------
  98. // A class representing a channel
  99. //-----------------------------------------------------------------------------
  100. class CDmeChannel : public CDmeOperator
  101. {
  102. DEFINE_ELEMENT( CDmeChannel, CDmeOperator );
  103. public:
  104. virtual bool IsDirty(); // ie needs to operate
  105. virtual void Operate();
  106. virtual void GetInputAttributes ( CUtlVector< CDmAttribute * > &attrs );
  107. virtual void GetOutputAttributes( CUtlVector< CDmAttribute * > &attrs );
  108. void SetInput ( CDmElement* pElement, const char* pAttribute, int index = 0 );
  109. void SetOutput( CDmElement* pElement, const char* pAttribute, int index = 0 );
  110. void SetInput( CDmAttribute *pAttribute, int index = 0 );
  111. void SetOutput( CDmAttribute *pAttribute, int index = 0 );
  112. CDmElement *GetFromElement() const;
  113. CDmElement *GetToElement() const;
  114. CDmAttribute *GetFromAttribute();
  115. CDmAttribute *GetToAttribute();
  116. int GetFromArrayIndex() const;
  117. int GetToArrayIndex() const;
  118. ChannelMode_t GetMode();
  119. void SetMode( ChannelMode_t mode );
  120. void ClearLog();
  121. CDmeLog *GetLog();
  122. void SetLog( CDmeLog *pLog );
  123. CDmeLog *CreateLog( DmAttributeType_t type );
  124. template < class T > CDmeTypedLog<T> *CreateLog();
  125. void ClearTimeMetric();
  126. void SetCurrentTime( DmeTime_t time, DmeTime_t start, DmeTime_t end );
  127. void SetCurrentTime( DmeTime_t time ); // Simple version. Only works if multiple active channels clips do not reference the same channels
  128. DmeTime_t GetCurrentTime() const;
  129. void SetChannelToPlayToSelf( const char *outputAttributeName, float defaultValue, bool force = false );
  130. // need this until we have the EditApply message queue
  131. void OnAttributeChanged( CDmAttribute *pAttribute );
  132. template< class T >
  133. bool GetCurrentPlaybackValue( T& value );
  134. template< class T >
  135. bool GetPlaybackValueAtTime( DmeTime_t time, T& value );
  136. void Play();
  137. void SetNextKeyCurveType( int nCurveType );
  138. // Builds a clip stack for the channel
  139. CDmeClip* FindOwnerClipForChannel( CDmeClip *pRoot );
  140. bool BuildClipStack( DmeClipStack_t *pClipStack, CDmeClip *pRoot, CDmeClip *pShot );
  141. protected:
  142. // Used to cache off handles to attributes
  143. CDmAttribute* SetupFromAttribute();
  144. CDmAttribute* SetupToAttribute();
  145. void Record();
  146. void Pass();
  147. CDmaElement< CDmElement > m_fromElement;
  148. CDmaString m_fromAttribute;
  149. CDmaVar< int > m_fromIndex;
  150. CDmaElement< CDmElement > m_toElement;
  151. CDmaString m_toAttribute;
  152. CDmaVar< int > m_toIndex;
  153. CDmaVar< int > m_mode;
  154. CDmaElement< CDmeLog > m_log;
  155. DmAttributeHandle_t m_FromAttributeHandle;
  156. DmAttributeHandle_t m_ToAttributeHandle;
  157. DmeTime_t m_timeOutsideTimeframe;
  158. DmeTime_t m_tCurrentTime;
  159. DmeTime_t m_tPreviousTime;
  160. int m_nRecordLayerIndex;
  161. int m_nNextCurveType;
  162. friend class CDmeChannelRecordingMgr;
  163. };
  164. //-----------------------------------------------------------------------------
  165. // Inline methods
  166. //-----------------------------------------------------------------------------
  167. template < class T >
  168. inline CDmeTypedLog<T> *CDmeChannel::CreateLog()
  169. {
  170. return CastElement< CDmeTypedLog<T> >( CreateLog( CDmAttributeInfo<T>::AttributeType() ) );
  171. }
  172. inline CDmAttribute *CDmeChannel::GetFromAttribute()
  173. {
  174. CDmAttribute *pAttribute = g_pDataModel->GetAttribute( m_FromAttributeHandle );
  175. if ( !pAttribute )
  176. {
  177. pAttribute = SetupFromAttribute();
  178. }
  179. return pAttribute;
  180. }
  181. inline CDmAttribute *CDmeChannel::GetToAttribute()
  182. {
  183. CDmAttribute *pAttribute = g_pDataModel->GetAttribute( m_ToAttributeHandle );
  184. if ( !pAttribute )
  185. {
  186. pAttribute = SetupToAttribute();
  187. }
  188. return pAttribute;
  189. }
  190. template< class T >
  191. inline bool CDmeChannel::GetPlaybackValueAtTime( DmeTime_t time, T& value )
  192. {
  193. CDmeTypedLog< T > *pLog = CastElement< CDmeTypedLog< T > >( GetLog() );
  194. if ( !pLog || pLog->IsEmpty() )
  195. return false;
  196. DmeTime_t t0 = pLog->GetBeginTime();
  197. DmeTime_t tn = pLog->GetEndTime();
  198. PlayMode_t pmode = PM_HOLD;
  199. switch ( pmode )
  200. {
  201. case PM_HOLD:
  202. time = clamp( time, t0, tn );
  203. break;
  204. case PM_LOOP:
  205. if ( tn == t0 )
  206. {
  207. time = t0;
  208. }
  209. else
  210. {
  211. time -= t0;
  212. time = time % ( tn - t0 );
  213. time += t0;
  214. }
  215. break;
  216. }
  217. value = pLog->GetValue( time );
  218. return true;
  219. }
  220. template< class T >
  221. inline bool CDmeChannel::GetCurrentPlaybackValue( T& value )
  222. {
  223. return GetPlaybackValueAtTime( GetCurrentTime(), value );
  224. }
  225. #endif // DMECHANNEL_H