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

0001 #ifndef oxygentimeline_h
0002 #define oxygentimeline_h
0003 
0004 /*
0005 * this file is part of the oxygen gtk engine
0006 * SPDX-FileCopyrightText: 2010 Hugo Pereira Da Costa <hugo.pereira@free.fr>
0007 *
0008 * SPDX-License-Identifier: LGPL-2.0-or-later
0009 */
0010 
0011 #include <glib.h>
0012 #include <cassert>
0013 #include <cmath>
0014 
0015 namespace Oxygen
0016 {
0017 
0018     //! timeline used to handle animations
0019     class TimeLine
0020     {
0021 
0022         public:
0023 
0024         //! constructor
0025         TimeLine( int = 0 );
0026 
0027         //! copy constructor
0028         /*! warning other timeline state is not copied */
0029         TimeLine( const TimeLine& );
0030 
0031         //! destructor
0032         virtual ~TimeLine( void );
0033 
0034         //! assignment operator
0035         TimeLine& operator = (const TimeLine& );
0036 
0037         //! connect callback
0038         void connect( GSourceFunc func, gpointer data )
0039         {
0040             _func = func;
0041             _data = data;
0042         }
0043 
0044         //! disconnect
0045         void disconnect( void )
0046         {
0047             _func = 0L;
0048             _data = 0L;
0049         }
0050 
0051         //!@name accessors
0052         //@{
0053 
0054         //! value (between 0 and 1)
0055         double value( void ) const
0056         { return _value; }
0057 
0058         //! true if connected
0059         bool isConnected( void ) const
0060         { return _func && _data; }
0061 
0062         //! true if running
0063         bool isRunning( void ) const
0064         { return _running; }
0065 
0066         //@}
0067 
0068         //!@name modifiers
0069         //@{
0070 
0071         //! duration
0072         void setDuration( int value )
0073         { _duration = value; }
0074 
0075         //! enable state
0076         void setEnabled( bool value )
0077         { _enabled = value; }
0078 
0079         //! direction
0080         enum Direction
0081         {
0082             Forward,
0083             Backward
0084         };
0085 
0086         //! direction
0087         void setDirection( Direction direction )
0088         { _direction = direction; }
0089 
0090         //! start
0091         void start( void );
0092 
0093         //! stop
0094         void stop( void );
0095 
0096         //! update value and running state
0097         /*!
0098         also emits signal when value has changed since last time.
0099         returns true if timeline is still running
0100         */
0101         bool update( void );
0102 
0103         //@}
0104 
0105         //! steps
0106         static void setSteps( int value )
0107         { _steps = value; }
0108 
0109         protected:
0110 
0111         //! run callback
0112         void trigger( void ) const
0113         { if( _func )  (_func)(_data); }
0114 
0115         //! digitize value, based on steps
0116         double digitize( const double& value ) const
0117         {
0118             if( _steps > 0 ) return std::floor( value*_steps )/_steps;
0119             else return value;
0120         }
0121 
0122 
0123         private:
0124 
0125         //! duration
0126         int _duration;
0127 
0128         //! enable state
0129         bool _enabled;
0130 
0131         //! direction
0132         Direction _direction;
0133 
0134         //! true if timer is running
0135         bool _running;
0136 
0137         //! value (between 0 and 1)
0138         double _value;
0139 
0140         //! time (at which _value was last calculated)
0141         int _time;
0142 
0143         //! timer
0144         GTimer* _timer;
0145 
0146         //! source function
0147         GSourceFunc _func;
0148 
0149         //! data
0150         gpointer _data;
0151 
0152         //! steps
0153         static int _steps;
0154 
0155     };
0156 
0157 
0158 };
0159 
0160 #endif