animationComponent_ScriptBinding.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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. #include "console/engineAPI.h"
  23. #include "T3D/components/animation/animationcomponent.h"
  24. DefineEngineMethod(AnimationComponent, playThread, bool, (S32 slot, const char* name, bool transition, F32 transitionTime), (-1, "", true, 0.5),
  25. "@brief Start a new animation thread, or restart one that has been paused or "
  26. "stopped.\n\n"
  27. "@param slot thread slot to play. Valid range is 0 - 3)\n" // 3 = AnimationComponent::MaxScriptThreads-1
  28. "@param name name of the animation sequence to play in this slot. If not "
  29. "specified, the paused or stopped thread in this slot will be resumed.\n"
  30. "@return true if successful, false if failed\n\n"
  31. "@tsexample\n"
  32. "%obj.playThread( 0, \"ambient\" ); // Play the ambient sequence in slot 0\n"
  33. "%obj.setThreadTimeScale( 0, 0.5 ); // Play at half-speed\n"
  34. "%obj.pauseThread( 0 ); // Pause the sequence\n"
  35. "%obj.playThread( 0 ); // Resume playback\n"
  36. "%obj.playThread( 0, \"spin\" ); // Replace the sequence in slot 0\n"
  37. "@endtsexample\n"
  38. "@see pauseThread()\n"
  39. "@see stopThread()\n"
  40. "@see setThreadDir()\n"
  41. "@see setThreadTimeScale()\n"
  42. "@see destroyThread()\n")
  43. {
  44. return object->playThread(slot, name, transition, transitionTime);
  45. }
  46. DefineEngineMethod(AnimationComponent, setThreadDir, bool, (S32 slot, bool fwd), ,
  47. "@brief Set the playback direction of an animation thread.\n\n"
  48. "@param slot thread slot to modify\n"
  49. "@param fwd true to play the animation forwards, false to play backwards\n"
  50. "@return true if successful, false if failed\n\n"
  51. "@see playThread()\n")
  52. {
  53. if (slot >= 0 && slot < AnimationComponent::MaxScriptThreads)
  54. {
  55. if (object->setThreadDir(slot, fwd))
  56. return true;
  57. }
  58. return false;
  59. }
  60. DefineEngineMethod(AnimationComponent, setThreadTimeScale, bool, (S32 slot, F32 scale), ,
  61. "@brief Set the playback time scale of an animation thread.\n\n"
  62. "@param slot thread slot to modify\n"
  63. "@param scale new thread time scale (1=normal speed, 0.5=half speed etc)\n"
  64. "@return true if successful, false if failed\n\n"
  65. "@see playThread\n")
  66. {
  67. if (slot >= 0 && slot < AnimationComponent::MaxScriptThreads)
  68. {
  69. if (object->setThreadTimeScale(slot, scale))
  70. return true;
  71. }
  72. return false;
  73. }
  74. DefineEngineMethod(AnimationComponent, setThreadPosition, bool, (S32 slot, F32 pos), ,
  75. "@brief Set the position within an animation thread.\n\n"
  76. "@param slot thread slot to modify\n"
  77. "@param pos position within thread\n"
  78. "@return true if successful, false if failed\n\n"
  79. "@see playThread\n")
  80. {
  81. if (slot >= 0 && slot < AnimationComponent::MaxScriptThreads)
  82. {
  83. if (object->setThreadPosition(slot, pos))
  84. return true;
  85. }
  86. return false;
  87. }
  88. DefineEngineMethod(AnimationComponent, setThreadAnimation, bool, (S32 slot, const char* name), (""),
  89. "@brief Force-sets the animation in a particular thread without starting it playing."
  90. "@param slot thread slot to play. Valid range is 0 - 3)\n" // 3 = AnimationComponent::MaxScriptThreads-1
  91. "@param name name of the animation sequence to play in this slot. If not "
  92. "specified, the paused or stopped thread in this slot will be resumed.\n"
  93. "@return true if successful, false if failed\n\n")
  94. {
  95. return object->setThreadAnimation(slot, name);
  96. }
  97. DefineEngineMethod(AnimationComponent, getThreadAnimation, String, (S32 slot), ,
  98. "@brief Force-sets the animation in a particular thread without starting it playing."
  99. "@param slot thread slot to play. Valid range is 0 - 3)\n" // 3 = AnimationComponent::MaxScriptThreads-1
  100. "@param name name of the animation sequence to play in this slot. If not "
  101. "specified, the paused or stopped thread in this slot will be resumed.\n"
  102. "@return true if successful, false if failed\n\n")
  103. {
  104. if (slot >= 0 && slot < AnimationComponent::MaxScriptThreads)
  105. {
  106. if (TSShape* shape = object->getShape())
  107. {
  108. S32 seq = object->getThreadSequenceID(slot);
  109. if (seq != -1)
  110. {
  111. String animationName = object->getAnimationName(seq);
  112. return animationName;
  113. }
  114. }
  115. }
  116. return "";
  117. }
  118. DefineEngineMethod(AnimationComponent, stopThread, bool, (S32 slot), ,
  119. "@brief Stop an animation thread.\n\n"
  120. "If restarted using playThread, the animation "
  121. "will start from the beginning again.\n"
  122. "@param slot thread slot to stop\n"
  123. "@return true if successful, false if failed\n\n"
  124. "@see playThread\n")
  125. {
  126. if (slot >= 0 && slot < AnimationComponent::MaxScriptThreads)
  127. {
  128. if (object->stopThread(slot))
  129. return true;
  130. }
  131. return false;
  132. }
  133. DefineEngineMethod(AnimationComponent, destroyThread, bool, (S32 slot), ,
  134. "@brief Destroy an animation thread, which prevents it from playing.\n\n"
  135. "@param slot thread slot to destroy\n"
  136. "@return true if successful, false if failed\n\n"
  137. "@see playThread\n")
  138. {
  139. if (slot >= 0 && slot < AnimationComponent::MaxScriptThreads)
  140. {
  141. if (object->destroyThread(slot))
  142. return true;
  143. }
  144. return false;
  145. }
  146. DefineEngineMethod(AnimationComponent, pauseThread, bool, (S32 slot), ,
  147. "@brief Pause an animation thread.\n\n"
  148. "If restarted using playThread, the animation "
  149. "will resume from the paused position.\n"
  150. "@param slot thread slot to stop\n"
  151. "@return true if successful, false if failed\n\n"
  152. "@see playThread\n")
  153. {
  154. if (slot >= 0 && slot < AnimationComponent::MaxScriptThreads)
  155. {
  156. if (object->pauseThread(slot))
  157. return true;
  158. }
  159. return false;
  160. }
  161. DefineEngineMethod(AnimationComponent, getAnimationCount, S32, (), ,
  162. "Get the total number of sequences in the shape.\n"
  163. "@return the number of sequences in the shape\n\n")
  164. {
  165. return object->getAnimationCount();
  166. }
  167. DefineEngineMethod(AnimationComponent, getAnimationIndex, S32, (const char* name), ,
  168. "Find the index of the sequence with the given name.\n"
  169. "@param name name of the sequence to lookup\n"
  170. "@return index of the sequence with matching name, or -1 if not found\n\n"
  171. "@tsexample\n"
  172. "// Check if a given sequence exists in the shape\n"
  173. "if ( %this.getSequenceIndex( \"walk\" ) == -1 )\n"
  174. " echo( \"Could not find 'walk' sequence\" );\n"
  175. "@endtsexample\n")
  176. {
  177. return object->getAnimationIndex(name);
  178. }
  179. DefineEngineMethod(AnimationComponent, getAnimationName, const char*, (S32 index), ,
  180. "Get the name of the indexed sequence.\n"
  181. "@param index index of the sequence to query (valid range is 0 - getSequenceCount()-1)\n"
  182. "@return the name of the sequence\n\n"
  183. "@tsexample\n"
  184. "// print the name of all sequences in the shape\n"
  185. "%count = %this.getSequenceCount();\n"
  186. "for ( %i = 0; %i < %count; %i++ )\n"
  187. " echo( %i SPC %this.getSequenceName( %i ) );\n"
  188. "@endtsexample\n")
  189. {
  190. return object->getAnimationName(index);
  191. }