behaviorTemplate_ScriptBinding.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. ConsoleMethodGroupBeginWithDocs(BehaviorTemplate, SimObject)
  23. /*! Create an instance of this behavior.
  24. @return (BehaviorInstance inst) The behavior instance created
  25. */
  26. ConsoleMethodWithDocs(BehaviorTemplate, createInstance, ConsoleInt, 2, 2, ())
  27. {
  28. BehaviorInstance *inst = object->createInstance();
  29. return inst ? inst->getId() : 0;
  30. }
  31. //-----------------------------------------------------------------------------
  32. /*!
  33. Adds a named BehaviorField to a Behavior Template
  34. @param fieldName The name of this field
  35. @param desc The Description of this field
  36. @param type The DataType for this field (default, int, float, Point2F, bool, enum, Object, keybind, color)
  37. @param defaultValue The Default value for this field
  38. @param userData An extra data field that can be used for custom data on a per-field basis<br>Usage for default types<br>
  39. -enum: a TAB separated list of possible values<br>
  40. -object: the scene-object type that are valid choices for the field. The object types observe inheritance, so if you have a SceneObject field you will be able to choose t2dStaticSrpites, t2dAnimatedSprites, etc.
  41. @return Whether the field was added or not.
  42. */
  43. ConsoleMethodWithDocs(BehaviorTemplate, addBehaviorField, ConsoleBool, 5, 7, (fieldName, desc, type, [defaultValue, userData]))
  44. {
  45. const char *defValue = argc > 5 ? argv[5] : NULL;
  46. const char *typeInfo = argc > 6 ? argv[6] : NULL;
  47. return object->addBehaviorField(argv[2], argv[3], argv[4], defValue, typeInfo);
  48. }
  49. //-----------------------------------------------------------------------------
  50. /*! Get the number of behavior fields.
  51. @return Returns the number of behavior fields.
  52. */
  53. ConsoleMethodWithDocs(BehaviorTemplate, getBehaviorFieldCount, ConsoleInt, 2, 2, ())
  54. {
  55. return object->getBehaviorFieldCount();
  56. }
  57. //-----------------------------------------------------------------------------
  58. /*! Gets a tab-delimited description of the behavior field.
  59. @param fieldIndex The index of the behavior field.
  60. @return A tab-delimited description of the behavior field of the format <Name><Type><DefaultValue> or an empty string if behavior field is not found.
  61. */
  62. ConsoleMethodWithDocs(BehaviorTemplate, getBehaviorField, ConsoleString, 3, 3, (int fieldIndex))
  63. {
  64. // Fetch behavior field.
  65. BehaviorTemplate::BehaviorField* pField = object->getBehaviorField( dAtoi(argv[2]) );
  66. // Was the field found?
  67. if( !pField )
  68. {
  69. // No, so warn.
  70. Con::warnf("getBehaviorField() - Could not find the behavior field '%s' on behavior '%s'", argv[2], object->getFriendlyName() );
  71. return StringTable->EmptyString;
  72. }
  73. // Format and return behavior field.
  74. char* pBuffer = Con::getReturnBuffer(1024);
  75. dSprintf(pBuffer, 1024, "%s\t%s\t%s", pField->mName, pField->mType, pField->mDefaultValue);
  76. return pBuffer;
  77. }
  78. //-----------------------------------------------------------------------------
  79. /*! Gets the UserData associated with a field by index in the field list
  80. @param fieldIndex The index of the behavior field.
  81. @return Returns a string representing the user data of this field
  82. */
  83. ConsoleMethodWithDocs(BehaviorTemplate, getBehaviorFieldUserData, ConsoleString, 3, 3, (int fieldIndex))
  84. {
  85. // Fetch behavior field.
  86. BehaviorTemplate::BehaviorField* pField = object->getBehaviorField( dAtoi(argv[2]) );
  87. // Was the field found?
  88. if( !pField )
  89. {
  90. // No, so warn.
  91. Con::warnf("getBehaviorFieldUserData() - Could not find the behavior field '%s' on behavior '%s'", argv[2], object->getFriendlyName() );
  92. return StringTable->EmptyString;
  93. }
  94. return pField->mUserData;
  95. }
  96. //-----------------------------------------------------------------------------
  97. /*! Gets the description of the field.
  98. @param fieldIndex The index of the behavior field.
  99. @return Returns the field description.
  100. */
  101. ConsoleMethodWithDocs(BehaviorTemplate, getBehaviorFieldDescription, ConsoleString, 3, 3, (int fieldIndex))
  102. {
  103. // Fetch behavior field.
  104. BehaviorTemplate::BehaviorField* pField = object->getBehaviorField( dAtoi(argv[2]) );
  105. // Was the field found?
  106. if( !pField )
  107. {
  108. // No, so warn.
  109. Con::warnf("getBehaviorFieldDescription() - Could not find the behavior field '%s' on behavior '%s'", argv[2], object->getFriendlyName() );
  110. return StringTable->EmptyString;
  111. }
  112. return pField->mDescription ? pField->mDescription : StringTable->EmptyString;
  113. }
  114. //-----------------------------------------------------------------------------
  115. /*! Adds a behavior output.
  116. @param outputName The output name to use.
  117. @param label The short label name to show in the editor.
  118. @param description The long description to show in the editor.
  119. @return Whether the output was added or not.
  120. */
  121. ConsoleMethodWithDocs(BehaviorTemplate, addBehaviorOutput, ConsoleBool, 5, 5, (outputName, label, description))
  122. {
  123. // Add behavior output.
  124. return object->addBehaviorOutput( argv[2], argv[3], argv[4] );
  125. }
  126. //-----------------------------------------------------------------------------
  127. /*! Get the number of behavior outputs.
  128. @return Returns the number of behavior outputs.
  129. */
  130. ConsoleMethodWithDocs(BehaviorTemplate, getBehaviorOutputCount, ConsoleInt, 2, 2, ())
  131. {
  132. return object->getBehaviorOutputCount();
  133. }
  134. //-----------------------------------------------------------------------------
  135. /*! Gets a comma-delimited description of the behavior output.
  136. @param outputIndex The index of the behavior output.
  137. @return A comma-delimited description of the behavior output of the format <Name>,<Label><,Description> or an empty string if behavior output is not found.
  138. */
  139. ConsoleMethodWithDocs(BehaviorTemplate, getBehaviorOutput, ConsoleString, 3, 3, (int outputIndex))
  140. {
  141. // Fetch behavior output.
  142. BehaviorTemplate::BehaviorPortOutput* pPortOutput = object->getBehaviourOutput( dAtoi(argv[2]) );
  143. // Was the output found?
  144. if( !pPortOutput )
  145. {
  146. // No, so warn.
  147. Con::warnf("getBehaviorOutput() - Could not find the behavior output '%s' on behavior '%s'", argv[2], object->getFriendlyName() );
  148. return StringTable->EmptyString;
  149. }
  150. // Format and return behavior output.
  151. char* pBuffer = Con::getReturnBuffer(1024);
  152. dSprintf(pBuffer, 1024, "%s,%s,%s", pPortOutput->mName, pPortOutput->mLabel, pPortOutput->mDescription );
  153. return pBuffer;
  154. }
  155. //-----------------------------------------------------------------------------
  156. /*! Gets whether the behavior has the behavior output or not.
  157. @param outputName The output name to check.
  158. @return Whether the behavior has the behavior output or not.
  159. */
  160. ConsoleMethodWithDocs(BehaviorTemplate, hasBehaviorOutput, ConsoleBool, 3, 3, (outputName))
  161. {
  162. return object->hasBehaviorOutput( argv[2] );
  163. }
  164. //-----------------------------------------------------------------------------
  165. /*! Adds a behavior input.
  166. @param inputName The input name to use.
  167. @param label The short label name to show in the editor.
  168. @param description The long description to show in the editor.
  169. @return Whether the input was added or not.
  170. */
  171. ConsoleMethodWithDocs(BehaviorTemplate, addBehaviorInput, ConsoleBool, 5, 5, (inputName, label, description))
  172. {
  173. // Add behavior input.
  174. return object->addBehaviorInput( argv[2], argv[3], argv[4] );
  175. }
  176. //-----------------------------------------------------------------------------
  177. /*! Get the number of behavior inputs.
  178. @return Returns the number of behavior inputs.
  179. */
  180. ConsoleMethodWithDocs(BehaviorTemplate, getBehaviorInputCount, ConsoleInt, 2, 2, ())
  181. {
  182. return object->getBehaviorInputCount();
  183. }
  184. //-----------------------------------------------------------------------------
  185. /*! Gets a comma-delimited description of the behavior input.
  186. @param inputIndex The index of the behavior input.
  187. @return A comma-delimited description of the behavior input of the format <Name>,<Label><,Description> or an empty string if behavior input is not found.
  188. */
  189. ConsoleMethodWithDocs(BehaviorTemplate, getBehaviorInput, ConsoleString, 3, 3, (int inputIndex))
  190. {
  191. // Fetch behavior input.
  192. BehaviorTemplate::BehaviorPortInput* pPortInput = object->getBehaviourInput( dAtoi(argv[2]) );
  193. // Was the input found?
  194. if( !pPortInput )
  195. {
  196. // No, so warn.
  197. Con::warnf("getBehaviorOutput() - Could not find the behavior input '%s' on behavior '%s'", argv[2], object->getFriendlyName() );
  198. return StringTable->EmptyString;
  199. }
  200. // Format and return behavior input.
  201. char* pBuffer = Con::getReturnBuffer(1024);
  202. dSprintf(pBuffer, 1024, "%s,%s,%s", pPortInput->mName, pPortInput->mLabel, pPortInput->mDescription );
  203. return pBuffer;
  204. }
  205. //-----------------------------------------------------------------------------
  206. /*! Gets whether the behavior has the behavior input or not.
  207. @param inputName The input name to check.
  208. @return Whether the behavior has the behavior input or not.
  209. */
  210. ConsoleMethodWithDocs(BehaviorTemplate, hasBehaviorInput, ConsoleBool, 3, 3, (inputName))
  211. {
  212. return object->hasBehaviorInput( argv[2] );
  213. }
  214. ConsoleMethodGroupEndWithDocs(BehaviorTemplate)