AnimationAsset.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. #include "console/consoleTypes.h"
  23. #include "2d/assets/ImageAsset.h"
  24. #include "2d/assets/AnimationAsset.h"
  25. // Script bindings.
  26. #include "AnimationAsset_ScriptBinding.h"
  27. // Debug Profiling.
  28. #include "debug/profiler.h"
  29. //------------------------------------------------------------------------------
  30. ConsoleType( animationAssetPtr, TypeAnimationAssetPtr, sizeof(AssetPtr<AnimationAsset>), ASSET_ID_FIELD_PREFIX )
  31. //-----------------------------------------------------------------------------
  32. ConsoleGetType( TypeAnimationAssetPtr )
  33. {
  34. // Fetch asset Id.
  35. return (*((AssetPtr<AnimationAsset>*)dptr)).getAssetId();
  36. }
  37. //-----------------------------------------------------------------------------
  38. ConsoleSetType( TypeAnimationAssetPtr )
  39. {
  40. // Was a single argument specified?
  41. if( argc == 1 )
  42. {
  43. // Yes, so fetch field value.
  44. const char* pFieldValue = argv[0];
  45. // Fetch asset pointer.
  46. AssetPtr<AnimationAsset>* pAssetPtr = dynamic_cast<AssetPtr<AnimationAsset>*>((AssetPtrBase*)(dptr));
  47. // Is the asset pointer the correct type?
  48. if ( pAssetPtr == NULL )
  49. {
  50. // No, so fail.
  51. Con::warnf( "(TypeAnimationAssetPtr) - Failed to set asset Id '%d'.", pFieldValue );
  52. return;
  53. }
  54. // Set asset.
  55. pAssetPtr->setAssetId( pFieldValue );
  56. return;
  57. }
  58. // Warn.
  59. Con::warnf( "(TypeAnimationAssetPtr) - Cannot set multiple args to a single asset." );
  60. }
  61. //------------------------------------------------------------------------------
  62. IMPLEMENT_CONOBJECT(AnimationAsset);
  63. //------------------------------------------------------------------------------
  64. AnimationAsset::AnimationAsset() : mAnimationTime(1.0f),
  65. mAnimationCycle(true),
  66. mRandomStart(false),
  67. mAnimationIntegration(0.0f)
  68. {
  69. // Set Vector Associations.
  70. VECTOR_SET_ASSOCIATION( mAnimationFrames );
  71. VECTOR_SET_ASSOCIATION( mValidatedFrames );
  72. }
  73. //------------------------------------------------------------------------------
  74. AnimationAsset::~AnimationAsset()
  75. {
  76. }
  77. //------------------------------------------------------------------------------
  78. void AnimationAsset::initPersistFields()
  79. {
  80. // Call parent.
  81. Parent::initPersistFields();
  82. addProtectedField("Image", TypeImageAssetPtr, Offset(mImageAsset, AnimationAsset), &setImage, &defaultProtectedGetFn, &writeImage, "");
  83. addProtectedField("AnimationFrames", TypeS32Vector, Offset(mAnimationFrames, AnimationAsset), &setAnimationFrames, &defaultProtectedGetFn, &writeAnimationFrames, "");
  84. addProtectedField("AnimationTime", TypeF32, Offset(mAnimationTime, AnimationAsset), &setAnimationTime, &defaultProtectedGetFn, &defaultProtectedWriteFn, "");
  85. addProtectedField("AnimationCycle", TypeBool, Offset(mAnimationCycle, AnimationAsset), &setAnimationCycle, &defaultProtectedGetFn, &writeAnimationCycle, "");
  86. addProtectedField("RandomStart", TypeBool, Offset(mRandomStart, AnimationAsset), &setRandomStart, &defaultProtectedGetFn, &writeRandomStart, "");
  87. }
  88. //------------------------------------------------------------------------------
  89. bool AnimationAsset::onAdd()
  90. {
  91. // Call Parent.
  92. if(!Parent::onAdd())
  93. return false;
  94. // Return Okay.
  95. return true;
  96. }
  97. //------------------------------------------------------------------------------
  98. void AnimationAsset::onRemove()
  99. {
  100. // Call Parent.
  101. Parent::onRemove();
  102. }
  103. //------------------------------------------------------------------------------
  104. void AnimationAsset::onAssetRefresh( void )
  105. {
  106. // Ignore if not yet added to the sim.
  107. if ( !isProperlyAdded() )
  108. return;
  109. // Call parent.
  110. Parent::onAssetRefresh();
  111. }
  112. //------------------------------------------------------------------------------
  113. void AnimationAsset::copyTo(SimObject* object)
  114. {
  115. // Call to parent.
  116. Parent::copyTo(object);
  117. // Cast to asset.
  118. AnimationAsset* pAsset = static_cast<AnimationAsset*>(object);
  119. // Sanity!
  120. AssertFatal(pAsset != NULL, "AnimationAsset::copyTo() - Object is not the correct type.");
  121. // Copy state.
  122. pAsset->setImage( getImage().getAssetId() );
  123. pAsset->setAnimationFrames( Con::getData( TypeS32Vector, (void*)&getSpecifiedAnimationFrames(), 0 ) );
  124. pAsset->setAnimationTime( getAnimationTime() );
  125. pAsset->setAnimationCycle( getAnimationCycle() );
  126. pAsset->setRandomStart( getRandomStart() );
  127. }
  128. //------------------------------------------------------------------------------
  129. void AnimationAsset::setImage( const char* pAssetId )
  130. {
  131. // Ignore no change.
  132. if ( mImageAsset.getAssetId() == StringTable->insert( pAssetId ) )
  133. return;
  134. // Update.
  135. mImageAsset = pAssetId;
  136. // Validate frames.
  137. validateFrames();
  138. // Refresh the asset.
  139. refreshAsset();
  140. }
  141. //------------------------------------------------------------------------------
  142. void AnimationAsset::setAnimationFrames( const char* pAnimationFrames )
  143. {
  144. // Debug Profiling.
  145. PROFILE_SCOPE(AnimationAsset_SetAnimationFrames);
  146. // Clear any existing frames.
  147. mAnimationFrames.clear();
  148. // Fetch frame count.
  149. const U32 frameCount = StringUnit::getUnitCount( pAnimationFrames, " \t\n" );
  150. // Iterate frames.
  151. for( U32 frameIndex = 0; frameIndex < frameCount; ++frameIndex )
  152. {
  153. // Store frame.
  154. mAnimationFrames.push_back( dAtoi( StringUnit::getUnit( pAnimationFrames, frameIndex, " \t\n" ) ) );
  155. }
  156. // Validate frames.
  157. validateFrames();
  158. // Refresh the asset.
  159. refreshAsset();
  160. }
  161. //------------------------------------------------------------------------------
  162. void AnimationAsset::setAnimationTime( const F32 animationTime )
  163. {
  164. // Ignore no change,
  165. if ( mIsEqual( animationTime, mAnimationTime ) )
  166. return;
  167. // Update.
  168. mAnimationTime = animationTime;
  169. // Refresh the asset.
  170. refreshAsset();
  171. }
  172. //------------------------------------------------------------------------------
  173. void AnimationAsset::setAnimationCycle( const bool animationCycle )
  174. {
  175. // Ignore no change.
  176. if ( animationCycle == mAnimationCycle )
  177. return;
  178. // Update.
  179. mAnimationCycle = animationCycle;
  180. // Refresh the asset.
  181. refreshAsset();
  182. }
  183. //------------------------------------------------------------------------------
  184. void AnimationAsset::setRandomStart( const bool randomStart )
  185. {
  186. // Ignore no change.
  187. if ( randomStart == mRandomStart )
  188. return;
  189. // Update.
  190. mRandomStart = randomStart;
  191. // Refresh the asset.
  192. refreshAsset();
  193. }
  194. //------------------------------------------------------------------------------
  195. void AnimationAsset::validateFrames( void )
  196. {
  197. // Debug Profiling.
  198. PROFILE_SCOPE(AnimationAsset_ValidateFrames);
  199. // Clear validated frames.
  200. mValidatedFrames.clear();
  201. // Finish if we don't have a valid image asset.
  202. if ( mImageAsset.isNull() )
  203. return;
  204. // Fetch Animation Frame Count.
  205. const U32 animationFrameCount = (U32)mAnimationFrames.size();
  206. // Finish if no animation frames are specified.
  207. if ( animationFrameCount == 0 )
  208. return;
  209. // Fetch image asset frame count.
  210. const S32 imageAssetFrameCount = (S32)mImageAsset->getFrameCount();
  211. // Finish if the image has no frames.
  212. if ( imageAssetFrameCount == 0 )
  213. return;
  214. // Validate each specified frame.
  215. for ( U32 frameIndex = 0; frameIndex < animationFrameCount; ++frameIndex )
  216. {
  217. // Fetch frame.
  218. S32 frame = mAnimationFrames[frameIndex];
  219. // Valid Frame?
  220. if ( frame < 0 || frame >= imageAssetFrameCount )
  221. {
  222. // No, warn.
  223. Con::warnf( "AnimationAsset::validateFrames() - Animation asset '%s' specifies an out-of-bound frame of '%d' (key-index:'%d') against image asset Id '%s'.",
  224. getAssetName(),
  225. frame,
  226. frameIndex,
  227. mImageAsset.getAssetId() );
  228. if ( frame < 0 )
  229. frame = 0;
  230. else if ( frame >= imageAssetFrameCount )
  231. frame = imageAssetFrameCount-1;
  232. }
  233. // Use frame.
  234. mValidatedFrames.push_back( frame );
  235. }
  236. }
  237. //------------------------------------------------------------------------------
  238. bool AnimationAsset::isAssetValid( void ) const
  239. {
  240. return mImageAsset.notNull() && mImageAsset->isAssetValid() && mValidatedFrames.size() > 0;
  241. }
  242. //------------------------------------------------------------------------------
  243. void AnimationAsset::initializeAsset( void )
  244. {
  245. // Call parent.
  246. Parent::initializeAsset();
  247. // Currently there is no specific initialization required.
  248. }