SpriteBatchItem.cc 30 KB

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