00001
00002
00003
00004
00005
00006
00007
00013 #if ! defined ( OPERATORBASE_HH )
00014 #define OPERATORBASE_HH
00015
00016
00017 #include <iostream>
00018 #include <string>
00019 #include <sstream>
00020 #include <list>
00021 #include <algorithm>
00022
00023
00024 #include "piave_base.hh"
00025 #include "storybase.hh"
00026
00027 namespace PIAVE {
00028
00029
00033 template< class V >
00034 struct KeyFrame
00035 {
00036 KeyFrame( const Time p=0, const V& v = V() ) : pos(p), value( v ) {}
00037 bool operator < ( const KeyFrame& o ) const { return pos<o.pos; }
00038 bool operator > ( const KeyFrame& o ) const { return pos>o.pos; }
00039 bool operator == ( const KeyFrame& o ) const { return pos==o.pos; }
00040 bool operator != ( const KeyFrame& o ) const { return pos!=o.pos; }
00041
00043 Time pos;
00045 V value;
00046 };
00047
00051 class OpParameterBase
00052 {
00053 public:
00054 OpParameterBase() {};
00055
00056 virtual int fixMe( int fix_lvl=0 ) = 0;
00057 virtual void setParameter( const PropertyNode * p ) = 0;
00058
00059 };
00060
00061
00062 template< class V >
00063 class OpParameter : public OpParameterBase
00064 {
00065 public:
00066 OpParameter( const std::string &name, const std::string &type, const V& v ) :
00067 _name( name ),
00068 _type( type ),
00069 _isDynamic( false ),
00070 _hasMin( false ),
00071 _hasMax( false ),
00072 _min(),
00073 _max(),
00074 _about(),
00075 _path( 0 )
00076 { makeNewKeyFrame( 0, v ); };
00077
00078 typedef V value_type;
00079 typedef typename std::list< KeyFrame<V> >::iterator iterator;
00080 typedef typename std::list< KeyFrame<V> >::const_iterator const_iterator;
00081
00082 void setName( const std::string &name ) { _name = name; };
00083 void setIsDynamic( bool m=true ) { _isDynamic = m; };
00084 void setHasMin( bool m=true ) { _hasMin = m; };
00085 void setHasMax( bool m=true ) { _hasMax = m; };
00086 void setMin( const V &v ) { _min = v; };
00087 void setMax( const V &v ) { _max = v; };
00088 void setAbout( const std::string &about ) { _about = about; };
00089
00090 std::string getName() const { return _name; }
00091 bool getIsDynamic() const { return _isDynamic; }
00092 bool getHasMin() const { return _hasMin; }
00093 bool getHasMax() const { return _hasMax; }
00094 V getMin() const { return _min; }
00095 V getMax() const { return _max; }
00096 std::string getAbout() const { return _about; }
00097
00098 iterator pbegin() { return _path.begin(); }
00099 const_iterator pbegin() const { return _path.begin(); }
00100 iterator pend() { return _path.end(); }
00101 const_iterator pend() const { return _path.end(); }
00102 KeyFrame<V>& pfront() { return _path.front(); }
00103 const KeyFrame<V>& pfront() const { return _path.front(); }
00104 KeyFrame<V>& pback() { return _path.back(); }
00105 const KeyFrame<V>& pback() const { return _path.back(); }
00106 int psize() const { return _path.size(); }
00107
00108 bool removeKeyFrame( Time t );
00109 KeyFrame<V> * getKeyFrame( Time t );
00110 KeyFrame<V> * makeNewKeyFrame( Time t, const V &v );
00111
00112 virtual V getValue( Time t=0 ) const = 0;
00113
00114 private:
00115 std::string _name;
00116 std::string _type;
00117 bool _isDynamic;
00118 bool _hasMin;
00119 bool _hasMax;
00120 V _min;
00121 V _max;
00122 std::string _about;
00123
00124 protected:
00125 std::list< KeyFrame<V> > _path;
00126
00127 };
00128
00129
00130 class OpParDouble : public OpParameter<double>
00131 {
00132 public:
00133 OpParDouble( const std::string &name, const double& v ) :
00134 OpParameter<double>( name, "double", v ) {};
00135
00136 virtual int fixMe( int fix_lvl=0 ) {};
00137 virtual void setParameter( const PropertyNode * p );
00138 virtual double getValue( Time t = 0 ) const;
00139
00140 };
00141
00142 class OpParString : public OpParameter<std::string>
00143 {
00144 public:
00145 OpParString( const std::string &name, const std::string& v ) :
00146 OpParameter<std::string>( name, "string", v ) {};
00147
00148 virtual int fixMe( int fix_lvl=0 ) {};
00149 virtual void setParameter( const PropertyNode * p );
00150 virtual std::string getValue( Time t = 0 ) const;
00151
00152 };
00153
00154 class OpParColor : public OpParameter<Color>
00155 {
00156 public:
00157 OpParColor( const std::string &name, const Color& v ) :
00158 OpParameter<Color>( name, "color", v ) {};
00159
00160 virtual int fixMe( int fix_lvl=0 ) {};
00161 virtual void setParameter( const PropertyNode * p );
00162 virtual Color getValue( Time t = 0 ) const;
00163
00164 };
00165
00166 class OpParBox : public OpParameter<Box>
00167 {
00168 public:
00169 OpParBox( const std::string &name, const Box& v ) :
00170 OpParameter<Box>( name, "box", v ) {};
00171
00172 virtual int fixMe( int fix_lvl=0 ) {};
00173 virtual void setParameter( const PropertyNode * p );
00174 virtual Box getValue( Time t = 0 ) const;
00175
00176 };
00177
00178
00179
00180
00181
00182
00183
00184 class Operator : public StoryElement, public PluginBase
00185 {
00186 public:
00187 Operator( const std::string & name ) :
00188 StoryElement( name ),
00189 PluginBase( )
00190 {};
00191
00192 virtual ~Operator() {}
00193
00194 void addParameter( OpParameterBase * v ) { _parameter.push_back( v ); };
00195 virtual void setParameter( const PropertyNode * p );
00196
00197 private:
00198 std::list< OpParameterBase* > _parameter;
00199 };
00200
00204 class UnaryOp : public Operator
00205 {
00206 public:
00208 UnaryOp( const std::string & name) :
00209 Operator( name )
00210 {};
00211
00213 virtual void renderFrame( Frame & f, Time t ) = 0;
00214 };
00215
00216
00220 class BinaryOp : public Operator
00221 {
00222 public:
00223 BinaryOp( const std::string & name) :
00224 Operator( name )
00225 {};
00232 virtual void renderFrame( Frame & dest, Frame & src_a,
00233 Frame & src_b, Time t ) = 0;
00234 };
00235
00236
00240 class NullUOp : public UnaryOp
00241 {
00242 public:
00243 NullUOp() : UnaryOp( "NullUOp" ) {};
00244 virtual void renderFrame( Frame & src, Time t ) {};
00245 virtual int fixMe( int fix_lvl=0 ) { return 0; }
00246 virtual PropertyNode * getProperties( PropertyNode * p=0 ) const { return 0; };
00247 };
00248
00249
00250
00254 class NullBOp : public BinaryOp
00255 {
00256 public:
00257 NullBOp() : BinaryOp( "NullBOp" ) {};
00258 virtual void renderFrame( Frame & dest, Frame & src_a, Frame & src_b, Time t ) {};
00259 virtual int fixMe( int fix_lvl=0 ) { return 0; }
00260 virtual PropertyNode * getProperties( PropertyNode * p=0 ) const { return 0; };
00261 };
00262
00263
00267 class CopyAOp : public BinaryOp
00268 {
00269 public:
00270 CopyAOp() : BinaryOp( "CopyAOp" ) {};
00271 virtual void renderFrame( Frame & dest, Frame & src_a, Frame & src_b, Time t )
00272 { dest = src_a; };
00273 virtual int fixMe( int fix_lvl=0 ) { return 0; }
00274 virtual PropertyNode * getProperties( PropertyNode * p=0 ) const { return 0; };
00275 };
00276
00277
00282 class ReplaceAudioOp : public BinaryOp
00283 {
00284 public:
00285 ReplaceAudioOp() : BinaryOp( "ReplaceAudioOp" ) {};
00286 virtual void renderFrame( Frame & dest, Frame & src_a, Frame & src_b, Time t );
00287 virtual int fixMe( int fix_lvl=0 ) { return 0; }
00288 virtual PropertyNode * getProperties( PropertyNode * p=0 ) const { return 0; };
00289 };
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00306 template<class V>
00307 bool
00308 OpParameter<V>::removeKeyFrame( Time t )
00309 {
00310 iterator i = std::find( pbegin(), pend(), t );
00311 if ( i == pend() ) {
00312 WARN( "couldn't erase, no value at "<<PRINTV(t) );
00313 return false;
00314 }
00315 if ( i== pbegin() ) {
00316 WARN( "cannot erase first value" );
00317 return false;
00318 }
00319
00320 _path.erase( i );
00321
00322 return true;
00323 }
00324
00325 template<class V>
00326 KeyFrame<V> *
00327 OpParameter<V>::makeNewKeyFrame( Time t, const V &v )
00328 {
00329 iterator i = std::find( pbegin(), pend(), t );
00330 if ( i != pend() ) {
00331 INFO( "returning existing KeyFrame" );
00332 i->value = v;
00333 return &*i;
00334 }
00335 return &*_path.insert( std::lower_bound( pbegin(), pend(), t ),
00336 KeyFrame<V>( t, v ) );
00337 }
00338
00339 template<class V>
00340 KeyFrame<V> *
00341 OpParameter<V>::getKeyFrame( Time t )
00342 {
00343 iterator i = std::find( pbegin(), pend(), t );
00344 if ( i == pend() ) return 0;
00345 return &*i;
00346 }
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369 };
00370
00371
00372 #endif