OpenWalnut  1.5.0dev
WGESwitchCallback.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WGESWITCHCALLBACK_H
26 #define WGESWITCHCALLBACK_H
27 
28 #include <osg/Node>
29 #include <osg/Switch>
30 
31 /**
32  * 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
33  * here. If a bool prop is used, the callback can trigger between child 0 and 1. Technically, WPropDouble is also possible.
34  */
35 template < typename PropType >
36 class WGESwitchCallback: public osg::NodeCallback
37 {
38 public:
39  /**
40  * Creates new instance.
41  *
42  * \param prop the property which controls switch.
43  */
44  explicit WGESwitchCallback( PropType prop );
45 
46  /**
47  * Destructor.
48  */
49  virtual ~WGESwitchCallback();
50 
51  /**
52  * This operator gets called by OSG every update cycle.
53  *
54  * \param node the osg node
55  * \param nv the node visitor
56  */
57  virtual void operator()( osg::Node* node, osg::NodeVisitor* nv );
58 
59 protected:
60 private:
61  /**
62  * The prop controlling the node switch
63  */
64  PropType m_prop;
65 };
66 
67 template < typename PropType >
69  osg::NodeCallback(),
70  m_prop( prop )
71 {
72 }
73 
74 template < typename PropType >
76 {
77  // cleanup
78 }
79 
80 template < typename PropType >
81 void WGESwitchCallback< PropType >::operator()( osg::Node* node, osg::NodeVisitor* nv )
82 {
83  // is it a switch?
84  osg::Switch* s = node->asSwitch();
85  if( !s )
86  {
87  traverse( node, nv );
88  return;
89  }
90 
91  // turn off all the others and enable the current one
92  s->setAllChildrenOff();
93  s->setSingleChildOn( m_prop->get() );
94 
95  traverse( node, nv );
96 }
97 
98 #endif // WGESWITCHCALLBACK_H
99 
This callback is able to switch a osg::Switch node using a property.
virtual ~WGESwitchCallback()
Destructor.
virtual void operator()(osg::Node *node, osg::NodeVisitor *nv)
This operator gets called by OSG every update cycle.
WGESwitchCallback(PropType prop)
Creates new instance.
PropType m_prop
The prop controlling the node switch.