VShapeAnimationTrack.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //-----------------------------------------------------------------------------
  2. // Verve
  3. // Copyright (C) 2014 - Violent Tulip
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to
  7. // deal in the Software without restriction, including without limitation the
  8. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. // sell copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. // IN THE SOFTWARE.
  22. //-----------------------------------------------------------------------------
  23. #include "Verve/Extension/Animation/VShapeAnimationTrack.h"
  24. #include "Verve/Extension/Animation/VShapeAnimationEvent.h"
  25. #include "Verve/Core/VGroup.h"
  26. #include "console/consoleTypes.h"
  27. //-----------------------------------------------------------------------------
  28. IMPLEMENT_CONOBJECT( VShapeAnimationTrack );
  29. //-----------------------------------------------------------------------------
  30. VShapeAnimationTrack::VShapeAnimationTrack( void ) :
  31. mThreadIndex( 0 )
  32. {
  33. setLabel( "AnimationTrack" );
  34. }
  35. //-----------------------------------------------------------------------------
  36. void VShapeAnimationTrack::initPersistFields()
  37. {
  38. docsURL;
  39. Parent::initPersistFields();
  40. addField( "ThreadIndex", TypeS32, Offset( mThreadIndex, VShapeAnimationTrack ), "The index of the Animation Thread to play." );
  41. }
  42. //-----------------------------------------------------------------------------
  43. //
  44. // Controller Methods.
  45. //
  46. //-----------------------------------------------------------------------------
  47. //-----------------------------------------------------------------------------
  48. //
  49. // VShapeAnimationTrack::onControllerEvent( pEvent );
  50. //
  51. // When the controller's state changes, this method is called. If the
  52. // controller is paused, or stops playing, then the animation will cease to
  53. // play. If the controller resumes play, the animation will continue.
  54. //
  55. // For a full list of possible events, see the 'eControllerEventType'
  56. // declaration in VController.h.
  57. //
  58. //-----------------------------------------------------------------------------
  59. bool VShapeAnimationTrack::onControllerEvent( VController::eControllerEventType pEvent )
  60. {
  61. if ( !Parent::onControllerEvent( pEvent ) )
  62. {
  63. // Skip.
  64. return false;
  65. }
  66. // Enabled?
  67. if ( !isEnabled() )
  68. {
  69. // Continue Processing Events.
  70. return true;
  71. }
  72. switch ( pEvent )
  73. {
  74. case VController::k_EventPlay :
  75. {
  76. // Play Animation.
  77. VTorque::setAnimationTimeScale( getSceneObject(), mThreadIndex, ( ( isControllerPlayingForward() ) ? 1.f : -1.f ) );
  78. } break;
  79. case VController::k_EventPause :
  80. case VController::k_EventStop :
  81. {
  82. // Stop Animation.
  83. VTorque::setAnimationTimeScale( getSceneObject(), mThreadIndex, 0.f );
  84. } break;
  85. }
  86. return true;
  87. }
  88. //-----------------------------------------------------------------------------
  89. //
  90. // VShapeAnimationTrack::onControllerReset( pTime, pForward );
  91. //
  92. // Reset the animation state of the target object. If there is a Next Event,
  93. // then the animation is positioned accordingly.
  94. //
  95. //-----------------------------------------------------------------------------
  96. void VShapeAnimationTrack::onControllerReset( const S32 &pTime, const bool &pForward )
  97. {
  98. VTorque::SceneObjectType *object = getSceneObject();
  99. if ( !object )
  100. {
  101. // Parent Call.
  102. Parent::onControllerReset( pTime, pForward );
  103. return;
  104. }
  105. VShapeAnimationEvent *event;
  106. if ( getCurrentEvent( event ) )
  107. {
  108. // Stop Animation.
  109. VTorque::stopAnimation( object, mThreadIndex );
  110. }
  111. // Parent Call.
  112. Parent::onControllerReset( pTime, pForward );
  113. if ( getCurrentEvent( event ) )
  114. {
  115. // Play Animation.
  116. VTorque::playAnimation( object, mThreadIndex, event->mAnimationData );
  117. // Set Position.
  118. VTorque::setAnimationPosition( object, mThreadIndex, event->getAnimationPosition( pTime ) );
  119. // Stop Animation.
  120. VTorque::setAnimationTimeScale( object, mThreadIndex, 0.f );
  121. }
  122. }
  123. #ifdef VT_EDITOR
  124. //-----------------------------------------------------------------------------
  125. //
  126. // Debug Methods.
  127. //
  128. //-----------------------------------------------------------------------------
  129. DefineEngineMethod( VShapeAnimationTrack, updateTrack, void, (),, "( void ) - Update the Track.\n"
  130. "@return No return value." )
  131. {
  132. for ( ITreeNode *node = object->mChildNode; node != NULL; node = node->mSiblingNextNode )
  133. {
  134. VShapeAnimationEvent *currEvent = ( VShapeAnimationEvent* )node;
  135. VShapeAnimationEvent *nextEvent = ( VShapeAnimationEvent* )node->mSiblingNextNode;
  136. if ( !currEvent->mAutoDuration )
  137. {
  138. // Skip.
  139. continue;
  140. }
  141. if ( VTorque::isAnimationLooping( object->getSceneObject(), currEvent->mAnimationData ) )
  142. {
  143. if ( !nextEvent )
  144. {
  145. // Update Duration.
  146. currEvent->setDuration( object->getControllerDuration() - currEvent->getTriggerTime() );
  147. }
  148. else
  149. {
  150. // Update Duration.
  151. currEvent->setDuration( mAbs( nextEvent->getTriggerTime() - currEvent->getTriggerTime() ) );
  152. }
  153. }
  154. else
  155. {
  156. // Update Duration.
  157. currEvent->setDuration( ( S32 )( 1000 * VTorque::getAnimationDuration( object->getSceneObject(), currEvent->mAnimationData ) ) );
  158. }
  159. }
  160. }
  161. #endif