123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- //-----------------------------------------------------------------------------
- // Copyright (c) 2013 GarageGames, LLC
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to
- // deal in the Software without restriction, including without limitation the
- // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
- // sell copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- // IN THE SOFTWARE.
- //-----------------------------------------------------------------------------
- #ifndef _CONSOLE_H_
- #include "console/console.h"
- #endif
- #ifndef _CONSOLEINTERNAL_H_
- #include "console/consoleInternal.h"
- #endif
- #ifndef _GBITMAP_H_
- #include "graphics/gBitmap.h"
- #endif
- #ifndef _UTILITY_H_
- #include "2d/core/Utility.h"
- #endif
- #ifndef _SCENE_OBJECT_H_
- #include "2d/sceneobject/SceneObject.h"
- #endif
- #ifndef _FONT_ASSET_H_
- #include "2d/assets/FontAsset.h"
- #endif
- // Script bindings.
- #include "FontAsset_ScriptBinding.h"
- //------------------------------------------------------------------------------
- IMPLEMENT_CONOBJECT(FontAsset);
- //------------------------------------------------------------------------------
- ConsoleType( FontAssetPtr, TypeFontAssetPtr, sizeof(AssetPtr<FontAsset>), ASSET_ID_FIELD_PREFIX )
- //-----------------------------------------------------------------------------
- ConsoleGetType( TypeFontAssetPtr )
- {
- // Fetch asset Id.
- return (*((AssetPtr<FontAsset>*)dptr)).getAssetId();
- }
- //-----------------------------------------------------------------------------
- ConsoleSetType( TypeFontAssetPtr )
- {
- // Was a single argument specified?
- if( argc == 1 )
- {
- // Yes, so fetch field value.
- const char* pFieldValue = argv[0];
- // Fetch asset pointer.
- AssetPtr<FontAsset>* pAssetPtr = dynamic_cast<AssetPtr<FontAsset>*>((AssetPtrBase*)(dptr));
- // Is the asset pointer the correct type?
- if (pAssetPtr == NULL )
- {
- // No, so fail.
- Con::warnf( "(TypeFontAssetPtr) - Failed to set asset Id '%d'.", pFieldValue );
- return;
- }
- // Set asset.
- pAssetPtr->setAssetId( pFieldValue );
- return;
- }
- // Warn.
- Con::warnf( "(TypeFontAssetPtr) - Cannot set multiple args to a single asset." );
- }
- //------------------------------------------------------------------------------
- FontAsset::FontAsset() : mFontFile(StringTable->EmptyString),
- mBitmapFont()
- {
- }
- //------------------------------------------------------------------------------
- FontAsset::~FontAsset()
- {
-
- }
- //------------------------------------------------------------------------------
- void FontAsset::initPersistFields()
- {
- // Call parent.
- Parent::initPersistFields();
- // Fields.
- addProtectedField("FontFile", TypeAssetLooseFilePath, Offset(mFontFile, FontAsset), &setFontFile, &defaultProtectedGetFn, &writeFontFile, "The loose file pointing to a .fnt file");
- }
- //------------------------------------------------------------------------------
- bool FontAsset::onAdd()
- {
- // Call Parent.
- if (!Parent::onAdd())
- return false;
- return true;
- }
- //------------------------------------------------------------------------------
- void FontAsset::onRemove()
- {
- // Call Parent.
- Parent::onRemove();
- }
- //------------------------------------------------------------------------------
- void FontAsset::setFontFile( const char* pFontFile )
- {
- // Sanity!
- AssertFatal( pFontFile != NULL, "Cannot use a NULL Font file." );
- // Fetch Font file.
- pFontFile = StringTable->insert( pFontFile );
- // Ignore no change.
- if (pFontFile == mFontFile )
- return;
- // Update.
- mFontFile = getOwned() ? expandAssetFilePath( pFontFile ) : StringTable->insert( pFontFile );
- // Refresh the asset.
- refreshAsset();
- }
- //------------------------------------------------------------------------------
- void FontAsset::copyTo(SimObject* object)
- {
- // Call to parent.
- Parent::copyTo(object);
- // Cast to asset.
- FontAsset* pAsset = static_cast<FontAsset*>(object);
- // Sanity!
- AssertFatal(pAsset != NULL, "FontAsset::copyTo() - Object is not the correct type.");
- // Copy state.
- pAsset->setFontFile( getFontFile() );
- }
- //------------------------------------------------------------------------------
- void FontAsset::initializeAsset( void )
- {
- // Call parent.
- Parent::initializeAsset();
- // Ensure the Font file is expanded.
- mFontFile = expandAssetFilePath( mFontFile );
- // Build the Font data
- buildFontData();
- }
- //------------------------------------------------------------------------------
- void FontAsset::onAssetRefresh( void )
- {
- // Ignore if not yet added to the sim.
- if (!isProperlyAdded() )
- return;
- // Call parent.
- Parent::onAssetRefresh();
- buildFontData();
- }
- //-----------------------------------------------------------------------------
- void FontAsset::buildFontData( void )
- {
- FileStream fStream;
- if (!fStream.open(mFontFile, FileStream::Read))
- {
- Con::printf("Failed to open file '%s'.", expandAssetFilePath(mFontFile));
- return;
- }
- mBitmapFont.mPageName.clear();
- mBitmapFont.parseFont(fStream);
- fStream.close();
- //load the images
- mBitmapFont.mTexture.clear();
- for (auto iter = mBitmapFont.mPageName.begin(); iter != mBitmapFont.mPageName.end(); iter++)
- {
- mBitmapFont.mTexture.push_back(mBitmapFont.LoadTexture(expandAssetFilePath(*iter)));
- }
- }
- //-----------------------------------------------------------------------------
- void FontAsset::onTamlPreWrite( void )
- {
- // Call parent.
- Parent::onTamlPreWrite();
- // Ensure the Font file is collapsed.
- mFontFile = collapseAssetFilePath( mFontFile );
- }
- //-----------------------------------------------------------------------------
- void FontAsset::onTamlPostWrite( void )
- {
- // Call parent.
- Parent::onTamlPostWrite();
- // Ensure the Font file is expanded.
- mFontFile = expandAssetFilePath( mFontFile );
- }
- //------------------------------------------------------------------------------
- void FontAsset::onTamlCustomWrite( TamlCustomNodes& customNodes )
- {
- // Debug Profiling.
- PROFILE_SCOPE(FontAsset_OnTamlCustomWrite);
- // Call parent.
- Parent::onTamlCustomWrite( customNodes );
- }
- //-----------------------------------------------------------------------------
- void FontAsset::onTamlCustomRead( const TamlCustomNodes& customNodes )
- {
- // Debug Profiling.
- PROFILE_SCOPE(FontAsset_OnTamlCustomRead);
- // Call parent.
- Parent::onTamlCustomRead( customNodes );
- }
|