FontAsset.cc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 _CONSOLE_H_
  23. #include "console/console.h"
  24. #endif
  25. #ifndef _CONSOLEINTERNAL_H_
  26. #include "console/consoleInternal.h"
  27. #endif
  28. #ifndef _GBITMAP_H_
  29. #include "graphics/gBitmap.h"
  30. #endif
  31. #ifndef _UTILITY_H_
  32. #include "2d/core/Utility.h"
  33. #endif
  34. #ifndef _SCENE_OBJECT_H_
  35. #include "2d/sceneobject/SceneObject.h"
  36. #endif
  37. #ifndef _FONT_ASSET_H_
  38. #include "2d/assets/FontAsset.h"
  39. #endif
  40. // Script bindings.
  41. #include "FontAsset_ScriptBinding.h"
  42. //------------------------------------------------------------------------------
  43. IMPLEMENT_CONOBJECT(FontAsset);
  44. //------------------------------------------------------------------------------
  45. ConsoleType( FontAssetPtr, TypeFontAssetPtr, sizeof(AssetPtr<FontAsset>), ASSET_ID_FIELD_PREFIX )
  46. //-----------------------------------------------------------------------------
  47. ConsoleGetType( TypeFontAssetPtr )
  48. {
  49. // Fetch asset Id.
  50. return (*((AssetPtr<FontAsset>*)dptr)).getAssetId();
  51. }
  52. //-----------------------------------------------------------------------------
  53. ConsoleSetType( TypeFontAssetPtr )
  54. {
  55. // Was a single argument specified?
  56. if( argc == 1 )
  57. {
  58. // Yes, so fetch field value.
  59. const char* pFieldValue = argv[0];
  60. // Fetch asset pointer.
  61. AssetPtr<FontAsset>* pAssetPtr = dynamic_cast<AssetPtr<FontAsset>*>((AssetPtrBase*)(dptr));
  62. // Is the asset pointer the correct type?
  63. if (pAssetPtr == NULL )
  64. {
  65. // No, so fail.
  66. Con::warnf( "(TypeFontAssetPtr) - Failed to set asset Id '%d'.", pFieldValue );
  67. return;
  68. }
  69. // Set asset.
  70. pAssetPtr->setAssetId( pFieldValue );
  71. return;
  72. }
  73. // Warn.
  74. Con::warnf( "(TypeFontAssetPtr) - Cannot set multiple args to a single asset." );
  75. }
  76. //------------------------------------------------------------------------------
  77. FontAsset::FontAsset() : mFontFile(StringTable->EmptyString),
  78. mBitmapFont()
  79. {
  80. }
  81. //------------------------------------------------------------------------------
  82. FontAsset::~FontAsset()
  83. {
  84. }
  85. //------------------------------------------------------------------------------
  86. void FontAsset::initPersistFields()
  87. {
  88. // Call parent.
  89. Parent::initPersistFields();
  90. // Fields.
  91. addProtectedField("FontFile", TypeAssetLooseFilePath, Offset(mFontFile, FontAsset), &setFontFile, &defaultProtectedGetFn, &writeFontFile, "The loose file pointing to a .fnt file");
  92. }
  93. //------------------------------------------------------------------------------
  94. bool FontAsset::onAdd()
  95. {
  96. // Call Parent.
  97. if (!Parent::onAdd())
  98. return false;
  99. return true;
  100. }
  101. //------------------------------------------------------------------------------
  102. void FontAsset::onRemove()
  103. {
  104. // Call Parent.
  105. Parent::onRemove();
  106. }
  107. //------------------------------------------------------------------------------
  108. void FontAsset::setFontFile( const char* pFontFile )
  109. {
  110. // Sanity!
  111. AssertFatal( pFontFile != NULL, "Cannot use a NULL Font file." );
  112. // Fetch Font file.
  113. pFontFile = StringTable->insert( pFontFile );
  114. // Ignore no change.
  115. if (pFontFile == mFontFile )
  116. return;
  117. // Update.
  118. mFontFile = getOwned() ? expandAssetFilePath( pFontFile ) : StringTable->insert( pFontFile );
  119. // Refresh the asset.
  120. refreshAsset();
  121. }
  122. //------------------------------------------------------------------------------
  123. void FontAsset::copyTo(SimObject* object)
  124. {
  125. // Call to parent.
  126. Parent::copyTo(object);
  127. // Cast to asset.
  128. FontAsset* pAsset = static_cast<FontAsset*>(object);
  129. // Sanity!
  130. AssertFatal(pAsset != NULL, "FontAsset::copyTo() - Object is not the correct type.");
  131. // Copy state.
  132. pAsset->setFontFile( getFontFile() );
  133. }
  134. //------------------------------------------------------------------------------
  135. void FontAsset::initializeAsset( void )
  136. {
  137. // Call parent.
  138. Parent::initializeAsset();
  139. // Ensure the Font file is expanded.
  140. mFontFile = expandAssetFilePath( mFontFile );
  141. // Build the Font data
  142. buildFontData();
  143. }
  144. //------------------------------------------------------------------------------
  145. void FontAsset::onAssetRefresh( void )
  146. {
  147. // Ignore if not yet added to the sim.
  148. if (!isProperlyAdded() )
  149. return;
  150. // Call parent.
  151. Parent::onAssetRefresh();
  152. buildFontData();
  153. }
  154. //-----------------------------------------------------------------------------
  155. void FontAsset::buildFontData( void )
  156. {
  157. FileStream fStream;
  158. if (!fStream.open(mFontFile, FileStream::Read))
  159. {
  160. Con::printf("Failed to open file '%s'.", expandAssetFilePath(mFontFile));
  161. return;
  162. }
  163. mBitmapFont.mPageName.clear();
  164. mBitmapFont.parseFont(fStream);
  165. fStream.close();
  166. //load the images
  167. mBitmapFont.mTexture.clear();
  168. for (auto iter = mBitmapFont.mPageName.begin(); iter != mBitmapFont.mPageName.end(); iter++)
  169. {
  170. mBitmapFont.mTexture.push_back(mBitmapFont.LoadTexture(expandAssetFilePath(*iter)));
  171. }
  172. }
  173. //-----------------------------------------------------------------------------
  174. void FontAsset::onTamlPreWrite( void )
  175. {
  176. // Call parent.
  177. Parent::onTamlPreWrite();
  178. // Ensure the Font file is collapsed.
  179. mFontFile = collapseAssetFilePath( mFontFile );
  180. }
  181. //-----------------------------------------------------------------------------
  182. void FontAsset::onTamlPostWrite( void )
  183. {
  184. // Call parent.
  185. Parent::onTamlPostWrite();
  186. // Ensure the Font file is expanded.
  187. mFontFile = expandAssetFilePath( mFontFile );
  188. }
  189. //------------------------------------------------------------------------------
  190. void FontAsset::onTamlCustomWrite( TamlCustomNodes& customNodes )
  191. {
  192. // Debug Profiling.
  193. PROFILE_SCOPE(FontAsset_OnTamlCustomWrite);
  194. // Call parent.
  195. Parent::onTamlCustomWrite( customNodes );
  196. }
  197. //-----------------------------------------------------------------------------
  198. void FontAsset::onTamlCustomRead( const TamlCustomNodes& customNodes )
  199. {
  200. // Debug Profiling.
  201. PROFILE_SCOPE(FontAsset_OnTamlCustomRead);
  202. // Call parent.
  203. Parent::onTamlCustomRead( customNodes );
  204. }