123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436 |
- //-----------------------------------------------------------------------------
- // 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.
- //-----------------------------------------------------------------------------
- #include "console/consoleTypes.h"
- #include "2d/assets/ImageAsset.h"
- #include "2d/assets/AnimationAsset.h"
- // Script bindings.
- #include "AnimationAsset_ScriptBinding.h"
- // Debug Profiling.
- #include "debug/profiler.h"
- //------------------------------------------------------------------------------
- ConsoleType( animationAssetPtr, TypeAnimationAssetPtr, sizeof(AssetPtr<AnimationAsset>), ASSET_ID_FIELD_PREFIX )
- //-----------------------------------------------------------------------------
- ConsoleGetType( TypeAnimationAssetPtr )
- {
- // Fetch asset Id.
- return (*((AssetPtr<AnimationAsset>*)dptr)).getAssetId();
- }
- //-----------------------------------------------------------------------------
- ConsoleSetType( TypeAnimationAssetPtr )
- {
- // Was a single argument specified?
- if( argc == 1 )
- {
- // Yes, so fetch field value.
- const char* pFieldValue = argv[0];
- // Fetch asset pointer.
- AssetPtr<AnimationAsset>* pAssetPtr = dynamic_cast<AssetPtr<AnimationAsset>*>((AssetPtrBase*)(dptr));
- // Is the asset pointer the correct type?
- if ( pAssetPtr == NULL )
- {
- // No, so fail.
- Con::warnf( "(TypeAnimationAssetPtr) - Failed to set asset Id '%d'.", pFieldValue );
- return;
- }
- // Set asset.
- pAssetPtr->setAssetId( pFieldValue );
- return;
- }
- // Warn.
- Con::warnf( "(TypeAnimationAssetPtr) - Cannot set multiple args to a single asset." );
- }
- //------------------------------------------------------------------------------
- IMPLEMENT_CONOBJECT(AnimationAsset);
- //------------------------------------------------------------------------------
- AnimationAsset::AnimationAsset() : mAnimationTime(1.0f),
- mAnimationCycle(true),
- mRandomStart(false),
- mAnimationIntegration(0.0f),
- mNamedCellsMode(false)
- {
- // Set Vector Associations.
- VECTOR_SET_ASSOCIATION( mAnimationFrames );
- VECTOR_SET_ASSOCIATION( mNamedAnimationFrames );
- VECTOR_SET_ASSOCIATION( mValidatedFrames );
- }
- //------------------------------------------------------------------------------
- AnimationAsset::~AnimationAsset()
- {
- }
- //------------------------------------------------------------------------------
- void AnimationAsset::initPersistFields()
- {
- // Call parent.
- Parent::initPersistFields();
- addProtectedField("Image", TypeImageAssetPtr, Offset(mImageAsset, AnimationAsset), &setImage, &defaultProtectedGetFn, &writeImage, "");
- addProtectedField("AnimationFrames", TypeS32Vector, Offset(mAnimationFrames, AnimationAsset), &setAnimationFrames, &defaultProtectedGetFn, &writeAnimationFrames, "");
- addProtectedField("NamedAnimationFrames", TypeStringTableEntryVector, Offset(mNamedAnimationFrames, AnimationAsset), &setNamedAnimationFrames, &defaultProtectedGetFn, &writeNamedAnimationFrames, "");
- addProtectedField("AnimationTime", TypeF32, Offset(mAnimationTime, AnimationAsset), &setAnimationTime, &defaultProtectedGetFn, &defaultProtectedWriteFn, "");
- addProtectedField("AnimationCycle", TypeBool, Offset(mAnimationCycle, AnimationAsset), &setAnimationCycle, &defaultProtectedGetFn, &writeAnimationCycle, "");
- addProtectedField("RandomStart", TypeBool, Offset(mRandomStart, AnimationAsset), &setRandomStart, &defaultProtectedGetFn, &writeRandomStart, "");
- addProtectedField("NamedCellsMode", TypeBool, Offset(mNamedCellsMode, AnimationAsset), &setNamedCellsMode, &defaultProtectedGetFn, &writeNamedCellsMode, "");
- }
- //------------------------------------------------------------------------------
- bool AnimationAsset::onAdd()
- {
- // Call Parent.
- if(!Parent::onAdd())
- return false;
- // Return Okay.
- return true;
- }
- //------------------------------------------------------------------------------
- void AnimationAsset::onRemove()
- {
- // Call Parent.
- Parent::onRemove();
- }
- //------------------------------------------------------------------------------
- void AnimationAsset::onAssetRefresh( void )
- {
- // Ignore if not yet added to the sim.
- if ( !isProperlyAdded() )
- return;
- // Call parent.
- Parent::onAssetRefresh();
- }
- //------------------------------------------------------------------------------
- void AnimationAsset::copyTo(SimObject* object)
- {
- // Call to parent.
- Parent::copyTo(object);
- // Cast to asset.
- AnimationAsset* pAsset = static_cast<AnimationAsset*>(object);
- // Sanity!
- AssertFatal(pAsset != NULL, "AnimationAsset::copyTo() - Object is not the correct type.");
- // Copy state.
- pAsset->setImage( getImage().getAssetId() );
- // Are we in named cells mode?
- if ( !pAsset->getNamedCellsMode() )
- pAsset->setAnimationFrames( Con::getData( TypeS32Vector, (void*)&getSpecifiedAnimationFrames(), 0 ) );
- else
- pAsset->setNamedAnimationFrames( Con::getData( TypeStringTableEntryVector, (void*)&getSpecifiedNamedAnimationFrames(), 0 ) );
- pAsset->setAnimationTime( getAnimationTime() );
- pAsset->setAnimationCycle( getAnimationCycle() );
- pAsset->setRandomStart( getRandomStart() );
- pAsset->setNamedCellsMode( getNamedCellsMode() );
- }
- //------------------------------------------------------------------------------
- void AnimationAsset::setImage( const char* pAssetId )
- {
- // Ignore no change.
- if ( mImageAsset.getAssetId() == StringTable->insert( pAssetId ) )
- return;
- // Update.
- mImageAsset = pAssetId;
- // Validate frames.
- validateFrames();
- // Refresh the asset.
- refreshAsset();
- }
- //------------------------------------------------------------------------------
- void AnimationAsset::setAnimationFrames( const char* pAnimationFrames )
- {
- // Debug Profiling.
- PROFILE_SCOPE(AnimationAsset_SetAnimationFrames);
- // Clear any existing frames.
- mAnimationFrames.clear();
- // Fetch frame count.
- const U32 frameCount = StringUnit::getUnitCount( pAnimationFrames, " \t\n" );
- // Iterate frames.
- for( U32 frameIndex = 0; frameIndex < frameCount; ++frameIndex )
- {
- // Store frame.
- mAnimationFrames.push_back( dAtoi( StringUnit::getUnit( pAnimationFrames, frameIndex, " \t\n" ) ) );
- }
- mNamedCellsMode = false;
- // Validate frames.
- validateFrames();
- // Refresh the asset.
- refreshAsset();
- }
- //------------------------------------------------------------------------------
- void AnimationAsset::setNamedAnimationFrames( const char* pAnimationFrames )
- {
- // Clear any existing frames.
- mNamedAnimationFrames.clear();
- // Fetch frame count.
- const U32 frameCount = StringUnit::getUnitCount( pAnimationFrames, " \t\n" );
- // Iterate frames.
- for( U32 frameIndex = 0; frameIndex < frameCount; ++frameIndex )
- {
- // Store frame.
- mNamedAnimationFrames.push_back( StringTable->insert( StringUnit::getUnit( pAnimationFrames, frameIndex, " \t\n" ) ) );
- }
- mNamedCellsMode = true;
- // Validate frames.
- validateFrames();
- // Refresh the asset.
- refreshAsset();
- }
- //------------------------------------------------------------------------------
- void AnimationAsset::setAnimationTime( const F32 animationTime )
- {
- // Ignore no change,
- if ( mIsEqual( animationTime, mAnimationTime ) )
- return;
- // Update.
- mAnimationTime = animationTime;
- // Refresh the asset.
- refreshAsset();
- }
- //------------------------------------------------------------------------------
- void AnimationAsset::setAnimationCycle( const bool animationCycle )
- {
- // Ignore no change.
- if ( animationCycle == mAnimationCycle )
- return;
- // Update.
- mAnimationCycle = animationCycle;
- // Refresh the asset.
- refreshAsset();
- }
- //------------------------------------------------------------------------------
- void AnimationAsset::setRandomStart( const bool randomStart )
- {
- // Ignore no change.
- if ( randomStart == mRandomStart )
- return;
- // Update.
- mRandomStart = randomStart;
- // Refresh the asset.
- refreshAsset();
- }
- //------------------------------------------------------------------------------
- void AnimationAsset::setNamedCellsMode( const bool namedCellsMode )
- {
- // Ignore no change.
- if ( namedCellsMode == mNamedCellsMode)
- return;
- // Update.
- mNamedCellsMode = namedCellsMode;
- // Refresh the asset.
- refreshAsset();
- }
- //------------------------------------------------------------------------------
- void AnimationAsset::validateNumericalFrames( void )
- {
- // Clear validated frames.
- mValidatedFrames.clear();
- // Fetch Animation Frame Count.
- const U32 animationFrameCount = (U32)mAnimationFrames.size();
- // Finish if no animation frames are specified.
- if ( animationFrameCount == 0 )
- return;
- // Fetch image asset frame count.
- const S32 imageAssetFrameCount = (S32)mImageAsset->getFrameCount();
- // Finish if the image has no frames.
- if ( imageAssetFrameCount == 0 )
- return;
- // Validate each specified frame.
- for ( U32 frameIndex = 0; frameIndex < animationFrameCount; ++frameIndex )
- {
- // Fetch frame.
- S32 frame = mAnimationFrames[frameIndex];
- // Valid Frame?
- if ( frame < 0 || frame >= imageAssetFrameCount )
- {
- // No, warn.
- Con::warnf( "AnimationAsset::validateFrames() - Animation asset '%s' specifies an out-of-bound frame of '%d' (key-index:'%d') against image asset Id '%s'.",
- getAssetName(),
- frame,
- frameIndex,
- mImageAsset.getAssetId() );
- // Set the frame to a valid one.
- if ( frame < 0 )
- frame = 0;
- else if ( frame >= imageAssetFrameCount )
- frame = imageAssetFrameCount-1;
- }
- // Use frame.
- mValidatedFrames.push_back( frame );
- }
- }
- //------------------------------------------------------------------------------
- void AnimationAsset::validateNamedFrames( void )
- {
- mValidatedNameFrames.clear();
- // Fetch Animation Frame Count.
- const U32 animationFrameCount = (U32)mNamedAnimationFrames.size();
- // Finish if no animation frames are specified.
- if ( animationFrameCount == 0 )
- return;
- // Fetch image asset frame count.
- const S32 imageAssetFrameCount = (S32)mImageAsset->getFrameCount();
- // Finish if the image has no frames.
- if ( imageAssetFrameCount == 0 )
- return;
- // Validate each specified frame.
- for ( U32 frameIndex = 0; frameIndex < animationFrameCount; ++frameIndex )
- {
- // Fetch frame.
- const char* frame = mNamedAnimationFrames[frameIndex];
- // Valid Frame?
- if ( frame == StringTable->EmptyString || !mImageAsset->containsFrame(frame) )
- {
- // No, warn.
- Con::warnf( "AnimationAsset::validateNamedFrames() - Animation asset '%s' specifies a bad frame '%s' against image asset Id '%s'.",
- getAssetName(),
- frame,
- mImageAsset.getAssetId() );
- }
- // Use frame.
- mValidatedNameFrames.push_back( StringTable->insert(frame) );
- }
- }
- //------------------------------------------------------------------------------
- void AnimationAsset::validateFrames( void )
- {
- // Debug Profiling.
- PROFILE_SCOPE(AnimationAsset_ValidateFrames);
- // Finish if we don't have a valid image asset.
- if ( mImageAsset.isNull() )
- return;
- if (mNamedCellsMode)
- {
- validateNamedFrames();
- }
- else
- {
- validateNumericalFrames();
- }
- }
- //------------------------------------------------------------------------------
- bool AnimationAsset::isAssetValid( void ) const
- {
- return mImageAsset.notNull() && mImageAsset->isAssetValid() && (mValidatedFrames.size() > 0 || mValidatedNameFrames.size() > 0);
- }
- //------------------------------------------------------------------------------
- void AnimationAsset::initializeAsset( void )
- {
- // Call parent.
- Parent::initializeAsset();
- // Currently there is no specific initialization required.
- }
|