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.

303 lines
8.2 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #ifndef SENTENCE_H
  7. #define SENTENCE_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. // X360 optimizes out the extra memory needed by the editors in these types
  12. #ifndef _X360
  13. #define PHONEME_EDITOR 1
  14. #endif
  15. #include "utlvector.h"
  16. class CUtlBuffer;
  17. #define CACHED_SENTENCE_VERSION 1
  18. #define CACHED_SENTENCE_VERSION_ALIGNED 4
  19. #define CACHED_SENTENCE_VERSION_PACKED 5
  20. //-----------------------------------------------------------------------------
  21. // Purpose: A sample point
  22. //-----------------------------------------------------------------------------
  23. // Can't do this due to backward compat issues
  24. //#ifdef _WIN32
  25. //#pragma pack (1)
  26. //#endif
  27. struct CEmphasisSample
  28. {
  29. float time;
  30. float value;
  31. void SetSelected( bool isSelected );
  32. #if PHONEME_EDITOR
  33. // Used by editors only
  34. bool selected;
  35. #endif
  36. };
  37. class CBasePhonemeTag
  38. {
  39. public:
  40. CBasePhonemeTag();
  41. CBasePhonemeTag( const CBasePhonemeTag& from );
  42. CBasePhonemeTag &operator=( const CBasePhonemeTag &from ) { memcpy( this, &from, sizeof(*this) ); return *this; }
  43. float GetStartTime() const { return m_flStartTime; }
  44. void SetStartTime( float startTime ) { m_flStartTime = startTime; }
  45. void AddStartTime( float startTime ) { m_flStartTime += startTime; }
  46. float GetEndTime() const { return m_flEndTime; }
  47. void SetEndTime( float endTime ) { m_flEndTime = endTime; }
  48. void AddEndTime( float startTime ) { m_flEndTime += startTime; }
  49. int GetPhonemeCode() const { return m_nPhonemeCode; }
  50. void SetPhonemeCode( int phonemeCode ) { m_nPhonemeCode = phonemeCode; }
  51. private:
  52. float m_flStartTime;
  53. float m_flEndTime;
  54. unsigned short m_nPhonemeCode;
  55. };
  56. //-----------------------------------------------------------------------------
  57. // Purpose:
  58. //-----------------------------------------------------------------------------
  59. class CPhonemeTag : public CBasePhonemeTag
  60. {
  61. typedef CBasePhonemeTag BaseClass;
  62. public:
  63. CPhonemeTag( void );
  64. CPhonemeTag( const char *phoneme );
  65. CPhonemeTag( const CPhonemeTag& from );
  66. ~CPhonemeTag( void );
  67. void SetTag( const char *phoneme );
  68. char const *GetTag() const;
  69. unsigned int ComputeDataCheckSum();
  70. #if PHONEME_EDITOR
  71. bool m_bSelected;
  72. unsigned int m_uiStartByte;
  73. unsigned int m_uiEndByte;
  74. #endif
  75. void SetSelected( bool isSelected );
  76. bool GetSelected() const;
  77. void SetStartAndEndBytes( unsigned int start, unsigned int end );
  78. unsigned int GetStartByte() const;
  79. unsigned int GetEndByte() const;
  80. private:
  81. char *m_szPhoneme;
  82. };
  83. //-----------------------------------------------------------------------------
  84. // Purpose:
  85. //-----------------------------------------------------------------------------
  86. class CWordTag
  87. {
  88. public:
  89. CWordTag( void );
  90. CWordTag( const char *word );
  91. CWordTag( const CWordTag& from );
  92. ~CWordTag( void );
  93. void SetWord( const char *word );
  94. const char *GetWord() const;
  95. int IndexOfPhoneme( CPhonemeTag *tag );
  96. unsigned int ComputeDataCheckSum();
  97. float m_flStartTime;
  98. float m_flEndTime;
  99. CUtlVector < CPhonemeTag *> m_Phonemes;
  100. #if PHONEME_EDITOR
  101. bool m_bSelected;
  102. unsigned int m_uiStartByte;
  103. unsigned int m_uiEndByte;
  104. #endif
  105. void SetSelected( bool isSelected );
  106. bool GetSelected() const;
  107. void SetStartAndEndBytes( unsigned int start, unsigned int end );
  108. unsigned int GetStartByte() const;
  109. unsigned int GetEndByte() const;
  110. private:
  111. char *m_pszWord;
  112. };
  113. // A sentence can be closed captioned
  114. // The default case is the entire sentence shown at start time
  115. //
  116. // "<persist:2.0><clr:255,0,0,0>The <I>default<I> case"
  117. // "<sameline>is the <U>entire<U> sentence shown at <B>start time<B>"
  118. // Commands that aren't closed at end of phrase are automatically terminated
  119. //
  120. // Commands
  121. // <linger:2.0> The line should persist for 2.0 seconds beyond m_flEndTime
  122. // <sameline> Don't go to new line for next phrase on stack
  123. // <clr:r,g,b,a> Push current color onto stack and start drawing with new
  124. // color until we reach the next <clr> marker or a <clr> with no commands which
  125. // means restore the previous color
  126. // <U> Underline text (start/end)
  127. // <I> Italics text (start/end)
  128. // <B> Bold text (start/end)
  129. // <position:where> Draw caption at special location ??? needed
  130. // <cr> Go to new line
  131. // Close Captioning Support
  132. // The phonemes drive the mouth in english, but the CC text can
  133. // be one of several languages
  134. enum
  135. {
  136. CC_ENGLISH = 0,
  137. CC_FRENCH,
  138. CC_GERMAN,
  139. CC_ITALIAN,
  140. CC_KOREAN,
  141. CC_SCHINESE, // Simplified Chinese
  142. CC_SPANISH,
  143. CC_TCHINESE, // Traditional Chinese
  144. CC_JAPANESE,
  145. CC_RUSSIAN,
  146. CC_THAI,
  147. CC_PORTUGUESE,
  148. // etc etc
  149. CC_NUM_LANGUAGES
  150. };
  151. //-----------------------------------------------------------------------------
  152. // Purpose: A sentence is a box of words, and words contain phonemes
  153. //-----------------------------------------------------------------------------
  154. class CSentence
  155. {
  156. public:
  157. static char const *NameForLanguage( int language );
  158. static int LanguageForName( char const *name );
  159. static void ColorForLanguage( int language, unsigned char& r, unsigned char& g, unsigned char& b );
  160. // Construction
  161. CSentence( void );
  162. ~CSentence( void );
  163. // Assignment operator
  164. CSentence& operator =(const CSentence& src );
  165. void Append( float starttime, const CSentence& src );
  166. void SetText( const char *text );
  167. const char *GetText( void ) const;
  168. void InitFromDataChunk( void *data, int size );
  169. void InitFromBuffer( CUtlBuffer& buf );
  170. void SaveToBuffer( CUtlBuffer& buf );
  171. // This strips out all of the stuff used by the editor, leaving just one blank work, no sentence text, and just
  172. // the phonemes without the phoneme text...(same as the cacherestore version below)
  173. void MakeRuntimeOnly();
  174. // This is a compressed save of just the data needed to drive phonemes in the engine (no word / sentence text, etc )
  175. void CacheSaveToBuffer( CUtlBuffer& buf, int version );
  176. void CacheRestoreFromBuffer( CUtlBuffer& buf );
  177. // Add word/phoneme to sentence
  178. void AddPhonemeTag( CWordTag *word, CPhonemeTag *tag );
  179. void AddWordTag( CWordTag *tag );
  180. void Reset( void );
  181. void ResetToBase( void );
  182. void MarkNewPhraseBase( void );
  183. int GetWordBase( void );
  184. int CountPhonemes( void );
  185. // For legacy loading, try to find a word that contains the time
  186. CWordTag *EstimateBestWord( float time );
  187. CWordTag *GetWordForPhoneme( CPhonemeTag *phoneme );
  188. void SetTextFromWords( void );
  189. float GetIntensity( float time, float endtime );
  190. void Resort( void );
  191. CEmphasisSample *GetBoundedSample( int number, float endtime );
  192. int GetNumSamples( void );
  193. CEmphasisSample *GetSample( int index );
  194. // Compute start and endtime based on all words
  195. void GetEstimatedTimes( float& start, float &end );
  196. void SetVoiceDuck( bool shouldDuck ) { m_bShouldVoiceDuck = shouldDuck; }
  197. bool GetVoiceDuck() const { return m_bShouldVoiceDuck; }
  198. unsigned int ComputeDataCheckSum();
  199. void SetDataCheckSum( unsigned int chk );
  200. unsigned int GetDataCheckSum() const;
  201. int GetRuntimePhonemeCount() const;
  202. const CBasePhonemeTag *GetRuntimePhoneme( int i ) const;
  203. void ClearRuntimePhonemes();
  204. void AddRuntimePhoneme( const CPhonemeTag *src );
  205. void CreateEventWordDistribution( char const *pszText, float flSentenceDuration );
  206. static int CountWords( char const *pszText );
  207. static bool ShouldSplitWord( char in );
  208. public:
  209. #if PHONEME_EDITOR
  210. char *m_szText;
  211. CUtlVector< CWordTag * > m_Words;
  212. #endif
  213. CUtlVector < CBasePhonemeTag *> m_RunTimePhonemes;
  214. #if PHONEME_EDITOR
  215. int m_nResetWordBase;
  216. #endif
  217. // Phoneme emphasis data
  218. CUtlVector< CEmphasisSample > m_EmphasisSamples;
  219. #if PHONEME_EDITOR
  220. unsigned int m_uCheckSum;
  221. #endif
  222. bool m_bIsValid : 8;
  223. bool m_bStoreCheckSum : 8;
  224. bool m_bShouldVoiceDuck : 8;
  225. bool m_bIsCached : 8;
  226. private:
  227. void ParseDataVersionOnePointZero( CUtlBuffer& buf );
  228. void ParsePlaintext( CUtlBuffer& buf );
  229. void ParseWords( CUtlBuffer& buf );
  230. void ParseEmphasis( CUtlBuffer& buf );
  231. void ParseOptions( CUtlBuffer& buf );
  232. void ParseCloseCaption( CUtlBuffer& buf );
  233. void ResetCloseCaptionAll( void );
  234. friend class PhonemeEditor;
  235. };
  236. //#ifdef _WIN32
  237. //#pragma pack ()
  238. //#endif
  239. #endif // SENTENCE_H