SpriteBatchItem.cc 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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_BATCH_ITEM_H_
  23. #include "2d/core/SpriteBatchItem.h"
  24. #endif
  25. #ifndef _SPRITE_BATCH_H_
  26. #include "2d/core/SpriteBatch.h"
  27. #endif
  28. #ifndef _SCENE_OBJECT_H_
  29. #include "2d/sceneobject/SceneObject.h"
  30. #endif
  31. //-----------------------------------------------------------------------------
  32. StringTableEntry spritesItemTypeName = StringTable->insert( "Sprite" );
  33. static StringTableEntry spriteNameName = StringTable->insert("Name");
  34. static StringTableEntry spriteLogicalPositionName = StringTable->insert("LogicalPosition");
  35. static StringTableEntry spriteVisibleName = StringTable->insert("Visible");
  36. static StringTableEntry spriteLocalPositionName = StringTable->insert("Position");
  37. static StringTableEntry spriteLocalAngleName = StringTable->insert("Angle");
  38. static StringTableEntry spriteSizeName = StringTable->insert("Size");
  39. static StringTableEntry spriteDepthName = StringTable->insert("Depth");
  40. static StringTableEntry spriteFlipXName = StringTable->insert("FlipX");
  41. static StringTableEntry spriteFlipYName = StringTable->insert("FlipY");
  42. static StringTableEntry spriteSortPointName = StringTable->insert("SortPoint");
  43. static StringTableEntry spriteRenderGroupName = StringTable->insert("RenderGroup");
  44. static StringTableEntry spriteBlendModeName = StringTable->insert("BlendMode");
  45. static StringTableEntry spriteSrcBlendFactorName = StringTable->insert("SrcBlendFactor");
  46. static StringTableEntry spriteDstBlendFactorName = StringTable->insert("DstBlendFactor");
  47. static StringTableEntry spriteBlendColorName = StringTable->insert("BlendColor");
  48. static StringTableEntry spriteAlphaTestName = StringTable->insert("AlphaTest");
  49. static StringTableEntry spriteImageName = StringTable->insert("Image");
  50. static StringTableEntry spriteImageFrameName = StringTable->insert("Frame");
  51. static StringTableEntry spriteNamedImageFrameName = StringTable->insert("NamedFrame");
  52. static StringTableEntry spriteAnimationName = StringTable->insert("Animation");
  53. static StringTableEntry spriteDataObjectName = StringTable->insert("DataObject");
  54. static StringTableEntry spriteUserDataName = StringTable->insert("UserData");
  55. //------------------------------------------------------------------------------
  56. SpriteBatchItem::SpriteBatchItem() : mProxyId( SpriteBatch::INVALID_SPRITE_PROXY )
  57. {
  58. resetState();
  59. }
  60. //------------------------------------------------------------------------------
  61. SpriteBatchItem::~SpriteBatchItem()
  62. {
  63. resetState();
  64. }
  65. //------------------------------------------------------------------------------
  66. void SpriteBatchItem::resetState( void )
  67. {
  68. // Call parent.
  69. Parent::resetState();
  70. // Do we have a proxy.
  71. if ( mProxyId != SpriteBatch::INVALID_SPRITE_PROXY )
  72. {
  73. // Sanity!
  74. AssertFatal( mSpriteBatch != NULL, "Cannot remove proxy with NULL sprite batch." );
  75. // Destroy proxy.
  76. mSpriteBatch->destroyQueryProxy( this );
  77. }
  78. mSpriteBatch = NULL;
  79. mBatchId = 0;
  80. mName = StringTable->EmptyString;
  81. mLogicalPosition.resetState();
  82. mVisible = true;
  83. mExplicitMode = false;
  84. mTriangleRun = false;
  85. mLocalPosition.SetZero();
  86. for (U32 i = 0; i < 4; i++)
  87. mExplicitVerts[i].SetZero();
  88. mDepth = 0.0f;
  89. mLocalAngle = 0.0f;
  90. setSize( Vector2( 1.0f, 1.0f ) );
  91. mFlipX = false;
  92. mFlipY = false;
  93. mSortPoint.SetZero();
  94. mRenderGroup = StringTable->EmptyString;
  95. mBlendMode = true;
  96. mSrcBlendFactor = GL_SRC_ALPHA;
  97. mDstBlendFactor = GL_ONE_MINUS_SRC_ALPHA;
  98. mBlendColor = ColorF(1.0f,1.0f,1.0f,1.0f);
  99. mAlphaTest = -1.0f;
  100. mUseComplexColor = false;
  101. mComplexColor0 = ColorF(1.0f, 1.0f, 1.0f, 1.0f);
  102. mComplexColor1 = ColorF(1.0f, 1.0f, 1.0f, 1.0f);
  103. mComplexColor2 = ColorF(1.0f, 1.0f, 1.0f, 1.0f);
  104. mComplexColor3 = ColorF(1.0f, 1.0f, 1.0f, 1.0f);
  105. mDataObject = NULL;
  106. mLocalTransformDirty = true;
  107. mLocalAABB.lowerBound.Set( -0.5f, -0.5f );
  108. mLocalAABB.upperBound.Set( 0.5f, 0.5f );
  109. mRenderAABB.lowerBound.Set( -0.5f, -0.5f );
  110. mRenderAABB.upperBound.Set( 0.5f, 0.5f );
  111. mRenderPosition.setZero();
  112. mLastBatchTransformId = 0;
  113. mSpriteBatchQueryKey = 0;
  114. mUserData = NULL;
  115. // Only animates if the scene is not paused.
  116. mSelfTick = false;
  117. }
  118. //------------------------------------------------------------------------------
  119. void SpriteBatchItem::setBatchParent( SpriteBatch* pSpriteBatch, const U32 batchId )
  120. {
  121. // Sanity!
  122. AssertFatal( pSpriteBatch != NULL, "Cannot assign a NULL batch parent." );
  123. AssertFatal( mSpriteBatch == NULL, "Cannot assign batch parent as one is already assigned." );
  124. AssertFatal( batchId != 0, "Cannot assign a zero batch Id." );
  125. // Assign.
  126. mSpriteBatch = pSpriteBatch;
  127. mBatchId = batchId;
  128. // Create proxy.
  129. mSpriteBatch->createQueryProxy( this );
  130. }
  131. //------------------------------------------------------------------------------
  132. void SpriteBatchItem::copyTo( SpriteBatchItem* pSpriteBatchItem ) const
  133. {
  134. // Call parent.
  135. Parent::copyTo( pSpriteBatchItem );
  136. // Set sprite batch item.
  137. pSpriteBatchItem->setLogicalPosition( getLogicalPosition() );
  138. pSpriteBatchItem->setName( getName() );
  139. pSpriteBatchItem->setVisible( getVisible() );
  140. pSpriteBatchItem->setLocalPosition( getLocalPosition() );
  141. pSpriteBatchItem->setDepth( getDepth() );
  142. pSpriteBatchItem->setLocalAngle( getLocalAngle() );
  143. pSpriteBatchItem->setSize( getSize() );
  144. pSpriteBatchItem->setFlipX( getFlipX() );
  145. pSpriteBatchItem->setFlipY( getFlipY() );
  146. pSpriteBatchItem->setSortPoint( getSortPoint() );
  147. pSpriteBatchItem->setRenderGroup( getRenderGroup() );
  148. pSpriteBatchItem->setBlendMode( getBlendMode() );
  149. pSpriteBatchItem->setSrcBlendFactor( getSrcBlendFactor() );
  150. pSpriteBatchItem->setDstBlendFactor( getDstBlendFactor() );
  151. pSpriteBatchItem->setBlendColor( getBlendColor() );
  152. pSpriteBatchItem->setAlphaTest( getAlphaTest() );
  153. }
  154. //------------------------------------------------------------------------------
  155. const ColorF& SpriteBatchItem::getComplexColor(const S8 cornerID)
  156. {
  157. if (cornerID == 1)
  158. {
  159. return mComplexColor0;
  160. }
  161. else if (cornerID == 2)
  162. {
  163. return mComplexColor1;
  164. }
  165. else if (cornerID == 3)
  166. {
  167. return mComplexColor2;
  168. }
  169. else if (cornerID == 4)
  170. {
  171. return mComplexColor3;
  172. }
  173. else
  174. {
  175. return ColorF(1.0f, 1.0f, 1.0f, 1.0f);
  176. }
  177. }
  178. //------------------------------------------------------------------------------
  179. void SpriteBatchItem::prepareRender( SceneRenderRequest* pSceneRenderRequest, const U32 batchTransformId )
  180. {
  181. // Debug Profiling.
  182. PROFILE_SCOPE(SpriteBatchItem_PrepareRender);
  183. // Sanity!
  184. AssertFatal( pSceneRenderRequest != NULL, "Cannot prepare a sprite batch with a NULL scene render request." );
  185. // Update the world transform.
  186. updateWorldTransform( batchTransformId );
  187. pSceneRenderRequest->mWorldPosition = mRenderPosition;
  188. pSceneRenderRequest->mDepth = getDepth();
  189. pSceneRenderRequest->mSortPoint = getSortPoint();
  190. pSceneRenderRequest->mSerialId = getBatchId();
  191. pSceneRenderRequest->mRenderGroup = getRenderGroup();
  192. pSceneRenderRequest->mBlendMode = getBlendMode();
  193. pSceneRenderRequest->mSrcBlendFactor = getSrcBlendFactor();
  194. pSceneRenderRequest->mDstBlendFactor = getDstBlendFactor();
  195. pSceneRenderRequest->mBlendColor = getBlendColor();
  196. pSceneRenderRequest->mAlphaTest = getAlphaTest();
  197. }
  198. //------------------------------------------------------------------------------
  199. void SpriteBatchItem::render( BatchRender* pBatchRenderer, const SceneRenderRequest* pSceneRenderRequest, const U32 batchTransformId )
  200. {
  201. // Debug Profiling.
  202. PROFILE_SCOPE(SpriteBatchItem_Render);
  203. // Update the world transform.
  204. updateWorldTransform( batchTransformId );
  205. // Set the blend mode.
  206. pBatchRenderer->setBlendMode( pSceneRenderRequest );
  207. // Set the alpha test mode.
  208. pBatchRenderer->setAlphaTestMode( pSceneRenderRequest );
  209. // Render.
  210. if (mTriangleRun) {
  211. Parent::render(
  212. mDrawData.vertexCount,
  213. mDrawData.vertexArray.data(),
  214. mDrawData.textureArray.data(),
  215. mDrawData.colorArray.data(),
  216. pBatchRenderer
  217. );
  218. }
  219. else if (mExplicitMode) {
  220. Parent::render(mFlipX, mFlipY,
  221. mRenderOOBB[0],
  222. mRenderOOBB[1],
  223. mRenderOOBB[2],
  224. mRenderOOBB[3],
  225. mExplicitUVs[0],
  226. mExplicitUVs[1],
  227. mExplicitUVs[2],
  228. mExplicitUVs[3],
  229. pBatchRenderer);
  230. }
  231. else if (mUseComplexColor) {
  232. Parent::renderComplex(mFlipX, mFlipY,
  233. mRenderOOBB[0],
  234. mRenderOOBB[1],
  235. mRenderOOBB[2],
  236. mRenderOOBB[3],
  237. pBatchRenderer,
  238. mComplexColor0,
  239. mComplexColor1,
  240. mComplexColor2,
  241. mComplexColor3);
  242. }
  243. else {
  244. Parent::render(mFlipX, mFlipY,
  245. mRenderOOBB[0],
  246. mRenderOOBB[1],
  247. mRenderOOBB[2],
  248. mRenderOOBB[3],
  249. pBatchRenderer);
  250. }
  251. }
  252. //------------------------------------------------------------------------------
  253. // When something else (like an animation runtime) is controlling the vertices and uv's
  254. // just past them through to the render stage.
  255. void SpriteBatchItem::setExplicitVertices( const F32* vertices, const F32* uvs)
  256. {
  257. mExplicitMode = true;
  258. mExplicitVerts[0].x = vertices[0];
  259. mExplicitVerts[0].y = vertices[1];
  260. mExplicitVerts[1].x = vertices[2];
  261. mExplicitVerts[1].y = vertices[3];
  262. mExplicitVerts[2].x = vertices[4];
  263. mExplicitVerts[2].y = vertices[5];
  264. mExplicitVerts[3].x = vertices[6];
  265. mExplicitVerts[3].y = vertices[7];
  266. if (uvs) {
  267. mExplicitUVs[0].x = uvs[0];
  268. mExplicitUVs[0].y = uvs[1];
  269. mExplicitUVs[1].x = uvs[2];
  270. mExplicitUVs[1].y = uvs[3];
  271. mExplicitUVs[2].x = uvs[4];
  272. mExplicitUVs[2].y = uvs[5];
  273. mExplicitUVs[3].x = uvs[6];
  274. mExplicitUVs[3].y = uvs[7];
  275. }
  276. }
  277. //------------------------------------------------------------------------------
  278. void SpriteBatchItem::updateLocalTransform( void )
  279. {
  280. // Debug Profiling.
  281. PROFILE_SCOPE(SpriteBatchItem_UpdateLocalTransform);
  282. // Sanity!
  283. AssertFatal( mSpriteBatch != NULL, "SpriteBatchItem::updateLocalTransform() - Cannot update local transform with a NULL sprite batch." );
  284. // Finish if local transform is not dirty.
  285. if ( !mLocalTransformDirty )
  286. return;
  287. // Set local transform.
  288. b2Transform localTransform;
  289. localTransform.p = mLocalPosition;
  290. localTransform.q.Set(mLocalAngle);
  291. // Set local size vertices.
  292. if (!mExplicitMode)
  293. {
  294. // Calculate half size.
  295. const F32 halfWidth = mSize.x * 0.5f;
  296. const F32 halfHeight = mSize.y * 0.5f;
  297. mLocalOOBB[0].Set( -halfWidth, -halfHeight );
  298. mLocalOOBB[1].Set( +halfWidth, -halfHeight );
  299. mLocalOOBB[2].Set( +halfWidth, +halfHeight );
  300. mLocalOOBB[3].Set( -halfWidth, +halfHeight );
  301. }
  302. else
  303. {
  304. mLocalOOBB[0] = mExplicitVerts[0];
  305. mLocalOOBB[1] = mExplicitVerts[1];
  306. mLocalOOBB[2] = mExplicitVerts[2];
  307. mLocalOOBB[3] = mExplicitVerts[3];
  308. }
  309. // Calculate local OOBB.
  310. CoreMath::mCalculateOOBB( mLocalOOBB, localTransform, mLocalOOBB );
  311. // Calculate local AABB.
  312. CoreMath::mOOBBtoAABB( mLocalOOBB, mLocalAABB );
  313. // Move query proxy.
  314. mSpriteBatch->moveQueryProxy( this, mLocalAABB );
  315. // Flag local transform as NOT dirty.
  316. mLocalTransformDirty = false;
  317. }
  318. //------------------------------------------------------------------------------
  319. void SpriteBatchItem::updateWorldTransform( const U32 batchTransformId )
  320. {
  321. // Debug Profiling.
  322. PROFILE_SCOPE(SpriteBatchItem_UpdateWorldTransform);
  323. // Sanity!
  324. AssertFatal( mSpriteBatch != NULL, "SpriteBatchItem::updateWorldTransform() - Cannot update transform with a NULL sprite batch." );
  325. // Update the local transform if needed.
  326. if ( mLocalTransformDirty )
  327. {
  328. updateLocalTransform();
  329. }
  330. // Finish if the batch transform is up-to-date.
  331. else if ( batchTransformId == mLastBatchTransformId )
  332. return;
  333. // Fetch world transform.
  334. const b2Transform& worldTransform = mSpriteBatch->getBatchTransform();
  335. // Calculate world OOBB.
  336. CoreMath::mCalculateOOBB( mLocalOOBB, worldTransform, mRenderOOBB );
  337. // Calculate render AABB.
  338. CoreMath::mOOBBtoAABB( mRenderOOBB, mRenderAABB );
  339. // Calculate the render position.
  340. mRenderPosition = mRenderAABB.GetCenter();
  341. // Note the last batch transform Id.
  342. mLastBatchTransformId = batchTransformId;
  343. }
  344. //------------------------------------------------------------------------------
  345. void SpriteBatchItem::onTamlCustomWrite( TamlCustomNode* pParentNode )
  346. {
  347. // Add sprite node.
  348. TamlCustomNode* pSpriteNode = pParentNode->addNode( spritesItemTypeName );
  349. // Write name.
  350. if ( getName() != StringTable->EmptyString )
  351. pSpriteNode->addField( spriteNameName, getName() );
  352. // Static frame provider?
  353. if ( isStaticFrameProvider() )
  354. {
  355. // Fetch image asset Id.
  356. StringTableEntry assetId = getImage();
  357. // Do we have an image?
  358. if ( assetId != StringTable->EmptyString )
  359. {
  360. // Yes, so write image asset Id.
  361. pSpriteNode->addField( spriteImageName, assetId );
  362. // Write the image frame.
  363. if ( isUsingNamedImageFrame() )
  364. pSpriteNode->addField( spriteNamedImageFrameName, getNamedImageFrame() );
  365. else
  366. pSpriteNode->addField( spriteImageFrameName, getImageFrame() );
  367. }
  368. }
  369. else
  370. {
  371. // Fetch animation asset Id.
  372. StringTableEntry assetId = getAnimation();
  373. // Do we have an animation?
  374. if ( assetId != StringTable->EmptyString )
  375. {
  376. // Yes, so write animation asset Id.
  377. pSpriteNode->addField( spriteAnimationName, assetId );
  378. }
  379. }
  380. // Write visible.
  381. if ( !mVisible )
  382. pSpriteNode->addField( spriteVisibleName, mVisible );
  383. // Write local position.
  384. pSpriteNode->addField( spriteLocalPositionName, mLocalPosition );
  385. // Write local angle.
  386. if ( mNotZero(mLocalAngle) )
  387. pSpriteNode->addField( spriteLocalAngleName, mRadToDeg(mLocalAngle) );
  388. // Write size.
  389. pSpriteNode->addField( spriteSizeName, mSize );
  390. // Write depth.
  391. if ( mNotZero(mDepth) )
  392. pSpriteNode->addField( spriteDepthName, mDepth );
  393. // Write flipX
  394. if ( mFlipX )
  395. pSpriteNode->addField( spriteFlipXName, mFlipX );
  396. // Write flipY
  397. if ( mFlipY )
  398. pSpriteNode->addField( spriteFlipYName, mFlipY );
  399. // Write sort point.
  400. if ( mSortPoint.notZero() )
  401. pSpriteNode->addField( spriteSortPointName, mSortPoint );
  402. // Write render group.
  403. if ( mRenderGroup != StringTable->EmptyString )
  404. pSpriteNode->addField( spriteRenderGroupName, mRenderGroup );
  405. // Write blend mode.
  406. if ( !mBlendMode )
  407. pSpriteNode->addField( spriteBlendModeName, mBlendMode );
  408. // Write source blend factor.
  409. if ( mBlendMode && mSrcBlendFactor != GL_SRC_ALPHA )
  410. pSpriteNode->addField( spriteSrcBlendFactorName, SceneObject::getSrcBlendFactorDescription(mSrcBlendFactor) );
  411. // Write destination blend factor.
  412. if ( mBlendMode && mDstBlendFactor != GL_ONE_MINUS_SRC_ALPHA )
  413. pSpriteNode->addField( spriteDstBlendFactorName, SceneObject::getDstBlendFactorDescription(mDstBlendFactor) );
  414. // Write blend color.
  415. if ( mBlendMode && mBlendColor != ColorF(1.0f, 1.0f, 1.0f, 1.0f) )
  416. pSpriteNode->addField( spriteBlendColorName, mBlendColor );
  417. // Write alpha test.
  418. if ( mBlendMode && mAlphaTest >= 0.0f )
  419. pSpriteNode->addField( spriteAlphaTestName, mAlphaTest );
  420. // Write logical position.
  421. if ( getLogicalPosition().isValid() )
  422. pSpriteNode->addField( spriteLogicalPositionName, getLogicalPosition().getString() );
  423. // Write data object.
  424. if ( getDataObject() != NULL )
  425. pSpriteNode->addNode( getDataObject() );
  426. if ( getUserData() != NULL)
  427. {
  428. const char* UserDatastr = (const char*) getUserData();
  429. pSpriteNode->addField( "UserData", UserDatastr );
  430. }
  431. }
  432. //------------------------------------------------------------------------------
  433. void SpriteBatchItem::onTamlCustomRead( const TamlCustomNode* pSpriteNode )
  434. {
  435. // Sanity!
  436. AssertFatal( mSpriteBatch != NULL, "SpriteBatchItem::onTamlCustomRead() - Cannot read sprite batch item with sprite batch." );
  437. // Fetch sprite fields.
  438. const TamlCustomFieldVector& spriteField = pSpriteNode->getFields();
  439. // Iterate property fields.
  440. for ( TamlCustomFieldVector::const_iterator fieldItr = spriteField.begin(); fieldItr != spriteField.end(); ++fieldItr )
  441. {
  442. // Fetch sprite field.
  443. TamlCustomField* pSpriteField = *fieldItr;
  444. // Fetch sprite field name.
  445. StringTableEntry fieldName = pSpriteField->getFieldName();
  446. // Reset image frame.
  447. S32 imageFrame = -1;
  448. if ( fieldName == spriteNameName )
  449. {
  450. setName( pSpriteField->getFieldValue() );
  451. }
  452. else if ( fieldName == spriteImageName )
  453. {
  454. setImage( pSpriteField->getFieldValue() );
  455. // Set image frame if it's available.
  456. if ( imageFrame != -1 )
  457. setImageFrame( imageFrame );
  458. }
  459. else if ( fieldName == spriteImageFrameName )
  460. {
  461. pSpriteField->getFieldValue( imageFrame );
  462. // Set image frame if image is available.
  463. if ( getImage() != StringTable->EmptyString )
  464. setImageFrame( imageFrame );
  465. }
  466. else if ( fieldName == spriteNamedImageFrameName )
  467. {
  468. if ( getImage() != StringTable->EmptyString )
  469. setNamedImageFrame( pSpriteField->getFieldValue() );
  470. }
  471. else if ( fieldName == spriteAnimationName )
  472. {
  473. setAnimation( pSpriteField->getFieldValue() );
  474. }
  475. else if ( fieldName == spriteVisibleName )
  476. {
  477. bool visible;
  478. pSpriteField->getFieldValue( visible );
  479. setVisible( visible );
  480. }
  481. else if ( fieldName == spriteLocalPositionName )
  482. {
  483. Vector2 localPosition;
  484. pSpriteField->getFieldValue( localPosition );
  485. setLocalPosition( localPosition );
  486. }
  487. else if ( fieldName == spriteLocalAngleName )
  488. {
  489. F32 localAngle;
  490. pSpriteField->getFieldValue( localAngle );
  491. setLocalAngle( mDegToRad( localAngle ) );
  492. }
  493. else if ( fieldName == spriteSizeName )
  494. {
  495. Vector2 size;
  496. pSpriteField->getFieldValue( size );
  497. setSize( size );
  498. }
  499. else if ( fieldName == spriteDepthName )
  500. {
  501. F32 depth;
  502. pSpriteField->getFieldValue( depth );
  503. setDepth( depth );
  504. }
  505. else if ( fieldName == spriteFlipXName )
  506. {
  507. bool flipX;
  508. pSpriteField->getFieldValue( flipX );
  509. setFlipX( flipX );
  510. }
  511. else if ( fieldName == spriteFlipYName )
  512. {
  513. bool flipY;
  514. pSpriteField->getFieldValue( flipY );
  515. setFlipY( flipY );
  516. }
  517. else if ( fieldName == spriteSortPointName )
  518. {
  519. Vector2 sortPoint;
  520. pSpriteField->getFieldValue( sortPoint );
  521. setSortPoint( sortPoint );
  522. }
  523. else if ( fieldName == spriteRenderGroupName )
  524. {
  525. setRenderGroup( pSpriteField->getFieldValue() );
  526. }
  527. else if ( fieldName == spriteBlendModeName )
  528. {
  529. bool blendMode;
  530. pSpriteField->getFieldValue( blendMode );
  531. setBlendMode( blendMode );
  532. }
  533. else if ( fieldName == spriteSrcBlendFactorName )
  534. {
  535. setSrcBlendFactor( (GLenum)SceneObject::getSrcBlendFactorEnum( pSpriteField->getFieldValue() ) );
  536. }
  537. else if ( fieldName == spriteDstBlendFactorName )
  538. {
  539. setDstBlendFactor( (GLenum)SceneObject::getDstBlendFactorEnum( pSpriteField->getFieldValue() ) );
  540. }
  541. else if ( fieldName == spriteBlendColorName )
  542. {
  543. ColorF blendColor;
  544. pSpriteField->getFieldValue( blendColor );
  545. setBlendColor( blendColor );
  546. }
  547. else if ( fieldName == spriteAlphaTestName )
  548. {
  549. F32 alphaTest;
  550. pSpriteField->getFieldValue( alphaTest );
  551. setAlphaTest( alphaTest );
  552. }
  553. // Logical position.
  554. else if ( fieldName == spriteLogicalPositionName )
  555. {
  556. // Fetch logical position.
  557. const char* pLogicalPositionArgs = pSpriteField->getFieldValue();
  558. // Is there any data?
  559. if ( dStrlen( pLogicalPositionArgs ) == 0 )
  560. {
  561. // No, so warn.
  562. Con::warnf( "SpriteBatchItem::onTamlCustomRead() - Encountered an empty sprite key. This sprite will no longer be addressable by logical position." );
  563. continue;
  564. }
  565. // Set logical position.
  566. setLogicalPosition( LogicalPosition( pLogicalPositionArgs ) );
  567. }
  568. else if ( fieldName == spriteUserDataName )
  569. {
  570. StringTableEntry UserDatastr = StringTable->insert(pSpriteField->getFieldValue());
  571. setUserData((void *)UserDatastr);
  572. }
  573. }
  574. // Fetch sprite children.
  575. const TamlCustomNodeVector& spriteChildren = pSpriteNode->getChildren();
  576. // Set the data object if a single child exists.
  577. if ( spriteChildren.size() == 1 )
  578. setDataObject( spriteChildren[0]->getProxyObject<SimObject>(true) );
  579. }
  580. //------------------------------------------------------------------------------
  581. void SpriteBatchItem::WriteCustomTamlSchema( const AbstractClassRep* pClassRep, TiXmlElement* pParentElement )
  582. {
  583. // Sanity!
  584. AssertFatal( pClassRep != NULL, "SpriteBatchItem::WriteCustomTamlSchema() - ClassRep cannot be NULL." );
  585. AssertFatal( pParentElement != NULL, "SpriteBatchItem::WriteCustomTamlSchema() - Parent Element cannot be NULL." );
  586. // Create batch item element.
  587. TiXmlElement* pBatchItemElement = new TiXmlElement( "xs:element" );
  588. pBatchItemElement->SetAttribute( "name", spritesItemTypeName );
  589. pBatchItemElement->SetAttribute( "minOccurs", 0 );
  590. pBatchItemElement->SetAttribute( "maxOccurs", 1 );
  591. pParentElement->LinkEndChild( pBatchItemElement );
  592. // Create complex type Element.
  593. TiXmlElement* pBatchItemComplexTypeElement = new TiXmlElement( "xs:complexType" );
  594. pBatchItemElement->LinkEndChild( pBatchItemComplexTypeElement );
  595. // Create "Name" attribute.
  596. TiXmlElement* pBatchItemName = new TiXmlElement( "xs:attribute" );
  597. pBatchItemName->SetAttribute( "name", spriteNameName );
  598. pBatchItemName->SetAttribute( "type", "xs:string" );
  599. pBatchItemComplexTypeElement->LinkEndChild( pBatchItemName );
  600. // "Create "Image" attribute.
  601. TiXmlElement* pBatchItemImage = new TiXmlElement( "xs:attribute" );
  602. pBatchItemImage->SetAttribute( "name", spriteImageName );
  603. pBatchItemImage->SetAttribute( "type", "AssetId_ConsoleType" );
  604. pBatchItemComplexTypeElement->LinkEndChild( pBatchItemImage );
  605. // "Create "Image Frame" attribute.
  606. TiXmlElement* pBatchItemImageFrame = new TiXmlElement( "xs:attribute" );
  607. pBatchItemImageFrame->SetAttribute( "name", spriteImageFrameName );
  608. pBatchItemImageFrame->SetAttribute( "type", "xs:positiveInteger" );
  609. pBatchItemComplexTypeElement->LinkEndChild( pBatchItemImageFrame );
  610. // "Create "Animation" attribute.
  611. TiXmlElement* pBatchItemAnimation = new TiXmlElement( "xs:attribute" );
  612. pBatchItemAnimation->SetAttribute( "name", spriteAnimationName );
  613. pBatchItemAnimation->SetAttribute( "type", "AssetId_ConsoleType" );
  614. pBatchItemComplexTypeElement->LinkEndChild( pBatchItemAnimation );
  615. // Create "Visible" attribute.
  616. TiXmlElement* pBatchItemVisible = new TiXmlElement( "xs:attribute" );
  617. pBatchItemVisible->SetAttribute( "name", spriteVisibleName );
  618. pBatchItemVisible->SetAttribute( "type", "xs:boolean" );
  619. pBatchItemComplexTypeElement->LinkEndChild( pBatchItemVisible );
  620. // Create "Local Position" attribute.
  621. TiXmlElement* pBatchItemPosition = new TiXmlElement( "xs:attribute" );
  622. pBatchItemPosition->SetAttribute( "name", spriteLocalPositionName );
  623. pBatchItemPosition->SetAttribute( "type", "Vector2_ConsoleType" );
  624. pBatchItemComplexTypeElement->LinkEndChild( pBatchItemPosition );
  625. // Create "Size" attribute.
  626. TiXmlElement* pBatchItemSize = new TiXmlElement( "xs:attribute" );
  627. pBatchItemSize->SetAttribute( "name", spriteSizeName );
  628. pBatchItemSize->SetAttribute( "type", "Vector2_ConsoleType" );
  629. pBatchItemComplexTypeElement->LinkEndChild( pBatchItemSize );
  630. // Create "Local Angle" attribute.
  631. TiXmlElement* pBatchItemAngle = new TiXmlElement( "xs:attribute" );
  632. pBatchItemAngle->SetAttribute( "name", spriteLocalAngleName );
  633. pBatchItemAngle->SetAttribute( "type", "xs:float" );
  634. pBatchItemComplexTypeElement->LinkEndChild( pBatchItemAngle );
  635. // Create "Depth" attribute.
  636. TiXmlElement* pBatchItemDepth = new TiXmlElement( "xs:attribute" );
  637. pBatchItemDepth->SetAttribute( "name", spriteDepthName );
  638. pBatchItemDepth->SetAttribute( "type", "xs:float" );
  639. pBatchItemComplexTypeElement->LinkEndChild( pBatchItemDepth );
  640. // Create "FlipX" attribute.
  641. TiXmlElement* pBatchItemFlipX = new TiXmlElement( "xs:attribute" );
  642. pBatchItemFlipX->SetAttribute( "name", spriteFlipXName );
  643. pBatchItemFlipX->SetAttribute( "type", "xs:boolean" );
  644. pBatchItemComplexTypeElement->LinkEndChild( pBatchItemFlipX );
  645. // Create "FlipY" attribute.
  646. TiXmlElement* pBatchItemFlipY = new TiXmlElement( "xs:attribute" );
  647. pBatchItemFlipY->SetAttribute( "name", spriteFlipYName );
  648. pBatchItemFlipY->SetAttribute( "type", "xs:boolean" );
  649. pBatchItemComplexTypeElement->LinkEndChild( pBatchItemFlipY );
  650. // Create "Sort Point" attribute.
  651. TiXmlElement* pBatchItemSortPoint = new TiXmlElement( "xs:attribute" );
  652. pBatchItemSortPoint->SetAttribute( "name", spriteSortPointName );
  653. pBatchItemSortPoint->SetAttribute( "type", "Vector2_ConsoleType" );
  654. pBatchItemComplexTypeElement->LinkEndChild( pBatchItemSortPoint );
  655. // Create "Render Group" attribute.
  656. TiXmlElement* pBatchItemRenderGroup = new TiXmlElement( "xs:attribute" );
  657. pBatchItemRenderGroup->SetAttribute( "name", spriteRenderGroupName );
  658. pBatchItemRenderGroup->SetAttribute( "type", "xs:string" );
  659. pBatchItemComplexTypeElement->LinkEndChild( pBatchItemRenderGroup );
  660. // Create "Blend Mode" attribute.
  661. TiXmlElement* pBatchItemBlendMode = new TiXmlElement( "xs:attribute" );
  662. pBatchItemBlendMode->SetAttribute( "name", spriteBlendModeName );
  663. pBatchItemBlendMode->SetAttribute( "type", "xs:boolean" );
  664. pBatchItemComplexTypeElement->LinkEndChild( pBatchItemBlendMode );
  665. // Create "Source Blend Factor" attribute.
  666. TiXmlElement* pBatchItemSrcBlendFactor = new TiXmlElement( "xs:attribute" );
  667. pBatchItemSrcBlendFactor->SetAttribute( "name", spriteSrcBlendFactorName );
  668. pBatchItemComplexTypeElement->LinkEndChild( pBatchItemSrcBlendFactor );
  669. TiXmlElement* pBatchItemSrcBlendFactorType = new TiXmlElement( "xs:simpleType" );
  670. pBatchItemSrcBlendFactor->LinkEndChild( pBatchItemSrcBlendFactorType );
  671. TiXmlElement* pBatchItemSrcBlendFactorTypeRestriction = new TiXmlElement( "xs:restriction" );
  672. pBatchItemSrcBlendFactorTypeRestriction->SetAttribute( "base", "xs:string" );
  673. pBatchItemSrcBlendFactorType->LinkEndChild( pBatchItemSrcBlendFactorTypeRestriction );
  674. const S32 srcBlendFactorEnumsCount = srcBlendFactorTable.size;
  675. for( S32 index = 0; index < srcBlendFactorEnumsCount; ++index )
  676. {
  677. // Add enumeration element.
  678. TiXmlElement* pSrcBlendFactorEnumeration = new TiXmlElement( "xs:enumeration" );
  679. pSrcBlendFactorEnumeration->SetAttribute( "value", srcBlendFactorTable.table[index].label );
  680. pBatchItemSrcBlendFactorTypeRestriction->LinkEndChild( pSrcBlendFactorEnumeration );
  681. }
  682. // Create "Destination Blend Factor" attribute.
  683. TiXmlElement* pBatchItemDstBlendFactor = new TiXmlElement( "xs:attribute" );
  684. pBatchItemDstBlendFactor->SetAttribute( "name", spriteDstBlendFactorName );
  685. pBatchItemComplexTypeElement->LinkEndChild( pBatchItemDstBlendFactor );
  686. TiXmlElement* pBatchItemDstBlendFactorType = new TiXmlElement( "xs:simpleType" );
  687. pBatchItemDstBlendFactor->LinkEndChild( pBatchItemDstBlendFactorType );
  688. TiXmlElement* pBatchItemDstBlendFactorTypeRestriction = new TiXmlElement( "xs:restriction" );
  689. pBatchItemDstBlendFactorTypeRestriction->SetAttribute( "base", "xs:string" );
  690. pBatchItemDstBlendFactorType->LinkEndChild( pBatchItemDstBlendFactorTypeRestriction );
  691. const S32 dstBlendFactorEnumsCount = dstBlendFactorTable.size;
  692. for( S32 index = 0; index < dstBlendFactorEnumsCount; ++index )
  693. {
  694. // Add enumeration element.
  695. TiXmlElement* pDstBlendFactorEnumeration = new TiXmlElement( "xs:enumeration" );
  696. pDstBlendFactorEnumeration->SetAttribute( "value", dstBlendFactorTable.table[index].label );
  697. pBatchItemDstBlendFactorTypeRestriction->LinkEndChild( pDstBlendFactorEnumeration );
  698. }
  699. // Create "Blend Color" attribute.
  700. TiXmlElement* pBatchItemBlendColor = new TiXmlElement( "xs:attribute" );
  701. pBatchItemBlendColor->SetAttribute( "name", spriteBlendColorName );
  702. pBatchItemBlendColor->SetAttribute( "type", "Color_Enums" );
  703. pBatchItemComplexTypeElement->LinkEndChild( pBatchItemBlendColor );
  704. // Create "Alpha Test" attribute.
  705. TiXmlElement* pBatchItemAlphaTest = new TiXmlElement( "xs:attribute" );
  706. pBatchItemAlphaTest->SetAttribute( "name", spriteAlphaTestName );
  707. pBatchItemAlphaTest->SetAttribute( "type", "xs:float" );
  708. pBatchItemComplexTypeElement->LinkEndChild( pBatchItemAlphaTest );
  709. // Create "Logical Position" attribute.
  710. TiXmlElement* pBatchItemLogicalPosition = new TiXmlElement( "xs:attribute" );
  711. pBatchItemLogicalPosition->SetAttribute( "name", spriteLogicalPositionName );
  712. pBatchItemLogicalPosition->SetAttribute( "type", "xs:string" );
  713. pBatchItemComplexTypeElement->LinkEndChild( pBatchItemLogicalPosition );
  714. }