AnimationAsset.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. mNamedCellsMode(false)
  69. {
  70. // Set Vector Associations.
  71. VECTOR_SET_ASSOCIATION( mAnimationFrames );
  72. VECTOR_SET_ASSOCIATION( mNamedAnimationFrames );
  73. VECTOR_SET_ASSOCIATION( mValidatedFrames );
  74. }
  75. //------------------------------------------------------------------------------
  76. AnimationAsset::~AnimationAsset()
  77. {
  78. }
  79. //------------------------------------------------------------------------------
  80. void AnimationAsset::initPersistFields()
  81. {
  82. // Call parent.
  83. Parent::initPersistFields();
  84. addProtectedField("Image", TypeImageAssetPtr, Offset(mImageAsset, AnimationAsset), &setImage, &defaultProtectedGetFn, &writeImage, "");
  85. addProtectedField("AnimationFrames", TypeS32Vector, Offset(mAnimationFrames, AnimationAsset), &setAnimationFrames, &defaultProtectedGetFn, &writeAnimationFrames, "");
  86. addProtectedField("NamedAnimationFrames", TypeStringTableEntryVector, Offset(mNamedAnimationFrames, AnimationAsset), &setNamedAnimationFrames, &defaultProtectedGetFn, &writeNamedAnimationFrames, "");
  87. addProtectedField("AnimationTime", TypeF32, Offset(mAnimationTime, AnimationAsset), &setAnimationTime, &defaultProtectedGetFn, &defaultProtectedWriteFn, "");
  88. addProtectedField("AnimationCycle", TypeBool, Offset(mAnimationCycle, AnimationAsset), &setAnimationCycle, &defaultProtectedGetFn, &writeAnimationCycle, "");
  89. addProtectedField("RandomStart", TypeBool, Offset(mRandomStart, AnimationAsset), &setRandomStart, &defaultProtectedGetFn, &writeRandomStart, "");
  90. addProtectedField("NamedCellsMode", TypeBool, Offset(mNamedCellsMode, AnimationAsset), &setNamedCellsMode, &defaultProtectedGetFn, &writeNamedCellsMode, "");
  91. }
  92. //------------------------------------------------------------------------------
  93. bool AnimationAsset::onAdd()
  94. {
  95. // Call Parent.
  96. if(!Parent::onAdd())
  97. return false;
  98. // Return Okay.
  99. return true;
  100. }
  101. //------------------------------------------------------------------------------
  102. void AnimationAsset::onRemove()
  103. {
  104. // Call Parent.
  105. Parent::onRemove();
  106. }
  107. //------------------------------------------------------------------------------
  108. void AnimationAsset::onAssetRefresh( void )
  109. {
  110. // Ignore if not yet added to the sim.
  111. if ( !isProperlyAdded() )
  112. return;
  113. // Call parent.
  114. Parent::onAssetRefresh();
  115. }
  116. //------------------------------------------------------------------------------
  117. void AnimationAsset::copyTo(SimObject* object)
  118. {
  119. // Call to parent.
  120. Parent::copyTo(object);
  121. // Cast to asset.
  122. AnimationAsset* pAsset = static_cast<AnimationAsset*>(object);
  123. // Sanity!
  124. AssertFatal(pAsset != NULL, "AnimationAsset::copyTo() - Object is not the correct type.");
  125. // Copy state.
  126. pAsset->setImage( getImage().getAssetId() );
  127. // Are we in named cells mode?
  128. if ( !pAsset->getNamedCellsMode() )
  129. pAsset->setAnimationFrames( Con::getData( TypeS32Vector, (void*)&getSpecifiedAnimationFrames(), 0 ) );
  130. else
  131. pAsset->setNamedAnimationFrames( Con::getData( TypeStringTableEntryVector, (void*)&getSpecifiedNamedAnimationFrames(), 0 ) );
  132. pAsset->setAnimationTime( getAnimationTime() );
  133. pAsset->setAnimationCycle( getAnimationCycle() );
  134. pAsset->setRandomStart( getRandomStart() );
  135. pAsset->setNamedCellsMode( getNamedCellsMode() );
  136. }
  137. //------------------------------------------------------------------------------
  138. void AnimationAsset::setImage( const char* pAssetId )
  139. {
  140. // Ignore no change.
  141. if ( mImageAsset.getAssetId() == StringTable->insert( pAssetId ) )
  142. return;
  143. // Update.
  144. mImageAsset = pAssetId;
  145. // Validate frames.
  146. validateFrames();
  147. // Refresh the asset.
  148. refreshAsset();
  149. }
  150. //------------------------------------------------------------------------------
  151. void AnimationAsset::setAnimationFrames( const char* pAnimationFrames )
  152. {
  153. // Debug Profiling.
  154. PROFILE_SCOPE(AnimationAsset_SetAnimationFrames);
  155. // Clear any existing frames.
  156. mAnimationFrames.clear();
  157. // Fetch frame count.
  158. const U32 frameCount = StringUnit::getUnitCount( pAnimationFrames, " \t\n" );
  159. // Iterate frames.
  160. for( U32 frameIndex = 0; frameIndex < frameCount; ++frameIndex )
  161. {
  162. // Store frame.
  163. mAnimationFrames.push_back( dAtoi( StringUnit::getUnit( pAnimationFrames, frameIndex, " \t\n" ) ) );
  164. }
  165. mNamedCellsMode = false;
  166. // Validate frames.
  167. validateFrames();
  168. // Refresh the asset.
  169. refreshAsset();
  170. }
  171. //------------------------------------------------------------------------------
  172. void AnimationAsset::setNamedAnimationFrames( const char* pAnimationFrames )
  173. {
  174. // Clear any existing frames.
  175. mNamedAnimationFrames.clear();
  176. // Fetch frame count.
  177. const U32 frameCount = StringUnit::getUnitCount( pAnimationFrames, " \t\n" );
  178. // Iterate frames.
  179. for( U32 frameIndex = 0; frameIndex < frameCount; ++frameIndex )
  180. {
  181. // Store frame.
  182. mNamedAnimationFrames.push_back( StringTable->insert( StringUnit::getUnit( pAnimationFrames, frameIndex, " \t\n" ) ) );
  183. }
  184. mNamedCellsMode = true;
  185. // Validate frames.
  186. validateFrames();
  187. // Refresh the asset.
  188. refreshAsset();
  189. }
  190. //------------------------------------------------------------------------------
  191. void AnimationAsset::setAnimationTime( const F32 animationTime )
  192. {
  193. // Ignore no change,
  194. if ( mIsEqual( animationTime, mAnimationTime ) )
  195. return;
  196. // Update.
  197. mAnimationTime = animationTime;
  198. // Refresh the asset.
  199. refreshAsset();
  200. }
  201. //------------------------------------------------------------------------------
  202. void AnimationAsset::setAnimationCycle( const bool animationCycle )
  203. {
  204. // Ignore no change.
  205. if ( animationCycle == mAnimationCycle )
  206. return;
  207. // Update.
  208. mAnimationCycle = animationCycle;
  209. // Refresh the asset.
  210. refreshAsset();
  211. }
  212. //------------------------------------------------------------------------------
  213. void AnimationAsset::setRandomStart( const bool randomStart )
  214. {
  215. // Ignore no change.
  216. if ( randomStart == mRandomStart )
  217. return;
  218. // Update.
  219. mRandomStart = randomStart;
  220. // Refresh the asset.
  221. refreshAsset();
  222. }
  223. //------------------------------------------------------------------------------
  224. void AnimationAsset::setNamedCellsMode( const bool namedCellsMode )
  225. {
  226. // Ignore no change.
  227. if ( namedCellsMode == mNamedCellsMode)
  228. return;
  229. // Update.
  230. mNamedCellsMode = namedCellsMode;
  231. // Refresh the asset.
  232. refreshAsset();
  233. }
  234. //------------------------------------------------------------------------------
  235. void AnimationAsset::validateNumericalFrames( void )
  236. {
  237. // Clear validated frames.
  238. mValidatedFrames.clear();
  239. // Fetch Animation Frame Count.
  240. const U32 animationFrameCount = (U32)mAnimationFrames.size();
  241. // Finish if no animation frames are specified.
  242. if ( animationFrameCount == 0 )
  243. return;
  244. // Fetch image asset frame count.
  245. const S32 imageAssetFrameCount = (S32)mImageAsset->getFrameCount();
  246. // Finish if the image has no frames.
  247. if ( imageAssetFrameCount == 0 )
  248. return;
  249. // Validate each specified frame.
  250. for ( U32 frameIndex = 0; frameIndex < animationFrameCount; ++frameIndex )
  251. {
  252. // Fetch frame.
  253. S32 frame = mAnimationFrames[frameIndex];
  254. // Valid Frame?
  255. if ( frame < 0 || frame >= imageAssetFrameCount )
  256. {
  257. // No, warn.
  258. Con::warnf( "AnimationAsset::validateFrames() - Animation asset '%s' specifies an out-of-bound frame of '%d' (key-index:'%d') against image asset Id '%s'.",
  259. getAssetName(),
  260. frame,
  261. frameIndex,
  262. mImageAsset.getAssetId() );
  263. // Set the frame to a valid one.
  264. if ( frame < 0 )
  265. frame = 0;
  266. else if ( frame >= imageAssetFrameCount )
  267. frame = imageAssetFrameCount-1;
  268. }
  269. // Use frame.
  270. mValidatedFrames.push_back( frame );
  271. }
  272. }
  273. //------------------------------------------------------------------------------
  274. void AnimationAsset::validateNamedFrames( void )
  275. {
  276. mValidatedNameFrames.clear();
  277. // Fetch Animation Frame Count.
  278. const U32 animationFrameCount = (U32)mNamedAnimationFrames.size();
  279. // Finish if no animation frames are specified.
  280. if ( animationFrameCount == 0 )
  281. return;
  282. // Fetch image asset frame count.
  283. const S32 imageAssetFrameCount = (S32)mImageAsset->getFrameCount();
  284. // Finish if the image has no frames.
  285. if ( imageAssetFrameCount == 0 )
  286. return;
  287. // Validate each specified frame.
  288. for ( U32 frameIndex = 0; frameIndex < animationFrameCount; ++frameIndex )
  289. {
  290. // Fetch frame.
  291. const char* frame = mNamedAnimationFrames[frameIndex];
  292. // Valid Frame?
  293. if ( frame == StringTable->EmptyString || !mImageAsset->containsFrame(frame) )
  294. {
  295. // No, warn.
  296. Con::warnf( "AnimationAsset::validateNamedFrames() - Animation asset '%s' specifies a bad frame '%s' against image asset Id '%s'.",
  297. getAssetName(),
  298. frame,
  299. mImageAsset.getAssetId() );
  300. }
  301. // Use frame.
  302. mValidatedNameFrames.push_back( StringTable->insert(frame) );
  303. }
  304. }
  305. //------------------------------------------------------------------------------
  306. void AnimationAsset::validateFrames( void )
  307. {
  308. // Debug Profiling.
  309. PROFILE_SCOPE(AnimationAsset_ValidateFrames);
  310. // Finish if we don't have a valid image asset.
  311. if ( mImageAsset.isNull() )
  312. return;
  313. if (mNamedCellsMode)
  314. {
  315. validateNamedFrames();
  316. }
  317. else
  318. {
  319. validateNumericalFrames();
  320. }
  321. }
  322. //------------------------------------------------------------------------------
  323. bool AnimationAsset::isAssetValid( void ) const
  324. {
  325. return mImageAsset.notNull() && mImageAsset->isAssetValid() && (mValidatedFrames.size() > 0 || mValidatedNameFrames.size() > 0);
  326. }
  327. //------------------------------------------------------------------------------
  328. void AnimationAsset::initializeAsset( void )
  329. {
  330. // Call parent.
  331. Parent::initializeAsset();
  332. // Currently there is no specific initialization required.
  333. }