VShapeAnimationEvent.cpp 5.6 KB

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