OpenWalnut  1.4.0
WGESwitchCallback.h
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #ifndef WGESWITCHCALLBACK_H
00026 #define WGESWITCHCALLBACK_H
00027 
00028 #include <osg/Node>
00029 #include <osg/Switch>
00030 
00031 /**
00032  * This callback is able to switch a osg::Switch node using a property. Although this callback is a template, only int and bool props are useful
00033  * here. If a bool prop is used, the callback can trigger between child 0 and 1. Technically, WPropDouble is also possible.
00034  */
00035 template < typename PropType >
00036 class WGESwitchCallback: public osg::NodeCallback
00037 {
00038 public:
00039     /**
00040      * Creates new instance.
00041      *
00042      * \param prop the property which controls switch.
00043      */
00044     explicit WGESwitchCallback( PropType prop );
00045 
00046     /**
00047      * Destructor.
00048      */
00049     virtual ~WGESwitchCallback();
00050 
00051      /**
00052      * This operator gets called by OSG every update cycle.
00053      *
00054      * \param node the osg node
00055      * \param nv the node visitor
00056      */
00057     virtual void operator()( osg::Node* node, osg::NodeVisitor* nv );
00058 
00059 protected:
00060 private:
00061     /**
00062      * The prop controlling the node switch
00063      */
00064      PropType m_prop;
00065 };
00066 
00067 template < typename PropType >
00068 WGESwitchCallback< PropType >::WGESwitchCallback( PropType prop ):
00069     osg::NodeCallback(),
00070     m_prop( prop )
00071 {
00072 }
00073 
00074 template < typename PropType >
00075 WGESwitchCallback< PropType >::~WGESwitchCallback()
00076 {
00077     // cleanup
00078 }
00079 
00080 template < typename PropType >
00081 void WGESwitchCallback< PropType >::operator()( osg::Node* node, osg::NodeVisitor* nv )
00082 {
00083     // is it a switch?
00084     osg::Switch* s = node->asSwitch();
00085     if( !s )
00086     {
00087         traverse( node, nv );
00088         return;
00089     }
00090 
00091     // turn off all the others and enable the current one
00092     s->setAllChildrenOff();
00093     s->setSingleChildOn( m_prop->get() );
00094 
00095     traverse( node, nv );
00096 }
00097 
00098 #endif  // WGESWITCHCALLBACK_H
00099