guiSpriteCtrl.cc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  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 _GUISPRITECTRL_H_
  23. #include "2d/gui/guiSpriteCtrl.h"
  24. #endif
  25. #ifndef _CONSOLE_H_
  26. #include "console/console.h"
  27. #endif
  28. #ifndef _CONSOLETYPES_H_
  29. #include "console/consoleTypes.h"
  30. #endif
  31. #ifndef _DGL_H_
  32. #include "graphics/dgl.h"
  33. #endif
  34. #ifndef _H_GUIDEFAULTCONTROLRENDER_
  35. #include "gui/guiDefaultControlRender.h"
  36. #endif
  37. #include "guiSpriteCtrl_ScriptBindings.h"
  38. //-----------------------------------------------------------------------------
  39. IMPLEMENT_CONOBJECT(GuiSpriteCtrl);
  40. //-----------------------------------------------------------------------------
  41. GuiSpriteCtrl::GuiSpriteCtrl( void ) :
  42. mImageAssetId(StringTable->EmptyString),
  43. mFrame(0),
  44. mNamedImageFrameId(StringTable-> EmptyString),
  45. mAnimationAssetId(StringTable->EmptyString),
  46. mBitmapName(StringTable->EmptyString)
  47. {
  48. // Set to self ticking.
  49. mSelfTick = true;
  50. // Default to static provider.
  51. mStaticProvider = true;
  52. mTileImage = false;
  53. mPositionOffset.set(0, 0);
  54. mImageColor = FluidColorI(255, 255, 255, 255);
  55. mImageSize.set(10, 10);
  56. mFullSize = true;
  57. mConstrainProportions = true;
  58. mClampImage = true;
  59. mSingleFrameBitmap = true;
  60. }
  61. //-----------------------------------------------------------------------------
  62. GuiSpriteCtrl::~GuiSpriteCtrl()
  63. {
  64. }
  65. //-----------------------------------------------------------------------------
  66. void GuiSpriteCtrl::initPersistFields()
  67. {
  68. // Call parent.
  69. Parent::initPersistFields();
  70. addGroup("GuiSpriteCtrl");
  71. addProtectedField("Image", TypeAssetId, Offset(mImageAssetId, GuiSpriteCtrl), &setImage, &defaultProtectedGetFn, &writeImage, "The image asset Id used for the image.");
  72. addProtectedField("Frame", TypeS32, Offset(mFrame, GuiSpriteCtrl), &setImageFrame, &defaultProtectedGetFn, &writeImageFrame, "The image frame used for the image or bitmap.");
  73. addProtectedField("NamedFrame", TypeString, Offset(mNamedImageFrameId, GuiSpriteCtrl), &setNamedImageFrame, &defaultProtectedGetFn, &writeNamedImageFrame, "The named image frame used for the image.");
  74. addProtectedField("Animation", TypeAssetId, Offset(mAnimationAssetId, GuiSpriteCtrl), &setAnimation, &defaultProtectedGetFn, &writeAnimation, "The animation to use.");
  75. addProtectedField("Bitmap", TypeFilename, Offset(mBitmapName, GuiSpriteCtrl), &setBitmapName, &getBitmapName, &writeBitmapName, "The bitmap to use in absence of an asset.");
  76. addProtectedField("singleFrameBitmap", TypeBool, Offset(mSingleFrameBitmap, GuiSpriteCtrl), &defaultProtectedSetFn, &defaultProtectedGetFn, &writeSingleFrameBitmap, "If true, will assume there is a single frame when scanning the bitmap.");
  77. addField("tileImage", TypeBool, Offset(mTileImage, GuiSpriteCtrl), &writeTileImage);
  78. addField("positionOffset", TypePoint2I, Offset(mPositionOffset, GuiSpriteCtrl), &writePositionOffset);
  79. addField("imageColor", TypeFluidColorI, Offset(mImageColor, GuiSpriteCtrl), &writeImageColor);
  80. addField("imageSize", TypePoint2I, Offset(mImageSize, GuiSpriteCtrl), &writeImageSize);
  81. addField("fullSize", TypeBool, Offset(mFullSize, GuiSpriteCtrl), &writeFullSize);
  82. addField("clampImage", TypeBool, Offset(mClampImage, GuiSpriteCtrl), &writeClampImage);
  83. addField("constrainProportions", TypeBool, Offset(mConstrainProportions, GuiSpriteCtrl), &writeConstrainProportions);
  84. endGroup("GuiSpriteCtrl");
  85. }
  86. //------------------------------------------------------------------------------
  87. void GuiSpriteCtrl::copyTo(SimObject* object)
  88. {
  89. // Call to parent.
  90. Parent::copyTo(object);
  91. // Cast to control.
  92. GuiSpriteCtrl* pGuiSpriteCtrl = static_cast<GuiSpriteCtrl*>(object);
  93. // Sanity!
  94. AssertFatal(pGuiSpriteCtrl != NULL, "GuiSpriteCtrl::copyTo() - Object is not the correct type.");
  95. // Copy asset fields.
  96. if ( mImageAssetId != StringTable->EmptyString )
  97. {
  98. if ( !isUsingNamedImageFrame() )
  99. pGuiSpriteCtrl->setImage( getImage(), getImageFrame() );
  100. else
  101. pGuiSpriteCtrl->setImage( getImage(), getNamedImageFrame() );
  102. }
  103. else if ( mAnimationAssetId != StringTable->EmptyString )
  104. {
  105. pGuiSpriteCtrl->setAnimation( getAnimation() );
  106. }
  107. }
  108. //-----------------------------------------------------------------------------
  109. bool GuiSpriteCtrl::onWake()
  110. {
  111. // Call parent.
  112. if (!Parent::onWake())
  113. return false;
  114. mActive = true;
  115. if ( mImageAssetId != StringTable->EmptyString )
  116. {
  117. if ( mNamedImageFrameId != StringTable->EmptyString)
  118. {
  119. // Set the image asset and named frame
  120. setImage( mImageAssetId, mNamedImageFrameId );
  121. }
  122. else
  123. {
  124. // Set image asset and numerical frame.
  125. setImage( mImageAssetId, mFrame );
  126. }
  127. }
  128. else if ( mAnimationAssetId != StringTable->EmptyString )
  129. {
  130. // Play animation asset.
  131. setAnimation( mAnimationAssetId );
  132. }
  133. else if (mBitmapName != StringTable->EmptyString)
  134. {
  135. setBitmap(mBitmapName);
  136. }
  137. else
  138. {
  139. // Not good, so warn.
  140. Con::warnf("GuiSpriteCtrl::onWake() - No Image, Animation, or bitmap defined.");
  141. }
  142. return true;
  143. }
  144. //-----------------------------------------------------------------------------
  145. void GuiSpriteCtrl::onSleep()
  146. {
  147. // Clear assets.
  148. ImageFrameProvider::clearAssets();
  149. mBitmapTextureHandle = NULL;
  150. // Call parent.
  151. Parent::onSleep();
  152. }
  153. void GuiSpriteCtrl::setBitmap(const char *name)
  154. {
  155. mBitmapName = StringTable->insert(name);
  156. if (*mBitmapName) {
  157. mBitmapTextureHandle = TextureHandle(mBitmapName, TextureHandle::BitmapTexture, true);
  158. mBitmapTextureHandle.setFilter(GL_LINEAR);
  159. }
  160. else
  161. mBitmapTextureHandle = NULL;
  162. }
  163. U32 GuiSpriteCtrl::constructBitmapArray()
  164. {
  165. if (mSingleFrameBitmap)
  166. return 0;
  167. if (mBitmapArrayRects.size())
  168. return mBitmapArrayRects.size();
  169. GBitmap *bmp = mBitmapTextureHandle.getBitmap();
  170. // Make sure the texture exists.
  171. if (!bmp)
  172. return 0;
  173. //get the separator color
  174. ColorI sepColor;
  175. if (!bmp || !bmp->getColor(0, 0, sepColor))
  176. {
  177. Con::errorf("GuiSpriteCtrl::constructBitmapArray: Failed to create bitmap array from %s - couldn't ascertain seperator color!", mBitmapName);
  178. AssertFatal(false, avar("GuiSpriteCtrl::constructBitmapArray: Failed to create bitmap array from %s - couldn't ascertain seperator color!", mBitmapName));
  179. return 0;
  180. }
  181. //now loop through all the scroll pieces, and find the bounding rectangle for each piece in each state
  182. S32 curY = 0;
  183. // ascertain the height of this row...
  184. ColorI color;
  185. mBitmapArrayRects.clear();
  186. while (curY < (S32)bmp->getHeight())
  187. {
  188. // skip any sep colors
  189. bmp->getColor(0, curY, color);
  190. if (color == sepColor)
  191. {
  192. curY++;
  193. continue;
  194. }
  195. // ok, process left to right, grabbing bitmaps as we go...
  196. S32 curX = 0;
  197. while (curX < (S32)bmp->getWidth())
  198. {
  199. bmp->getColor(curX, curY, color);
  200. if (color == sepColor)
  201. {
  202. curX++;
  203. continue;
  204. }
  205. S32 startX = curX;
  206. while (curX < (S32)bmp->getWidth())
  207. {
  208. bmp->getColor(curX, curY, color);
  209. if (color == sepColor)
  210. break;
  211. curX++;
  212. }
  213. S32 stepY = curY;
  214. while (stepY < (S32)bmp->getHeight())
  215. {
  216. bmp->getColor(startX, stepY, color);
  217. if (color == sepColor)
  218. break;
  219. stepY++;
  220. }
  221. mBitmapArrayRects.push_back(RectI(startX, curY, curX - startX, stepY - curY));
  222. }
  223. // ok, now skip to the next separation color on column 0
  224. while (curY < (S32)bmp->getHeight())
  225. {
  226. bmp->getColor(0, curY, color);
  227. if (color == sepColor)
  228. break;
  229. curY++;
  230. }
  231. }
  232. return mBitmapArrayRects.size();
  233. }
  234. Point2I GuiSpriteCtrl::constrain(Point2I &point, bool grow)
  235. {
  236. if (!mConstrainProportions)
  237. {
  238. return point;
  239. }
  240. F32 targetRatio = getAspectRatio();
  241. F32 currentRatio = (F32)(point.y / point.x);
  242. if (targetRatio > currentRatio) //Too short
  243. {
  244. return grow ? Point2I(point.x, mRound(point.x * targetRatio)) : Point2I(mRound(point.y / targetRatio), point.y);
  245. }
  246. else if (targetRatio < currentRatio) //Too tall
  247. {
  248. return grow ? Point2I(mRound(point.y / targetRatio), point.y) : Point2I(point.x, mRound(point.x * targetRatio));
  249. }
  250. return point;
  251. }
  252. Point2I GuiSpriteCtrl::constrainLockX(Point2I &point)
  253. {
  254. if (!mConstrainProportions)
  255. {
  256. return point;
  257. }
  258. F32 targetRatio = getAspectRatio();
  259. return Point2I(point.x, mRound(point.x * targetRatio));
  260. }
  261. Point2I GuiSpriteCtrl::constrainLockY(Point2I &point)
  262. {
  263. if (!mConstrainProportions)
  264. {
  265. return point;
  266. }
  267. F32 targetRatio = getAspectRatio();
  268. return Point2I(point.y / targetRatio, point.y);
  269. }
  270. F32 GuiSpriteCtrl::getAspectRatio()
  271. {
  272. RectI src = getSourceRect(usesAsset() ? getProviderTexture() : mBitmapTextureHandle);
  273. if (src.isValidRect())
  274. {
  275. return ((F32)src.extent.y / (F32)src.extent.x);
  276. }
  277. return 1.0;
  278. }
  279. Point2I GuiSpriteCtrl::applyAlignment(RectI &bounds, Point2I &size)
  280. {
  281. Point2I offset = Point2I(0, 0);
  282. AlignmentType align = getAlignmentType();
  283. VertAlignmentType vAlign = getVertAlignmentType();
  284. if (align == AlignmentType::RightAlign)
  285. {
  286. offset.x = bounds.extent.x - size.x;
  287. }
  288. else if (align == AlignmentType::CenterAlign)
  289. {
  290. S32 halfW = mRound(size.x / 2);
  291. offset.x = mRound(bounds.extent.x / 2) - halfW;
  292. }
  293. if (vAlign == VertAlignmentType::BottomVAlign)
  294. {
  295. offset.y = bounds.extent.y - size.y;
  296. }
  297. else if (vAlign == VertAlignmentType::MiddleVAlign)
  298. {
  299. S32 halfH = mRound(size.y / 2);
  300. offset.y = mRound(bounds.extent.y / 2) - halfH;
  301. }
  302. //Apply the image offset
  303. if (mClampImage)
  304. {
  305. S32 maxX = bounds.extent.x - size.x;
  306. S32 maxY = bounds.extent.y - size.y;
  307. offset.x = mClamp(offset.x + mPositionOffset.x, 0, maxX);
  308. offset.y = mClamp(offset.y + mPositionOffset.y, 0, maxY);
  309. }
  310. else
  311. {
  312. offset.x = offset.x + mPositionOffset.x;
  313. offset.y = offset.y + mPositionOffset.y;
  314. }
  315. return offset;
  316. }
  317. //-----------------------------------------------------------------------------
  318. bool GuiSpriteCtrl::setImage( const char* pImageAssetId, const U32 frame )
  319. {
  320. // Sanity!
  321. AssertFatal( pImageAssetId != NULL, "Cannot use a NULL asset Id." );
  322. //Capture the current tick state
  323. bool isTicking = isProcessingTicks();
  324. // Reset animation.
  325. if ( mAnimationAssetId != StringTable->EmptyString )
  326. mAnimationAssetId = StringTable->EmptyString;
  327. // Fetch the asset Id.
  328. if ( mImageAssetId != pImageAssetId )
  329. mImageAssetId = StringTable->insert(pImageAssetId);
  330. // Finish if not awake.
  331. if ( !isAwake() )
  332. return true;
  333. // Call parent.
  334. if ( !ImageFrameProvider::setImage(pImageAssetId, frame) )
  335. return false;
  336. //Restore the tick state
  337. if (isProcessingTicks() != isTicking)
  338. {
  339. setProcessTicks(true);
  340. }
  341. // Update control.
  342. setUpdate();
  343. return true;
  344. }
  345. //-----------------------------------------------------------------------------
  346. bool GuiSpriteCtrl::setImage( const char* pImageAssetId, const char* pNamedFrame )
  347. {
  348. // Sanity!
  349. AssertFatal( pImageAssetId != NULL, "Cannot use a NULL asset Id." );
  350. //Capture the current tick state
  351. bool isTicking = isProcessingTicks();
  352. // Reset animation.
  353. if ( mAnimationAssetId != StringTable->EmptyString )
  354. mAnimationAssetId = StringTable->EmptyString;
  355. // Fetch the asset Id.
  356. if ( mImageAssetId != pImageAssetId )
  357. mImageAssetId = StringTable->insert(pImageAssetId);
  358. // Finish if not awake.
  359. if ( !isAwake() )
  360. return true;
  361. // Call parent.
  362. if ( !ImageFrameProvider::setImage(pImageAssetId, pNamedFrame) )
  363. return false;
  364. //Restore the tick state
  365. if (isProcessingTicks() != isTicking)
  366. {
  367. setProcessTicks(true);
  368. }
  369. // Update control.
  370. setUpdate();
  371. return true;
  372. }
  373. //-----------------------------------------------------------------------------
  374. bool GuiSpriteCtrl::setImageFrame( const U32 frame )
  375. {
  376. // Check Existing Image.
  377. if ( mImageAssetId == StringTable->EmptyString && mBitmapName == StringTable->EmptyString )
  378. {
  379. // Warn.
  380. Con::warnf("GuiSpriteCtrl::setImageFrame() - Cannot set frame without existing image asset or bitmap.");
  381. // Return Here.
  382. return false;
  383. }
  384. // Set frame.
  385. mFrame = frame;
  386. // Finish if not awake.
  387. if ( !isAwake() )
  388. return true;
  389. if(usesAsset())
  390. {
  391. // Call parent.
  392. if ( !ImageFrameProvider::setImageFrame(frame) )
  393. return false;
  394. }
  395. // Update control.
  396. setUpdate();
  397. return true;
  398. }
  399. //-----------------------------------------------------------------------------
  400. bool GuiSpriteCtrl::setNamedImageFrame( const char* pNamedFrame )
  401. {
  402. // Check Existing Image.
  403. if ( mImageAssetId == StringTable->EmptyString )
  404. {
  405. // Warn.
  406. Con::warnf("GuiSpriteCtrl::setNamedImageFrame() - Cannot set named frame without existing asset Id.");
  407. // Return Here.
  408. return false;
  409. }
  410. // Set named frame.
  411. mNamedImageFrameId = StringTable->insert(pNamedFrame);
  412. // Finish if not awake.
  413. if ( !isAwake() )
  414. return true;
  415. // Call parent.
  416. if ( !ImageFrameProvider::setNamedImageFrame(pNamedFrame) )
  417. return false;
  418. // Update control.
  419. setUpdate();
  420. return true;
  421. }
  422. //-----------------------------------------------------------------------------
  423. bool GuiSpriteCtrl::setAnimation( const char* pAnimationAssetId )
  424. {
  425. // Sanity!
  426. AssertFatal( pAnimationAssetId != NULL, "Cannot use a NULL asset Id." );
  427. // Reset the image asset Id.
  428. if ( mImageAssetId != StringTable->EmptyString )
  429. mImageAssetId = StringTable->EmptyString;
  430. // Fetch the asset Id.
  431. if ( mAnimationAssetId != pAnimationAssetId )
  432. mAnimationAssetId = StringTable->insert(pAnimationAssetId);
  433. // Finish if not awake.
  434. if ( !isAwake() )
  435. return true;
  436. // Play animation asset if it's valid.
  437. if ( mAnimationAssetId != StringTable->EmptyString )
  438. ImageFrameProvider::setAnimation( mAnimationAssetId );
  439. return true;
  440. }
  441. void GuiSpriteCtrl::onRender(Point2I offset, const RectI &updateRect)
  442. {
  443. RectI ctrlRect = applyMargins(offset, mBounds.extent, NormalState, mProfile);
  444. if (!ctrlRect.isValidRect())
  445. {
  446. return;
  447. }
  448. renderUniversalRect(ctrlRect, mProfile, NormalState);
  449. RectI fillRect = applyBorders(ctrlRect.point, ctrlRect.extent, NormalState, mProfile);
  450. RectI contentRect = applyPadding(fillRect.point, fillRect.extent, NormalState, mProfile);
  451. if (contentRect.isValidRect() && (usesAsset() || mBitmapName != StringTable->EmptyString))
  452. {
  453. RectI oldClip = dglGetClipRect();
  454. RectI clipRect = contentRect;
  455. if (clipRect.intersect(oldClip))
  456. {
  457. dglSetClipRect(clipRect);
  458. dglSetBitmapModulation(ColorF(mImageColor));
  459. Point2I imageOffset = Point2I(0, 0);
  460. Point2I size = constrain(mImageSize);
  461. if (mTileImage) //Tile the image
  462. {
  463. imageOffset = mPositionOffset;
  464. imageOffset.x = (mPositionOffset.x % size.x);
  465. if (imageOffset.x > 0)
  466. {
  467. imageOffset.x -= size.x;
  468. }
  469. imageOffset.y = (mPositionOffset.y % size.y);
  470. if (imageOffset.y > 0)
  471. {
  472. imageOffset.y -= size.y;
  473. }
  474. RenderTiledImage(contentRect, imageOffset, size);
  475. }
  476. else if (mFullSize) //Fill with the image
  477. {
  478. size = constrain(contentRect.extent, false);
  479. if (mConstrainProportions)
  480. {
  481. imageOffset = applyAlignment(contentRect, size);
  482. }
  483. RenderImage(contentRect, imageOffset, size);
  484. }
  485. else //Position the image by profile alignment
  486. {
  487. if (mClampImage)
  488. {
  489. if (size.x > contentRect.extent.x)
  490. {
  491. size.x = contentRect.extent.x;
  492. size = constrainLockX(size);
  493. }
  494. if (size.y > contentRect.extent.y)
  495. {
  496. size.y = contentRect.extent.y;
  497. size = constrainLockY(size);
  498. }
  499. }
  500. imageOffset = applyAlignment(contentRect, size);
  501. RenderImage(contentRect, imageOffset, size);
  502. }
  503. dglClearBitmapModulation();
  504. dglSetClipRect(oldClip);
  505. //Render the childen
  506. renderChildControls(offset, contentRect, updateRect);
  507. }
  508. //One more thing...
  509. if (usesAsset() && !isStaticFrameProvider() && !isAnimationFinished() && !isAnimationPaused() && !isProcessingTicks())
  510. {
  511. setProcessTicks(true);
  512. }
  513. }
  514. }
  515. void GuiSpriteCtrl::RenderImage(RectI &bounds, Point2I &offset, Point2I &size)
  516. {
  517. TextureObject* texture = usesAsset() ? (TextureObject *) getProviderTexture() : (TextureObject *)mBitmapTextureHandle;
  518. RectI srcRegion = getSourceRect(texture);
  519. if (!srcRegion.isValidRect())
  520. {
  521. return;
  522. }
  523. RectI dstRegion = RectI(bounds.point + offset, size);
  524. dglDrawBitmapStretchSR(texture, dstRegion, srcRegion, false);
  525. }
  526. void GuiSpriteCtrl::RenderTiledImage(RectI &bounds, Point2I &start, Point2I &size)
  527. {
  528. TextureObject* texture = usesAsset() ? (TextureObject *)getProviderTexture() : (TextureObject *)mBitmapTextureHandle;
  529. RectI srcRegion = getSourceRect(texture);
  530. if (!srcRegion.isValidRect())
  531. {
  532. return;
  533. }
  534. RectI dstRegion;
  535. float xdone = ((float)(bounds.extent.x - start.x) / (float)size.x) + 1;
  536. float ydone = ((float)(bounds.extent.y - start.y) / (float)size.y) + 1;
  537. // We manually draw each repeat because non power of two textures will
  538. // not tile correctly when rendered with dglDrawBitmapTile().
  539. for (int y = 0; y < ydone; ++y)
  540. {
  541. for (int x = 0; x < xdone; ++x)
  542. {
  543. dstRegion.set((size.x * x) + start.x + bounds.point.x, (size.y * y) + start.y + bounds.point.y, size.x, size.y);
  544. dglDrawBitmapStretchSR(texture, dstRegion, srcRegion, false);
  545. }
  546. }
  547. }
  548. RectI GuiSpriteCtrl::getSourceRect(TextureObject* texture)
  549. {
  550. RectI srcRegion = RectI(0, 0, 0, 0);
  551. bool hasAsset = usesAsset();
  552. if (hasAsset)
  553. {
  554. const ImageAsset::FrameArea& frameArea = getProviderImageFrameArea();
  555. srcRegion = RectI(frameArea.mPixelArea.mPixelOffset, Point2I(frameArea.mPixelArea.mPixelWidth, frameArea.mPixelArea.mPixelHeight));
  556. }
  557. else if (!hasAsset && mSingleFrameBitmap)
  558. {
  559. srcRegion = RectI(0, 0, texture->getBitmapWidth(), texture->getBitmapHeight());
  560. }
  561. else if (!hasAsset && constructBitmapArray() > mFrame)
  562. {
  563. RectI* frameRect = mBitmapArrayRects.address();
  564. srcRegion = frameRect[mFrame];
  565. }
  566. return srcRegion;
  567. }
  568. void GuiSpriteCtrl::moveTo(S32 x, S32 y, S32 time, EasingFunction ease)
  569. {
  570. mStartOffset.set(mPositionOffset.x, mPositionOffset.y);
  571. mTargetOffset.set(x, y);
  572. mFluidMoveTo.setAnimationLength(getMax(time, 0));
  573. mFluidMoveTo.setEasingFunction(ease);
  574. mFluidMoveTo.startFluidAnimation();
  575. setProcessTicks(true);
  576. }
  577. void GuiSpriteCtrl::growTo(S32 x, S32 y, S32 time, EasingFunction ease)
  578. {
  579. mStartSize.set(mImageSize.x, mImageSize.y);
  580. mTargetSize.set(x, y);
  581. mFluidGrowTo.setAnimationLength(getMax(time, 0));
  582. mFluidGrowTo.setEasingFunction(ease);
  583. mFluidGrowTo.startFluidAnimation();
  584. setProcessTicks(true);
  585. }
  586. void GuiSpriteCtrl::fadeTo(const ColorI &color, S32 time, EasingFunction ease)
  587. {
  588. mImageColor.setAnimationLength(getMax(time, 0));
  589. mImageColor.setEasingFunction(ease);
  590. mImageColor.startFluidAnimation(color);
  591. setProcessTicks(true);
  592. }
  593. void GuiSpriteCtrl::processTick()
  594. {
  595. bool shouldWeContinue = false;
  596. //Animating
  597. shouldWeContinue |= update(Tickable::smTickSec);
  598. shouldWeContinue |= animateMoveTo();
  599. shouldWeContinue |= animateGrowTo();
  600. shouldWeContinue |= animateFadeTo();
  601. if (!shouldWeContinue)
  602. {
  603. setProcessTicks(false);
  604. }
  605. }
  606. bool GuiSpriteCtrl::update(const F32 elapsedTime)
  607. {
  608. //Note: although this overrides ImageFrameProviderCore::update, the return value has a different meaning.
  609. //True means the control should continue ticking. False means the animation now longer needs tickets.
  610. // Static provider or bitmap?
  611. if (!usesAsset() || isStaticFrameProvider())
  612. {
  613. return false;
  614. }
  615. // Finish if the animation has finished or paused.
  616. if (isAnimationFinished() || isAnimationPaused())
  617. return false;
  618. // Update the animation.
  619. if (updateAnimation(elapsedTime))
  620. {
  621. setUpdate();
  622. }
  623. // Finish if the animation has NOT finished.
  624. if (!isAnimationFinished())
  625. return true;
  626. // Perform callback.
  627. onAnimationEnd();
  628. //We no longer need ticks.
  629. return false;
  630. }
  631. bool GuiSpriteCtrl::animateMoveTo()
  632. {
  633. if (!mFluidMoveTo.isAnimating())
  634. {
  635. return false;
  636. }
  637. setUpdate();
  638. F32 progress = mFluidMoveTo.getProgress(32.0f);
  639. mPositionOffset.set(
  640. mFluidMoveTo.processValue(progress, mStartOffset.x, mTargetOffset.x),
  641. mFluidMoveTo.processValue(progress, mStartOffset.y, mTargetOffset.y));
  642. if (!mFluidMoveTo.isAnimating() && isMethod("onMoveToComplete"))
  643. {
  644. Con::executef(this, 1, "onMoveToComplete");
  645. }
  646. return mFluidMoveTo.isAnimating();
  647. }
  648. bool GuiSpriteCtrl::animateGrowTo()
  649. {
  650. if (!mFluidGrowTo.isAnimating())
  651. {
  652. return false;
  653. }
  654. setUpdate();
  655. F32 progress = mFluidGrowTo.getProgress(32.0f);
  656. mImageSize.set(
  657. mFluidGrowTo.processValue(progress, mStartSize.x, mTargetSize.x),
  658. mFluidGrowTo.processValue(progress, mStartSize.y, mTargetSize.y));
  659. if (!mFluidGrowTo.isAnimating() && isMethod("onGrowToComplete"))
  660. {
  661. Con::executef(this, 1, "onGrowToComplete");
  662. }
  663. return mFluidGrowTo.isAnimating();
  664. }
  665. bool GuiSpriteCtrl::animateFadeTo()
  666. {
  667. if (!mImageColor.isAnimating())
  668. {
  669. return false;
  670. }
  671. setUpdate();
  672. mImageColor.processTick();
  673. if (!mImageColor.isAnimating() && isMethod("onFadeToComplete"))
  674. {
  675. Con::executef(this, 1, "onFadeToComplete");
  676. }
  677. return mImageColor.isAnimating();
  678. }
  679. //------------------------------------------------------------------------------
  680. void GuiSpriteCtrl::onAnimationEnd( void )
  681. {
  682. // We've arrived at the end - pause it
  683. ImageFrameProvider::pauseAnimation(true);
  684. // Send a callback
  685. if(isMethod("onAnimationEnd"))
  686. Con::executef(this, 3, "onAnimationEnd", ImageFrameProvider::getCurrentAnimationAssetId());
  687. }