Equalizer 1.0

eVolve/config.cpp

00001 
00002 /* Copyright (c) 2006-2011, Stefan Eilemann <eile@equalizergraphics.com> 
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions are met:
00006  *
00007  * - Redistributions of source code must retain the above copyright notice, this
00008  *   list of conditions and the following disclaimer.
00009  * - Redistributions in binary form must reproduce the above copyright notice,
00010  *   this list of conditions and the following disclaimer in the documentation
00011  *   and/or other materials provided with the distribution.
00012  * - Neither the name of Eyescale Software GmbH nor the names of its
00013  *   contributors may be used to endorse or promote products derived from this
00014  *   software without specific prior written permission.
00015  *
00016  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00019  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00020  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00021  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00022  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00023  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00024  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00025  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00026  * POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00029 #include "config.h"
00030 
00031 
00032 namespace eVolve
00033 {
00034 
00035 Config::Config( co::base::RefPtr< eq::Server > parent )
00036         : eq::Config( parent )
00037         , _spinX( 5 )
00038         , _spinY( 5 )
00039         , _currentCanvas( 0 )
00040         , _messageTime( 0 ){}
00041 
00042 Config::~Config(){}
00043 
00044 bool Config::init()
00045 {
00046     // init distributed objects
00047     EQCHECK( registerObject( &_frameData ));
00048 
00049     _frameData.setOrtho( _initData.getOrtho( ));
00050     _initData.setFrameDataID( _frameData.getID( ));
00051 
00052     _frameData.setAutoObsolete( getLatency( ));
00053 
00054     EQCHECK( registerObject( &_initData ));
00055 
00056     // init config
00057     if( !eq::Config::init( _initData.getID( )))
00058     {
00059         _deregisterData();
00060         return false;
00061     }
00062 
00063     const eq::Canvases& canvases = getCanvases();
00064     if( canvases.empty( ))
00065         _currentCanvas = 0;
00066     else
00067         _currentCanvas = canvases.front();
00068 
00069     _setMessage( "Welcome to eVolve\nPress F1 for help" );
00070 
00071     return true;
00072 }
00073 
00074 bool Config::mapData( const eq::uint128_t& initDataID )
00075 {
00076     if( !_initData.isAttached() )
00077     {
00078         if( !mapObject( &_initData, initDataID ))
00079             return false;
00080         unmapObject( &_initData ); // data was retrieved, unmap immediately
00081     }
00082     else  // appNode, _initData is registered already
00083     {
00084         EQASSERT( _initData.getID() == initDataID );
00085     }
00086     return true;
00087 }
00088 
00089 bool Config::exit()
00090 {
00091     const bool ret = eq::Config::exit();
00092     _deregisterData();
00093 
00094     return ret;
00095 }
00096 
00097 void Config::_deregisterData()
00098 {
00099     deregisterObject( &_initData );
00100     deregisterObject( &_frameData );
00101 
00102     _initData.setFrameDataID( co::base::UUID::ZERO );
00103 }
00104 
00105 
00106 uint32_t Config::startFrame()
00107 {
00108     // update database
00109     _frameData.spinCamera( -0.001f * _spinX, -0.001f * _spinY );
00110     const co::base::uint128_t& version = _frameData.commit();
00111 
00112     _resetMessage();
00113 
00114     return eq::Config::startFrame( version );
00115 }
00116 
00117 void Config::_resetMessage()
00118 {
00119     // reset message after two seconds
00120     if( _messageTime > 0 && getTime() - _messageTime > 2000 )
00121     {
00122         _messageTime = 0;
00123         _frameData.setMessage( "" );
00124     }
00125 }
00126 
00127 bool Config::handleEvent( const eq::ConfigEvent* event )
00128 {
00129     switch( event->data.type )
00130     {
00131         case eq::Event::KEY_PRESS:
00132             if( _handleKeyEvent( event->data.keyPress ))
00133                 return true;
00134             break;
00135 
00136         case eq::Event::CHANNEL_POINTER_BUTTON_PRESS:
00137         {
00138             const co::base::UUID& viewID = event->data.context.view.identifier;
00139             _frameData.setCurrentViewID( viewID );
00140             if( viewID == co::base::UUID::ZERO )
00141             {
00142                 _currentCanvas = 0;
00143                 return true;
00144             }
00145             
00146             const eq::View* view = find< eq::View >( viewID );
00147             const eq::Layout* layout = view->getLayout();
00148             const eq::Canvases& canvases = getCanvases();
00149             for( eq::Canvases::const_iterator i = canvases.begin();
00150                  i != canvases.end(); ++i )
00151             {
00152                 eq::Canvas* canvas = *i;
00153                 const eq::Layout* canvasLayout = canvas->getActiveLayout();
00154 
00155                 if( canvasLayout == layout )
00156                 {
00157                     _currentCanvas = canvas;
00158                     return true;
00159                 }
00160             }
00161             return true;
00162         }
00163 
00164         case eq::Event::CHANNEL_POINTER_BUTTON_RELEASE:
00165             if( event->data.pointerButtonRelease.buttons == eq::PTR_BUTTON_NONE
00166                 && event->data.pointerButtonRelease.button  == eq::PTR_BUTTON1 )
00167             {
00168                 _spinY = event->data.pointerButtonRelease.dx;
00169                 _spinX = event->data.pointerButtonRelease.dy;
00170             }
00171             return true;
00172 
00173         case eq::Event::CHANNEL_POINTER_MOTION:
00174             if( event->data.pointerMotion.buttons == eq::PTR_BUTTON_NONE )
00175                 return true;
00176 
00177             if( event->data.pointerMotion.buttons == eq::PTR_BUTTON1 )
00178             {
00179                 _spinX = 0;
00180                 _spinY = 0;
00181 
00182                 _frameData.spinCamera(  -0.005f * event->data.pointerMotion.dy,
00183                                         -0.005f * event->data.pointerMotion.dx);
00184             }
00185             else if( event->data.pointerMotion.buttons == eq::PTR_BUTTON2 ||
00186                      event->data.pointerMotion.buttons == ( eq::PTR_BUTTON1 |
00187                                                        eq::PTR_BUTTON3 ))
00188             {
00189                 _frameData.moveCamera( .0, .0,
00190                                         .005f*event->data.pointerMotion.dy);
00191             }
00192             else if( event->data.pointerMotion.buttons == eq::PTR_BUTTON3 )
00193             {
00194                 _frameData.moveCamera( .0005f * event->data.pointerMotion.dx,
00195                                       -.0005f * event->data.pointerMotion.dy, 
00196                                        .0 );
00197             }
00198             return true;
00199 
00200         default:
00201             break;
00202     }
00203     return eq::Config::handleEvent( event );
00204 }
00205 
00206 bool Config::_handleKeyEvent( const eq::KeyEvent& event )
00207 {
00208     switch( event.key )
00209     {
00210         case eq::KC_F1:
00211         case 'h':
00212         case 'H':
00213             _frameData.toggleHelp();
00214             return true;
00215 
00216         case 'r':
00217         case 'R':
00218         case ' ':
00219             _spinX = 0;
00220             _spinY = 0;
00221             _frameData.reset();
00222             return true;
00223 
00224         case 'o':
00225         case 'O':
00226             _frameData.toggleOrtho();
00227             return true;
00228 
00229         case 's':
00230         case 'S':
00231         _frameData.toggleStatistics();
00232             return true;
00233 
00234         case 'l':
00235             _switchLayout( 1 );
00236             return true;
00237         case 'L':
00238             _switchLayout( -1 );
00239             return true;
00240 
00241         case 'q':
00242             _frameData.adjustQuality( -.1f );
00243             return true;
00244 
00245         case 'Q':
00246             _frameData.adjustQuality( .1f );
00247             return true;
00248 
00249         case 'c':
00250         case 'C':
00251         {
00252             const eq::Canvases& canvases = getCanvases();
00253             if( canvases.empty( ))
00254                 return true;
00255 
00256             _frameData.setCurrentViewID( co::base::UUID::ZERO );
00257 
00258             if( !_currentCanvas )
00259             {
00260                 _currentCanvas = canvases.front();
00261                 return true;
00262             }
00263 
00264             eq::Canvases::const_iterator i = std::find( canvases.begin(),
00265                                                         canvases.end(),
00266                                                         _currentCanvas );
00267             EQASSERT( i != canvases.end( ));
00268 
00269             ++i;
00270             if( i == canvases.end( ))
00271                 _currentCanvas = canvases.front();
00272             else
00273                 _currentCanvas = *i;
00274             return true;
00275         }
00276 
00277         case 'v':
00278         case 'V':
00279         {
00280             const eq::Canvases& canvases = getCanvases();
00281             if( !_currentCanvas && !canvases.empty( ))
00282                 _currentCanvas = canvases.front();
00283 
00284             if( !_currentCanvas )
00285                 return true;
00286 
00287             const eq::Layout* layout = _currentCanvas->getActiveLayout();
00288             if( !layout )
00289                 return true;
00290 
00291             const eq::View* current = 
00292                               find< eq::View >( _frameData.getCurrentViewID( ));
00293 
00294             const eq::Views& views = layout->getViews();
00295             EQASSERT( !views.empty( ))
00296 
00297             if( !current )
00298             {
00299                 _frameData.setCurrentViewID( views.front()->getID( ));
00300                 return true;
00301             }
00302 
00303             eq::Views::const_iterator i = std::find( views.begin(),
00304                                                           views.end(),
00305                                                           current );
00306             EQASSERT( i != views.end( ));
00307 
00308             ++i;
00309             if( i == views.end( ))
00310                 _frameData.setCurrentViewID( co::base::UUID::ZERO );
00311             else
00312                 _frameData.setCurrentViewID( (*i)->getID( ));
00313             return true;
00314         }
00315 
00316         default:
00317             break;
00318     }
00319     return false;
00320 }
00321 
00322 void Config::_setMessage( const std::string& message )
00323 {
00324     _frameData.setMessage( message );
00325     _messageTime = getTime();
00326 }
00327 
00328 void Config::_switchLayout( int32_t increment )
00329 {
00330     if( !_currentCanvas )
00331         return;
00332 
00333     _frameData.setCurrentViewID( co::base::UUID::ZERO );
00334 
00335     size_t index = _currentCanvas->getActiveLayoutIndex() + increment;
00336     const eq::Layouts& layouts = _currentCanvas->getLayouts();
00337     EQASSERT( !layouts.empty( ));
00338 
00339     index = ( index % layouts.size( ));
00340     _currentCanvas->useLayout( uint32_t( index ));
00341 
00342     const eq::Layout* layout = _currentCanvas->getActiveLayout();
00343     std::ostringstream stream;
00344     stream << "Layout ";
00345     if( layout )
00346     {
00347         const std::string& name = layout->getName();
00348         if( name.empty( ))
00349             stream << index;
00350         else
00351             stream << name;
00352     }
00353     else
00354         stream << "NONE";
00355 
00356     stream << " active";
00357     _setMessage( stream.str( ));
00358 }
00359 
00360 }
Generated on Sun May 8 2011 19:11:05 for Equalizer 1.0 by  doxygen 1.7.3