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.

223 lines
6.6 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef CL_DEMOACTION_H
  8. #define CL_DEMOACTION_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. // Forward declarations
  13. #include <KeyValues.h>
  14. class CUtlBuffer;
  15. class CBaseDemoAction;
  16. class CBaseActionEditDialog;
  17. class CDemoEditorPanel;
  18. /*
  19. namespace vgui
  20. {
  21. class Panel;
  22. }
  23. */
  24. //-----------------------------------------------------------------------------
  25. // Purpose: Types of demo actions we can take
  26. // Actions all have a name and can have a start time/frame to auto start or can
  27. // be fired in response to other events
  28. //-----------------------------------------------------------------------------
  29. typedef enum
  30. {
  31. DEMO_ACTION_UNKNOWN = 0, // error
  32. DEMO_ACTION_SKIPAHEAD, // SKip ahead in demo to time/frame
  33. DEMO_ACTION_STOPPLAYBACK, // Terminate playback
  34. DEMO_ACTION_PLAYCOMMANDS, // Type commands into console
  35. DEMO_ACTION_SCREENFADE_START, // Start fade w/ name
  36. DEMO_ACTION_SCREENFADE_STOP, // Cancel fade w/ name
  37. DEMO_ACTION_TEXTMESSAGE_START, // Start text message w/ name
  38. DEMO_ACTION_TEXTMESSAGE_STOP, // Stop text message by name
  39. DEMO_ACTION_PLAYCDTRACK_START, // Start playing cd track
  40. DEMO_ACTION_PLAYCDTRACK_STOP, // Cancel cd track
  41. DEMO_ACTION_PLAYSOUND_START, // Start playing sound
  42. DEMO_ACTION_PLAYSOUND_END, // Cancel sound
  43. DEMO_ACTION_ONSKIPPEDAHEAD, // Listener for named skip ahead succeeding
  44. DEMO_ACTION_ONSTOPPEDPLAYBACK, // Listener for stop event
  45. DEMO_ACTION_ONSCREENFADE_FINISHED, // Fire when screen fade of specified name finishes
  46. DEMO_ACTION_ONTEXTMESSAGE_FINISHED, // Fire when specified text message finishes
  47. DEMO_ACTION_ONPLAYCDTRACK_FINISHED, // Fire when played cd track finishes
  48. DEMO_ACTION_ONPLAYSOUND_FINISHED, // Fire when played sound finishes
  49. DEMO_ACTION_PAUSE, // Pause playback for N seconds w/auto resume
  50. DEMO_ACTION_CHANGEPLAYBACKRATE, // Slo-mo, etc
  51. DEMO_ACTION_ZOOM, // Zoom in/out with hold
  52. // Must be last
  53. NUM_DEMO_ACTIONS,
  54. } DEMOACTION;
  55. //-----------------------------------------------------------------------------
  56. // Purpose:
  57. //-----------------------------------------------------------------------------
  58. typedef enum
  59. {
  60. ACTION_USES_NEITHER = 0,
  61. ACTION_USES_TICK,
  62. ACTION_USES_TIME,
  63. NUM_TIMING_TYPES,
  64. } DEMOACTIONTIMINGTYPE;
  65. //-----------------------------------------------------------------------------
  66. // Purpose:
  67. //-----------------------------------------------------------------------------
  68. struct DemoActionTimingContext
  69. {
  70. int prevtick;
  71. int curtick;
  72. float prevtime;
  73. float curtime;
  74. };
  75. typedef CBaseDemoAction * (*DEMOACTIONFACTORY_FUNC)( void );
  76. typedef CBaseActionEditDialog *(*DEMOACTIONEDIT_FUNC)( CDemoEditorPanel *parent, CBaseDemoAction *action, bool newaction );
  77. abstract_class CBaseDemoAction
  78. {
  79. public:
  80. CBaseDemoAction();
  81. virtual ~CBaseDemoAction();
  82. virtual bool Init( KeyValues *pInitData );
  83. virtual bool Update( const DemoActionTimingContext& tc );
  84. // Do whatever the even is supposed to do
  85. virtual void FireAction( void ) = 0;
  86. virtual void Reset( void );
  87. virtual void SaveKeysToBuffer( int depth, CUtlBuffer& buf );
  88. virtual void OnActionFinished( void );
  89. // Public methods
  90. public:
  91. DEMOACTION GetType( void ) const;
  92. void SetType( DEMOACTION actionType );
  93. DEMOACTIONTIMINGTYPE GetTimingType( void ) const;
  94. void SetTimingType( DEMOACTIONTIMINGTYPE timingtype );
  95. void SetActionFired( bool fired );
  96. bool GetActionFired( void ) const;
  97. int GetStartTick( void ) const;
  98. void SetStartTick( int tick );
  99. float GetStartTime( void ) const;
  100. void SetStartTime( float time );
  101. void SetFinishedAction( bool finished );
  102. bool HasActionFinished( void ) const;
  103. char const *GetActionName( void ) const;
  104. void SetActionName( char const *name );
  105. bool ActionHasTarget( void ) const;
  106. char const *GetActionTarget( void ) const;
  107. void SetActionTarget( char const *name );
  108. void SaveToBuffer( int depth, int index, CUtlBuffer& buf );
  109. public:
  110. static void *operator new( size_t sz );
  111. static void operator delete( void *pMem );
  112. static char const *NameForType( DEMOACTION actionType );
  113. static DEMOACTION TypeForName( char const *name );
  114. static char const *NameForTimingType( DEMOACTIONTIMINGTYPE timingType );
  115. static DEMOACTIONTIMINGTYPE TimingTypeForName( char const *name );
  116. static void AddFactory( DEMOACTION actionType, DEMOACTIONFACTORY_FUNC func );
  117. static CBaseDemoAction *CreateDemoAction( DEMOACTION actionType );
  118. static void AddEditorFactory( DEMOACTION actionType, DEMOACTIONEDIT_FUNC func );
  119. static CBaseActionEditDialog *CreateActionEditor( DEMOACTION actionType, CDemoEditorPanel *parent, CBaseDemoAction *action, bool newaction );
  120. static bool HasEditorFactory( DEMOACTION actionType );
  121. // Serialization helper ( handles indenting )
  122. static void BufPrintf( int depth, CUtlBuffer& buf, PRINTF_FORMAT_STRING char const *fmt, ... );
  123. private:
  124. enum
  125. {
  126. MAX_ACTION_NAME = 64,
  127. };
  128. DEMOACTION m_Type;
  129. bool m_bActionFired;
  130. bool m_bActionFinished;
  131. char m_szActionName[ MAX_ACTION_NAME ];
  132. char m_szActionTarget[ MAX_ACTION_NAME ];
  133. DEMOACTIONTIMINGTYPE m_Timing;
  134. int m_nStartTick;
  135. float m_flStartTime;
  136. };
  137. #define DECLARE_DEMOACTION( type, classname ) \
  138. static CBaseDemoAction *FnCreate##classname( void ) \
  139. { \
  140. CBaseDemoAction *item = new classname(); \
  141. if ( item ) item->SetType( type ); \
  142. return item; \
  143. } \
  144. class CFactory##classname \
  145. { \
  146. public: \
  147. CFactory##classname() \
  148. { \
  149. CBaseDemoAction::AddFactory( \
  150. type, \
  151. FnCreate##classname ); \
  152. } \
  153. }; \
  154. static CFactory##classname g_Factory##classname;
  155. #define DECLARE_DEMOACTIONEDIT( type, classname ) \
  156. static CBaseActionEditDialog *FnCreateEditor##classname \
  157. ( CDemoEditorPanel *parent, \
  158. CBaseDemoAction *action, \
  159. bool newaction ) \
  160. { \
  161. CBaseActionEditDialog *editor = new classname( \
  162. parent, action, newaction ); \
  163. if ( editor ) editor->Init(); \
  164. return editor; \
  165. } \
  166. class CFactoryEditor##classname \
  167. { \
  168. public: \
  169. CFactoryEditor##classname() \
  170. { \
  171. CBaseDemoAction::AddEditorFactory( \
  172. type, \
  173. FnCreateEditor##classname ); \
  174. } \
  175. }; \
  176. static CFactoryEditor##classname g_FactoryEditor##classname;
  177. #endif // CL_DEMOACTION_H