File indexing completed on 2024-05-12 05:34:36

0001 /*
0002     this file is part of the oxygen gtk engine
0003     SPDX-FileCopyrightText: 2010 Hugo Pereira Da Costa <hugo.pereira@free.fr>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "oxygentimeline.h"
0009 #include "oxygentimelineserver.h"
0010 
0011 #include <cassert>
0012 
0013 namespace Oxygen
0014 {
0015 
0016     //_________________________________________________
0017     int TimeLine::_steps = 0;
0018 
0019     //_________________________________________________
0020     TimeLine::TimeLine( int duration ):
0021         _duration( duration ),
0022         _enabled( true ),
0023         _direction( Forward ),
0024         _running( false ),
0025         _value( 0 ),
0026         _time( 0 ),
0027         _timer( g_timer_new() ),
0028         _func( 0L ),
0029         _data( 0L )
0030     { TimeLineServer::instance().registerTimeLine( this ); }
0031 
0032     //_________________________________________________
0033     TimeLine::TimeLine( const TimeLine& other ):
0034         _duration( other._duration ),
0035         _enabled( other._enabled ),
0036         _direction( other._direction ),
0037         _running( false ),
0038         _value( 0 ),
0039         _time( 0 ),
0040         _timer( g_timer_new() ),
0041         _func( other._func ),
0042         _data( other._data )
0043     { TimeLineServer::instance().registerTimeLine( this ); }
0044 
0045     //_________________________________________________
0046     TimeLine::~TimeLine( void )
0047     {
0048         if( _timer ) g_timer_destroy( _timer );
0049         TimeLineServer::instance().unregisterTimeLine( this );
0050     }
0051 
0052     //_________________________________________________
0053     TimeLine& TimeLine::operator = ( const TimeLine& other )
0054     {
0055 
0056         stop();
0057 
0058         _duration = other._duration;
0059         _enabled = other._enabled;
0060         _direction = other._direction;
0061         _value = 0;
0062         _time = 0;
0063         _func = other._func;
0064         _data = other._data;
0065 
0066         return *this;
0067 
0068     }
0069 
0070     //_________________________________________________
0071     void TimeLine::start( void )
0072     {
0073 
0074         // do nothing if disabled
0075         if( (!_enabled) || _duration <= 0 ) return;
0076 
0077         assert( !_running );
0078 
0079         _value = (_direction == Forward ) ? 0:1;
0080         _time = 0;
0081         g_timer_start( _timer );
0082         _running = true;
0083 
0084         TimeLineServer::instance().start();
0085         trigger();
0086 
0087     }
0088 
0089     //_________________________________________________
0090     void TimeLine::stop( void )
0091     {
0092 
0093         if( !_running ) return;
0094 
0095         g_timer_stop( _timer );
0096         _running = false;
0097     }
0098 
0099     //_________________________________________________
0100     bool TimeLine::update( void )
0101     {
0102 
0103         if( !_running ) return false;
0104 
0105         // get time (msec)
0106         const int elapsed( int(1000*g_timer_elapsed(_timer, 0L)) );
0107         const double end( _direction == Forward ? 1:0 );
0108         if( elapsed >= _duration )
0109         {
0110 
0111             _time = _duration;
0112             _value = end;
0113             trigger();
0114             stop();
0115             return false;
0116 
0117         } else {
0118 
0119 
0120             // for debugging only
0121             assert( _time < _duration );
0122             assert( _time <= elapsed );
0123 
0124             double oldValue( _value );
0125             _value = digitize( ( _value*double(_duration - elapsed) + end*double(elapsed - _time) )/double(_duration - _time) );
0126             _time = elapsed;
0127 
0128             // trigger callback if value is actually changed
0129             if( _value != oldValue ) trigger();
0130 
0131             return true;
0132 
0133         }
0134 
0135     }
0136 
0137 }