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.

284 lines
6.2 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "vcdfile.h"
  8. #include "soundentry.h"
  9. #include "choreoscene.h"
  10. #include "scriplib.h"
  11. #include "cmdlib.h"
  12. #include "iscenetokenprocessor.h"
  13. #include "choreoevent.h"
  14. #include "scene.h"
  15. #include "project.h"
  16. #include "workspacemanager.h"
  17. #include "workspacebrowser.h"
  18. //-----------------------------------------------------------------------------
  19. // Purpose: Helper to scene module for parsing the .vcd file
  20. //-----------------------------------------------------------------------------
  21. class CSceneTokenProcessor : public ISceneTokenProcessor
  22. {
  23. public:
  24. const char *CurrentToken( void );
  25. bool GetToken( bool crossline );
  26. bool TokenAvailable( void );
  27. void Error( const char *fmt, ... );
  28. };
  29. //-----------------------------------------------------------------------------
  30. // Purpose:
  31. // Output : const
  32. //-----------------------------------------------------------------------------
  33. const char *CSceneTokenProcessor::CurrentToken( void )
  34. {
  35. return token;
  36. }
  37. //-----------------------------------------------------------------------------
  38. // Purpose:
  39. // Input : crossline -
  40. // Output : Returns true on success, false on failure.
  41. //-----------------------------------------------------------------------------
  42. bool CSceneTokenProcessor::GetToken( bool crossline )
  43. {
  44. return ::GetToken( crossline ) ? true : false;
  45. }
  46. //-----------------------------------------------------------------------------
  47. // Purpose:
  48. // Output : Returns true on success, false on failure.
  49. //-----------------------------------------------------------------------------
  50. bool CSceneTokenProcessor::TokenAvailable( void )
  51. {
  52. return ::TokenAvailable() ? true : false;
  53. }
  54. //-----------------------------------------------------------------------------
  55. // Purpose:
  56. // Input : *fmt -
  57. // ... -
  58. //-----------------------------------------------------------------------------
  59. void CSceneTokenProcessor::Error( const char *fmt, ... )
  60. {
  61. char string[ 2048 ];
  62. va_list argptr;
  63. va_start( argptr, fmt );
  64. vsprintf( string, fmt, argptr );
  65. va_end( argptr );
  66. Con_ColorPrintf( ERROR_R, ERROR_G, ERROR_B, string );
  67. ::Error( string );
  68. }
  69. static CSceneTokenProcessor g_TokenProcessor;
  70. ISceneTokenProcessor *tokenprocessor = &g_TokenProcessor;
  71. CVCDFile::CVCDFile( CScene *scene, char const *filename ) : m_pOwner( scene )
  72. {
  73. Q_strncpy( m_szName, filename, sizeof( m_szName ) );
  74. m_pScene = LoadScene( filename );
  75. LoadSoundsFromScene( m_pScene );
  76. m_pszComments = NULL;
  77. }
  78. CVCDFile::~CVCDFile()
  79. {
  80. while ( m_Sounds.Count() > 0 )
  81. {
  82. CSoundEntry *p = m_Sounds[ 0 ];
  83. m_Sounds.Remove( 0 );
  84. delete p;
  85. }
  86. delete[] m_pszComments;
  87. }
  88. CScene *CVCDFile::GetOwnerScene()
  89. {
  90. return m_pOwner;
  91. }
  92. //-----------------------------------------------------------------------------
  93. // Purpose:
  94. // Input : *filename -
  95. // Output : CChoreoScene
  96. //-----------------------------------------------------------------------------
  97. CChoreoScene *CVCDFile::LoadScene( char const *filename )
  98. {
  99. if ( filesystem->FileExists( filename ) )
  100. {
  101. char fullpath[ 512 ];
  102. filesystem->RelativePathToFullPath( filename, "GAME", fullpath, sizeof( fullpath ) );
  103. LoadScriptFile( fullpath );
  104. CChoreoScene *scene = ChoreoLoadScene( filename, this, &g_TokenProcessor, Con_Printf );
  105. return scene;
  106. }
  107. return NULL;
  108. }
  109. void CVCDFile::LoadSoundsFromScene( CChoreoScene *scene )
  110. {
  111. if ( !scene )
  112. return;
  113. CChoreoEvent *e;
  114. int c = scene->GetNumEvents();
  115. for ( int i = 0; i < c; i++ )
  116. {
  117. e = scene->GetEvent( i );
  118. if ( !e )
  119. continue;
  120. if ( e->GetType() != CChoreoEvent::SPEAK )
  121. continue;
  122. CSoundEntry *se = new CSoundEntry( this, e->GetParameters() );
  123. m_Sounds.AddToTail( se );
  124. }
  125. }
  126. char const *CVCDFile::GetName() const
  127. {
  128. return m_szName;
  129. }
  130. char const *CVCDFile::GetComments()
  131. {
  132. return m_pszComments ? m_pszComments : "";
  133. }
  134. void CVCDFile::SetComments( char const *comments )
  135. {
  136. delete[] m_pszComments;
  137. m_pszComments = V_strdup( comments );
  138. if ( GetOwnerScene() )
  139. {
  140. if ( GetOwnerScene()->GetOwnerProject() )
  141. {
  142. GetOwnerScene()->GetOwnerProject()->SetDirty( true );
  143. }
  144. }
  145. }
  146. int CVCDFile::GetSoundEntryCount() const
  147. {
  148. return m_Sounds.Count();
  149. }
  150. CSoundEntry *CVCDFile::GetSoundEntry( int index )
  151. {
  152. if ( index < 0 || index >= m_Sounds.Count() )
  153. return NULL;
  154. return m_Sounds[ index ];
  155. }
  156. void CVCDFile::ValidateTree( mxTreeView *tree, mxTreeViewItem* parent )
  157. {
  158. CUtlVector< mxTreeViewItem * > m_KnownItems;
  159. int c = GetSoundEntryCount();
  160. CSoundEntry *sound;
  161. for ( int i = 0; i < c; i++ )
  162. {
  163. sound = GetSoundEntry( i );
  164. if ( !sound )
  165. continue;
  166. char sz[ 256 ];
  167. Q_snprintf( sz, sizeof( sz ), "\"%s\" : script %s", sound->GetName(), sound->GetScriptFile() );
  168. mxTreeViewItem *spot = sound->FindItem( tree, parent );
  169. if ( !spot )
  170. {
  171. spot = tree->add( parent, sz );
  172. }
  173. m_KnownItems.AddToTail( spot );
  174. sound->SetOrdinal( i );
  175. tree->setLabel( spot, sz );
  176. tree->setImages( spot, sound->GetIconIndex(), sound->GetIconIndex() );
  177. tree->setUserData( spot, sound );
  178. sound->ValidateTree( tree, spot );
  179. }
  180. mxTreeViewItem *start = tree->getFirstChild( parent );
  181. while ( start )
  182. {
  183. mxTreeViewItem *next = tree->getNextChild( start );
  184. if ( m_KnownItems.Find( start ) == m_KnownItems.InvalidIndex() )
  185. {
  186. tree->remove( start );
  187. }
  188. start = next;
  189. }
  190. tree->sortTree( parent, true, CWorkspaceBrowser::CompareFunc, 0 );
  191. }
  192. void CVCDFile::Checkout(bool updatestateicons /*= true*/)
  193. {
  194. VSS_Checkout( GetName(), updatestateicons );
  195. }
  196. void CVCDFile::Checkin(bool updatestateicons /*= true*/)
  197. {
  198. VSS_Checkin( GetName(), updatestateicons );
  199. }
  200. bool CVCDFile::IsCheckedOut() const
  201. {
  202. return filesystem->IsFileWritable( GetName() );
  203. }
  204. int CVCDFile::GetIconIndex() const
  205. {
  206. if ( IsCheckedOut() )
  207. {
  208. return IMAGE_VCD_CHECKEDOUT;
  209. }
  210. else
  211. {
  212. return IMAGE_VCD;
  213. }
  214. }
  215. void CVCDFile::MoveChildUp( ITreeItem *child )
  216. {
  217. }
  218. void CVCDFile::MoveChildDown( ITreeItem *child )
  219. {
  220. }
  221. void CVCDFile::SetDirty( bool dirty )
  222. {
  223. if ( GetOwnerScene() )
  224. {
  225. GetOwnerScene()->SetDirty( dirty );
  226. }
  227. }
  228. bool CVCDFile::IsChildFirst( ITreeItem *child )
  229. {
  230. return false;
  231. }
  232. bool CVCDFile::IsChildLast( ITreeItem *child )
  233. {
  234. return false;
  235. }