ParticleAssetFieldCollection.cc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. #include "2d/assets/ParticleAssetFieldCollection.h"
  23. //-----------------------------------------------------------------------------
  24. // Set custom property name.
  25. static StringTableEntry particleAssetFieldNodeName = StringTable->insert("Fields");
  26. //-----------------------------------------------------------------------------
  27. ParticleAssetFieldCollection::ParticleAssetFieldCollection() :
  28. mpSelectedField( NULL )
  29. {
  30. }
  31. //-----------------------------------------------------------------------------
  32. ParticleAssetFieldCollection::~ParticleAssetFieldCollection()
  33. {
  34. }
  35. //------------------------------------------------------------------------------
  36. void ParticleAssetFieldCollection::copyTo( ParticleAssetFieldCollection& fieldCollection )
  37. {
  38. // Iterate the fields.
  39. for( typeFieldHash::iterator fieldItr = mFields.begin(); fieldItr != mFields.end(); ++fieldItr )
  40. {
  41. // Fetch field.
  42. ParticleAssetField* pParticleAssetField = fieldItr->value;
  43. // Find target field.
  44. ParticleAssetField* pTargetParticleAssetField = fieldCollection.findField( fieldItr->key );
  45. // Sanity!
  46. AssertFatal( pTargetParticleAssetField != NULL, "ParticleAssetFieldCollection::copyTo() - Could not find target particle asset field." );
  47. // Copy field.
  48. pParticleAssetField->copyTo( *pTargetParticleAssetField );
  49. }
  50. // Select the same field name.
  51. if ( getSelectedField() != NULL )
  52. fieldCollection.selectField( getSelectedField()->getFieldName() );
  53. }
  54. //------------------------------------------------------------------------------
  55. void ParticleAssetFieldCollection::addField( ParticleAssetField& particleAssetField, const char* pFieldName, F32 maxTime, F32 minValue, F32 maxValue, F32 defaultValue )
  56. {
  57. // Sanity!
  58. AssertFatal( pFieldName != NULL, "ParticleAssetFieldCollection::addField() - Field name cannot be NULL or empty." );
  59. // Set the field name.
  60. particleAssetField.setFieldName( pFieldName );
  61. // Sanity!
  62. AssertFatal( !mFields.contains( particleAssetField.getFieldName() ), "ParticleAssetFieldCollection::addField() - The particle field name already exists." );
  63. // Add to fields.
  64. mFields.insert( particleAssetField.getFieldName(), &particleAssetField );
  65. // Initialize the field.
  66. particleAssetField.initialize( maxTime, minValue, maxValue, defaultValue );
  67. }
  68. //-----------------------------------------------------------------------------
  69. ParticleAssetField* ParticleAssetFieldCollection::selectField( const char* pFieldName )
  70. {
  71. // Sanity!
  72. AssertFatal( pFieldName != NULL, "ParticleAssetFieldCollection::selectField() - Field name cannot be NULL or empty." );
  73. // Has a field-name been specified?
  74. if ( dStrlen(pFieldName) > 0 )
  75. {
  76. // Yes, so find the field.
  77. mpSelectedField = findField( pFieldName );
  78. // Did we find the field?
  79. if ( mpSelectedField == NULL )
  80. {
  81. // No, so warn.
  82. Con::warnf( "ParticleAssetFieldCollection::selectField() - Could not find the field name '%s'.", pFieldName );
  83. }
  84. }
  85. else
  86. {
  87. // No, so reset the field.
  88. mpSelectedField = NULL;
  89. }
  90. return mpSelectedField;
  91. }
  92. //-----------------------------------------------------------------------------
  93. ParticleAssetField* ParticleAssetFieldCollection::findField( const char* pFieldName )
  94. {
  95. // Sanity!
  96. AssertFatal( pFieldName != NULL, "ParticleAssetFieldCollection::findField() - Cannot find NULL field." );
  97. // Find the field.
  98. typeFieldHash::iterator fieldItr = mFields.find( StringTable->insert( pFieldName ) );
  99. // Return the field if it was found.
  100. return fieldItr == mFields.end() ? NULL : fieldItr->value;
  101. }
  102. //-----------------------------------------------------------------------------
  103. S32 ParticleAssetFieldCollection::setSingleDataKey( F32 value )
  104. {
  105. // Have we got a valid field selected?
  106. if ( !mpSelectedField )
  107. {
  108. // No, so warn.
  109. Con::warnf( "ParticleAssetFieldCollection::setSingleKey() - No field selected." );
  110. return -1;
  111. }
  112. // Set single data key.
  113. return mpSelectedField->setSingleDataKey( value );
  114. }
  115. //-----------------------------------------------------------------------------
  116. S32 ParticleAssetFieldCollection::addDataKey( F32 time, F32 value )
  117. {
  118. // Have we got a valid field selected?
  119. if ( !mpSelectedField )
  120. {
  121. // No, so warn.
  122. Con::warnf( "ParticleAssetFieldCollection::addDataKey() - No field selected." );
  123. return -1;
  124. }
  125. // Add Data Key.
  126. return mpSelectedField->addDataKey( time, value );
  127. }
  128. //-----------------------------------------------------------------------------
  129. bool ParticleAssetFieldCollection::removeDataKey( S32 index )
  130. {
  131. // Have we got a valid field selected?
  132. if ( !mpSelectedField )
  133. {
  134. // No, so warn.
  135. Con::warnf( "ParticleAssetFieldCollection::removeDataKey() - No field selected." );
  136. return false;
  137. }
  138. // Remove Data Key.
  139. return mpSelectedField->removeDataKey( index );
  140. }
  141. //-----------------------------------------------------------------------------
  142. bool ParticleAssetFieldCollection::clearDataKeys( void )
  143. {
  144. // Have we got a valid field selected?
  145. if ( !mpSelectedField )
  146. {
  147. // No, so warn.
  148. Con::warnf( "ParticleAssetFieldCollection::clearDataKeys() - No field selected." );
  149. return false;
  150. }
  151. // Clear Data Keys
  152. mpSelectedField->clearDataKeys();
  153. // Return Okay.
  154. return true;
  155. }
  156. //-----------------------------------------------------------------------------
  157. bool ParticleAssetFieldCollection::setDataKey( S32 index, F32 value )
  158. {
  159. // Have we got a valid field selected?
  160. if ( !mpSelectedField )
  161. {
  162. // No, so warn.
  163. Con::warnf( "ParticleAssetFieldCollection::setDataKey() - No field selected." );
  164. return false;
  165. }
  166. // Set the data-key value.
  167. return mpSelectedField->setDataKeyValue( index, value );
  168. }
  169. //-----------------------------------------------------------------------------
  170. F32 ParticleAssetFieldCollection::getDataKeyValue( S32 index ) const
  171. {
  172. // Have we got a valid field selected?
  173. if ( !mpSelectedField )
  174. {
  175. // No, so warn.
  176. Con::warnf( "ParticleAssetFieldCollection::getDataKeyValue() - No field selected." );
  177. return 0.0f;
  178. }
  179. // Get the data-key value.
  180. return mpSelectedField->getDataKeyValue( index );
  181. }
  182. //-----------------------------------------------------------------------------
  183. F32 ParticleAssetFieldCollection::getDataKeyTime( S32 index ) const
  184. {
  185. // Have we got a valid field selected?
  186. if ( !mpSelectedField )
  187. {
  188. // No, so warn.
  189. Con::warnf( "ParticleAssetFieldCollection::getDataKeyTime() - No field selected." );
  190. return 0.0f;
  191. }
  192. // Get the data-key time.
  193. return mpSelectedField->getDataKeyTime( index );
  194. }
  195. //-----------------------------------------------------------------------------
  196. const ParticleAssetField::DataKey& ParticleAssetFieldCollection::getDataKey( const U32 index ) const
  197. {
  198. // Have we got a valid field selected?
  199. if ( !mpSelectedField )
  200. {
  201. // No, so warn.
  202. Con::warnf( "ParticleAssetFieldCollection::getDataKey() - No field selected." );
  203. return ParticleAssetField::BadDataKey;
  204. }
  205. // Get the data-key.
  206. return mpSelectedField->getDataKey( index );
  207. }
  208. //-----------------------------------------------------------------------------
  209. U32 ParticleAssetFieldCollection::getDataKeyCount( void ) const
  210. {
  211. // Have we got a valid field selected?
  212. if ( !mpSelectedField )
  213. {
  214. // No, so warn.
  215. Con::warnf( "ParticleAssetFieldCollection::getDataKeyCount() - No field selected." );
  216. return -1;
  217. }
  218. // Get the data-key count.
  219. return mpSelectedField->getDataKeyCount();
  220. }
  221. //-----------------------------------------------------------------------------
  222. F32 ParticleAssetFieldCollection::getMinValue( void ) const
  223. {
  224. // Have we got a valid field selected?
  225. if ( !mpSelectedField )
  226. {
  227. // No, so warn.
  228. Con::warnf( "ParticleAssetFieldCollection::getMinValue() - No field selected." );
  229. return 0.0f;
  230. }
  231. // Get Min Value.
  232. return mpSelectedField->getMinValue();
  233. }
  234. //-----------------------------------------------------------------------------
  235. F32 ParticleAssetFieldCollection::getMaxValue( void ) const
  236. {
  237. // Have we got a valid field selected?
  238. if ( !mpSelectedField )
  239. {
  240. // No, so warn.
  241. Con::warnf( "ParticleAssetFieldCollection::getMaxValue() - No field selected." );
  242. return 0.0f;
  243. }
  244. // Get Max Value.
  245. return mpSelectedField->getMaxValue();
  246. }
  247. //-----------------------------------------------------------------------------
  248. F32 ParticleAssetFieldCollection::getMinTime( void ) const
  249. {
  250. // Have we got a valid field selected?
  251. if ( !mpSelectedField )
  252. {
  253. // No, so warn.
  254. Con::warnf( "ParticleAssetFieldCollection::getMinTime() - No field selected." );
  255. return 0.0f;
  256. }
  257. // Get Min Time.
  258. return mpSelectedField->getMinTime();
  259. }
  260. //-----------------------------------------------------------------------------
  261. F32 ParticleAssetFieldCollection::getMaxTime( void ) const
  262. {
  263. // Have we got a valid field selected?
  264. if ( !mpSelectedField )
  265. {
  266. // No, so warn.
  267. Con::warnf( "ParticleAssetFieldCollection::getMaxTime() - No field selected." );
  268. return 0.0f;
  269. }
  270. // Get Max Time.
  271. return mpSelectedField->getMaxTime();
  272. }
  273. //-----------------------------------------------------------------------------
  274. F32 ParticleAssetFieldCollection::getFieldValue( F32 time ) const
  275. {
  276. // Have we got a valid field selected?
  277. if ( !mpSelectedField )
  278. {
  279. // No, so warn.
  280. Con::warnf( "ParticleAssetFieldCollection::getFieldValue() - No field selected." );
  281. return 0.0f;
  282. }
  283. // Get Graph Value.
  284. return mpSelectedField->getFieldValue( time );
  285. }
  286. //-----------------------------------------------------------------------------
  287. bool ParticleAssetFieldCollection::setRepeatTime( const F32 repeatTime )
  288. {
  289. // Have we got a valid field selected?
  290. if ( !mpSelectedField )
  291. {
  292. // No, so warn.
  293. Con::warnf( "ParticleAssetFieldCollection::setRepeatTime() - No field selected." );
  294. return false;
  295. }
  296. // Set repeat time.
  297. return mpSelectedField->setRepeatTime( repeatTime );
  298. }
  299. //-----------------------------------------------------------------------------
  300. F32 ParticleAssetFieldCollection::getRepeatTime( void ) const
  301. {
  302. // Have we got a valid field selected?
  303. if ( !mpSelectedField )
  304. {
  305. // No, so warn.
  306. Con::warnf( "ParticleAssetFieldCollection::getRepeatTime() - No field selected." );
  307. return 0.0f;
  308. }
  309. // Get repeat time.
  310. return mpSelectedField->getRepeatTime();
  311. }
  312. //-----------------------------------------------------------------------------
  313. bool ParticleAssetFieldCollection::setValueScale( const F32 valueScale )
  314. {
  315. // Have we got a valid field selected?
  316. if ( !mpSelectedField )
  317. {
  318. // No, so warn.
  319. Con::warnf( "ParticleAssetFieldCollection::setValueScale() - No field selected." );
  320. return false;
  321. }
  322. // Set Value Scale.
  323. return mpSelectedField->setValueScale( valueScale );
  324. }
  325. //-----------------------------------------------------------------------------
  326. F32 ParticleAssetFieldCollection::getValueScale( void ) const
  327. {
  328. // Have we got a valid field selected?
  329. if ( !mpSelectedField )
  330. {
  331. // No, so warn.
  332. Con::warnf( "ParticleAssetFieldCollection::getValueScale() - No field selected." );
  333. return 0.0f;
  334. }
  335. // Get Value Scale.
  336. return mpSelectedField->getValueScale();
  337. }
  338. //------------------------------------------------------------------------------
  339. void ParticleAssetFieldCollection::onTamlCustomWrite( TamlCustomNodes& customNodes )
  340. {
  341. // Debug Profiling.
  342. PROFILE_SCOPE(ParticleAssetFieldCollection_OnTamlCustomWrite);
  343. // Finish if there no fields.
  344. if ( mFields.size() == 0 )
  345. return;
  346. // Add particle asset custom node.
  347. TamlCustomNode* pParticleAssetCustomNode = customNodes.addNode( particleAssetFieldNodeName );
  348. // Iterate the fields.
  349. for( typeFieldHash::iterator fieldItr = mFields.begin(); fieldItr != mFields.end(); ++fieldItr )
  350. {
  351. fieldItr->value->onTamlCustomWrite( pParticleAssetCustomNode );
  352. }
  353. }
  354. //-----------------------------------------------------------------------------
  355. void ParticleAssetFieldCollection::onTamlCustomRead( const TamlCustomNodes& customNodes )
  356. {
  357. // Debug Profiling.
  358. PROFILE_SCOPE(ParticleAssetFieldCollection_OnTamlCustomRead);
  359. // Find the particle asset custom node.
  360. const TamlCustomNode* pParticleAssetCustomNode = customNodes.findNode( particleAssetFieldNodeName );
  361. // Finish if we don't have a custom node.
  362. if ( pParticleAssetCustomNode == NULL )
  363. return;
  364. // Fetch children.
  365. const TamlCustomNodeVector& children = pParticleAssetCustomNode->getChildren();
  366. // Iterate the custom properties.
  367. for( TamlCustomNodeVector::const_iterator childNodeItr = children.begin(); childNodeItr != children.end(); ++childNodeItr )
  368. {
  369. // Fetch child node.
  370. TamlCustomNode* pChildNode = *childNodeItr;
  371. // Fetch node name.
  372. StringTableEntry nodeName = pChildNode->getNodeName();
  373. // Find the field.
  374. ParticleAssetField* pParticleAssetField = findField( nodeName );
  375. // Did we find the field?
  376. if ( pParticleAssetField == NULL )
  377. {
  378. // No, so warn.
  379. Con::warnf( "ParticleAssetFieldCollection::onTamlCustomRead() - Cannot find data field '%s'.", nodeName );
  380. continue;
  381. }
  382. // Read the alias.
  383. pParticleAssetField->onTamlCustomRead( pChildNode );
  384. }
  385. }
  386. //-----------------------------------------------------------------------------
  387. void ParticleAssetFieldCollection::WriteCustomTamlSchema( const AbstractClassRep* pClassRep, TiXmlElement* pParentElement ) const
  388. {
  389. // Sanity!
  390. AssertFatal( pClassRep != NULL, "ParticleAssetFieldCollection::WriteCustomTamlSchema() - ClassRep cannot be NULL." );
  391. AssertFatal( pParentElement != NULL, "ParticleAssetFieldCollection::WriteCustomTamlSchema() - Parent Element cannot be NULL." );
  392. char buffer[1024];
  393. // Create Fields node element.
  394. TiXmlElement* pFieldsNodeElement = new TiXmlElement( "xs:element" );
  395. dSprintf( buffer, sizeof(buffer), "%s.%s", pClassRep->getClassName(), particleAssetFieldNodeName );
  396. pFieldsNodeElement->SetAttribute( "name", buffer );
  397. pFieldsNodeElement->SetAttribute( "minOccurs", 0 );
  398. pFieldsNodeElement->SetAttribute( "maxOccurs", 1 );
  399. pParentElement->LinkEndChild( pFieldsNodeElement );
  400. // Create complex type.
  401. TiXmlElement* pFieldsNodeComplexTypeElement = new TiXmlElement( "xs:complexType" );
  402. pFieldsNodeElement->LinkEndChild( pFieldsNodeComplexTypeElement );
  403. // Create choice element.
  404. TiXmlElement* pFieldsNodeChoiceElement = new TiXmlElement( "xs:choice" );
  405. pFieldsNodeChoiceElement->SetAttribute( "minOccurs", 0 );
  406. pFieldsNodeChoiceElement->SetAttribute( "maxOccurs", "unbounded" );
  407. pFieldsNodeComplexTypeElement->LinkEndChild( pFieldsNodeChoiceElement );
  408. // Iterate the fields.
  409. for( typeFieldHash::const_iterator fieldItr = mFields.begin(); fieldItr != mFields.end(); ++fieldItr )
  410. {
  411. // Write schema for the field.
  412. fieldItr->value->WriteCustomTamlSchema( pClassRep, pFieldsNodeChoiceElement );
  413. }
  414. }