behaviorComponent_ScriptBinding.h 16 KB

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