ImageFont.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _DGL_H_
  23. #include "graphics/dgl.h"
  24. #endif
  25. #include "console/consoleTypes.h"
  26. #include "io/bitStream.h"
  27. #include "string/stringBuffer.h"
  28. #include "ImageFont.h"
  29. // Script bindings.
  30. #include "ImageFont_ScriptBinding.h"
  31. //------------------------------------------------------------------------------
  32. static EnumTable::Enums textAlignmentEnums[] =
  33. {
  34. { ImageFont::ALIGN_LEFT, "Left" },
  35. { ImageFont::ALIGN_CENTER, "Center" },
  36. { ImageFont::ALIGN_RIGHT, "Right" },
  37. };
  38. static EnumTable gTextAlignmentTable(3, &textAlignmentEnums[0]);
  39. //-----------------------------------------------------------------------------
  40. ImageFont::TextAlignment ImageFont::getTextAlignmentEnum(const char* label)
  41. {
  42. // Search for Mnemonic.
  43. for (U32 i = 0; i < (sizeof(textAlignmentEnums) / sizeof(EnumTable::Enums)); i++)
  44. {
  45. if( dStricmp(textAlignmentEnums[i].label, label) == 0)
  46. return (TextAlignment)textAlignmentEnums[i].index;
  47. }
  48. // Warn.
  49. Con::warnf("ImageFont::getTextAlignmentEnum() - Invalid text alignment of '%s'", label );
  50. return ImageFont::INVALID_ALIGN;
  51. }
  52. //-----------------------------------------------------------------------------
  53. const char* ImageFont::getTextAlignmentDescription(const ImageFont::TextAlignment alignment)
  54. {
  55. // Search for Mnemonic.
  56. for (U32 i = 0; i < (sizeof(textAlignmentEnums) / sizeof(EnumTable::Enums)); i++)
  57. {
  58. if( textAlignmentEnums[i].index == alignment )
  59. return textAlignmentEnums[i].label;
  60. }
  61. // Warn.
  62. Con::warnf( "ImageFont::getTextAlignmentDescription() - Invalid text alignment.");
  63. return StringTable->EmptyString;
  64. }
  65. //------------------------------------------------------------------------------
  66. IMPLEMENT_CONOBJECT(ImageFont);
  67. //-----------------------------------------------------------------------------
  68. ImageFont::ImageFont() :
  69. mTextAlignment( ImageFont::ALIGN_CENTER ),
  70. mFontSize( 1.0f, 1.0f ),
  71. mFontPadding( 0 )
  72. {
  73. // Use a static body by default.
  74. mBodyDefinition.type = b2_staticBody;
  75. // Set as auto-sizing.
  76. mAutoSizing = true;
  77. }
  78. //-----------------------------------------------------------------------------
  79. ImageFont::~ImageFont()
  80. {
  81. }
  82. //-----------------------------------------------------------------------------
  83. void ImageFont::initPersistFields()
  84. {
  85. // Call parent.
  86. Parent::initPersistFields();
  87. addProtectedField("image", TypeImageAssetPtr, Offset(mImageAsset, ImageFont), &setImage, &getImage, &writeImage, "");
  88. addProtectedField("text", TypeString, 0, setText, getText, &writeText, "The text to be displayed." );
  89. addProtectedField("textAlignment", TypeEnum, Offset(mTextAlignment, ImageFont), &setTextAlignment, &defaultProtectedGetFn, &writeTextAlignment, 1, &gTextAlignmentTable, "");
  90. addProtectedField("fontSize", TypeVector2, Offset(mFontSize, ImageFont), &setFontSize, &defaultProtectedGetFn,&writeFontSize, "" );
  91. addProtectedField("fontPadding", TypeF32, Offset(mFontPadding, ImageFont), &setFontPadding, &defaultProtectedGetFn, &writeFontPadding, "" );
  92. }
  93. //-----------------------------------------------------------------------------
  94. bool ImageFont::onAdd()
  95. {
  96. // Call Parent.
  97. if(!Parent::onAdd())
  98. return false;
  99. // Return Okay.
  100. return true;
  101. }
  102. //-----------------------------------------------------------------------------
  103. void ImageFont::onRemove()
  104. {
  105. // Call Parent.
  106. Parent::onRemove();
  107. }
  108. //------------------------------------------------------------------------------
  109. void ImageFont::copyTo(SimObject* object)
  110. {
  111. // Fetch font object.
  112. ImageFont* pFontObject = dynamic_cast<ImageFont*>(object);
  113. // Sanity.
  114. AssertFatal(pFontObject != NULL, "ImageFont::copyTo() - Object is not the correct type.");
  115. // Call parent.
  116. Parent::copyTo(object);
  117. // Copy.
  118. pFontObject->setImage( getImage() );
  119. pFontObject->setText( getText() );
  120. pFontObject->setTextAlignment( getTextAlignment() );
  121. pFontObject->setFontSize( getFontSize() );
  122. pFontObject->setFontPadding( getFontPadding() );
  123. }
  124. //------------------------------------------------------------------------------
  125. void ImageFont::scenePrepareRender( const SceneRenderState* pSceneRenderState, SceneRenderQueue* pSceneRenderQueue )
  126. {
  127. // Create a default render request.
  128. Scene::createDefaultRenderRequest( pSceneRenderQueue, this );
  129. }
  130. //------------------------------------------------------------------------------
  131. void ImageFont::sceneRender( const SceneRenderState* pSceneRenderState, const SceneRenderRequest* pSceneRenderRequest, BatchRender* pBatchRenderer )
  132. {
  133. // Finish if no image asset.
  134. if ( mImageAsset.isNull() )
  135. return;
  136. // Fetch number of characters to render.
  137. const U32 renderCharacters = mText.length();
  138. // Ignore if no text to render.
  139. if( renderCharacters == 0 )
  140. return;
  141. // Fetch render OOBB.
  142. const Vector2& renderOOBB0 = mRenderOOBB[0];
  143. const Vector2& renderOOBB1 = mRenderOOBB[1];
  144. const Vector2& renderOOBB3 = mRenderOOBB[3];
  145. Vector2 characterOOBB0;
  146. Vector2 characterOOBB1;
  147. Vector2 characterOOBB2;
  148. Vector2 characterOOBB3;
  149. // Calculate the starting render position based upon text alignment.
  150. switch( mTextAlignment )
  151. {
  152. case ALIGN_LEFT:
  153. {
  154. // Size is twice the padded text width as we're aligning to the left from the position expanding rightwards.
  155. characterOOBB0.Set( (renderOOBB0.x + renderOOBB1.x)*0.5f, renderOOBB0.y );
  156. }
  157. break;
  158. case ALIGN_RIGHT:
  159. {
  160. // Size is twice the padded text width as we're aligning to the right from the position expanding leftwards.
  161. characterOOBB0 = renderOOBB0;
  162. }
  163. break;
  164. default:
  165. {
  166. // Warn.
  167. Con::warnf("ImageFont() - Unknown text alignment!");
  168. }
  169. case ALIGN_CENTER:
  170. {
  171. // Size is the total padded text size as we're simply centered on the position.
  172. characterOOBB0 = renderOOBB0;
  173. }
  174. break;
  175. }
  176. // Calculate character width stride.
  177. Vector2 characterWidthStride = (renderOOBB1 - renderOOBB0);
  178. characterWidthStride.Normalize( mFontSize.x + mFontPadding );
  179. // Calculate character height stride.
  180. Vector2 characterHeightStride = (renderOOBB3 - renderOOBB0);
  181. characterHeightStride.Normalize( mFontSize.y );
  182. // Complete character OOBB.
  183. characterOOBB1 = characterOOBB0 + characterWidthStride;
  184. characterOOBB2 = characterOOBB1 + characterHeightStride;
  185. characterOOBB3 = characterOOBB2 - characterWidthStride;
  186. // Render all the characters.
  187. for( U32 characterIndex = 0; characterIndex < renderCharacters; ++characterIndex )
  188. {
  189. // Fetch character.
  190. U32 character = mText.getChar( characterIndex );
  191. // Set character to "space" if it's out of bounds.
  192. if ( character < 32 || character > 128 )
  193. character = 32;
  194. // Calculate character frame index.
  195. const U32 characterFrameIndex = character - 32;
  196. // Fetch current frame area.
  197. const ImageAsset::FrameArea::TexelArea& texelFrameArea = mImageAsset->getImageFrameArea( characterFrameIndex ).mTexelArea;
  198. // Fetch lower/upper texture coordinates.
  199. const Vector2& texLower = texelFrameArea.mTexelLower;
  200. const Vector2& texUpper = texelFrameArea.mTexelUpper;
  201. // Submit batched quad.
  202. pBatchRenderer->SubmitQuad(
  203. characterOOBB0,
  204. characterOOBB1,
  205. characterOOBB2,
  206. characterOOBB3,
  207. Vector2( texLower.x, texUpper.y ),
  208. Vector2( texUpper.x, texUpper.y ),
  209. Vector2( texUpper.x, texLower.y ),
  210. Vector2( texLower.x, texLower.y ),
  211. mImageAsset->getImageTexture() );
  212. // Translate character OOBB.
  213. characterOOBB0 += characterWidthStride;
  214. characterOOBB1 += characterWidthStride;
  215. characterOOBB2 += characterWidthStride;
  216. characterOOBB3 += characterWidthStride;
  217. }
  218. }
  219. //-----------------------------------------------------------------------------
  220. bool ImageFont::setImage( const char* pImageAssetId )
  221. {
  222. // Set asset.
  223. mImageAsset = pImageAssetId;
  224. // Finish if no image asset.
  225. if ( mImageAsset.isNull() )
  226. return false;
  227. // We need a minimum of 96 frames here.
  228. if ( mImageAsset->getFrameCount() < 96 )
  229. {
  230. // Warn.
  231. Con::warnf("ImageFont::setImage() - The image needs to have at least 96 frames to be used as a font! (%s)", mImageAsset.getAssetId() );
  232. mImageAsset.clear();
  233. return false;
  234. }
  235. // Return Okay.
  236. return true;
  237. }
  238. //-----------------------------------------------------------------------------
  239. void ImageFont::setText( const StringBuffer& text )
  240. {
  241. // Set text.
  242. mText.set( &text );
  243. calculateSpatials();
  244. }
  245. //-----------------------------------------------------------------------------
  246. void ImageFont::setTextAlignment( const TextAlignment alignment )
  247. {
  248. mTextAlignment = alignment;
  249. calculateSpatials();
  250. }
  251. //-----------------------------------------------------------------------------
  252. void ImageFont::setFontSize( const Vector2& size )
  253. {
  254. mFontSize = size;
  255. mFontSize.clampZero();
  256. calculateSpatials();
  257. }
  258. //-----------------------------------------------------------------------------
  259. void ImageFont::setFontPadding( const F32 padding )
  260. {
  261. mFontPadding = padding;
  262. calculateSpatials();
  263. }
  264. //-----------------------------------------------------------------------------
  265. void ImageFont::calculateSpatials( void )
  266. {
  267. // Fetch number of characters to render.
  268. const U32 renderCharacters = mText.length();
  269. // Set size as a single character if no text.
  270. if ( renderCharacters == 0 )
  271. {
  272. setSize( mFontSize );
  273. return;
  274. }
  275. // Calculate total font padding.
  276. const F32 totalFontPadding = (renderCharacters * mFontPadding) - mFontPadding;
  277. // Calculate total character size.
  278. const Vector2 totalFontSize( renderCharacters * mFontSize.x, mFontSize.y );
  279. // Calculate total padded text size.
  280. const Vector2 totalPaddedTextSize( totalFontSize.x + totalFontPadding, totalFontSize.y );
  281. // Calculate size (AABB) including alignment relative to position.
  282. // NOTE: For left/right alignment we have to double the size width as clipping is based upon size and
  283. // we're aligning to the position here. We cannot align to the size AABB itself as that changes
  284. // as the text length changes therefore it is not a stable point from which to align from.
  285. switch( mTextAlignment )
  286. {
  287. case ALIGN_LEFT:
  288. {
  289. // Size is twice the padded text width as we're aligning to the left from the position expanding rightwards.
  290. setSize( totalPaddedTextSize.x * 2.0f, totalPaddedTextSize.y );
  291. }
  292. break;
  293. case ALIGN_RIGHT:
  294. {
  295. // Size is twice the padded text width as we're aligning to the right from the position expanding leftwards.
  296. setSize( totalPaddedTextSize.x * 2.0f, totalPaddedTextSize.y );
  297. }
  298. break;
  299. case ALIGN_CENTER:
  300. {
  301. // Size is the total padded text size as we're simply centered on the position.
  302. setSize( totalPaddedTextSize );
  303. }
  304. break;
  305. default:
  306. {
  307. // Warn.
  308. Con::warnf("ImageFont() - Unknown text alignment!");
  309. }
  310. }
  311. }
  312. //-----------------------------------------------------------------------------
  313. bool ImageFont::setTextAlignment( void* obj, const char* data )
  314. {
  315. static_cast<ImageFont*>( obj )->setTextAlignment( getTextAlignmentEnum(data) ); return false;
  316. }