SpriteBatchItem.cc 29 KB

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