Resetting / Restarting Timeline animations

Hey Embers,

I’m curious if anyone else has tried resetting or restarting animations appended to Cinder’s timeline? I’m trying to do this with no luck so far, at least not using what I thought seemed to be the right approach.

I’ve tried calling removeTarget() on the Anim object, as well as getParent().removeSelf(), but none of it seems to be causing the intended effect.

I’ve also not been able to find any reset/restart code in Cinder’s samples, but I’m still looking.

Anyone tried anything similar?

Cheers,
Gazoo

It’s really just a matter of re-setting the start time.

#include "cinder/app/App.h"
#include "cinder/app/RendererGl.h"
#include "cinder/gl/gl.h"
#include "cinder/Timeline.h"

using namespace ci;
using namespace ci::app;
using namespace std;

class TweenResetApp : public ci::app::App
{
public:
    
    void draw ( ) override
    {
        gl::clear ( );
        gl::setMatricesWindow ( getWindowSize() );
        gl::translate ( getWindowCenter() );
        
        float edge = lerp ( 32.0f, 128.0f, _anim() );
        
        float r = _anim.isComplete() ? 1.0f : 0.0f;
        gl::ScopedColor color { Colorf ( r, 1.0f - r, 0 ) };
        gl::drawSolidRect( Rectf { -edge, -edge, edge, edge } );
    }
    
    void mouseDown ( MouseEvent e ) override
    {
       if ( !_tween || _tween->isComplete() )
       {
           if ( _tween ) _anim = _tween->getStartValue();
           _tween = app::timeline().apply( &_anim, 1.0f, 3.0f, EaseInOutAtan(22.0f) );
       }else
       {
           _anim = _tween->getStartValue();
           _tween->setStartTime( app::timeline().getCurrentTime() );
       }
    }
    
    Anim<float> _anim{0.0f};
    TweenRef<float> _tween;
};

CINDER_APP ( TweenResetApp, RendererGl );

Depending on your setup, the Tween may need to have autoRemove disabled so that the owning timeline doesn’t destroy it.

Much appreciate the response @lithium .

I was thinking there must be an easier way to simply undo/remove a tween. Especially because Cinder’s ci::app::timeline().apply() appears to do exactly that. I.e. remove any active tweens and apply the newly provided one.

For the record, I unfortunately cannot use apply() as my code applies consecutive animations together, and if I always trigger an .apply(), then it’ll wipe all the previously applied ones. I dug into libcinders code and found that it calls removeTarget(). I attempted to use this function on the time line and hand over a pointer to the Anim object, without luck still. I really have no idea why that didn’t / doesn’t work as everything points towards that being supposed to work.

I sniffed around a bit more and luckily stumbled upon the Anim objects stop() function. This does exactly that - remove any/all tweens applied to the Anim object.

Gazoo