VShapeAnimationEvent.cpp
Engine/source/Verve/Extension/Animation/VShapeAnimationEvent.cpp
Public Functions
Detailed Description
Public Functions
IMPLEMENT_CONOBJECT(VShapeAnimationEvent )
1 2//----------------------------------------------------------------------------- 3// Verve 4// Copyright (C) 2014 - Violent Tulip 5// 6// Permission is hereby granted, free of charge, to any person obtaining a copy 7// of this software and associated documentation files (the "Software"), to 8// deal in the Software without restriction, including without limitation the 9// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 10// sell copies of the Software, and to permit persons to whom the Software is 11// furnished to do so, subject to the following conditions: 12// 13// The above copyright notice and this permission notice shall be included in 14// all copies or substantial portions of the Software. 15// 16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 22// IN THE SOFTWARE. 23//----------------------------------------------------------------------------- 24#include "Verve/Extension/Animation/VShapeAnimationEvent.h" 25#include "Verve/Extension/Animation/VShapeAnimationTrack.h" 26 27#include "console/consoleTypes.h" 28 29//----------------------------------------------------------------------------- 30IMPLEMENT_CONOBJECT( VShapeAnimationEvent ); 31//----------------------------------------------------------------------------- 32 33VShapeAnimationEvent::VShapeAnimationEvent( void ) : 34 mAnimationData( String::EmptyString ), 35 mAutoDuration( true ) 36{ 37 setLabel( "AnimationEvent" ); 38} 39 40//----------------------------------------------------------------------------- 41 42void VShapeAnimationEvent::initPersistFields( void ) 43{ 44 Parent::initPersistFields(); 45 46 addField( "AnimationData", TypeRealString, Offset( mAnimationData, VShapeAnimationEvent ), "The name of the Animation Sequence to play upon triggering." ); 47 addField( "AutoDuration", TypeBool, Offset( mAutoDuration, VShapeAnimationEvent ), "Force the Event's Duration to match the length of the Animation." ); 48} 49 50//----------------------------------------------------------------------------- 51// 52// Callback Methods. 53// 54//----------------------------------------------------------------------------- 55 56//----------------------------------------------------------------------------- 57// 58// VShapeAnimationEvent::onTrigger( pTime, pDelta ); 59// 60// Play the desired animation. Also account for any offet in playtime, and 61// timescale. 62// 63//----------------------------------------------------------------------------- 64void VShapeAnimationEvent::onTrigger( const S32 &pTime, const S32 &pDelta ) 65{ 66 Parent::onTrigger( pTime, pDelta ); 67 68 VTorque::SceneObjectType *object = getSceneObject(); 69 VShapeAnimationTrack *track; 70 if ( !object || !getTrack( track ) ) 71 { 72 // Sanity! 73 return; 74 } 75 76 // Play Animation. 77 VTorque::playAnimation( object, track->getThreadIndex(), mAnimationData ); 78 79 // Set Position. 80 VTorque::setAnimationPosition( object, track->getThreadIndex(), getAnimationPosition( pTime + pDelta ) ); 81 82 // Set Time Scale. 83 VTorque::setAnimationTimeScale( object, track->getThreadIndex(), ( ( pDelta > 0 ) ? 1.f : -1.f ) ); 84} 85 86//----------------------------------------------------------------------------- 87// 88// VShapeAnimationEvent::onComplete( pTime, pDelta ); 89// 90// If the animation is cyclic, then it needs to be paused once the event has 91// finished playing. 92// 93//----------------------------------------------------------------------------- 94void VShapeAnimationEvent::onComplete( const S32 &pTime, const S32 &pDelta ) 95{ 96 // Fetch Object. 97 VTorque::SceneObjectType *object = getSceneObject(); 98 VShapeAnimationTrack *track; 99 if ( object && VTorque::isAnimationLooping( object, mAnimationData ) && getTrack( track ) ) 100 { 101 // Pause Animation. 102 VTorque::pauseAnimation( object, track->getThreadIndex() ); 103 } 104} 105 106//----------------------------------------------------------------------------- 107// 108// Property Methods. 109// 110//----------------------------------------------------------------------------- 111 112//----------------------------------------------------------------------------- 113// 114// VShapeAnimationEvent::getAnimationPosition( pTime ); 115// 116// Returns the time that the animation should be positioned at, at the given 117// time. This method considers whether the animation is cyclic or not and will 118// return the appropriate time regardless. Time is expressed in seconds and not 119// milliseconds. 120// 121//----------------------------------------------------------------------------- 122F32 VShapeAnimationEvent::getAnimationPosition( const S32 &pTime ) 123{ 124 // Fetch Object. 125 VSceneObjectTrack *track; 126 VTorque::SceneObjectType *object = getSceneObject(); 127 if ( !getTrack( track ) || !object ) 128 { 129 // Null. 130 return 0.f; 131 } 132 133 // Fetch Interp. 134 F32 interp = track->calculateInterp( pTime ); 135 if ( !isControllerPlayingForward() ) 136 { 137 // Flip. 138 interp = ( 1.f - interp ); 139 } 140 141 // Not Looping? 142 if ( !VTorque::isAnimationLooping( object, mAnimationData ) ) 143 { 144 // Return Interp. 145 return interp; 146 } 147 148 // Fetch Sequence Duration. 149 const S32 duration = ( S32 )( 1000 * VTorque::getAnimationDuration( object, mAnimationData ) ); 150 151 // Fetch Loop Interp. 152 const S32 loopInterp = S32( mDuration * interp ) % duration; 153 154 return ( F32 )loopInterp / ( F32 )duration; 155} 156