TuttleOFX  1
WarpOverlayInteract.cpp
Go to the documentation of this file.
00001 #include "WarpOverlayInteract.hpp"
00002 #include "WarpDefinitions.hpp"
00003 #include "WarpPlugin.hpp"
00004 #include "Bezier/bezier.hpp"
00005 
00006 #include <tuttle/plugin/opengl/gl.h>
00007 #include <tuttle/plugin/interact/interact.hpp>
00008 #include <tuttle/plugin/interact/overlay.hpp>
00009 #include <tuttle/plugin/interact/ParamPoint.hpp>
00010 #include <tuttle/plugin/interact/ParamTangent.hpp>
00011 #include <tuttle/plugin/interact/ParamPointRelativePoint.hpp>
00012 
00013 #include <ofxsImageEffect.h>
00014 #include <ofxsInteract.h>
00015 #include <boost/gil/gil_all.hpp>
00016 
00017 #include <vector>
00018 
00019 namespace tuttle {
00020 namespace plugin {
00021 namespace warp {
00022 
00023 bool longDown = false;
00024 
00025 WarpOverlayInteract::WarpOverlayInteract( OfxInteractHandle handle, OFX::ImageEffect* effect )
00026 : OFX::OverlayInteract( handle )
00027 , _infos( effect )
00028 , _interactScene( *effect, _infos )
00029 {
00030         _effect = effect;
00031         _plugin = static_cast<WarpPlugin*> ( _effect );
00032 
00033         //Points out
00034         for( std::size_t i = 0; i < kMaxNbPoints; ++i )
00035         {
00036 //              interact::PointInteract* point = new interact::ParamPoint<interact::FrameClip, eCoordinateSystemXY > ( _infos, _plugin->_paramPointOut[i], _plugin->_clipSrc );
00037 
00038                 typedef interact::ParamPoint<interact::FrameClip, eCoordinateSystemXY> PPoint;
00039                 typedef interact::ParamTangent<interact::FrameClip, eCoordinateSystemXY> PTangent;
00040 
00041                 //Points In
00042                 interact::AndActiveFunctor<>* activeIn = new interact::AndActiveFunctor<>( );
00043                 activeIn->push_back( new interact::IsActiveBooleanParamFunctor<>( _plugin->_paramOverlayIn ) );
00044                 activeIn->push_back( new interact::IsNotSecretParamFunctor<>( _plugin->_paramPointIn[i] ) );
00045 
00046                 _interactScene.push_back( new PTangent( _infos,
00047                                                                                                 _plugin->_paramPointIn[i],
00048                                                                                                 _plugin->_paramPointTgtIn[i * 2],
00049                                                                                                 _plugin->_paramPointTgtIn[i * 2 + 1],
00050                                                                                                 _plugin->_clipSrc ),
00051                                                                   activeIn,
00052                                                                   new interact::ColorRGBParam( _plugin->_paramOverlayInColor ) );
00053 
00054                 //Points out
00055                 interact::AndActiveFunctor<>* activeOut = new interact::AndActiveFunctor<>( );
00056                 activeOut->push_back( new interact::IsActiveBooleanParamFunctor<>( _plugin->_paramOverlayOut ) );
00057                 activeOut->push_back( new interact::IsNotSecretParamFunctor<>( _plugin->_paramPointOut[i] ) );
00058 
00059                 _interactScene.push_back( new PTangent( _infos,
00060                                                                                                 _plugin->_paramPointOut[i],
00061                                                                                                 _plugin->_paramPointTgtOut[i * 2],
00062                                                                                                 _plugin->_paramPointTgtOut[i * 2 + 1],
00063                                                                                                 _plugin->_clipSrc ),
00064                                                                   activeOut,
00065                                                                   new interact::ColorRGBParam( _plugin->_paramOverlayOutColor ) );
00066         }
00067 }
00068 
00069 bool WarpOverlayInteract::draw( const OFX::DrawArgs& args )
00070 {
00071         if( !_plugin->_paramOverlay->getValue( ) || !_plugin->_clipSrc->isConnected( ) )
00072                 return false;
00073 
00074         typedef boost::gil::point2<Scalar> Point2;
00075         bool displaySomething = false;
00076 
00077         //Recuperation du nombre de points positionnés
00078         const std::size_t nbPoints = _plugin->_paramNbPoints->getValue( );
00079 
00080         if( nbPoints > 1 )
00081         {
00082                 for( std::size_t c = 0; c < nbPoints - 1; ++c )
00083                 {
00084                         if( _plugin->_paramCurveBegin[c + 1]->getValue( ) )
00085                                 continue;
00086                         // Création des points et des ptTangente et recupération des valeurs
00087                         //points à relier
00088                         OfxPointD pIn1 = _plugin->_paramPointIn[c]->getValue( );
00089                         OfxPointD pIn2 = _plugin->_paramPointIn[c + 1]->getValue( );
00090 
00091                         OfxPointD pOut1 = _plugin->_paramPointOut[c]->getValue( );
00092                         OfxPointD pOut2 = _plugin->_paramPointOut[c + 1]->getValue( );
00093 
00094                         //Points de la tangente
00095                         if( longDown == false )
00096                         {
00097                                 OfxPointD tIn1 = _plugin->_paramPointTgtIn[( 2 * c ) + 1]->getValue( );
00098                                 OfxPointD tIn2 = _plugin->_paramPointTgtIn[( 2 * c ) + 2]->getValue( );
00099 
00100                                 OfxPointD tOut1 = _plugin->_paramPointTgtOut[( 2 * c ) + 1]->getValue( );
00101                                 OfxPointD tOut2 = _plugin->_paramPointTgtOut[( 2 * c ) + 2]->getValue( );
00102 
00103 
00104                                 //Création et remplissage du tableau necessaire à Bezier
00105                                 std::vector< point2<double> > tabPtsIn;
00106                                 tabPtsIn.push_back( point2<double>( pIn1.x, pIn1.y ) );
00107                                 tabPtsIn.push_back( point2<double>( tIn1.x, tIn1.y ) );
00108                                 tabPtsIn.push_back( point2<double>( tIn2.x, tIn2.y ) );
00109                                 tabPtsIn.push_back( point2<double>( pIn2.x, pIn2.y ) );
00110 
00111                                 std::vector< point2<double> > tabPtsOut;
00112                                 tabPtsOut.push_back( point2<double>( pOut1.x, pOut1.y ) );
00113                                 tabPtsOut.push_back( point2<double>( tOut1.x, tOut1.y ) );
00114                                 tabPtsOut.push_back( point2<double>( tOut2.x, tOut2.y ) );
00115                                 tabPtsOut.push_back( point2<double>( pOut2.x, pOut2.y ) );
00116 
00117                                 //Choix de la couleur des tangentes dans nuke
00118                                 double rIn, vIn, bIn;
00119                                 _plugin->_paramOverlayTgtInColor->getValue( rIn, vIn, bIn );
00120                                 glColor3f( rIn, vIn, bIn );
00121 
00122                                 //Si "overlay tangente" est sur afficher
00123                                 if( _plugin->_paramOverlayTgtIn->getValue( ) )
00124                                         bezier::drawBezier( tabPtsIn, _plugin->_paramNbPointsBezier->getValue( ), rIn, vIn, bIn );
00125 
00126                                 //Choix de la couleur des tangentes dans nuke
00127                                 double rOut, vOut, bOut;
00128                                 _plugin->_paramOverlayTgtOutColor->getValue( rOut, vOut, bOut );
00129                                 glColor3f( rOut, vOut, bOut );
00130 
00131                                 //Si "overlay tangente" est sur afficher
00132                                 if( _plugin->_paramOverlayTgtOut->getValue( ) )
00133                                         bezier::drawBezier( tabPtsOut, _plugin->_paramNbPointsBezier->getValue( ), rOut, vOut, bOut );
00134                         }
00135                 }
00136         }
00137         displaySomething |= _interactScene.draw( args );
00138 
00139         return displaySomething;
00140 }
00141 
00142 bool WarpOverlayInteract::penMotion( const OFX::PenArgs& args )
00143 {
00144         const std::size_t numPt = _plugin->_paramNbPoints->getValue( );
00145 
00146         if( _plugin->_paramMethod->getValue( ) == eParamMethodCreation )
00147         {
00148                 if( longDown == true )
00149                 {
00150                         // Tangente In
00151                         // Point courant
00152                         const point2<double> ptCurIn( ofxToGil( _plugin->_paramPointIn[numPt - 1]->getValue( ) ) );
00153                         // Point numéro 1 de la tangente
00154                         _plugin->_paramPointTgtIn[2 * ( numPt - 1 )]->setIsSecretAndDisabled( false );
00155                         _plugin->_paramPointTgtIn[2 * ( numPt - 1 )]->setValue( args.penPosition.x, args.penPosition.y );
00156                         // Point numéro 2 de la tangente
00157                         _plugin->_paramPointTgtIn[2 * ( numPt - 1 ) + 1]->setIsSecretAndDisabled( false );
00158                         _plugin->_paramPointTgtIn[2 * ( numPt - 1 ) + 1]->setValue( 2 * ptCurIn.x - args.penPosition.x, 2 * ptCurIn.y - args.penPosition.y );
00159 
00160                         // Tangente Out
00161                         // Point courant
00162                         const point2<double> ptCurOut( ofxToGil( _plugin->_paramPointOut[numPt - 1]->getValue( ) ) );
00163                         // Point numéro 1 de la tangente
00164                         _plugin->_paramPointTgtOut[2 * ( numPt - 1 )]->setIsSecretAndDisabled( false );
00165                         _plugin->_paramPointTgtOut[2 * ( numPt - 1 )]->setValue( args.penPosition.x, args.penPosition.y );
00166                         // Point numéro 2 de la tangente
00167                         _plugin->_paramPointTgtOut[2 * ( numPt - 1 ) + 1]->setIsSecretAndDisabled( false );
00168                         _plugin->_paramPointTgtOut[2 * ( numPt - 1 ) + 1]->setValue( 2 * ptCurOut.x - args.penPosition.x, 2 * ptCurOut.y - args.penPosition.y );
00169                 }
00170                 return _interactScene.penUp( args );
00171         }
00172         else if( ( numPt < kMaxNbPoints ) && ( _plugin->_paramMethod->getValue( ) == eParamMethodMove ) )
00173                 return _interactScene.penMotion( args );
00174 
00175         return false;
00176 }
00177 
00178 bool WarpOverlayInteract::penDown( const OFX::PenArgs& args )
00179 {
00180         const std::size_t nbPoints = _plugin->_paramNbPoints->getValue( );
00181 
00182         //S'il reste des points à placer et si le mode est "creation"
00183         if( ( nbPoints < kMaxNbPoints ) && ( _plugin->_paramMethod->getValue( ) == eParamMethodCreation ) )
00184         {
00185                 longDown = true;
00186 
00187                 _plugin->_paramPointIn[nbPoints]->setIsSecretAndDisabled( false );
00188                 _plugin->_paramPointIn[nbPoints]->setValue( args.penPosition.x, args.penPosition.y );
00189 
00190                 _plugin->_paramPointOut[nbPoints]->setIsSecretAndDisabled( false );
00191                 _plugin->_paramPointOut[nbPoints]->setValue( args.penPosition.x, args.penPosition.y );
00192 
00193                 _plugin->_paramNbPoints->setValue( nbPoints + 1 );
00194 
00195                 return _interactScene.penDown( args );
00196         }
00197 
00198                 //Si le mode est "move"
00199         else if( _plugin->_paramMethod->getValue( ) == eParamMethodMove )
00200         {
00201                 return _interactScene.penDown( args );
00202         }
00203                 //Si le mode est delete
00204         else if( _plugin->_paramMethod->getValue( ) == eParamMethodDelete )
00205         {
00206                 for( unsigned int i = 0; i < nbPoints; ++i )
00207                 {
00208                         if( ( abs( args.penPosition.x ) - ( _plugin->_paramPointIn[i]->getValue( ).x ) < seuil
00209                                   && abs( ( args.penPosition.y ) - ( _plugin->_paramPointIn[i]->getValue( ).y ) ) < seuil )
00210                                 || ( abs( args.penPosition.x ) - ( _plugin->_paramPointOut[i]->getValue( ).x ) < seuil
00211                                          && abs( ( args.penPosition.y ) - ( _plugin->_paramPointOut[i]->getValue( ).y ) ) < seuil ) )
00212                         {
00213                                 if( i < nbPoints - 1 )
00214                                 {
00215                                         for( std::size_t ptSuivants = i; ptSuivants < nbPoints; ++ptSuivants )
00216                                         {
00217                                                 _plugin->_paramPointIn[ptSuivants]->setValue( _plugin->_paramPointIn[ptSuivants + 1]->getValue( ) );
00218                                                 _plugin->_paramPointOut[ptSuivants]->setValue( _plugin->_paramPointOut[ptSuivants + 1]->getValue( ) );
00219 
00220                                                 _plugin->_paramPointTgtIn[2 * ptSuivants]->setValue( _plugin->_paramPointTgtIn[2 * ptSuivants + 2]->getValue( ) );
00221                                                 _plugin->_paramPointTgtIn[2 * ptSuivants + 1]->setValue( _plugin->_paramPointTgtIn[2 * ptSuivants + 3]->getValue( ) );
00222                                                 _plugin->_paramPointTgtOut[2 * ptSuivants]->setValue( _plugin->_paramPointTgtOut[2 * ptSuivants + 2]->getValue( ) );
00223                                                 _plugin->_paramPointTgtOut[2 * ptSuivants + 1]->setValue( _plugin->_paramPointTgtOut[2 * ptSuivants + 3]->getValue( ) );
00224                                         }
00225                                 }
00226 
00227                                 _plugin->_paramPointIn[nbPoints - 1]->setIsSecretAndDisabled( true );
00228                                 _plugin->_paramPointOut[nbPoints - 1]->setIsSecretAndDisabled( true );
00229                                 _plugin->_paramPointTgtIn[2 * nbPoints - 2]->setIsSecretAndDisabled( true );
00230                                 _plugin->_paramPointTgtIn[2 * nbPoints - 1]->setIsSecretAndDisabled( true );
00231                                 _plugin->_paramPointTgtOut[2 * nbPoints - 2]->setIsSecretAndDisabled( true );
00232                                 _plugin->_paramPointTgtOut[2 * nbPoints - 1]->setIsSecretAndDisabled( true );
00233 
00234                                 _plugin->_paramNbPoints->setValue( nbPoints - 1 );
00235                         }
00236                 }
00237         }
00238         return false;
00239 }
00240 
00241 bool WarpOverlayInteract::keyDown( const OFX::KeyArgs& args )
00242 {
00243         if( args.keySymbol == kOfxKey_space )
00244                 TUTTLE_LOG_DEBUG( TUTTLE_INFO, "test" );
00245         else
00246                 TUTTLE_LOG_DEBUG( TUTTLE_INFO, "test failed" );
00247         return false;
00248 }
00249 
00250 bool WarpOverlayInteract::penUp( const OFX::PenArgs& args )
00251 {
00252         const std::size_t nbPoints = _plugin->_paramNbPoints->getValue( );
00253 
00254         if( ( nbPoints < kMaxNbPoints ) && ( _plugin->_paramMethod->getValue( ) == eParamMethodCreation ) )
00255         {
00256                 longDown = false;
00257                 return _interactScene.penUp( args );
00258         }
00259         else if( _plugin->_paramMethod->getValue( ) == eParamMethodMove )
00260                 return _interactScene.penUp( args );
00261 
00262         return false;
00263 }
00264 
00265 }
00266 }
00267 }