| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320 |
- //-----------------------------------------------------------------------------
- // 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 _CONSOLETYPES_H_
- #include "console/consoleTypes.h"
- #endif
- #ifndef _PLATFORM_H_
- #include "platform/platform.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 _IMAGE_ASSET_H_
- #include "2d/assets/ImageAsset.h"
- #endif
- // Script bindings.
- #include "ImageAsset_ScriptBinding.h"
- // Debug Profiling.
- #include "debug/profiler.h"
- //------------------------------------------------------------------------------
- ImageAsset::FrameArea BadFrameArea(0, 0, 0, 0, 0.0f, 0.0f);
- //------------------------------------------------------------------------------
- ConsoleType( imageAssetPtr, TypeImageAssetPtr, sizeof(AssetPtr<ImageAsset>), ASSET_ID_FIELD_PREFIX )
- //-----------------------------------------------------------------------------
- ConsoleGetType( TypeImageAssetPtr )
- {
- // Fetch asset Id.
- return (*((AssetPtr<ImageAsset>*)dptr)).getAssetId();
- }
- //-----------------------------------------------------------------------------
- ConsoleSetType( TypeImageAssetPtr )
- {
- // Was a single argument specified?
- if( argc == 1 )
- {
- // Yes, so fetch field value.
- const char* pFieldValue = argv[0];
- // Fetch asset pointer.
- AssetPtr<ImageAsset>* pAssetPtr = dynamic_cast<AssetPtr<ImageAsset>*>((AssetPtrBase*)(dptr));
- // Is the asset pointer the correct type?
- if ( pAssetPtr == NULL )
- {
- // No, so fail.
- Con::warnf( "(TypeImageAssetPtr) - Failed to set asset Id '%d'.", pFieldValue );
- return;
- }
- // Set asset.
- pAssetPtr->setAssetId( pFieldValue );
- return;
- }
- // Warn.
- Con::warnf( "(TypeImageAssetPtr) - Cannot set multiple args to a single asset." );
- }
- //------------------------------------------------------------------------------
- IMPLEMENT_CONOBJECT(ImageAsset);
- //------------------------------------------------------------------------------
- static StringTableEntry cellCustomNodeName = StringTable->insert( "Cells" );
- static StringTableEntry cellNodeName = StringTable->insert( "Cell" );
- static StringTableEntry cellOffsetName = StringTable->insert( "Offset" );
- static StringTableEntry cellWidthName = StringTable->insert( "Width" );
- static StringTableEntry cellHeightName = StringTable->insert( "Height" );
- //------------------------------------------------------------------------------
- static EnumTable::Enums textureFilterLookup[] =
- {
- { ImageAsset::FILTER_NEAREST, "NEAREST" },
- { ImageAsset::FILTER_BILINEAR, "BILINEAR" },
- };
- EnumTable textureFilterTable(sizeof(textureFilterLookup) / sizeof(EnumTable::Enums), &textureFilterLookup[0]);
- //------------------------------------------------------------------------------
- ImageAsset::TextureFilterMode ImageAsset::getFilterModeEnum(const char* label)
- {
- // Search for Mnemonic.
- for(U32 i = 0; i < (sizeof(textureFilterLookup) / sizeof(EnumTable::Enums)); i++)
- if( dStricmp(textureFilterLookup[i].label, label) == 0)
- return((ImageAsset::TextureFilterMode)textureFilterLookup[i].index);
- // Warn.
- Con::warnf("ImageAsset::getFilterModeEnum() - Invalid filter-mode '%s'", label );
- return ImageAsset::FILTER_INVALID;
- }
- //------------------------------------------------------------------------------
- const char* ImageAsset::getFilterModeDescription( ImageAsset::TextureFilterMode filterMode )
- {
- // Search for Mode.
- for(U32 i = 0; i < (sizeof(textureFilterLookup) / sizeof(EnumTable::Enums)); i++)
- if( textureFilterLookup[i].index == filterMode )
- return textureFilterLookup[i].label;
- // Warn.
- Con::warnf("ImageAsset::getFilterModeDescription() - Invalid filter-mode." );
- return StringTable->EmptyString;
- }
- //------------------------------------------------------------------------------
- ImageAsset::ImageAsset() : mImageFile(StringTable->EmptyString),
- mForce16Bit(false),
- mLocalFilterMode(FILTER_INVALID),
- mExplicitMode(false),
- mCellRowOrder(true),
- mCellOffsetX(0),
- mCellOffsetY(0),
- mCellStrideX(0),
- mCellStrideY(0),
- mCellCountX(0),
- mCellCountY(0),
- mCellWidth(0),
- mCellHeight(0),
- mImageTextureHandle(NULL)
- {
- // Set Vector Associations.
- VECTOR_SET_ASSOCIATION( mFrames );
- VECTOR_SET_ASSOCIATION( mExplicitFrames );
- }
- //------------------------------------------------------------------------------
- ImageAsset::~ImageAsset()
- {
- }
- //------------------------------------------------------------------------------
- void ImageAsset::initPersistFields()
- {
- // Call parent.
- Parent::initPersistFields();
- // Fields.
- addProtectedField("ImageFile", TypeAssetLooseFilePath, Offset(mImageFile, ImageAsset), &setImageFile, &getImageFile, &defaultProtectedWriteFn, "");
- addProtectedField("Force16bit", TypeBool, Offset(mForce16Bit, ImageAsset), &setForce16Bit, &defaultProtectedGetFn, &writeForce16Bit, "");
- addProtectedField("FilterMode", TypeEnum, Offset(mLocalFilterMode, ImageAsset), &setFilterMode, &defaultProtectedGetFn, &writeFilterMode, 1, &textureFilterTable);
- addProtectedField("ExplicitMode", TypeBool, Offset(mExplicitMode, ImageAsset), &setExplicitMode, &defaultProtectedGetFn, &defaultProtectedNotWriteFn, "");
- addProtectedField("CellRowOrder", TypeBool, Offset(mCellRowOrder, ImageAsset), &setCellRowOrder, &defaultProtectedGetFn, &writeCellRowOrder, "");
- addProtectedField("CellOffsetX", TypeS32, Offset(mCellOffsetX, ImageAsset), &setCellOffsetX, &defaultProtectedGetFn, &writeCellOffsetX, "");
- addProtectedField("CellOffsetY", TypeS32, Offset(mCellOffsetY, ImageAsset), &setCellOffsetY, &defaultProtectedGetFn, &writeCellOffsetY, "");
- addProtectedField("CellStrideX", TypeS32, Offset(mCellStrideX, ImageAsset), &setCellStrideX, &defaultProtectedGetFn, &writeCellStrideX, "");
- addProtectedField("CellStrideY", TypeS32, Offset(mCellStrideY, ImageAsset), &setCellStrideY, &defaultProtectedGetFn, &writeCellStrideY, "");
- addProtectedField("CellCountX", TypeS32, Offset(mCellCountX, ImageAsset), &setCellCountX, &defaultProtectedGetFn, &writeCellCountX, "");
- addProtectedField("CellCountY", TypeS32, Offset(mCellCountY, ImageAsset), &setCellCountY, &defaultProtectedGetFn, &writeCellCountY, "");
- addProtectedField("CellWidth", TypeS32, Offset(mCellWidth, ImageAsset), &setCellWidth, &defaultProtectedGetFn, &writeCellWidth, "");
- addProtectedField("CellHeight", TypeS32, Offset(mCellHeight, ImageAsset), &setCellHeight, &defaultProtectedGetFn, &writeCellHeight, "");
- }
- //------------------------------------------------------------------------------
- bool ImageAsset::onAdd()
- {
- // Call Parent.
- if (!Parent::onAdd())
- return false;
- return true;
- }
- //------------------------------------------------------------------------------
- void ImageAsset::onRemove()
- {
- // Call Parent.
- Parent::onRemove();
- }
- //------------------------------------------------------------------------------
- void ImageAsset::setImageFile( const char* pImageFile )
- {
- // Sanity!
- AssertFatal( pImageFile != NULL, "Cannot use a NULL image file." );
- // Fetch image file.
- pImageFile = StringTable->insert( pImageFile );
- // Ignore no change,
- if ( pImageFile == mImageFile )
- return;
- // Update.
- mImageFile = getOwned() ? expandAssetFilePath( pImageFile ) : StringTable->insert( pImageFile );
- // Refresh the asset.
- refreshAsset();
- }
- //------------------------------------------------------------------------------
- void ImageAsset::copyTo(SimObject* object)
- {
- // Call to parent.
- Parent::copyTo(object);
- // Cast to asset.
- ImageAsset* pAsset = static_cast<ImageAsset*>(object);
- // Sanity!
- AssertFatal(pAsset != NULL, "ImageAsset::copyTo() - Object is not the correct type.");
- // Copy state.
- pAsset->setImageFile( getImageFile() );
- pAsset->setForce16Bit( getForce16Bit() );
- pAsset->setFilterMode( getFilterMode() );
- pAsset->setExplicitMode( getExplicitMode() );
- pAsset->setCellRowOrder( getCellRowOrder() );
- pAsset->setCellOffsetX( getCellCountX() );
- pAsset->setCellOffsetY( getCellCountY() );
- pAsset->setCellStrideX( getCellStrideX() );
- pAsset->setCellStrideY( getCellStrideY() );
- pAsset->setCellCountX( getCellCountX() );
- pAsset->setCellCountY( getCellCountY() );
- pAsset->setCellWidth( getCellWidth() );
- pAsset->setCellHeight( getCellHeight() );
- // Finish if not in explicit mode.
- if ( !getExplicitMode() )
- return;
- // Fetch the explicit cell count.
- const S32 explicitCellCount = getExplicitCellCount();
- // Finish if no explicit cells exist.
- if ( explicitCellCount == 0 )
- return;
- // Copy explicit cells.
- pAsset->clearExplicitCells();
- for( S32 index = 0; index < explicitCellCount; ++index )
- {
- // Fetch the cell pixel area.
- const FrameArea::PixelArea& pixelArea = getImageFrameArea( index ).mPixelArea;
- // Add the explicit cell.
- pAsset->addExplicitCell( pixelArea.mPixelOffset.x, pixelArea.mPixelOffset.y, pixelArea.mPixelWidth, pixelArea.mPixelHeight );
- }
- }
- //------------------------------------------------------------------------------
- void ImageAsset::setForce16Bit( const bool force16Bit )
- {
- // Ignore no change,
- if ( force16Bit == mForce16Bit )
- return;
- // Update.
- mForce16Bit = force16Bit;
- // Refresh the asset.
- refreshAsset();
- }
- //------------------------------------------------------------------------------
- void ImageAsset::setFilterMode( const ImageAsset::TextureFilterMode filterMode )
- {
- // Ignore no change,
- if ( filterMode == mLocalFilterMode )
- return;
- // Invalid filter mode?
- if ( filterMode == FILTER_INVALID )
- {
- // Yes, so warn.
- Con::warnf( "Cannot set an invalid filter mode." );
- return;
- }
- // Update.
- mLocalFilterMode = filterMode;
- // Refresh the asset.
- refreshAsset();
- }
- //------------------------------------------------------------------------------
- void ImageAsset::setExplicitMode( const bool explicitMode )
- {
- // Ignore no change,
- if ( explicitMode == mExplicitMode )
- return;
- // Update.
- mExplicitMode = explicitMode;
- // Refresh the asset.
- refreshAsset();
- }
- //------------------------------------------------------------------------------
- void ImageAsset::setCellRowOrder( const bool cellRowOrder )
- {
- // Ignore no change.
- if ( cellRowOrder == mCellRowOrder )
- return;
- // Update.
- mCellRowOrder = cellRowOrder;
- // Refresh the asset.
- refreshAsset();
- }
- //------------------------------------------------------------------------------
- void ImageAsset::setCellOffsetX( const S32 cellOffsetX )
- {
- // Ignore no change.
- if ( cellOffsetX == mCellOffsetX )
- return;
- // Valid?
- if ( cellOffsetX < 0 )
- {
- // No, so warn.
- Con::warnf( "Invalid CELL offset X '%d'.", cellOffsetX );
- return;
- }
- // Update.
- mCellOffsetX = cellOffsetX;
- // Refresh the asset.
- refreshAsset();
- }
- //------------------------------------------------------------------------------
- void ImageAsset::setCellOffsetY( const S32 cellOffsetY )
- {
- // Ignore no change.
- if ( cellOffsetY == mCellOffsetY )
- return;
- // Valid?
- if ( cellOffsetY < 0 )
- {
- // No, so warn.
- Con::warnf( "Invalid CELL offset Y '%d'.", cellOffsetY );
- return;
- }
- // Update.
- mCellOffsetY = cellOffsetY;
- // Refresh the asset.
- refreshAsset();
- }
- //------------------------------------------------------------------------------
- void ImageAsset::setCellStrideX( const S32 cellStrideX )
- {
- // Ignore no change.
- if ( cellStrideX == mCellStrideX )
- return;
- // Valid?
- if ( cellStrideX < 0 )
- {
- // No, so warn.
- Con::warnf( "Invalid CELL stride X '%d'.", cellStrideX );
- return;
- }
- // Update.
- mCellStrideX = cellStrideX;
- // Refresh the asset.
- refreshAsset();
- }
- //------------------------------------------------------------------------------
- void ImageAsset::setCellStrideY( const S32 cellStrideY )
- {
- // Ignore no change.
- if ( cellStrideY == mCellStrideY )
- return;
- // Valid?
- if ( cellStrideY < 0 )
- {
- // No, so warn.
- Con::warnf( "Invalid CELL stride Y '%d'.", cellStrideY );
- return;
- }
- // Update.
- mCellStrideY = cellStrideY;
- // Refresh the asset.
- refreshAsset();
- }
- //------------------------------------------------------------------------------
- void ImageAsset::setCellCountX( const S32 cellCountX )
- {
- // Ignore no change.
- if ( cellCountX == mCellCountX )
- return;
- // Valid?
- if ( cellCountX < 0 )
- {
- // No, so warn.
- Con::warnf( "Invalid CELL count X '%d'.", cellCountX );
- return;
- }
- // Update.
- mCellCountX = cellCountX;
- // Refresh the asset.
- refreshAsset();
- }
- //------------------------------------------------------------------------------
- void ImageAsset::setCellCountY( const S32 cellCountY )
- {
- // Ignore no change.
- if ( cellCountY == mCellCountY )
- return;
- // Valid?
- if ( cellCountY < 0 )
- {
- // No, so warn.
- Con::warnf( "Invalid CELL count Y '%d'.", cellCountY );
- return;
- }
- // Update.
- mCellCountY = cellCountY;
- // Refresh the asset.
- refreshAsset();
- }
- //------------------------------------------------------------------------------
- void ImageAsset::setCellWidth( const S32 cellWidth )
- {
- // Ignore no change.
- if ( cellWidth == mCellWidth )
- return;
- // Valid?
- if ( cellWidth < 0 )
- {
- // No, so warn.
- Con::warnf( "Invalid CELL width '%d'.", cellWidth );
- return;
- }
- // Update.
- mCellWidth = cellWidth;
- // Refresh the asset.
- refreshAsset();
- }
- //------------------------------------------------------------------------------
- void ImageAsset::setCellHeight( const S32 cellheight )
- {
- // Ignore no change.
- if ( cellheight == mCellHeight )
- return;
- // Valid?
- if ( cellheight < 0 )
- {
- // No, so warn.
- Con::warnf( "Invalid CELL height '%d'.", cellheight );
- return;
- }
- // Update.
- mCellHeight = cellheight;
- // Refresh the asset.
- refreshAsset();
- }
- //------------------------------------------------------------------------------
- bool ImageAsset::clearExplicitCells( void )
- {
- // Are we in explicit mode?
- if ( !getExplicitMode() )
- {
- // No, so warn.
- Con::warnf( "ImageAsset() - Cannot perform explicit cell operation when not in explicit mode." );
- return false;
- }
- // Clear explicit frames.
- mExplicitFrames.clear();
- // Refresh the asset.
- refreshAsset();
- return true;
- }
- //------------------------------------------------------------------------------
- bool ImageAsset::addExplicitCell( const S32 cellOffsetX, const S32 cellOffsetY, const S32 cellWidth, const S32 cellHeight )
- {
- // Are we in explicit mode?
- if ( !getExplicitMode() )
- {
- // No, so warn.
- Con::warnf( "ImageAsset::addCell() - Cannot perform explicit cell operation when not in explicit mode." );
- return false;
- }
- // Fetch the original image dimensions.
- const S32 imageWidth = getImageWidth();
- const S32 imageHeight = getImageHeight();
- // The Cell Offset X needs to be within the image.
- if ( cellOffsetX < 0 || cellOffsetX >= imageWidth )
- {
- // Warn.
- Con::warnf("ImageAsset::addCell() - Invalid Cell OffsetX of %d.", cellOffsetX );
- return false;
- }
- // The Cell Offset Y needs to be within the image.
- if ( cellOffsetY < 0 || cellOffsetY >= imageWidth )
- {
- // Warn.
- Con::warnf("ImageAsset::addCell() - Invalid Cell OffsetY of %d.", cellOffsetY );
- return false;
- }
- // The Cell Width needs to be within the image.
- if ( cellWidth <= 0 || (cellOffsetX+cellWidth) > imageWidth )
- {
- // Warn.
- Con::warnf("ImageAsset::addCell() - Invalid Cell Width of %d.", cellWidth );
- return false;
- }
- // The Cell Height needs to be within the image.
- if ( cellHeight <= 0 || (cellOffsetY+cellHeight) > imageHeight )
- {
- // Warn.
- Con::warnf("ImageAsset::addCell() - Invalid Cell Width of %d.", cellHeight );
- return false;
- }
- // Store frame.
- FrameArea::PixelArea pixelArea( cellOffsetX, cellOffsetY, cellWidth, cellHeight );
- mExplicitFrames.push_back( pixelArea );
- // Refresh the asset.
- refreshAsset();
- return true;
- }
- //------------------------------------------------------------------------------
- bool ImageAsset::insertExplicitCell( const S32 cellIndex, const S32 cellOffsetX, const S32 cellOffsetY, const S32 cellWidth, const S32 cellHeight )
- {
- // Are we in explicit mode?
- if ( !getExplicitMode() )
- {
- // No, so warn.
- Con::warnf( "ImageAsset::insertCell() - Cannot perform explicit cell operation when not in explicit mode." );
- return false;
- }
- // Fetch the original image dimensions.
- const S32 imageWidth = getImageWidth();
- const S32 imageHeight = getImageHeight();
- // Fetch the explicit frame count.
- const S32 explicitFramelCount = mExplicitFrames.size();
- // The cell index needs to be in range.
- if ( cellIndex < 0 )
- {
- // Warn.
- Con::warnf("ImageAsset::insertCell() - Invalid Cell Index of %d.", cellIndex );
- return false;
- }
- // The Cell Offset X needs to be within the image.
- if ( cellOffsetX < 0 || cellOffsetX >= imageWidth )
- {
- // Warn.
- Con::warnf("ImageAsset::insertCell() - Invalid Cell OffsetX of %d.", cellOffsetX );
- return false;
- }
- // The Cell Offset Y needs to be within the image.
- if ( cellOffsetY < 0 || cellOffsetY >= imageWidth )
- {
- // Warn.
- Con::warnf("ImageAsset::insertCell() - Invalid Cell OffsetY of %d.", cellOffsetY );
- return false;
- }
- // The Cell Width needs to be within the image.
- if ( cellWidth <= 0 || (cellOffsetX+cellWidth) > imageWidth )
- {
- // Warn.
- Con::warnf("ImageAsset::insertCell() - Invalid Cell Width of %d.", cellWidth );
- return false;
- }
- // The Cell Height needs to be within the image.
- if ( cellHeight <= 0 || (cellOffsetY+cellHeight) > imageHeight )
- {
- // Warn.
- Con::warnf("ImageAsset::insertCell() - Invalid Cell Width of %d.", cellHeight );
- return false;
- }
- // Configure frame.
- FrameArea::PixelArea pixelArea( cellOffsetX, cellOffsetY, cellWidth, cellHeight );
- // Insert frame appropriately.
- if ( cellIndex >= explicitFramelCount )
- {
- mExplicitFrames.push_back( pixelArea );
- }
- else
- {
- mExplicitFrames.insert( cellIndex );
- mExplicitFrames[cellIndex] = pixelArea;
- }
- // Refresh the asset.
- refreshAsset();
- return true;
- }
- //------------------------------------------------------------------------------
- bool ImageAsset::setExplicitCell( const S32 cellIndex, const S32 cellOffsetX, const S32 cellOffsetY, const S32 cellWidth, const S32 cellHeight )
- {
- // Are we in explicit mode?
- if ( !getExplicitMode() )
- {
- // No, so warn.
- Con::warnf( "ImageAsset::setCell() - Cannot perform explicit cell operation when not in explicit mode." );
- return false;
- }
- // Fetch the original image dimensions.
- const S32 imageWidth = getImageWidth();
- const S32 imageHeight = getImageHeight();
- // Fetch the explicit frame count.
- const S32 explicitFrameCount = mExplicitFrames.size();
- // The cell index needs to be in range.
- if ( cellIndex < 0 || cellIndex >= explicitFrameCount )
- {
- // Warn.
- Con::warnf("ImageAsset::setCell() - Invalid Cell Index of %d.", cellIndex );
- return false;
- }
- // The Cell Offset X needs to be within the image.
- if ( cellOffsetX < 0 || cellOffsetX >= imageWidth )
- {
- // Warn.
- Con::warnf("ImageAsset::setCell() - Invalid Cell OffsetX of %d.", cellOffsetX );
- return false;
- }
- // The Cell Offset Y needs to be within the image.
- if ( cellOffsetY < 0 || cellOffsetY >= imageWidth )
- {
- // Warn.
- Con::warnf("ImageAsset::setCell() - Invalid Cell OffsetY of %d.", cellOffsetY );
- return false;
- }
- // The Cell Width needs to be within the image.
- if ( cellWidth <= 0 || (cellOffsetX+cellWidth) > imageWidth )
- {
- // Warn.
- Con::warnf("ImageAsset::setCell() - Invalid Cell Width of %d.", cellWidth );
- return false;
- }
- // The Cell Height needs to be within the image.
- if ( cellHeight <= 0 || (cellOffsetY+cellHeight) > imageHeight )
- {
- // Warn.
- Con::warnf("ImageAsset::setCell() - Invalid Cell Width of %d.", cellHeight );
- return false;
- }
- // Configure frame.
- FrameArea::PixelArea pixelArea( cellOffsetX, cellOffsetY, cellWidth, cellHeight );
- // Set cell.
- mExplicitFrames[cellIndex] = pixelArea;
- // Refresh the asset.
- refreshAsset();
- return true;
- }
- //------------------------------------------------------------------------------
- bool ImageAsset::removeExplicitCell( const S32 cellIndex )
- {
- // Are we in explicit mode?
- if ( !getExplicitMode() )
- {
- // No, so warn.
- Con::warnf( "ImageAsset::removeCell() - Cannot perform explicit cell operation when not in explicit mode." );
- return false;
- }
- // Fetch the explicit frame count.
- const S32 explicitFrameCount = mExplicitFrames.size();
- // The cell index needs to be in range.
- if ( cellIndex < 0 || cellIndex >= explicitFrameCount )
- {
- // Warn.
- Con::warnf("ImageAsset::removeCell() - Invalid Cell Index of %d.", cellIndex );
- return false;
- }
- // Remove cell.
- mExplicitFrames.erase(cellIndex);
- // Refresh the asset.
- refreshAsset();
- return true;
- }
- //------------------------------------------------------------------------------
- void ImageAsset::setTextureFilter( const TextureFilterMode filterMode )
- {
- // Finish if no texture.
- if ( mImageTextureHandle.IsNull() )
- return;
- // Select Hardware Filter Mode.
- GLint glFilterMode;
- switch( filterMode )
- {
- // Nearest ("none").
- case FILTER_NEAREST:
- {
- glFilterMode = GL_NEAREST;
- } break;
- // Bilinear ("smooth").
- case FILTER_BILINEAR:
- {
- glFilterMode = GL_LINEAR;
- } break;
- // Huh?
- default:
- // Oh well...
- glFilterMode = GL_LINEAR;
- };
- // Set the texture objects filter mode.
- mImageTextureHandle.setFilter( glFilterMode );
- }
- //------------------------------------------------------------------------------
- void ImageAsset::initializeAsset( void )
- {
- // Call parent.
- Parent::initializeAsset();
- // Ensure the image-file is expanded.
- mImageFile = expandAssetFilePath( mImageFile );
- // Calculate the image.
- calculateImage();
- }
- //------------------------------------------------------------------------------
- void ImageAsset::onAssetRefresh( void )
- {
- // Ignore if not yet added to the sim.
- if ( !isProperlyAdded() )
- return;
- // Call parent.
- Parent::onAssetRefresh();
-
- // Compile image.
- calculateImage();
- }
- //-----------------------------------------------------------------------------
- void ImageAsset::onTamlPreWrite( void )
- {
- // Call parent.
- Parent::onTamlPreWrite();
- // Ensure the image-file is collapsed.
- mImageFile = collapseAssetFilePath( mImageFile );
- }
- //-----------------------------------------------------------------------------
- void ImageAsset::onTamlPostWrite( void )
- {
- // Call parent.
- Parent::onTamlPostWrite();
- // Ensure the image-file is expanded.
- mImageFile = expandAssetFilePath( mImageFile );
- }
- //------------------------------------------------------------------------------
- void ImageAsset::onTamlCustomWrite( TamlCustomNodes& customNodes )
- {
- // Debug Profiling.
- PROFILE_SCOPE(ImageAsset_OnTamlCustomWrite);
- // Call parent.
- Parent::onTamlCustomWrite( customNodes );
- // Finish if not in explicit mode.
- if ( !mExplicitMode )
- return;
- // Add cell custom node.
- TamlCustomNode* pCustomCellNodes = customNodes.addNode( cellCustomNodeName );
- // Iterate explicit frames.
- for( typeExplicitFrameAreaVector::iterator frameItr = mExplicitFrames.begin(); frameItr != mExplicitFrames.end(); ++frameItr )
- {
- // Fetch pixel area.
- const FrameArea::PixelArea& pixelArea = *frameItr;
- // Add cell alias.
- TamlCustomNode* pCellNode = pCustomCellNodes->addNode( cellNodeName );
- // Add cell properties.
- pCellNode->addField( cellOffsetName, pixelArea.mPixelOffset );
- pCellNode->addField( cellWidthName, pixelArea.mPixelWidth );
- pCellNode->addField( cellHeightName, pixelArea.mPixelHeight );
- }
- }
- //-----------------------------------------------------------------------------
- void ImageAsset::onTamlCustomRead( const TamlCustomNodes& customNodes )
- {
- // Debug Profiling.
- PROFILE_SCOPE(ImageAsset_OnTamlCustomRead);
- // Call parent.
- Parent::onTamlCustomRead( customNodes );
- // Find cell custom node.
- const TamlCustomNode* pCustomCellNodes = customNodes.findNode( cellCustomNodeName );
- // Finish if we don't have explicit cells.
- if ( pCustomCellNodes == NULL )
- return;
- // Set explicit mode.
- mExplicitMode = true;
- // Fetch children cell nodes.
- const TamlCustomNodeVector& cellNodes = pCustomCellNodes->getChildren();
- // Iterate cells.
- for( TamlCustomNodeVector::const_iterator cellNodeItr = cellNodes.begin(); cellNodeItr != cellNodes.end(); ++cellNodeItr )
- {
- // Fetch cell node.
- TamlCustomNode* pCellNode = *cellNodeItr;
- // Fetch node name.
- StringTableEntry nodeName = pCellNode->getNodeName();
- // Is this a valid alias?
- if ( nodeName != cellNodeName )
- {
- // No, so warn.
- Con::warnf( "ImageAsset::onTamlCustomRead() - Encountered an unknown custom name of '%s'. Only '%s' is valid.", nodeName, cellNodeName );
- continue;
- }
- Point2I cellOffset(-1, -1);
- S32 cellWidth = 0;
- S32 cellHeight = 0;
- // Fetch fields.
- const TamlCustomFieldVector& fields = pCellNode->getFields();
- // Iterate property fields.
- for ( TamlCustomFieldVector::const_iterator fieldItr = fields.begin(); fieldItr != fields.end(); ++fieldItr )
- {
- // Fetch field.
- const TamlCustomField* pField = *fieldItr;
- // Fetch field name.
- StringTableEntry fieldName = pField->getFieldName();
- // Check common fields.
- if ( fieldName == cellOffsetName )
- {
- pField->getFieldValue( cellOffset );
- }
- else if ( fieldName == cellWidthName )
- {
- pField->getFieldValue( cellWidth );
- }
- else if ( fieldName == cellHeightName )
- {
- pField->getFieldValue( cellHeight );
- }
- else
- {
- // Unknown name so warn.
- Con::warnf( "ImageAsset::onTamlCustomRead() - Encountered an unknown custom field name of '%s'.", fieldName );
- continue;
- }
- }
- // Is cell offset valid?
- if ( cellOffset.x < 0 || cellOffset.y < 0 )
- {
- // No, so warn.
- Con::warnf( "ImageAsset::onTamlCustomRead() - Cell offset of '(%d,%d)' is invalid or was not set.", cellOffset.x, cellOffset.y );
- continue;
- }
- // Is cell width valid?
- if ( cellWidth <= 0 )
- {
- // No, so warn.
- Con::warnf( "ImageAsset::onTamlCustomRead() - Cell width of '%d' is invalid or was not set.", cellWidth );
- continue;
- }
- // Is cell height valid?
- if ( cellHeight <= 0 )
- {
- // No, so warn.
- Con::warnf( "ImageAsset::onTamlCustomRead() - Cell height of '%d' is invalid or was not set.", cellHeight );
- continue;
- }
- // Add explicit frame.
- FrameArea::PixelArea pixelArea( cellOffset.x, cellOffset.y, cellWidth, cellHeight );
- mExplicitFrames.push_back( pixelArea );
- }
- }
- //------------------------------------------------------------------------------
- void ImageAsset::calculateImage( void )
- {
- // Debug Profiling.
- PROFILE_SCOPE(ImageAsset_CalculateImage);
- // Clear frames.
- mFrames.clear();
- // If we have an existing texture and we're setting to the same bitmap then force the texture manager
- // to refresh the texture itself.
- if ( !mImageTextureHandle.IsNull() && dStricmp(mImageTextureHandle.getTextureKey(), mImageFile) == 0 )
- TextureManager::refresh( mImageFile );
- // Get image texture.
- mImageTextureHandle.set( mImageFile, TextureHandle::BitmapTexture, true, getForce16Bit() );
- // Is the texture valid?
- if ( mImageTextureHandle.IsNull() )
- {
- // No, so warn.
- Con::warnf( "Image '%s' could not load texture '%s'.", getAssetId(), mImageFile );
- return;
- }
- // Is the local filter mode specified?
- if ( mLocalFilterMode != FILTER_INVALID )
- {
- // Yes, so set filter mode.
- setTextureFilter( mLocalFilterMode );
- }
- else
- {
- TextureFilterMode filterMode = FILTER_NEAREST;
- // No, so fetch the global filter.
- const char* pGlobalFilter = Con::getVariable( "$pref::T2D::imageAssetGlobalFilterMode" );
- // Fetch the global filter mode.
- if ( pGlobalFilter != NULL && dStrlen(pGlobalFilter) > 0 )
- filterMode = getFilterModeEnum( pGlobalFilter );
- // If global filter mode is invalid then use local filter mode.
- if ( filterMode == FILTER_INVALID )
- filterMode = FILTER_NEAREST;
- // Set filter mode.
- setTextureFilter( filterMode );
- }
- // Calculate according to mode.
- if ( mExplicitMode )
- {
- calculateExplicitMode();
- }
- else
- {
- calculateImplicitMode();
- }
- }
- //------------------------------------------------------------------------------
- void ImageAsset::calculateImplicitMode( void )
- {
- // Debug Profiling.
- PROFILE_SCOPE(ImageAsset_CalculateImplicitMode);
- // Sanity!
- AssertFatal( !mExplicitMode, "Cannot calculate implicit cells when in explicit mode." );
- // Fetch the texture object.
- TextureObject* pTextureObject = ((TextureObject*)mImageTextureHandle);
-
- // Calculate texel scales.
- const F32 texelWidthScale = 1.0f / (F32)pTextureObject->getTextureWidth();
- const F32 texelHeightScale = 1.0f / (F32)pTextureObject->getTextureHeight();
- // Fetch the original image dimensions.
- const S32 imageWidth = getImageWidth();
- const S32 imageHeight = getImageHeight();
- // Set full-frame as default.
- FrameArea frameArea( 0, 0, imageWidth, imageHeight, texelWidthScale, texelHeightScale );
- mFrames.push_back( frameArea );
- // Finish if no cell counts are specified. This is how we default to full-frame mode.
- if ( mCellCountX < 1 || mCellCountY < 1 )
- return;
- // The cell width needs to be at maximum the image width!
- if ( mCellWidth < 1 || mCellWidth > imageWidth )
- {
- // Warn.
- Con::warnf("ImageAsset::calculateImage() - Invalid Cell Width of %d.", mCellWidth );
- return;
- }
- // The cell height needs to be at maximum the image height!
- if ( mCellHeight < 1 || mCellHeight > imageWidth )
- {
- // Warn.
- Con::warnf("ImageAsset::calculateImage() - Invalid Cell Height of %d.", mCellHeight );
- return;
- }
- // The Cell Offset X needs to be within the image.
- if ( mCellOffsetX < 0 || mCellOffsetX >= imageWidth )
- {
- // Warn.
- Con::warnf("ImageAsset::calculateImage() - Invalid Cell OffsetX of %d.", mCellOffsetX );
- return;
- }
- // The Cell Offset Y needs to be within the image.
- if ( mCellOffsetY < 0 || mCellOffsetY >= imageWidth )
- {
- // Warn.
- Con::warnf("ImageAsset::calculateImage() - Invalid Cell OffsetY of %d.", mCellOffsetY );
- return;
- }
- // Are we using Cell-StrideX?
- S32 cellStepX;
- if ( mCellStrideX != 0 )
- {
- // Yes, so set stepX to be StrideX.
- cellStepX = mCellStrideX;
- }
- else
- {
- // No, so set stepY to be Cell Width.
- cellStepX = mCellWidth;
- }
- // Are we using Cell-StrideY?
- S32 cellStepY;
- if ( mCellStrideY != 0 )
- {
- // Yes, so set stepY to be StrideY.
- cellStepY = mCellStrideY;
- }
- else
- {
- // No, so set stepY to be Cell Height.
- cellStepY = mCellHeight;
- }
- // Calculate Final Cell Position X.
- S32 cellFinalPositionX = mCellOffsetX + ((mCellCountX-((cellStepX<0)?1:0))*cellStepX);
- // Off Left?
- if ( cellFinalPositionX < 0 )
- {
- // Warn.
- Con::warnf("ImageAsset::calculateImage() - Invalid Cell OffsetX(%d)/Width(%d)/CountX(%d); off image left-hand-side.", mCellOffsetX, mCellWidth, mCellCountX );
- return;
- }
- // Off Right?
- else if ( cellFinalPositionX > imageWidth )
- {
- // Warn.
- Con::warnf("ImageAsset::calculateImage() - Invalid Cell OffsetX(%d)/Width(%d)/CountX(%d); off image right-hand-side.", mCellOffsetX, mCellWidth, mCellCountX );
- return;
- }
- // Calculate Final Cell Position Y.
- S32 cellFinalPositionY = mCellOffsetY + ((mCellCountY-((cellStepY<0)?1:0))*cellStepY);
- // Off Top?
- if ( cellFinalPositionY < 0 )
- {
- // Warn.
- Con::warnf("ImageAsset::calculateImage() - Invalid Cell OffsetY(%d)/Height(%d)/CountY(%d); off image top-side.", mCellOffsetY, mCellHeight, mCellCountY );
- return;
- }
- // Off Bottom?
- else if ( cellFinalPositionY > imageHeight )
- {
- // Warn.
- Con::warnf("ImageAsset::calculateImage() - Invalid Cell OffsetY(%d)/Height(%d)/CountY(%d); off image bottom-side.", mCellOffsetY, mCellHeight, mCellCountY );
- return;
- }
- // Clear default frame.
- mFrames.clear();
- // Cell Row Order?
- if ( mCellRowOrder )
- {
- // Yes, so RowRow Order.
- for ( S32 y = 0, cellPositionY = mCellOffsetY; y < mCellCountY; y++, cellPositionY+=cellStepY )
- {
- for ( S32 x = 0, cellPositionX = mCellOffsetX; x < mCellCountX; x++, cellPositionX+=cellStepX )
- {
- // Set frame area.
- frameArea.setArea( cellPositionX, cellPositionY, mCellWidth, mCellHeight, texelWidthScale, texelHeightScale );
- // Store fame.
- mFrames.push_back( frameArea );
- }
- }
- return;
- }
- // No, so Column Order.
- for ( S32 x = 0, cellPositionX = mCellOffsetX; x < mCellCountX; x++, cellPositionX+=cellStepX )
- {
- for ( S32 y = 0, cellPositionY = mCellOffsetY; y < mCellCountY; y++, cellPositionY+=cellStepY )
- {
- // Set frame area.
- frameArea.setArea( cellPositionX, cellPositionY, mCellWidth, mCellHeight, texelWidthScale, texelHeightScale );
- // Store fame.
- mFrames.push_back( frameArea );
- }
- }
- }
- //------------------------------------------------------------------------------
- void ImageAsset::calculateExplicitMode( void )
- {
- // Debug Profiling.
- PROFILE_SCOPE(ImageAsset_CalculateExplicitMode);
- // Sanity!
- AssertFatal( mExplicitMode, "Cannot calculate explicit cells when not in explicit mode." );
- // Fetch the texture object.
- TextureObject* pTextureObject = ((TextureObject*)mImageTextureHandle);
-
- // Calculate texel scales.
- const F32 texelWidthScale = 1.0f / (F32)pTextureObject->getTextureWidth();
- const F32 texelHeightScale = 1.0f / (F32)pTextureObject->getTextureHeight();
- // Fetch the original image dimensions.
- const S32 imageWidth = getImageWidth();
- const S32 imageHeight = getImageHeight();
- // Clear default frame.
- mFrames.clear();
- // Are any explicit frames set.
- if ( mExplicitFrames.size() == 0 )
- {
- // No, so set full-frame as default.
- FrameArea frameArea( 0, 0, imageWidth, imageHeight, texelWidthScale, texelHeightScale );
- mFrames.push_back( frameArea );
- return;
- }
- // Iterate explicit frames.
- for( typeExplicitFrameAreaVector::iterator frameItr = mExplicitFrames.begin(); frameItr != mExplicitFrames.end(); ++frameItr )
- {
- // Fetch pixel area.
- const FrameArea::PixelArea& pixelArea = *frameItr;
- // Set frame area.
- FrameArea frameArea( pixelArea.mPixelOffset.x, pixelArea.mPixelOffset.y, pixelArea.mPixelWidth, pixelArea.mPixelHeight, texelWidthScale, texelHeightScale );
- // Store frame.
- mFrames.push_back( frameArea );
- }
- }
- //------------------------------------------------------------------------------
- bool ImageAsset::setFilterMode( void* obj, const char* data )
- {
- static_cast<ImageAsset*>(obj)->setFilterMode(getFilterModeEnum(data));
- return false;
- }
|