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