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.

302 lines
8.1 KiB

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