123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- //-----------------------------------------------------------------------------
- // Verve
- // Copyright (C) 2014 - Violent Tulip
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to
- // deal in the Software without restriction, including without limitation the
- // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
- // sell copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- // IN THE SOFTWARE.
- //-----------------------------------------------------------------------------
- #include "Verve/Extension/Animation/VShapeAnimationEvent.h"
- #include "Verve/Extension/Animation/VShapeAnimationTrack.h"
- #include "console/consoleTypes.h"
- //-----------------------------------------------------------------------------
- IMPLEMENT_CONOBJECT( VShapeAnimationEvent );
- //-----------------------------------------------------------------------------
- VShapeAnimationEvent::VShapeAnimationEvent( void ) :
- mAnimationData( String::EmptyString ),
- mAutoDuration( true )
- {
- setLabel( "AnimationEvent" );
- }
- //-----------------------------------------------------------------------------
- void VShapeAnimationEvent::initPersistFields( void )
- {
- Parent::initPersistFields();
- addField( "AnimationData", TypeRealString, Offset( mAnimationData, VShapeAnimationEvent ), "The name of the Animation Sequence to play upon triggering." );
- addField( "AutoDuration", TypeBool, Offset( mAutoDuration, VShapeAnimationEvent ), "Force the Event's Duration to match the length of the Animation." );
- }
- //-----------------------------------------------------------------------------
- //
- // Callback Methods.
- //
- //-----------------------------------------------------------------------------
- //-----------------------------------------------------------------------------
- //
- // VShapeAnimationEvent::onTrigger( pTime, pDelta );
- //
- // Play the desired animation. Also account for any offet in playtime, and
- // timescale.
- //
- //-----------------------------------------------------------------------------
- void VShapeAnimationEvent::onTrigger( const S32 &pTime, const S32 &pDelta )
- {
- Parent::onTrigger( pTime, pDelta );
- VTorque::SceneObjectType *object = getSceneObject();
- VShapeAnimationTrack *track;
- if ( !object || !getTrack( track ) )
- {
- // Sanity!
- return;
- }
- // Play Animation.
- VTorque::playAnimation( object, track->getThreadIndex(), mAnimationData );
- // Set Position.
- VTorque::setAnimationPosition( object, track->getThreadIndex(), getAnimationPosition( pTime + pDelta ) );
- // Set Time Scale.
- VTorque::setAnimationTimeScale( object, track->getThreadIndex(), ( ( pDelta > 0 ) ? 1.f : -1.f ) );
- }
- //-----------------------------------------------------------------------------
- //
- // VShapeAnimationEvent::onComplete( pTime, pDelta );
- //
- // If the animation is cyclic, then it needs to be paused once the event has
- // finished playing.
- //
- //-----------------------------------------------------------------------------
- void VShapeAnimationEvent::onComplete( const S32 &pTime, const S32 &pDelta )
- {
- // Fetch Object.
- VTorque::SceneObjectType *object = getSceneObject();
- VShapeAnimationTrack *track;
- if ( object && VTorque::isAnimationLooping( object, mAnimationData ) && getTrack( track ) )
- {
- // Pause Animation.
- VTorque::pauseAnimation( object, track->getThreadIndex() );
- }
- }
- //-----------------------------------------------------------------------------
- //
- // Property Methods.
- //
- //-----------------------------------------------------------------------------
- //-----------------------------------------------------------------------------
- //
- // VShapeAnimationEvent::getAnimationPosition( pTime );
- //
- // Returns the time that the animation should be positioned at, at the given
- // time. This method considers whether the animation is cyclic or not and will
- // return the appropriate time regardless. Time is expressed in seconds and not
- // milliseconds.
- //
- //-----------------------------------------------------------------------------
- F32 VShapeAnimationEvent::getAnimationPosition( const S32 &pTime )
- {
- // Fetch Object.
- VSceneObjectTrack *track;
- VTorque::SceneObjectType *object = getSceneObject();
- if ( !getTrack( track ) || !object )
- {
- // Null.
- return 0.f;
- }
- // Fetch Interp.
- F32 interp = track->calculateInterp( pTime );
- if ( !isControllerPlayingForward() )
- {
- // Flip.
- interp = ( 1.f - interp );
- }
- // Not Looping?
- if ( !VTorque::isAnimationLooping( object, mAnimationData ) )
- {
- // Return Interp.
- return interp;
- }
- // Fetch Sequence Duration.
- const S32 duration = ( S32 )( 1000 * VTorque::getAnimationDuration( object, mAnimationData ) );
- // Fetch Loop Interp.
- const S32 loopInterp = S32( mDuration * interp ) % duration;
- return ( F32 )loopInterp / ( F32 )duration;
- }
|