SpriteBatchItem.cc 31 KB

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