SpriteProxyBase.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. #ifndef _SPRITE_PROXY_BASE_H_
  23. #include "2d/core/SpriteProxyBase.h"
  24. #endif
  25. #ifndef _DGL_H_
  26. #include "graphics/dgl.h"
  27. #endif
  28. #ifndef _STRINGBUFFER_H_
  29. #include "string/stringBuffer.h"
  30. #endif
  31. //------------------------------------------------------------------------------
  32. SpriteProxyBase::SpriteProxyBase() :
  33. mpAnimationController(NULL)
  34. {
  35. resetState();
  36. }
  37. //------------------------------------------------------------------------------
  38. SpriteProxyBase::~SpriteProxyBase()
  39. {
  40. resetState();
  41. }
  42. //------------------------------------------------------------------------------
  43. void SpriteProxyBase::resetState( void )
  44. {
  45. clearAsset();
  46. mSelfTick = false;
  47. mAnimationPaused = false;
  48. }
  49. //------------------------------------------------------------------------------
  50. bool SpriteProxyBase::update( const F32 elapsedTime )
  51. {
  52. // Are we in static mode?
  53. if ( isStaticMode() )
  54. {
  55. // Yes, so turn-off tick processing.
  56. setProcessTicks( false );
  57. return false;
  58. }
  59. // Finish if no animation controller.
  60. if ( mpAnimationController == NULL )
  61. return false;
  62. // Finish if the animation has finished.
  63. if ( mpAnimationController->isAnimationFinished() )
  64. return false;
  65. // Finish if animation is paused.
  66. if ( mAnimationPaused )
  67. return true;
  68. // Update the animation.
  69. mpAnimationController->updateAnimation( Tickable::smTickSec );
  70. // Finish if the animation has NOT finished.
  71. if ( !mpAnimationController->isAnimationFinished() )
  72. return false;
  73. // Turn-off tick processing.
  74. setProcessTicks( false );
  75. // Perform callback.
  76. onAnimationEnd();
  77. // Flag animation as just finished.
  78. return true;
  79. }
  80. //------------------------------------------------------------------------------
  81. void SpriteProxyBase::processTick( void )
  82. {
  83. // Update using tick period.
  84. update( Tickable::smTickSec );
  85. }
  86. //------------------------------------------------------------------------------
  87. bool SpriteProxyBase::validRender( void ) const
  88. {
  89. // Are we in static mode?
  90. if ( isStaticMode() )
  91. {
  92. // Yes, so we must have an image asset and the frame must be in bounds.
  93. return mImageAsset.notNull() && ( getImageFrame() < mImageAsset->getFrameCount() );
  94. }
  95. // No, so we must have an animation controller and the animation must be valid.
  96. return mpAnimationController != NULL && mpAnimationController->isAnimationValid();
  97. }
  98. //------------------------------------------------------------------------------
  99. void SpriteProxyBase::render(
  100. const bool flipX,
  101. const bool flipY,
  102. const Vector2& vertexPos0,
  103. const Vector2& vertexPos1,
  104. const Vector2& vertexPos2,
  105. const Vector2& vertexPos3,
  106. BatchRender* pBatchRenderer ) const
  107. {
  108. // Finish if we can't render.
  109. if ( !validRender() )
  110. return;
  111. // Static mode?
  112. if ( isStaticMode() )
  113. {
  114. // Fetch current frame area.
  115. ImageAsset::FrameArea::TexelArea texelArea = mImageAsset->getImageFrameArea( mImageFrame ).mTexelArea;
  116. // Flip texture coordinates appropriately.
  117. texelArea.setFlip( flipX, flipY );
  118. // Fetch lower/upper texture coordinates.
  119. const Vector2& texLower = texelArea.mTexelLower;
  120. const Vector2& texUpper = texelArea.mTexelUpper;
  121. // Submit batched quad.
  122. pBatchRenderer->SubmitQuad(
  123. vertexPos0,
  124. vertexPos1,
  125. vertexPos2,
  126. vertexPos3,
  127. Vector2( texLower.x, texUpper.y ),
  128. Vector2( texUpper.x, texUpper.y ),
  129. Vector2( texUpper.x, texLower.y ),
  130. Vector2( texLower.x, texLower.y ),
  131. mImageAsset->getImageTexture() );
  132. return;
  133. }
  134. // Fetch current frame area.
  135. ImageAsset::FrameArea::TexelArea texelArea = mpAnimationController->getCurrentImageFrameArea().mTexelArea;
  136. // Flip texture coordinates appropriately.
  137. texelArea.setFlip( flipX, flipY );
  138. // Fetch lower/upper texture coordinates.
  139. const Vector2& texLower = texelArea.mTexelLower;
  140. const Vector2& texUpper = texelArea.mTexelUpper;
  141. // Submit batched quad.
  142. pBatchRenderer->SubmitQuad(
  143. vertexPos0,
  144. vertexPos1,
  145. vertexPos2,
  146. vertexPos3,
  147. Vector2( texLower.x, texUpper.y ),
  148. Vector2( texUpper.x, texUpper.y ),
  149. Vector2( texUpper.x, texLower.y ),
  150. Vector2( texLower.x, texLower.y ),
  151. mpAnimationController->getImageTexture() );
  152. }
  153. //-----------------------------------------------------------------------------
  154. void SpriteProxyBase::renderGui( GuiControl& owner, Point2I offset, const RectI &updateRect ) const
  155. {
  156. // Are we in static mode?
  157. if ( isStaticMode() )
  158. {
  159. // Do we have a valid image to render?
  160. if ( mImageAsset.notNull() && mImageFrame < mImageAsset->getFrameCount() )
  161. {
  162. // Yes, so calculate source region.
  163. const ImageAsset::FrameArea& frameArea = mImageAsset->getImageFrameArea( mImageFrame );
  164. RectI sourceRegion( frameArea.mPixelArea.mPixelOffset, Point2I(frameArea.mPixelArea.mPixelWidth, frameArea.mPixelArea.mPixelHeight) );
  165. // Calculate destination region.
  166. RectI destinationRegion(offset, owner.mBounds.extent);
  167. // Render image.
  168. dglClearBitmapModulation();
  169. dglDrawBitmapStretchSR( mImageAsset->getImageTexture(), destinationRegion, sourceRegion );
  170. }
  171. }
  172. else
  173. {
  174. // Do we have a valid animation to render?
  175. if ( mpAnimationController != NULL && mpAnimationController->getAnimationAsset().notNull() )
  176. {
  177. // Yes, so calculate source region.
  178. const ImageAsset::FrameArea& frameArea = mpAnimationController->getCurrentImageFrameArea();
  179. RectI sourceRegion( frameArea.mPixelArea.mPixelOffset, Point2I(frameArea.mPixelArea.mPixelWidth, frameArea.mPixelArea.mPixelHeight) );
  180. // Calculate destination region.
  181. RectI destinationRegion(offset, owner.mBounds.extent);
  182. // Render animation image.
  183. dglClearBitmapModulation();
  184. dglDrawBitmapStretchSR( mpAnimationController->getImageTexture(), destinationRegion, sourceRegion );
  185. // Update control.
  186. owner.setUpdate();
  187. }
  188. }
  189. // Render child controls.
  190. owner.renderChildControls(offset, updateRect);
  191. }
  192. //------------------------------------------------------------------------------
  193. void SpriteProxyBase::copyTo(SpriteProxyBase* pSpriteProxyBase) const
  194. {
  195. // Sanity!
  196. AssertFatal(pSpriteProxyBase != NULL, "SpriteProxyBase::copyTo - Copy object cannot be NULL.");
  197. // Set self ticking.
  198. pSpriteProxyBase->mSelfTick = mSelfTick;
  199. // Are we in static mode?
  200. if ( mStaticMode )
  201. {
  202. // Yes, so use the image/frame if we have an asset.
  203. if ( mImageAsset.notNull() )
  204. pSpriteProxyBase->setImage( getImage(), getImageFrame() );
  205. }
  206. else if ( mAnimationAsset.notNull() )
  207. {
  208. // No, so use current animation if we have an asset.
  209. if ( mAnimationAsset.notNull() )
  210. pSpriteProxyBase->setAnimation(getAnimation(), false );
  211. }
  212. }
  213. //------------------------------------------------------------------------------
  214. void SpriteProxyBase::clearAsset( void )
  215. {
  216. // Destroy animation controller if required.
  217. if ( mpAnimationController != NULL )
  218. {
  219. delete mpAnimationController;
  220. mpAnimationController = NULL;
  221. }
  222. mAnimationAsset = NULL;
  223. mImageAsset = NULL;
  224. mImageFrame = 0;
  225. mStaticMode = true;
  226. setProcessTicks( false );
  227. }
  228. //------------------------------------------------------------------------------
  229. bool SpriteProxyBase::setImage( const char* pImageAssetId, const U32 frame )
  230. {
  231. // Finish if invalid image asset.
  232. if ( pImageAssetId == NULL )
  233. return false;
  234. // Set asset.
  235. mImageAsset = pImageAssetId;
  236. // Set the image frame if the image asset was set.
  237. if ( mImageAsset.notNull() )
  238. setImageFrame( frame );
  239. // Destroy animation controller if required.
  240. if ( mpAnimationController != NULL )
  241. {
  242. delete mpAnimationController;
  243. mpAnimationController = NULL;
  244. }
  245. // Set Frame.
  246. mImageFrame = frame;
  247. // Set as static render.
  248. mStaticMode = true;
  249. // Turn-off tick processing.
  250. setProcessTicks( false );
  251. // Return Okay.
  252. return true;
  253. }
  254. //------------------------------------------------------------------------------
  255. bool SpriteProxyBase::setImageFrame( const U32 frame )
  256. {
  257. // Check Existing Image.
  258. if ( mImageAsset.isNull() )
  259. {
  260. // Warn.
  261. Con::warnf("SpriteProxyBase::setImageFrame() - Cannot set Frame without existing asset Id.");
  262. // Return Here.
  263. return false;
  264. }
  265. // Check Frame Validity.
  266. if ( frame >= mImageAsset->getFrameCount() )
  267. {
  268. // Warn.
  269. Con::warnf( "SpriteProxyBase::setImageFrame() - Invalid Frame #%d for asset Id '%s'.", frame, mImageAsset.getAssetId() );
  270. // Return Here.
  271. return false;
  272. }
  273. // Set Frame.
  274. mImageFrame = frame;
  275. // Return Okay.
  276. return true;
  277. }
  278. //-----------------------------------------------------------------------------
  279. bool SpriteProxyBase::setAnimation( const char* pAnimationAssetId, const bool autoRestore )
  280. {
  281. // Set as dynamic render.
  282. mStaticMode = false;
  283. // Ensure animation is un-paused.
  284. mAnimationPaused = false;
  285. // Create animation controller if required.
  286. if ( mpAnimationController == NULL )
  287. mpAnimationController = new AnimationController();
  288. // Reset static asset.
  289. mImageAsset.clear();
  290. // Fetch animation asset.
  291. mAnimationAsset = StringTable->insert( pAnimationAssetId );
  292. // Finish if we didn't get an animation.
  293. if ( mAnimationAsset.isNull() )
  294. return false;
  295. // Play Animation.
  296. if ( !mpAnimationController->playAnimation( mAnimationAsset, autoRestore ) )
  297. return false;
  298. // Turn-on tick processing.
  299. setProcessTicks( true );
  300. // Return Okay.
  301. return true;
  302. }