VShapeAnimationTrack.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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( void )
  37. {
  38. Parent::initPersistFields();
  39. addField( "ThreadIndex", TypeS32, Offset( mThreadIndex, VShapeAnimationTrack ), "The index of the Animation Thread to play." );
  40. }
  41. //-----------------------------------------------------------------------------
  42. //
  43. // Controller Methods.
  44. //
  45. //-----------------------------------------------------------------------------
  46. //-----------------------------------------------------------------------------
  47. //
  48. // VShapeAnimationTrack::onControllerEvent( pEvent );
  49. //
  50. // When the controller's state changes, this method is called. If the
  51. // controller is paused, or stops playing, then the animation will cease to
  52. // play. If the controller resumes play, the animation will continue.
  53. //
  54. // For a full list of possible events, see the 'eControllerEventType'
  55. // declaration in VController.h.
  56. //
  57. //-----------------------------------------------------------------------------
  58. bool VShapeAnimationTrack::onControllerEvent( VController::eControllerEventType pEvent )
  59. {
  60. if ( !Parent::onControllerEvent( pEvent ) )
  61. {
  62. // Skip.
  63. return false;
  64. }
  65. // Enabled?
  66. if ( !isEnabled() )
  67. {
  68. // Continue Processing Events.
  69. return true;
  70. }
  71. switch ( pEvent )
  72. {
  73. case VController::k_EventPlay :
  74. {
  75. // Play Animation.
  76. VTorque::setAnimationTimeScale( getSceneObject(), mThreadIndex, ( ( isControllerPlayingForward() ) ? 1.f : -1.f ) );
  77. } break;
  78. case VController::k_EventPause :
  79. case VController::k_EventStop :
  80. {
  81. // Stop Animation.
  82. VTorque::setAnimationTimeScale( getSceneObject(), mThreadIndex, 0.f );
  83. } break;
  84. }
  85. return true;
  86. }
  87. //-----------------------------------------------------------------------------
  88. //
  89. // VShapeAnimationTrack::onControllerReset( pTime, pForward );
  90. //
  91. // Reset the animation state of the target object. If there is a Next Event,
  92. // then the animation is positioned accordingly.
  93. //
  94. //-----------------------------------------------------------------------------
  95. void VShapeAnimationTrack::onControllerReset( const S32 &pTime, const bool &pForward )
  96. {
  97. VTorque::SceneObjectType *object = getSceneObject();
  98. if ( !object )
  99. {
  100. // Parent Call.
  101. Parent::onControllerReset( pTime, pForward );
  102. return;
  103. }
  104. VShapeAnimationEvent *event;
  105. if ( getCurrentEvent( event ) )
  106. {
  107. // Stop Animation.
  108. VTorque::stopAnimation( object, mThreadIndex );
  109. }
  110. // Parent Call.
  111. Parent::onControllerReset( pTime, pForward );
  112. if ( getCurrentEvent( event ) )
  113. {
  114. // Play Animation.
  115. VTorque::playAnimation( object, mThreadIndex, event->mAnimationData );
  116. // Set Position.
  117. VTorque::setAnimationPosition( object, mThreadIndex, event->getAnimationPosition( pTime ) );
  118. // Stop Animation.
  119. VTorque::setAnimationTimeScale( object, mThreadIndex, 0.f );
  120. }
  121. }
  122. #ifdef VT_EDITOR
  123. //-----------------------------------------------------------------------------
  124. //
  125. // Debug Methods.
  126. //
  127. //-----------------------------------------------------------------------------
  128. DefineEngineMethod( VShapeAnimationTrack, updateTrack, void, (),, "( void ) - Update the Track.\n"
  129. "@return No return value." )
  130. {
  131. for ( ITreeNode *node = object->mChildNode; node != NULL; node = node->mSiblingNextNode )
  132. {
  133. VShapeAnimationEvent *currEvent = ( VShapeAnimationEvent* )node;
  134. VShapeAnimationEvent *nextEvent = ( VShapeAnimationEvent* )node->mSiblingNextNode;
  135. if ( !currEvent->mAutoDuration )
  136. {
  137. // Skip.
  138. continue;
  139. }
  140. if ( VTorque::isAnimationLooping( object->getSceneObject(), currEvent->mAnimationData ) )
  141. {
  142. if ( !nextEvent )
  143. {
  144. // Update Duration.
  145. currEvent->setDuration( object->getControllerDuration() - currEvent->getTriggerTime() );
  146. }
  147. else
  148. {
  149. // Update Duration.
  150. currEvent->setDuration( mAbs( nextEvent->getTriggerTime() - currEvent->getTriggerTime() ) );
  151. }
  152. }
  153. else
  154. {
  155. // Update Duration.
  156. currEvent->setDuration( ( S32 )( 1000 * VTorque::getAnimationDuration( object->getSceneObject(), currEvent->mAnimationData ) ) );
  157. }
  158. }
  159. }
  160. #endif