SpriteProxyBase.cc 13 KB

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