1 #ifndef oxygentimeline_h
2 #define oxygentimeline_h
3 
4 /*
5 * this file is part of the oxygen gtk engine
6 * Copyright (c) 2010 Hugo Pereira Da Costa <hugo.pereira@free.fr>
7 *
8 * This  library is free  software; you can  redistribute it and/or
9 * modify it  under  the terms  of the  GNU Lesser  General  Public
10 * License  as published  by the Free  Software  Foundation; either
11 * version 2 of the License, or(at your option ) any later version.
12 *
13 * This library is distributed  in the hope that it will be useful,
14 * but  WITHOUT ANY WARRANTY; without even  the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License  along  with  this library;  if not,  write to  the Free
20 * Software Foundation, Inc., 51  Franklin St, Fifth Floor, Boston,
21 * MA 02110-1301, USA.
22 */
23 
24 #include <glib.h>
25 #include <cassert>
26 #include <cmath>
27 
28 namespace Oxygen
29 {
30 
31     //! timeline used to handle animations
32     class TimeLine
33     {
34 
35         public:
36 
37         //! constructor
38         TimeLine( int = 0 );
39 
40         //! copy constructor
41         /*! warning other timeline state is not copied */
42         TimeLine( const TimeLine& );
43 
44         //! destructor
45         virtual ~TimeLine( void );
46 
47         //! assignment operator
48         TimeLine& operator = (const TimeLine& );
49 
50         //! connect callback
connect(GSourceFunc func,gpointer data)51         void connect( GSourceFunc func, gpointer data )
52         {
53             _func = func;
54             _data = data;
55         }
56 
57         //! disconnect
disconnect(void)58         void disconnect( void )
59         {
60             _func = 0L;
61             _data = 0L;
62         }
63 
64         //!@name accessors
65         //@{
66 
67         //! value (between 0 and 1)
value(void)68         double value( void ) const
69         { return _value; }
70 
71         //! true if connected
isConnected(void)72         bool isConnected( void ) const
73         { return _func && _data; }
74 
75         //! true if running
isRunning(void)76         bool isRunning( void ) const
77         { return _running; }
78 
79         //@}
80 
81         //!@name modifiers
82         //@{
83 
84         //! duration
setDuration(int value)85         void setDuration( int value )
86         { _duration = value; }
87 
88         //! enable state
setEnabled(bool value)89         void setEnabled( bool value )
90         { _enabled = value; }
91 
92         //! direction
93         enum Direction
94         {
95             Forward,
96             Backward
97         };
98 
99         //! direction
setDirection(Direction direction)100         void setDirection( Direction direction )
101         { _direction = direction; }
102 
103         //! start
104         void start( void );
105 
106         //! stop
107         void stop( void );
108 
109         //! update value and running state
110         /*!
111         also emits signal when value has changed since last time.
112         returns true if timeline is still running
113         */
114         bool update( void );
115 
116         //@}
117 
118         //! steps
setSteps(int value)119         static void setSteps( int value )
120         { _steps = value; }
121 
122         protected:
123 
124         //! run callback
trigger(void)125         void trigger( void ) const
126         { if( _func )  (_func)(_data); }
127 
128         //! digitize value, based on steps
digitize(const double & value)129         double digitize( const double& value ) const
130         {
131             if( _steps > 0 ) return std::floor( value*_steps )/_steps;
132             else return value;
133         }
134 
135 
136         private:
137 
138         //! duration
139         int _duration;
140 
141         //! enable state
142         bool _enabled;
143 
144         //! direction
145         Direction _direction;
146 
147         //! true if timer is running
148         bool _running;
149 
150         //! value (between 0 and 1)
151         double _value;
152 
153         //! time (at which _value was last calculated)
154         int _time;
155 
156         //! timer
157         GTimer* _timer;
158 
159         //! source function
160         GSourceFunc _func;
161 
162         //! data
163         gpointer _data;
164 
165         //! steps
166         static int _steps;
167 
168     };
169 
170 
171 };
172 
173 #endif
174