1 /*
2 * this file is part of the oxygen gtk engine
3 * Copyright (c) 2010 Hugo Pereira Da Costa <hugo.pereira@free.fr>
4 *
5 * This  library is free  software; you can  redistribute it and/or
6 * modify it  under  the terms  of the  GNU Lesser  General  Public
7 * License  as published  by the Free  Software  Foundation; either
8 * version 2 of the License, or(at your option ) any later version.
9 *
10 * This library is distributed  in the hope that it will be useful,
11 * but  WITHOUT ANY WARRANTY; without even  the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License  along  with  this library;  if not,  write to  the Free
17 * Software Foundation, Inc., 51  Franklin St, Fifth Floor, Boston,
18 * MA 02110-1301, USA.
19 */
20 
21 #include "oxygentimeline.h"
22 #include "oxygentimelineserver.h"
23 
24 #include <cassert>
25 
26 namespace Oxygen
27 {
28 
29     //_________________________________________________
30     int TimeLine::_steps = 0;
31 
32     //_________________________________________________
TimeLine(int duration)33     TimeLine::TimeLine( int duration ):
34         _duration( duration ),
35         _enabled( true ),
36         _direction( Forward ),
37         _running( false ),
38         _value( 0 ),
39         _time( 0 ),
40         _timer( g_timer_new() ),
41         _func( 0L ),
42         _data( 0L )
43     { TimeLineServer::instance().registerTimeLine( this ); }
44 
45     //_________________________________________________
TimeLine(const TimeLine & other)46     TimeLine::TimeLine( const TimeLine& other ):
47         _duration( other._duration ),
48         _enabled( other._enabled ),
49         _direction( other._direction ),
50         _running( false ),
51         _value( 0 ),
52         _time( 0 ),
53         _timer( g_timer_new() ),
54         _func( other._func ),
55         _data( other._data )
56     { TimeLineServer::instance().registerTimeLine( this ); }
57 
58     //_________________________________________________
~TimeLine(void)59     TimeLine::~TimeLine( void )
60     {
61         if( _timer ) g_timer_destroy( _timer );
62         TimeLineServer::instance().unregisterTimeLine( this );
63     }
64 
65     //_________________________________________________
operator =(const TimeLine & other)66     TimeLine& TimeLine::operator = ( const TimeLine& other )
67     {
68 
69         stop();
70 
71         _duration = other._duration;
72         _enabled = other._enabled;
73         _direction = other._direction;
74         _value = 0;
75         _time = 0;
76         _func = other._func;
77         _data = other._data;
78 
79         return *this;
80 
81     }
82 
83     //_________________________________________________
start(void)84     void TimeLine::start( void )
85     {
86 
87         // do nothing if disabled
88         if( (!_enabled) || _duration <= 0 ) return;
89 
90         assert( !_running );
91 
92         _value = (_direction == Forward ) ? 0:1;
93         _time = 0;
94         g_timer_start( _timer );
95         _running = true;
96 
97         TimeLineServer::instance().start();
98         trigger();
99 
100     }
101 
102     //_________________________________________________
stop(void)103     void TimeLine::stop( void )
104     {
105 
106         if( !_running ) return;
107 
108         g_timer_stop( _timer );
109         _running = false;
110     }
111 
112     //_________________________________________________
update(void)113     bool TimeLine::update( void )
114     {
115 
116         if( !_running ) return false;
117 
118         // get time (msec)
119         const int elapsed( int(1000*g_timer_elapsed(_timer, 0L)) );
120         const double end( _direction == Forward ? 1:0 );
121         if( elapsed >= _duration )
122         {
123 
124             _time = _duration;
125             _value = end;
126             trigger();
127             stop();
128             return false;
129 
130         } else {
131 
132 
133             // for debugging only
134             assert( _time < _duration );
135             assert( _time <= elapsed );
136 
137             double oldValue( _value );
138             _value = digitize( ( _value*double(_duration - elapsed) + end*double(elapsed - _time) )/double(_duration - _time) );
139             _time = elapsed;
140 
141             // trigger callback if value is actually changed
142             if( _value != oldValue ) trigger();
143 
144             return true;
145 
146         }
147 
148     }
149 
150 }
151