behaviorComponent_ScriptBinding.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 _BEHAVIORCOMPONENT_RAISEEVENT_H_
  23. #include "behaviorComponentRaiseEvent.h"
  24. #endif
  25. //-----------------------------------------------------------------------------
  26. /*! Copies a behaviors values to a component
  27. */
  28. ConsoleFunctionWithDocs( copyBehaviorToComponent, ConsoleBool, 3, 3, (behavior, component))
  29. {
  30. // Fetch behavior/component Ids.
  31. const S32 behaviorId = dAtoi( argv[1] );
  32. const S32 componentId = dAtoi( argv[2] );
  33. // Fetch behavior.
  34. BehaviorInstance* pBehavior = dynamic_cast<BehaviorInstance*>( Sim::findObject( behaviorId ) );
  35. // Sanity!
  36. if ( !pBehavior )
  37. {
  38. Con::warnf( "copyBehaviorToComponent() - Could not find behavior '%d'.", behaviorId );
  39. return false;
  40. }
  41. // Fetch component.
  42. SimComponent* pComponent = dynamic_cast<SimComponent*>( Sim::findObject( componentId ) );
  43. // Sanity!
  44. if( !pComponent )
  45. {
  46. Con::errorf( "copyBehaviorToComponent() - cannot find component '%d'.", componentId );
  47. return false;
  48. }
  49. // Fetch template.
  50. BehaviorTemplate* pTemplate = pBehavior->getTemplate();
  51. // Fetch template field count.
  52. const U32 fieldCount = pTemplate->getBehaviorFieldCount();
  53. const char* pFieldValue = NULL;
  54. BehaviorTemplate::BehaviorField* pField = NULL;
  55. // Copy behavior fields.
  56. for( U32 index = 0; index < fieldCount; ++index )
  57. {
  58. // Fetch behavior field.
  59. pField = pTemplate->getBehaviorField( index );
  60. // Fetch field value from behavior (if any).
  61. pFieldValue = pBehavior->getDataField( pField->mName, NULL );
  62. // Set field value on component.
  63. pComponent->setDataField( pField->mName, NULL, pFieldValue );
  64. }
  65. return true;
  66. }
  67. //-----------------------------------------------------------------------------
  68. ConsoleMethodGroupBeginWithDocs(BehaviorComponent, DynamicConsoleMethodComponent)
  69. /*! Add a behavior to the object
  70. @param bi The behavior instance to add
  71. @return (bool success) Whether or not the behavior was successfully added
  72. */
  73. ConsoleMethodWithDocs( BehaviorComponent, addBehavior, ConsoleBool, 3, 3, (BehaviorInstance bi))
  74. {
  75. return object->addBehavior( dynamic_cast<BehaviorInstance *>( Sim::findObject( argv[2] ) ) );
  76. }
  77. //-----------------------------------------------------------------------------
  78. /*!
  79. @param bi The behavior instance to remove
  80. @param deleteBehavior Whether or not to delete the behavior
  81. @return (bool success) Whether the behavior was successfully removed
  82. */
  83. ConsoleMethodWithDocs( BehaviorComponent, removeBehavior, ConsoleBool, 3, 4, (BehaviorInstance bi, [bool deleteBehavior = true]))
  84. {
  85. bool deleteBehavior = true;
  86. if (argc > 3)
  87. deleteBehavior = dAtob(argv[3]);
  88. return object->removeBehavior( dynamic_cast<BehaviorInstance *>( Sim::findObject( argv[2] ) ), deleteBehavior );
  89. }
  90. //-----------------------------------------------------------------------------
  91. /*! Clear all behavior instances
  92. @return No return value
  93. */
  94. ConsoleMethodWithDocs( BehaviorComponent, clearBehaviors, ConsoleVoid, 2, 2, ())
  95. {
  96. object->clearBehaviors();
  97. }
  98. //-----------------------------------------------------------------------------
  99. /*! Get the count of behaviors on an object
  100. @return (int count) The number of behaviors on an object
  101. */
  102. ConsoleMethodWithDocs( BehaviorComponent, getBehaviorCount, ConsoleInt, 2, 2, ())
  103. {
  104. return object->getBehaviorCount();
  105. }
  106. //-----------------------------------------------------------------------------
  107. /*! gets a behavior
  108. @param BehaviorTemplateName The name of the template of the behavior instance you want
  109. @return (BehaviorInstance bi) The behavior instance you requested
  110. */
  111. ConsoleMethodWithDocs( BehaviorComponent, getBehavior, ConsoleInt, 3, 3, (string BehaviorTemplateName))
  112. {
  113. BehaviorInstance* pBehaviorInstance = object->getBehavior( StringTable->insert( argv[2] ) );
  114. return pBehaviorInstance ? pBehaviorInstance->getId() : 0;
  115. }
  116. //-----------------------------------------------------------------------------
  117. /*! Gets a particular behavior
  118. @param index The index of the behavior to get
  119. @return (BehaviorInstance bi) The behavior instance you requested
  120. */
  121. ConsoleMethodWithDocs( BehaviorComponent, getBehaviorByIndex, ConsoleInt, 3, 3, (int index))
  122. {
  123. BehaviorInstance *bInstance = object->getBehavior( dAtoi(argv[2]) );
  124. return ( bInstance != NULL ) ? bInstance->getId() : 0;
  125. }
  126. //-----------------------------------------------------------------------------
  127. /*!
  128. @param inst The behavior instance you want to reorder
  129. @param desiredIndex The index you want the behavior instance to be reordered to
  130. @return (bool success) Whether or not the behavior instance was successfully reordered
  131. */
  132. ConsoleMethodWithDocs( BehaviorComponent, reOrder, ConsoleBool, 3, 3, (BehaviorInstance inst, [int desiredIndex = 0]))
  133. {
  134. BehaviorInstance *inst = dynamic_cast<BehaviorInstance *>( Sim::findObject( argv[1] ) );
  135. if( inst == NULL )
  136. return false;
  137. U32 idx = 0;
  138. if( argc > 2 )
  139. idx = dAtoi( argv[2] );
  140. return object->reOrder( inst, idx );
  141. }
  142. //-----------------------------------------------------------------------------
  143. /*! Connects a behavior output to a behavior input.
  144. @param outputBehavior The behavior that owns the output.
  145. @param inputBehavior The behavior that owns the input.
  146. @param outputName The output name owned by the output behavior.
  147. @param inputName The input name owned by the input behavior.
  148. @return (bool success) Whether the connection was successful or not.
  149. */
  150. ConsoleMethodWithDocs( BehaviorComponent, connect, ConsoleBool, 6, 6, (outputBehavior, inputBehavior, outputName, inputName))
  151. {
  152. // Find output behavior.
  153. BehaviorInstance* pOutputBehavior = dynamic_cast<BehaviorInstance*>( Sim::findObject( argv[2] ) );
  154. // Did we find the behavior?
  155. if ( !pOutputBehavior )
  156. {
  157. // No, so warn.
  158. Con::warnf("BehaviorComponent::connect() - Could not find output behavior '%s'.", argv[2] );
  159. return false;
  160. }
  161. // Find input behavior.
  162. BehaviorInstance* pInputBehavior = dynamic_cast<BehaviorInstance*>( Sim::findObject( argv[3] ) );
  163. // Did we find the behavior?
  164. if ( !pInputBehavior )
  165. {
  166. // No, so warn.
  167. Con::warnf("BehaviorComponent::connect() - Could not find input behavior '%s'.", argv[3] );
  168. return false;
  169. }
  170. // Fetch port names.
  171. StringTableEntry pOutputName = StringTable->insert( argv[4] );
  172. StringTableEntry pInputName = StringTable->insert( argv[5] );
  173. // Perform the connection.
  174. return object->connect( pOutputBehavior, pInputBehavior, pOutputName, pInputName );
  175. }
  176. //-----------------------------------------------------------------------------
  177. /*! Connects a behavior output to a behavior input.
  178. @param outputBehavior The behavior that owns the output.
  179. @param inputBehavior The behavior that owns the input.
  180. @param outputName The output name owned by the output behavior.
  181. @param inputName The input name owned by the input behavior.
  182. @return (bool success) Whether the disconnection was successful or not.
  183. */
  184. ConsoleMethodWithDocs( BehaviorComponent, disconnect, ConsoleBool, 6, 6, (outputBehavior, inputBehavior, outputName, inputName))
  185. {
  186. // Find output behavior.
  187. BehaviorInstance* pOutputBehavior = dynamic_cast<BehaviorInstance*>( Sim::findObject( argv[2] ) );
  188. // Did we find the behavior?
  189. if ( !pOutputBehavior )
  190. {
  191. // No, so warn.
  192. Con::warnf("BehaviorComponent::disconnect() - Could not find output behavior '%s'.", argv[2] );
  193. return false;
  194. }
  195. // Find input behavior.
  196. BehaviorInstance* pInputBehavior = dynamic_cast<BehaviorInstance*>( Sim::findObject( argv[3] ) );
  197. // Did we find the behavior?
  198. if ( !pInputBehavior )
  199. {
  200. // No, so warn.
  201. Con::warnf("BehaviorComponent::disconnect() - Could not find input behavior '%s'.", argv[3] );
  202. return false;
  203. }
  204. // Fetch port names.
  205. StringTableEntry pOutputName = StringTable->insert( argv[4] );
  206. StringTableEntry pInputName = StringTable->insert( argv[5] );
  207. // Perform the disconnection.
  208. return object->disconnect( pOutputBehavior, pInputBehavior, pOutputName, pInputName );
  209. }
  210. //-----------------------------------------------------------------------------
  211. /*! Raise a signal on the behavior output on the specified behavior.
  212. @param outputBehavior The behavior that owns the output.
  213. @param outputName The output name owned by the output behavior.
  214. @param [deltaTime] Optional time-delta (ms) when the raise should occur.
  215. @return (bool success) Whether the signal raise was successful or not.
  216. */
  217. ConsoleMethodWithDocs( BehaviorComponent, raise, ConsoleBool, 4, 5, (outputBehavior, outputName, [deltaTime]))
  218. {
  219. // Find output behavior.
  220. BehaviorInstance* pOutputBehavior = dynamic_cast<BehaviorInstance*>( Sim::findObject( argv[2] ) );
  221. // Did we find the behavior?
  222. if ( !pOutputBehavior )
  223. {
  224. // No, so warn.
  225. Con::warnf("BehaviorComponent::raise() - Could not find output behavior '%s'.", argv[2] );
  226. return false;
  227. }
  228. // Fetch output name.
  229. StringTableEntry pOutputName = StringTable->insert( argv[3] );
  230. // Perform the signal raising immediately if no schedule time specified.
  231. if ( argc < 5 )
  232. return object->raise( pOutputBehavior, pOutputName );
  233. // Fetch time delta.
  234. const U32 timeDelta = U32( dAtoi(argv[4]) );
  235. // Schedule raise event.
  236. BehaviorComponentRaiseEvent* pEvent = new BehaviorComponentRaiseEvent( pOutputBehavior, pOutputName );
  237. Sim::postEvent( object, pEvent, Sim::getCurrentTime() + timeDelta );
  238. return true;
  239. }
  240. //-----------------------------------------------------------------------------
  241. /*! Gets the number of connections on the behavior output on the specified behavior.
  242. @param outputBehavior The behavior that owns the output.
  243. @param outputName The output name owned by the output behavior.
  244. @return The number of connections on the behavior output on the specified behavior.
  245. */
  246. ConsoleMethodWithDocs( BehaviorComponent, getBehaviorConnectionCount, ConsoleInt, 4, 4, (outputBehavior, outputName))
  247. {
  248. // Find output behavior.
  249. BehaviorInstance* pOutputBehavior = dynamic_cast<BehaviorInstance*>( Sim::findObject( argv[2] ) );
  250. // Did we find the behavior?
  251. if ( !pOutputBehavior )
  252. {
  253. // No, so warn.
  254. Con::warnf("BehaviorComponent::getBehaviorConnectionCount() - Could not find output behavior '%s'.", argv[2] );
  255. return false;
  256. }
  257. // Fetch output name.
  258. StringTableEntry pOutputName = StringTable->insert( argv[3] );
  259. // Return the connection count.
  260. return object->getBehaviorConnectionCount( pOutputBehavior, pOutputName );
  261. }
  262. //-----------------------------------------------------------------------------
  263. /*! Gets a comma-delimited list of connections on the behavior output on the specified behavior.
  264. @param outputBehavior The behavior that owns the output.
  265. @param outputName The output name owned by the output behavior.
  266. @param connectionIndex The connection index.
  267. @return Returns a comma-delimited list of connections on the behavior output on the specified behavior of the format <OutputBehavior>,<InputBehavior>,<OutputName>,<InputName>.
  268. */
  269. ConsoleMethodWithDocs( BehaviorComponent, getBehaviorConnection, ConsoleString, 5, 5, (outputBehavior, outputName, connectionIndex))
  270. {
  271. // Find output behavior.
  272. BehaviorInstance* pOutputBehavior = dynamic_cast<BehaviorInstance*>( Sim::findObject( argv[2] ) );
  273. // Did we find the behavior?
  274. if ( !pOutputBehavior )
  275. {
  276. // No, so warn.
  277. Con::warnf("BehaviorComponent::getBehaviorConnections() - Could not find output behavior '%s'.", argv[2] );
  278. return NULL;
  279. }
  280. // Fetch output name.
  281. StringTableEntry pOutputName = StringTable->insert( argv[3] );
  282. // Fetch connection index.
  283. const U32 connectionIndex = dAtoi( argv[4] );
  284. // Fetch connection.
  285. const BehaviorComponent::BehaviorPortConnection* pBehaviorConnection = object->getBehaviorConnection( pOutputBehavior, pOutputName, connectionIndex );
  286. // Finish if there are on connections.
  287. if ( pBehaviorConnection == NULL )
  288. return StringTable->EmptyString;
  289. // Format and return behavior input.
  290. char* pBuffer = Con::getReturnBuffer(1024);
  291. dSprintf(pBuffer, 1024, "%d,%d,%s,%s",
  292. pBehaviorConnection->mOutputInstance->getId(),
  293. pBehaviorConnection->mInputInstance->getId(),
  294. pBehaviorConnection->mOutputName,
  295. pBehaviorConnection->mInputName );
  296. return pBuffer;
  297. }
  298. ConsoleMethodGroupEndWithDocs(BehaviorComponent)