ParticleAsset_ScriptBinding.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. //-----------------------------------------------------------------------------
  23. /// Particle asset accessors.
  24. //-----------------------------------------------------------------------------
  25. ConsoleMethod(ParticleAsset, setLifeMode, void, 3, 3, "(lifeMode) Sets the life-mode of the particle effect.\n"
  26. "@param lifeMode The life-mode of the particle effect (either INFINITE, CYCLE, KILL or STOP.\n"
  27. "A life-mode of INFINITE causes the particle effect to last forever.\n"
  28. "A life-mode of CYCLE causes the particle effect to restart playing when its specified 'lifetime' has been reached.\n"
  29. "A life-mode of KILL causes the particle effect to be deleted when its specified 'lifetime' has been reached.\n"
  30. "A life-mode of STOP causes the particle effect to stop playing (but not be deleted) when its specified lifetime has been reached.\n"
  31. "@return No return value.")
  32. {
  33. object->setLifeMode( ParticleAsset::getParticleAssetLifeModeEnum( argv[2] ) );
  34. }
  35. //-----------------------------------------------------------------------------
  36. ConsoleMethod( ParticleAsset, getLifeMode, const char*, 2, 2, "() - Gets the life-mode of the particle effect.\n"
  37. "@return The life-mode of the particle effect." )
  38. {
  39. return ParticleAsset::getParticleAssetLifeModeDescription( object->getLifeMode() );
  40. }
  41. //-----------------------------------------------------------------------------
  42. ConsoleMethod(ParticleAsset, setLifetime, void, 3, 3, "(lifeTime) Sets the lifetime of the particle effect.\n"
  43. "@param lifeTime The lifetime of the particle effect. This is used according to the 'lifeMode' setting.\n"
  44. "@return No return value." )
  45. {
  46. object->setLifetime( dAtof(argv[2]) );
  47. }
  48. //-----------------------------------------------------------------------------
  49. ConsoleMethod(ParticleAsset, getLifetime, F32, 2, 2, "() Gets the lifetime of the particle effect.\n"
  50. "@return The lifetime of the particle effect." )
  51. {
  52. return object->getLifetime();
  53. }
  54. //-----------------------------------------------------------------------------
  55. /// Particle asset fields.
  56. //-----------------------------------------------------------------------------
  57. ConsoleMethod(ParticleAsset, getSelectableFieldCount, S32, 2, 2, "() Gets the number of available selectable fields.\n"
  58. "@return The number of available selectable fields." )
  59. {
  60. return object->getParticleFields().getFields().size();
  61. }
  62. //-----------------------------------------------------------------------------
  63. ConsoleMethod(ParticleAsset, getSelectableFieldName, const char*, 3, 3, "(fieldIndex) Gets the selectable field at the specified index.\n"
  64. "@return The selectable field name at the specified index." )
  65. {
  66. // Fetch the field hash.
  67. const ParticleAssetFieldCollection::typeFieldHash& fieldHash = object->getParticleFields().getFields();
  68. // Fetch the index.
  69. S32 fieldIndex = dAtoi( argv[2] );
  70. // Is the field index valid?
  71. if ( fieldIndex >= 0 && fieldIndex < (S32)fieldHash.size() )
  72. {
  73. // Yes, but because the fields are in a hash-table, we'll have to iterate and get O(index).
  74. for( ParticleAssetFieldCollection::typeFieldHash::const_iterator fieldItr = fieldHash.begin(); fieldItr != fieldHash.end(); ++fieldItr, --fieldIndex )
  75. {
  76. // Skip if this is not the field index we're looking for?
  77. if ( fieldIndex != 0 )
  78. continue;
  79. // Found it so return the field name.
  80. return fieldItr->value->getFieldName();
  81. }
  82. }
  83. // Warn.
  84. Con::warnf( "ParticleAsset::getSelectableFieldName() - Index '%d' is out of range.", fieldIndex );
  85. return StringTable->EmptyString;
  86. }
  87. //-----------------------------------------------------------------------------
  88. ConsoleMethod(ParticleAsset, selectField, bool, 3, 3, "(fieldName) Select the specified field by its name.\n"
  89. "@param fieldName The field name to use for the selection. Use an empty name to deselect to stop accidental changes.\n"
  90. "@return Whether the field was successfully selected or not.")
  91. {
  92. return object->getParticleFields().selectField( argv[2] ) != NULL;
  93. }
  94. //-----------------------------------------------------------------------------
  95. ConsoleMethod(ParticleAsset, deselectField, void, 2, 2, "() Deselect any selected field. If no field is selected then nothing happens.\n"
  96. "@return No return value.")
  97. {
  98. object->getParticleFields().deselectField();
  99. }
  100. //-----------------------------------------------------------------------------
  101. ConsoleMethod(ParticleAsset, getSelectedField, const char*, 2, 2, "() Gets the selected field name or nothing if no field is selected.\n"
  102. "@return The selected field name or nothing if no fields is selected.")
  103. {
  104. // Get the selected field.
  105. const ParticleAssetField* pParticleAssetField = object->getParticleFields().getSelectedField();
  106. return pParticleAssetField == NULL ? StringTable->EmptyString : pParticleAssetField->getFieldName();
  107. }
  108. //-----------------------------------------------------------------------------
  109. ConsoleMethod(ParticleAsset, setSingleDataKey, S32, 3, 3, "(value) Sets a single data-key at time-zero with the specified value. All existing keys are cleared.\n"
  110. "@param value The value to set the key to.\n"
  111. "@return Returns the index of the new data-key (always zero) or -1 on failure.")
  112. {
  113. return object->getParticleFields().setSingleDataKey( dAtof(argv[2]) );
  114. }
  115. //-----------------------------------------------------------------------------
  116. ConsoleMethod(ParticleAsset, addDataKey, S32, 4, 4, "(time, value) Add Data-Key to Graph.\n"
  117. "@param time The key time.\n"
  118. "@param value The value at specified time\n"
  119. "@return Returns the index of the new data-key or -1 on failure.")
  120. {
  121. return object->getParticleFields().addDataKey( dAtof(argv[2]), dAtof(argv[3]) );
  122. }
  123. //-----------------------------------------------------------------------------
  124. ConsoleMethod(ParticleAsset, removeDataKey, bool, 3, 3, "(keyIndex) Remove the data-key from the field.\n"
  125. "@param keyIndex The index of the data-key you want to remove.\n"
  126. "@return Whether the operation was successful or not.")
  127. {
  128. return object->getParticleFields().removeDataKey( dAtoi(argv[2]) );
  129. }
  130. //-----------------------------------------------------------------------------
  131. ConsoleMethod(ParticleAsset, clearDataKeys, bool, 2, 2, "() Clears all data-key(s) from the field.\n"
  132. "@return Whether the operation was successful or not.")
  133. {
  134. return object->getParticleFields().clearDataKeys();
  135. }
  136. //-----------------------------------------------------------------------------
  137. ConsoleMethod(ParticleAsset, setDataKeyValue, bool, 4, 4, "(keyIndex, value) Set data-key value for the field.\n"
  138. "@param keyIndex The index of the key to be modified.\n"
  139. "@param value The value to change the key to.\n"
  140. "@return Whether the operation was successful or not.")
  141. {
  142. // Set Data Key.
  143. return object->getParticleFields().setDataKey( dAtoi(argv[2]), dAtof(argv[3]) );
  144. }
  145. //-----------------------------------------------------------------------------
  146. ConsoleMethod(ParticleAsset, getDataKeyCount, S32, 2, 2, "() Gets the data-key count.\n"
  147. "@return The number of data-keys in the currently selected field or -1 if no field is selected.")
  148. {
  149. // Get Data Key Count.
  150. return object->getParticleFields().getDataKeyCount();
  151. }
  152. //-----------------------------------------------------------------------------
  153. ConsoleMethod(ParticleAsset, getDataKey, const char*, 3, 3, "(keyIndex) Gets the data-key at the specified index from the field.\n"
  154. "@param keyIndex The index of the data-key to be retrieved.\n"
  155. "@return The data-key comprising both the time and value or nothing if the key is invalid.")
  156. {
  157. // Fetch the key index.
  158. const S32 keyIndex = dAtoi(argv[2]);
  159. // Fetch the data-key.
  160. const ParticleAssetField::DataKey dataKey = object->getParticleFields().getDataKey( keyIndex );
  161. // Finish if the data-key is bad.
  162. if ( dataKey == ParticleAssetField::BadDataKey )
  163. return StringTable->EmptyString;
  164. // Create Returnable Buffer.
  165. char* pBuffer = Con::getReturnBuffer(32);
  166. // Format Buffer.
  167. dSprintf(pBuffer, 32, "%f %f", dataKey.mTime, dataKey.mValue );
  168. // Return buffer.
  169. return pBuffer;
  170. }
  171. //-----------------------------------------------------------------------------
  172. ConsoleMethod(ParticleAsset, getMinValue, F32, 2, 2, "() Get the minimum value for the field.\n"
  173. "@return The minimum value for the field or always 0.0 if no field is selected." )
  174. {
  175. return object->getParticleFields().getMinValue();
  176. }
  177. //-----------------------------------------------------------------------------
  178. ConsoleMethod(ParticleAsset, getMaxValue, F32, 2, 2, "() Get the maximum value for the field.\n"
  179. "@return The maximum value for the field or always 0.0 if no field is selected." )
  180. {
  181. return object->getParticleFields().getMaxValue();
  182. }
  183. //-----------------------------------------------------------------------------
  184. ConsoleMethod(ParticleAsset, getMinTime, F32, 2, 2, "() Get the minimum time for the field.\n"
  185. "@return The minimum time for the field or always 0.0 if no field is selected." )
  186. {
  187. return object->getParticleFields().getMinTime();
  188. }
  189. //-----------------------------------------------------------------------------
  190. ConsoleMethod(ParticleAsset, getMaxTime, F32, 2, 2, "() Get the maximum time for the field.\n"
  191. "@return The maximum time for the field or always 0.0 if no field is selected." )
  192. {
  193. return object->getParticleFields().getMaxTime();
  194. }
  195. //-----------------------------------------------------------------------------
  196. ConsoleMethod(ParticleAsset, getFieldValue, F32, 3, 3, "(time) Get the fields' value at the specified time.\n"
  197. "@param time The time to sample the field value at.\n"
  198. "@return The fields' value at the specified time or always 0.0 if no field is selected.")
  199. {
  200. return object->getParticleFields().getFieldValue( dAtof(argv[2]) );
  201. }
  202. //-----------------------------------------------------------------------------
  203. ConsoleMethod(ParticleAsset, setRepeatTime, bool, 3, 3, "(repeatTime) Sets the time period to repeat (cycle) the fields' values at.\n"
  204. "@return Whether the operation was successful or not.")
  205. {
  206. return object->getParticleFields().setRepeatTime( dAtof(argv[2]) );
  207. }
  208. //-----------------------------------------------------------------------------
  209. ConsoleMethod(ParticleAsset, getRepeatTime, F32, 2, 2, "() Gets the time period that the fields' value repeat (cycle) at.\n"
  210. "@return The time period that the fields' value repeat (cycle) at.\n" )
  211. {
  212. return object->getParticleFields().getRepeatTime();
  213. }
  214. //-----------------------------------------------------------------------------
  215. ConsoleMethod(ParticleAsset, setValueScale, bool, 3, 3, "(valueScale) Set the scaling of field values retrieved from the field. This does not alter the actual data-key values.\n"
  216. "@param valueScale The scale for field values retrieved from the field.\n"
  217. "@return Whether the operation was successful or not.")
  218. {
  219. return object->getParticleFields().setValueScale( dAtof(argv[2]) );
  220. }
  221. //-----------------------------------------------------------------------------
  222. ConsoleMethod(ParticleAsset, getValueScale, F32, 2, 2, "() Gets the scaling of field values' retrieved from the field.\n"
  223. "@return The scaling of field values' retrieved from the field." )
  224. {
  225. return object->getParticleFields().getValueScale();
  226. }
  227. //-----------------------------------------------------------------------------
  228. /// Emitter asset methods.
  229. //-----------------------------------------------------------------------------
  230. ConsoleMethod(ParticleAsset, createEmitter, const char*, 2, 2, "() - Creates and add a new emitter.\n"
  231. "@return The new emitter that was added or 0 if failed.")
  232. {
  233. // Find the emitter.
  234. ParticleAssetEmitter* pEmitter = object->createEmitter();
  235. return pEmitter == NULL ? StringTable->EmptyString : pEmitter->getIdString();
  236. }
  237. //-----------------------------------------------------------------------------
  238. ConsoleMethod(ParticleAsset, addEmitter, bool, 3, 3, "(emitterId) - Adds an existing emitter.\n"
  239. "@param emitterId The emitter to add.\n"
  240. "@return On success it returns the ID of the emitter, or 0 if failed.")
  241. {
  242. // Find the emitter.
  243. ParticleAssetEmitter* pEmitter = Sim::findObject<ParticleAssetEmitter>( argv[2] );
  244. // Did we find the emitter?
  245. if ( pEmitter == NULL )
  246. {
  247. // No, so warn.
  248. Con::warnf( "ParticleAsset::addEmitter() - Could not find the emitter '%s'.", argv[2] );
  249. return false;
  250. }
  251. return object->addEmitter(pEmitter);
  252. }
  253. //-----------------------------------------------------------------------------
  254. ConsoleMethod(ParticleAsset, removeEmitter, bool, 3, 4, "(emitterId, [bool deleteEmitter]) - Removes an emitter.\n"
  255. "@param emitterId The emitter to remove.\n"
  256. "@return No return value.")
  257. {
  258. // Find the emitter.
  259. ParticleAssetEmitter* pEmitter = Sim::findObject<ParticleAssetEmitter>( argv[2] );
  260. // Did we find the emitter?
  261. if ( pEmitter == NULL )
  262. {
  263. // No, so warn.
  264. Con::warnf( "ParticleAsset::removeEmitter() - Could not find the emitter '%s'.", argv[2] );
  265. return false;
  266. }
  267. bool deleteEmitter = true;
  268. if (argc > 3)
  269. deleteEmitter = dAtob(argv[3]);
  270. // Remove the emitter.
  271. object->removeEmitter( pEmitter, deleteEmitter );
  272. return true;
  273. }
  274. //-----------------------------------------------------------------------------
  275. ConsoleMethod(ParticleAsset, clearEmitters, void, 2, 2, "() Clear all the emitters.\n"
  276. "@return No return Value.")
  277. {
  278. // Clear Emitters.
  279. object->clearEmitters();
  280. }
  281. //-----------------------------------------------------------------------------
  282. ConsoleMethod(ParticleAsset, getEmitterCount, S32, 2, 2, "() Gets the emitter count.\n"
  283. "@return Returns the number of emitters as an integer.")
  284. {
  285. // Get Emitter Count.
  286. return object->getEmitterCount();
  287. }
  288. //-----------------------------------------------------------------------------
  289. ConsoleMethod(ParticleAsset, getEmitter, S32, 3, 3, "(emitterIndex) Gets the emitter at the specified index.\n"
  290. "@param emitterIndex The index for the desired emitter\n"
  291. "@return The emitter or 0 if not found.")
  292. {
  293. // Get the emitter.
  294. ParticleAssetEmitter* pEmitter = object->getEmitter( dAtoi(argv[2]) );
  295. return pEmitter == NULL ? 0 : pEmitter->getId();
  296. }
  297. //-----------------------------------------------------------------------------
  298. ConsoleMethod(ParticleAsset, findEmitter, S32, 3, 3, "(emitterName) Finds the emitter by its name.\n"
  299. "@param emitterName The name of the desired emitter.\n"
  300. "@return The emitter or 0 if not found.")
  301. {
  302. // Find the emitter.
  303. ParticleAssetEmitter* pEmitter = object->findEmitter( argv[2] );
  304. return pEmitter == NULL ? 0 : pEmitter->getId();
  305. }
  306. //-----------------------------------------------------------------------------
  307. ConsoleMethod(ParticleAsset, moveEmitter, void, 4, 4, "(fromEmitterIndex, toEmitterIndex) Moves the emitter order.\n"
  308. "@param fromEmitterIndex The source index of the emitter to move.\n"
  309. "@param toEmitterIndex The destination index to move the emitter to.\n"
  310. "@return No return value.")
  311. {
  312. // Move Emitter Object.
  313. object->moveEmitter( dAtoi(argv[2]), dAtoi(argv[3]) );
  314. }