Counter Strike : Global Offensive Source Code
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.

304 lines
13 KiB

  1. //====== Copyright � 1996-2008, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose: CSFMScriptMgr declaration, defines the interface of the sfm script
  4. // manager class which is used to run scripts and provides and interface for
  5. // script commands to perform operations.
  6. //
  7. //=============================================================================
  8. #ifndef SFMSCRIPTMGR_H
  9. #define SFMSCRIPTMGR_H
  10. #ifdef _WIN32
  11. #pragma once
  12. #endif
  13. #include "sfmobjects/sfmrigutils.h"
  14. #include "movieobjects/dmechannel.h"
  15. #include "tier1/UtlBuffer.h"
  16. #include "tier1/UtlStack.h"
  17. #include "tier1/fmtstr.h"
  18. // Forward declarations
  19. class CDmeDag;
  20. class CDmeClip;
  21. class CDmeFilmClip;
  22. class CDmeAnimationSet;
  23. class CDmeRigHandle;
  24. class CDmeRigBaseConstraintOperator;
  25. class CDmeRig;
  26. class CDmeGameModel;
  27. class CSFMAnimSetScriptContext;
  28. class CDmePresetGroupInfo;
  29. //-------------------------------------------------------------------------------------------------
  30. // CSFMScriptContext -- The script context class is a storage container which provides information
  31. // to the script manager about the current state, including dag node and time selection.
  32. //
  33. //-------------------------------------------------------------------------------------------------
  34. class CSFMScriptContext
  35. {
  36. public:
  37. enum SelectMode_t
  38. {
  39. SELECT_ADD,
  40. SELECT_REMOVE,
  41. SELECT_TOGGLE,
  42. };
  43. public:
  44. // Constructor
  45. CSFMScriptContext( CDmeClip *pMovie, CDmeFilmClip *pShot, DmeTime_t time, DmeFramerate_t framerate, CDmeAnimationSet *pActiveAnimSet = NULL, const CUtlVector< CDmeDag * > *pSelectedDagList = NULL, CDmElement *pSharedPresetGroupSettings = NULL );
  46. // Destructor, destroys the animation set context if created
  47. ~CSFMScriptContext();
  48. // Get dag node the primary selection
  49. CDmeDag *PrimaryDagSelection() const;
  50. // Clear the current selection.
  51. void ClearSelection();
  52. // Add the specified node to the current selection.
  53. void AddToSelection( CDmeDag *pDagNode, SelectMode_t mode );
  54. // Remove the specified dag node from the current selection
  55. bool RemoveFromSelection( CDmeDag *pDagNode, bool preserveOrder = true );
  56. // Add the list of dag nodes to the current selection
  57. void AddListSelection( const CUtlVector< CDmeDag* > &selection );
  58. // Push the current selection on to the stack
  59. void PushSelection();
  60. // Pop the last selection from the stack
  61. void PopSelection();
  62. // Make the time selection include the entire shot
  63. void TimeSelectAll( DmeTime_t handleTime = DMETIME_ZERO );
  64. // Select time relative to the shot
  65. void TimeSelectShot( float leftFalloff, float leftHold, float rightHold, float rightFalloff, int leftFalloffType, int rightFalloffType );
  66. // Select time specified with in frames
  67. void TimeSelectFrames( int leftFalloff, int leftHold, int rightHold, int rightFalloff, int leftFalloffType, int rightFalloffType );
  68. // Set the channel operating mode
  69. void SetChannelMode( ChannelMode_t mode );
  70. // Get the current time a frame relative to the start of the shot
  71. void SetCurrentFrame( int frame );
  72. // Get the current frame relative to the start of the shot
  73. int GetCurrentFrame() const;
  74. // Set the current ( global ) time
  75. void SetCurrentTime( DmeTime_t globalTime );
  76. // Set the active animation set
  77. void SetActiveAnimationSet( CDmeAnimationSet *pAnimationSet );
  78. // Get the specified animation set context
  79. CSFMAnimSetScriptContext *GetAnimSetContext( int index ) const;
  80. // Find the animation set context for the specified animation set.
  81. CSFMAnimSetScriptContext *FindAnimationSetContext( CDmeAnimationSet *pAnimationSet ) const;
  82. // Get the first selected dag node in the selection list and initialize the iterator
  83. CDmeDag *GetFirstSelectedDag();
  84. // Get the next selected dag node in the selection list
  85. CDmeDag *GetNextSelectedDag();
  86. // Start a new rig or continue an existing one if a rig with the specified name already exists
  87. CDmeRig *BeginRig( const char *pchRigName, bool bAllowAppend );
  88. // End the current rig
  89. void EndRig();
  90. // Add an element to the currently active rig
  91. void AddElementToRig( CDmElement *pElement, CDmeAnimationSet *pAnimSet );
  92. // Accessors
  93. CDmeClip *Movie() const { return m_pMovie; }
  94. CDmeFilmClip *Shot() const { return m_pShot; }
  95. CDmeAnimationSet *ActiveAnimationSet() const { return m_pActiveAnimSet; }
  96. CDmeRig *CurrentRig() const { return m_pCurrentRig; }
  97. const DmeLog_TimeSelection_t &TimeSelection() const { return m_TimeSelection; }
  98. const CUtlVector< CDmeDag * > &Selection() const { return m_Selection; }
  99. DmeTime_t CurrentTime() const { return m_CurrentTime; }
  100. ChannelMode_t ChannelMode() const { return m_ChannelMode; }
  101. int NumAnimationSets() const { return m_AnimSetContextList.Count(); }
  102. CDmElement *SharedPresetGroupSettings() const { return m_pSharedPresetGroupSettings; }
  103. private:
  104. // Initialize the context.
  105. void InititalizeContext();
  106. // Destroy all of the animation set contexts
  107. void DestroyAnimSetContexts();
  108. private:
  109. bool m_bPythonInitalized; // Flag indicating if python is initalized
  110. CDmeClip *m_pMovie; // Current movie in which the script is operating
  111. CDmeFilmClip *m_pShot; // Current shot in which the script is operating
  112. CDmeAnimationSet *m_pActiveAnimSet; // Currently selected animation set, used as a default
  113. CDmeRig *m_pCurrentRig; // Currently active rig
  114. ChannelMode_t m_ChannelMode; // Mode in which channels will be operated
  115. DmeTime_t m_CurrentTime; // Time at which the script is being run
  116. DmeFramerate_t m_Framerate; // The framerate setting of the current session
  117. DmeLog_TimeSelection_t m_TimeSelection; // Time selection to be used with any time dependent operations
  118. CUtlVector< CDmeDag * > m_Selection; // Set of selected dag nodes for script operation
  119. CUtlStack< CUtlVector< CDmeDag * > > m_SelectionStack; // Stack of selection sets used to store and restore selections
  120. CUtlVector< CSFMAnimSetScriptContext* > m_AnimSetContextList; // List of the animation set contexts, one for each animation set in the group
  121. int m_CurrentControlIndex; // Index used by the control iteration functions
  122. int m_CurrentDagIndex; // Index used by the dag iteration functions
  123. CDmElement *m_pSharedPresetGroupSettings; // Session's shared preset group settings
  124. };
  125. //-------------------------------------------------------------------------------------------------
  126. // CSFMScriptMgr
  127. //
  128. //-------------------------------------------------------------------------------------------------
  129. class CSFMScriptMgr
  130. {
  131. public:
  132. // Constructor
  133. CSFMScriptMgr( CSFMScriptContext &scriptContext );
  134. // Destructor
  135. ~CSFMScriptMgr();
  136. static void SetInterfaceFactory( CreateInterfaceFn factory );
  137. // Initialize the python script interface and register the sfm module.
  138. static void InitializeScriptInterface();
  139. // Shutdown the python script interface
  140. static void ShutdownScriptInterface();
  141. // Run the script provided in the buffer.
  142. static bool RunScript( const CUtlBuffer &scriptBuffer, CSFMScriptContext &scriptContext, CUtlString *pErrorMsg = NULL );
  143. // Find an animation set using the provided path
  144. CDmeAnimationSet *FindAnimationSet( const char *pchName, CDmeAnimationSet *pDefaultAnimSet = NULL ) const;
  145. // Set the currently active animation set that will be used when an animation set is not explicitly specified
  146. bool SetActiveAnimationSet( const char *pchAnimSetPath ) const;
  147. // Create an animation set for the specified element
  148. CDmeAnimationSet *CreateDagAnimationSet( CDmeDag *pElement, const char *pName ) const;
  149. // Create a game model referencing the specified mdl
  150. CDmeGameModel *CreateGameModel( const char *pRelativeModelName ) const;
  151. // Find a dag by name and animation set path
  152. CDmeDag *FindDag( const char *pchAnimSetDagPath, CDmeAnimationSet **pAnimationSet = NULL, const char **pDagName = NULL ) const;
  153. // Add all of the dag nodes of the animation set to the current selection
  154. bool SelectAnimSet( const char *pchAnimSetPath = NULL ) const;
  155. // Add the controls and dag nodes within the specified selection group to the current selection set
  156. bool Select( const char *pchName, CSFMScriptContext::SelectMode_t mode = CSFMScriptContext::SELECT_ADD ) const;
  157. // Make the primary selection dag node the parent of all other selected dag nodes
  158. bool ParentDags( bool bMaintainWorldPos, bool bParentToWorld, ReParentLogMode_t logMode ) const;
  159. // Create a rig handle with the specified name, add it to the active rig if any.
  160. CDmeRigHandle *CreateRigHandle( char const *pchHandleName, char const *pchRigSubGroup, const Vector &p, const Quaternion &q, bool bPositionControl, bool bRotationControl ) const;
  161. // Create a dag and associated control for the specified attachment position.
  162. CDmeDag *CreateAttachmentHandle( const char *pchAttachmentName ) const;
  163. // Create a dag node and add controls for it to the specified animation set
  164. CDmeDag *CreateDag( const char *pName, const Vector &pos, const Quaternion &orient, CDmeAnimationSet *pAnimSet = NULL, CDmeDag *pParent = NULL ) const;
  165. // Create a constraint of the specified type using the current selection.
  166. CDmeConstraintTarget *CreateConstraint( char const *pchConstraintName, char const *pchRigSubGroup, int constrainType, bool bPreserveOffset, float weight, bool bCreateControls, bool bOperate ) const;
  167. // Create an aim constraint using the current selection.
  168. CDmeConstraintTarget *CreateAimConstraint( char const *pchConstraintName, char const *pchRigSubGroup, bool bPreserveOffset, float weight, bool bCreateControls, const Vector &upVector, TransformSpace_t upSpace, const CDmeDag *pRefDag ) const;
  169. // Create an IK constraint using he current selection
  170. CDmeConstraintTarget *CreateIKConstraint( char const *pchConstraintName, char const *pchRigSubGroup, bool bPreserveOffset, bool bCreateControls, const Vector &poleVector, CDmeDag *pPoleVectorTarget ) const;
  171. // Remove all constraints from the currently selected dag nodes.
  172. void RemoveConstraints() const;
  173. // Generate position and orientation logs for the currently selected dag nodes.
  174. bool GenerateLogSamples( CDmeDag *pParent, bool bPosition, bool bOrientation, bool bWorld ) const;
  175. // Get the averaged position of the selected dag nodes
  176. void GetDagPosition( Vector &position, TransformSpace_t space, const CDmeDag *pReferenceSpaceDag = NULL) const;
  177. // Get the average orientation of the selected dag nodes
  178. void GetDagRotation( Vector &rotation, TransformSpace_t space, const CDmeDag *pReferenceSpaceDag = NULL ) const;
  179. // Move the selected dag nodes to the specified position or by specified amount
  180. void MoveDags( const Vector &offset, TransformSpace_t space, bool bRelative, bool bOffsetMode, CDmeDag *pReferenceDag );
  181. // Rotate the selected dag nodes to the specified position or by specified amount
  182. void RotateDags( const Vector &offset, TransformSpace_t space, bool bRelative, bool bOffsetMode, CDmeDag *pReferenceDag );
  183. // Set the current position and rotation as the default for the transforms of the selected dag nodes
  184. void SetDagTransformDefaults( bool bPosition, bool bOrientation ) const;
  185. // Set the currently selected dag nodes to their reference pose position.
  186. void SetReferencePose() const;
  187. // Clear the error message buffer
  188. void ClearErrorMessage();
  189. // Append the provided string to the current error message
  190. void AppendErrorMessage( const char *errorString );
  191. // Get the current error message string
  192. const char *GetErrorMessage() const;
  193. // Get the script context
  194. const CSFMScriptContext &Context() const { return m_Context; }
  195. CSFMScriptContext &Context() { return m_Context; }
  196. private:
  197. // Execute the specified script
  198. bool ExecuteScript( const CUtlBuffer &scriptBuffer );
  199. // Separate the target name from the path of a animation set / dag name path.
  200. static const char *SeparatePath( const char *pchAnimSetDagPath, char *pchAnimSetPath, int maxLength );
  201. // Construct a name for constraint on the specified dag
  202. void ConstructNameForConstraint( EConstraintType constraintType, CDmeDag *pDag, const char *pchConstraintName, CFmtStr &name ) const;
  203. // Perform processing which is done on all newly created constraints
  204. CDmeConstraintTarget *ProcessNewConstraint( CDmeDag *pSalveDag, CDmeRigBaseConstraintOperator* pConstraint, const char *pchRigSubGroup, bool bCreateControls ) const;
  205. private:
  206. CSFMScriptContext &m_Context;
  207. CUtlString m_ErrorBuffer;
  208. static CreateInterfaceFn sm_pToolsFactory;
  209. };
  210. //-------------------------------------------------------------------------------------------------
  211. // Global pointer to the active script manager, created and destroyed by RunScript()
  212. extern CSFMScriptMgr *g_pScriptMgr;
  213. #endif // SFMSCRIPTMGR_H