W3DDisplayString.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // FILE: W3DDisplayString.cpp /////////////////////////////////////////////////
  24. //-----------------------------------------------------------------------------
  25. //
  26. // Westwood Studios Pacific.
  27. //
  28. // Confidential Information
  29. // Copyright (C) 2001 - All Rights Reserved
  30. //
  31. //-----------------------------------------------------------------------------
  32. //
  33. // Project: RTS3
  34. //
  35. // File name: W3DDisplayString.cpp
  36. //
  37. // Created: Colin Day, July 2001
  38. //
  39. // Desc: Display string W3D implementation, display strings hold
  40. // double byte characters and all the data we need to render
  41. // those strings to the screen.
  42. //
  43. //-----------------------------------------------------------------------------
  44. ///////////////////////////////////////////////////////////////////////////////
  45. // SYSTEM INCLUDES ////////////////////////////////////////////////////////////
  46. #include <stdlib.h>
  47. // USER INCLUDES //////////////////////////////////////////////////////////////
  48. #include "GameClient/GameClient.h"
  49. #include "W3DDevice/GameClient/W3DDisplayString.h"
  50. #include "GameClient/HotKey.h"
  51. #include "GameClient/GameFont.h"
  52. #include "GameClient/GlobalLanguage.h"
  53. // DEFINES ////////////////////////////////////////////////////////////////////
  54. #ifdef _INTERNAL
  55. // for occasional debugging...
  56. //#pragma optimize("", off)
  57. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  58. #endif
  59. // PRIVATE TYPES //////////////////////////////////////////////////////////////
  60. // PRIVATE DATA ///////////////////////////////////////////////////////////////
  61. // PUBLIC DATA ////////////////////////////////////////////////////////////////
  62. // PRIVATE PROTOTYPES /////////////////////////////////////////////////////////
  63. // PRIVATE FUNCTIONS //////////////////////////////////////////////////////////
  64. ///////////////////////////////////////////////////////////////////////////////
  65. // PUBLIC FUNCTIONS ///////////////////////////////////////////////////////////
  66. ///////////////////////////////////////////////////////////////////////////////
  67. // W3DDisplayString::W3DDisplayString =========================================
  68. /** */
  69. //=============================================================================
  70. W3DDisplayString::W3DDisplayString( void )
  71. {
  72. m_textChanged = FALSE;
  73. m_textPos.x = 0;
  74. m_textPos.y = 0;
  75. m_currTextColor = 0;
  76. m_currDropColor = 0;
  77. m_size.x = 0;
  78. m_size.y = 0;
  79. m_fontChanged = FALSE;
  80. m_clipRegion.lo.x = 0;
  81. m_clipRegion.lo.y = 0;
  82. m_clipRegion.hi.x = 0;
  83. m_clipRegion.hi.y = 0;
  84. m_lastResourceFrame = 0;
  85. m_useHotKey = FALSE;
  86. m_hotKeyPos.x = 0;
  87. m_hotKeyPos.y = 0;
  88. m_hotKeyColor = GameMakeColor(255,255,255,255);
  89. } // end W3DDisplayString
  90. // W3DDisplayString::~W3DDisplayString ========================================
  91. /** */
  92. //=============================================================================
  93. W3DDisplayString::~W3DDisplayString( void )
  94. {
  95. } // end ~W3DDisplayString
  96. // W3DDisplayString::textChanged ==============================================
  97. /** This method automatically gets called from some methods in the display
  98. * class so that we can write our own code here to to appropriate things
  99. * on the changing of string data */
  100. //=============================================================================
  101. void W3DDisplayString::notifyTextChanged( void )
  102. {
  103. // extend functionality
  104. DisplayString::notifyTextChanged();
  105. if(TheGlobalLanguageData)
  106. {
  107. if(TheGlobalLanguageData->m_useHardWrap == TRUE)
  108. {
  109. m_textRenderer.Set_Use_Hard_Word_Wrap(true);
  110. m_textRendererHotKey.Set_Use_Hard_Word_Wrap(true);
  111. }
  112. else
  113. {
  114. m_textRenderer.Set_Use_Hard_Word_Wrap(false);
  115. m_textRendererHotKey.Set_Use_Hard_Word_Wrap(false);
  116. }
  117. }
  118. // get our new text extents
  119. computeExtents();
  120. //
  121. // set a flag so that if it comes that we need to render this string
  122. // we know we must first build the sentence
  123. //
  124. m_textChanged = TRUE;
  125. // reset data for our text renderer
  126. m_textRenderer.Reset();
  127. m_textRendererHotKey.Reset();
  128. } // end notifyTextChanged
  129. // W3DDisplayString::Draw =====================================================
  130. /** Draw the text at the specified location in in the specified colors
  131. * in the parameters. Since we keep an instance of the rendered text
  132. * texture around, this is the time to check to see if any of our
  133. * properties have changed since the last render, like color, or
  134. * position, or content. If they have, we need to rebuild the sentence
  135. * texture for rendering */
  136. //=============================================================================
  137. void W3DDisplayString::draw( Int x, Int y, Color color, Color dropColor )
  138. {
  139. draw(x,y, color, dropColor, 1, 1);
  140. }
  141. void W3DDisplayString::draw( Int x, Int y, Color color, Color dropColor, Int xDrop, Int yDrop )
  142. {
  143. Bool needNewPolys = FALSE;
  144. // sanity
  145. if( getTextLength() == 0 )
  146. return; // nothing to draw
  147. // if our font or text has changed we need to build a new sentence
  148. if( m_fontChanged || m_textChanged )
  149. {
  150. if(m_useHotKey)
  151. {
  152. m_textRenderer.Set_Hot_Key_Parse(TRUE);
  153. m_textRenderer.Build_Sentence( getText().str(), &m_hotKeyPos.x, &m_hotKeyPos.y );
  154. m_hotkey.translate(TheHotKeyManager->searchHotKey(getText()));
  155. if(!m_hotkey.isEmpty())
  156. m_textRendererHotKey.Build_Sentence(m_hotkey.str(), NULL, NULL);
  157. else
  158. {
  159. m_useHotKey = FALSE;
  160. m_textRendererHotKey.Reset();
  161. }
  162. }
  163. else
  164. m_textRenderer.Build_Sentence( getText().str(), NULL, NULL );
  165. m_fontChanged = FALSE;
  166. m_textChanged = FALSE;
  167. needNewPolys = TRUE;
  168. } // end if
  169. //
  170. // if our position has changed, or our colors have chagned, or our
  171. // text data has changed, we need to redo the texture quads
  172. //
  173. if( needNewPolys ||
  174. x != m_textPos.x ||
  175. y != m_textPos.y ||
  176. color != m_currTextColor ||
  177. dropColor != m_currDropColor )
  178. {
  179. // save the new attributes of the text position and color
  180. m_textPos.x = x;
  181. m_textPos.y = y;
  182. m_currTextColor = color;
  183. m_currDropColor = dropColor;
  184. // reset the quads
  185. m_textRenderer.Reset_Polys();
  186. // draw the shadow
  187. m_textRenderer.Set_Location( Vector2( m_textPos.x + xDrop, m_textPos.y + yDrop) );
  188. m_textRenderer.Draw_Sentence( m_currDropColor );
  189. // draw the text
  190. m_textRenderer.Set_Location( Vector2( m_textPos.x, m_textPos.y ) );
  191. m_textRenderer.Draw_Sentence( m_currTextColor );
  192. if(m_useHotKey)
  193. {
  194. m_textRendererHotKey.Reset_Polys();
  195. m_textRendererHotKey.Set_Location( Vector2( m_textPos.x + m_hotKeyPos.x , m_textPos.y +m_hotKeyPos.y) );
  196. m_textRendererHotKey.Draw_Sentence( m_hotKeyColor );
  197. m_textRendererHotKey.Render();
  198. }
  199. } // end if
  200. // render the text
  201. m_textRenderer.Render();
  202. // we are for sure using display resources now
  203. if( TheGameClient )
  204. usingResources( TheGameClient->getFrame() );
  205. } // end draw
  206. // W3DDisplayString::getSize ==================================================
  207. /** Get the render size width and height of the string in this instance
  208. * with the font associated with it */
  209. //=============================================================================
  210. void W3DDisplayString::getSize( Int *width, Int *height )
  211. {
  212. // assign the width and height we have stored to parameters present
  213. if( width )
  214. *width = m_size.x;
  215. if( height )
  216. *height = m_size.y;
  217. } // end getSize
  218. // DisplayString::appendChar ==================================================
  219. /** Get text with up to charPos characters, -1 = all characters */
  220. //=============================================================================
  221. Int W3DDisplayString::getWidth( Int charPos )
  222. {
  223. FontCharsClass * font;
  224. Int width = 0;
  225. Int count = 0;
  226. font = m_textRenderer.Peek_Font();
  227. if ( font )
  228. {
  229. const WideChar *text = m_textString.str();
  230. WideChar ch;
  231. while ( (ch = *text++ ) != 0 && ( charPos == -1 || count < charPos ) )
  232. {
  233. if ( ch != (WideChar)'\n' )
  234. {
  235. width += font->Get_Char_Spacing( ch );
  236. }
  237. count++;
  238. }
  239. }
  240. return width;
  241. }
  242. // W3DDisplayString::setFont ==================================================
  243. /** Set the font for this particular display string */
  244. //=============================================================================
  245. void W3DDisplayString::setFont( GameFont *font )
  246. {
  247. // sanity
  248. if( font == NULL )
  249. return;
  250. // if the new font is the same as our existing font do nothing
  251. if( m_font == font )
  252. return;
  253. // extending functionality
  254. DisplayString::setFont( font );
  255. // set the font in our renderer
  256. m_textRenderer.Set_Font( static_cast<FontCharsClass *>(m_font->fontData) );
  257. m_textRendererHotKey.Set_Font( static_cast<FontCharsClass *>(TheFontLibrary->getFont(font->nameString,font->pointSize, TRUE)->fontData) );
  258. // recompute extents for text with new font
  259. computeExtents();
  260. // set flag telling us the font has changed since last render
  261. m_fontChanged = TRUE;
  262. } // end setFont
  263. // W3DDisplayString::setClipRegion ============================================
  264. /** Set the clipping region for the text */
  265. //=============================================================================
  266. void W3DDisplayString::setClipRegion( IRegion2D *region )
  267. {
  268. // extend functionality
  269. DisplayString::setClipRegion( region );
  270. // only consider regions that are actual changes
  271. if( region->lo.x != m_clipRegion.lo.x ||
  272. region->lo.y != m_clipRegion.lo.y ||
  273. region->hi.x != m_clipRegion.hi.x ||
  274. region->hi.y != m_clipRegion.hi.y )
  275. {
  276. // assign new region
  277. m_clipRegion = *region;
  278. // set new region in renderer
  279. m_textRenderer.Set_Clipping_Rect( RectClass( m_clipRegion.lo.x,
  280. m_clipRegion.lo.y,
  281. m_clipRegion.hi.x,
  282. m_clipRegion.hi.y ) );
  283. m_textRendererHotKey.Set_Clipping_Rect( RectClass( m_clipRegion.lo.x,
  284. m_clipRegion.lo.y,
  285. m_clipRegion.hi.x,
  286. m_clipRegion.hi.y ) );
  287. } // end if
  288. } // end setClipRegion
  289. // W3DDisplayString::computeExtents ===========================================
  290. /** Update the width and height of our string */
  291. //=============================================================================
  292. void W3DDisplayString::computeExtents( void )
  293. {
  294. UnsignedInt len = getTextLength();
  295. // if we have no string, or no font we don't have a size yet
  296. if( len == 0 || m_font == NULL )
  297. {
  298. m_size.x = 0;
  299. m_size.y = 0;
  300. } // end if
  301. else
  302. {
  303. Vector2 extents = m_textRenderer.Get_Formatted_Text_Extents(getText().str()); //Get_Text_Extents( getText().str() );
  304. m_size.x = extents.X;
  305. m_size.y = extents.Y;
  306. } // end else
  307. } // end computeExtents
  308. // W3DDisplayString::setWordWrap ===========================================
  309. /** Set the wordwrap of the m_textRenderer */
  310. //=============================================================================
  311. void W3DDisplayString::setWordWrap( Int wordWrap )
  312. {
  313. // set the Word Wrap
  314. if(m_textRenderer.Set_Wrapping_Width(wordWrap))
  315. notifyTextChanged();
  316. }// void setWordWrap( Int wordWrap )
  317. void W3DDisplayString::setUseHotkey( Bool useHotkey, Color hotKeyColor )
  318. {
  319. m_useHotKey = useHotkey;
  320. m_hotKeyColor = hotKeyColor;
  321. m_textRenderer.Set_Hot_Key_Parse(useHotkey);
  322. notifyTextChanged();
  323. }
  324. // W3DDisplayString::setWordWrapCentered ======================================
  325. /** Set the whether or not we want to center each new line in a text string */
  326. //=============================================================================
  327. void W3DDisplayString::setWordWrapCentered( Bool isCentered )
  328. {
  329. // set the Word Wrap
  330. if( m_textRenderer.Set_Word_Wrap_Centered(isCentered) )
  331. notifyTextChanged();
  332. }// void setWordWrap( Int wordWrap )