VShapeAnimationEvent.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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/VShapeAnimationEvent.h"
  24. #include "Verve/Extension/Animation/VShapeAnimationTrack.h"
  25. #include "console/consoleTypes.h"
  26. //-----------------------------------------------------------------------------
  27. IMPLEMENT_CONOBJECT( VShapeAnimationEvent );
  28. //-----------------------------------------------------------------------------
  29. VShapeAnimationEvent::VShapeAnimationEvent( void ) :
  30. mAnimationData( String::EmptyString ),
  31. mAutoDuration( true )
  32. {
  33. setLabel( "AnimationEvent" );
  34. }
  35. //-----------------------------------------------------------------------------
  36. void VShapeAnimationEvent::initPersistFields( void )
  37. {
  38. Parent::initPersistFields();
  39. addField( "AnimationData", TypeRealString, Offset( mAnimationData, VShapeAnimationEvent ), "The name of the Animation Sequence to play upon triggering." );
  40. addField( "AutoDuration", TypeBool, Offset( mAutoDuration, VShapeAnimationEvent ), "Force the Event's Duration to match the length of the Animation." );
  41. }
  42. //-----------------------------------------------------------------------------
  43. //
  44. // Callback Methods.
  45. //
  46. //-----------------------------------------------------------------------------
  47. //-----------------------------------------------------------------------------
  48. //
  49. // VShapeAnimationEvent::onTrigger( pTime, pDelta );
  50. //
  51. // Play the desired animation. Also account for any offet in playtime, and
  52. // timescale.
  53. //
  54. //-----------------------------------------------------------------------------
  55. void VShapeAnimationEvent::onTrigger( const S32 &pTime, const S32 &pDelta )
  56. {
  57. Parent::onTrigger( pTime, pDelta );
  58. VTorque::SceneObjectType *object = getSceneObject();
  59. VShapeAnimationTrack *track;
  60. if ( !object || !getTrack( track ) )
  61. {
  62. // Sanity!
  63. return;
  64. }
  65. // Play Animation.
  66. VTorque::playAnimation( object, track->getThreadIndex(), mAnimationData );
  67. // Set Position.
  68. VTorque::setAnimationPosition( object, track->getThreadIndex(), getAnimationPosition( pTime + pDelta ) );
  69. // Set Time Scale.
  70. VTorque::setAnimationTimeScale( object, track->getThreadIndex(), ( ( pDelta > 0 ) ? 1.f : -1.f ) );
  71. }
  72. //-----------------------------------------------------------------------------
  73. //
  74. // VShapeAnimationEvent::onComplete( pTime, pDelta );
  75. //
  76. // If the animation is cyclic, then it needs to be paused once the event has
  77. // finished playing.
  78. //
  79. //-----------------------------------------------------------------------------
  80. void VShapeAnimationEvent::onComplete( const S32 &pTime, const S32 &pDelta )
  81. {
  82. // Fetch Object.
  83. VTorque::SceneObjectType *object = getSceneObject();
  84. VShapeAnimationTrack *track;
  85. if ( object && VTorque::isAnimationLooping( object, mAnimationData ) && getTrack( track ) )
  86. {
  87. // Pause Animation.
  88. VTorque::pauseAnimation( object, track->getThreadIndex() );
  89. }
  90. }
  91. //-----------------------------------------------------------------------------
  92. //
  93. // Property Methods.
  94. //
  95. //-----------------------------------------------------------------------------
  96. //-----------------------------------------------------------------------------
  97. //
  98. // VShapeAnimationEvent::getAnimationPosition( pTime );
  99. //
  100. // Returns the time that the animation should be positioned at, at the given
  101. // time. This method considers whether the animation is cyclic or not and will
  102. // return the appropriate time regardless. Time is expressed in seconds and not
  103. // milliseconds.
  104. //
  105. //-----------------------------------------------------------------------------
  106. F32 VShapeAnimationEvent::getAnimationPosition( const S32 &pTime )
  107. {
  108. // Fetch Object.
  109. VSceneObjectTrack *track;
  110. VTorque::SceneObjectType *object = getSceneObject();
  111. if ( !getTrack( track ) || !object )
  112. {
  113. // Null.
  114. return 0.f;
  115. }
  116. // Fetch Interp.
  117. F32 interp = track->calculateInterp( pTime );
  118. if ( !isControllerPlayingForward() )
  119. {
  120. // Flip.
  121. interp = ( 1.f - interp );
  122. }
  123. // Not Looping?
  124. if ( !VTorque::isAnimationLooping( object, mAnimationData ) )
  125. {
  126. // Return Interp.
  127. return interp;
  128. }
  129. // Fetch Sequence Duration.
  130. const S32 duration = ( S32 )( 1000 * VTorque::getAnimationDuration( object, mAnimationData ) );
  131. // Fetch Loop Interp.
  132. const S32 loopInterp = S32( mDuration * interp ) % duration;
  133. return ( F32 )loopInterp / ( F32 )duration;
  134. }