behaviorTemplate_ScriptBinding.h 12 KB

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