OpenWalnut  1.4.0
WGEShaderPropertyDefineOptions.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 WGESHADERPROPERTYDEFINEOPTIONS_H
26 #define WGESHADERPROPERTYDEFINEOPTIONS_H
27 
28 #include <string>
29 #include <vector>
30 
31 #ifndef Q_MOC_RUN
32 #include <boost/shared_ptr.hpp>
33 #endif
34 #ifndef Q_MOC_RUN
35 #include "boost/tuple/tuple.hpp"
36 #endif
37 #ifndef Q_MOC_RUN
38 #include <boost/signals2.hpp>
39 #endif
40 
41 #include "../../common/WProperties.h"
42 #include "../../common/WPropertyTypes.h"
43 #include "../../common/exceptions/WPreconditionNotMet.h"
44 
45 #include "WGEShaderDefineOptions.h"
46 
47 template< typename PropType >
49 
50 
51 /**
52  * This is a WGEShaderDefineOptions class which additionally uses a property to automatically control the active options. This is very useful if
53  * you have some WPropInt or WPropSelection which controls some features in your shader. Especially with WPropSelection Instances, you can even
54  * activate multiple options if your selection allows this ( see WPropertyVariable<>::PropertyConstraint for details ). If used with a WPropBool,
55  * it is useful to switch on/off an option for example.
56  *
57  * \note You can use inherited WGEShaderDefineOptions methods too. This might create some kind of inconsistency since they of course do not
58  * update the property.
59  */
60 template< typename PropType = WPropSelection, typename PropIndexAdapter = WGEShaderPropertyDefineOptionsIndexAdapter< PropType > >
62 {
63 public:
64  /**
65  * Convenience typedef for a boost_shared_ptr< WGEShaderPropertyDefineOptions >.
66  */
67  typedef boost::shared_ptr< WGEShaderPropertyDefineOptions > SPtr;
68 
69  /**
70  * Convenience typedef for a boost_shared_ptr< const WGEShaderPropertyDefineOptions >.
71  */
72  typedef boost::shared_ptr< const WGEShaderPropertyDefineOptions > ConstSPtr;
73 
74  /**
75  * Create a new instance of this class. The first option is mandatory and is set as default. The specified property controls the activations.
76  *
77  * \param prop the property controlling this thing.
78  *
79  * \param first fist option. Is default.
80  * \param option2 another option
81  * \param option3 another option
82  * \param option4 another option
83  * \param option5 another option
84  * \param option6 another option
85  * \param option7 another option
86  * \param option8 another option
87  * \param option9 another option
88  * \param option10 another option
89  */
90  WGEShaderPropertyDefineOptions( PropType prop, std::string first,
91  std::string option2 = "", std::string option3 = "", std::string option4 = "", std::string option5 = "",
92  std::string option6 = "", std::string option7 = "", std::string option8 = "", std::string option9 = "",
93  std::string option10 = "" );
94 
95  /**
96  * Create a new instance of this class. The first option is mandatory and is set as default. The specified property controls the activations.
97  *
98  * \param prop the property controlling this thing.
99  *
100  * \param options the list of options. Must have a size greater 0.
101  */
102  WGEShaderPropertyDefineOptions( PropType prop, std::vector< std::string > options );
103 
104  /**
105  * Destructor.
106  */
108 
109  /**
110  * Returns the property associated with this instance.
111  *
112  * \return
113  */
114  PropType getProperty() const;
115 
116 protected:
117 private:
118  /**
119  * The property controlling this instance and the active options list.
120  */
121  PropType m_property;
122 
123  /**
124  * The connection associated with the properties update condition.
125  */
126  boost::signals2::connection m_connection;
127 
128  /**
129  * Called by the property update mechanism. This handles the new value in the property.
130  */
131  void propUpdated();
132 };
133 
134 /**
135  * Contains some utility functions related to the WGEShaderPropertyDefineOptions class.
136  */
138 {
139  /**
140  * This tuple contains name, description and define-name of an option.
141  */
142  typedef boost::tuple< std::string, std::string, std::string > NameDescriptionDefineTuple;
143 
144  /**
145  * A little bit more comfortable way to create a list of shader-defines and the corresponding property.
146  *
147  * \param propName the name of the property to create
148  * \param propDescription the description of the property to create
149  * \param propGroup the owning group of the property
150  * \param defines the list of names, descriptions and defines
151  *
152  * \return a WGEShaderPropertyDefineOptions instance associated with a new property. This can be acquired using getProperty().
153  */
154  WGEShaderPropertyDefineOptions< WPropSelection >::SPtr createSelection( std::string propName, std::string propDescription,
155  WProperties::SPtr propGroup,
156  std::vector< NameDescriptionDefineTuple > defines );
157 }
158 
159 /**
160  * Class converts the specified property value to an index list. The generic case for all int-castable property types is trivial. WPropSelection
161  * is a specialization of this class.
162  *
163  * \tparam PropType The property. WPropInt for example.
164  */
165 template< typename PropType >
167 {
168 public:
169  /**
170  * The type of the index-list to create.
171  */
173 
174  /**
175  * Converts the specified property value to an index list.
176  *
177  * \param in the value to convert to an index list
178  *
179  * \return the new index list
180  */
181  IdxList operator()( const typename PropType::element_type::ValueType& in ) const
182  {
183  return IdxList( 1, typename IdxList::value_type( in ) );
184  }
185 };
186 
187 /**
188  * Class converts the specified property value to an index list. The generic case for all int-castable property types is trivial. This is the
189  * specialization for WPropSelection which allows multiple options to be active if the selection has multiple selected items.
190  *
191  * \tparam PropType The property. WPropInt for example.
192  */
193 template<>
195 {
196 public:
197  /**
198  * The type of the index-list to create.
199  */
201 
202  /**
203  * Converts the specified property value to an index list.
204  *
205  * \param in the value to convert to an index list
206  *
207  * \return the new index list
208  */
209  IdxList operator()( const WPVBaseTypes::PV_SELECTION& in ) const
210  {
211  return in.getIndexList();
212  }
213 };
214 
215 template< typename PropType, typename PropIndexAdapter >
217  std::string option2, std::string option3, std::string option4, std::string option5,
218  std::string option6, std::string option7, std::string option8, std::string option9,
219  std::string option10 ):
220  WGEShaderDefineOptions( first, option2, option3, option4, option5, option6, option7, option8, option9, option10 ),
221  m_property( prop )
222 {
223  // if the prop changes -> update options
224  m_connection = m_property->getValueChangeCondition()->subscribeSignal(
226  );
227  propUpdated();
228 }
229 
230 template< typename PropType, typename PropIndexAdapter >
232  WGEShaderDefineOptions( options ),
233  m_property( prop )
234 {
235  // if the prop changes -> update options
236  m_connection = m_property->getValueChangeCondition()->subscribeSignal(
238  );
239  propUpdated();
240 }
241 
242 template< typename PropType, typename PropIndexAdapter >
244 {
245  // cleanup
246  m_connection.disconnect();
247 }
248 
249 template< typename PropType, typename PropIndexAdapter >
251 {
252  PropIndexAdapter functor;
253  setActivationList( functor( m_property->get() ) );
254 }
255 
256 template< typename PropType, typename PropIndexAdapter >
258 {
259  return m_property;
260 }
261 
262 #endif // WGESHADERPROPERTYDEFINEOPTIONS_H
263 
void propUpdated()
Called by the property update mechanism.
IndexList getIndexList() const
Casts the selector to a list of indices currently selected.
This is a WGEShaderDefineOptions class which additionally uses a property to automatically control th...
boost::shared_ptr< const WGEShaderPropertyDefineOptions > ConstSPtr
Convenience typedef for a boost_shared_ptr< const WGEShaderPropertyDefineOptions >.
boost::signals2::connection m_connection
The connection associated with the properties update condition.
WGEShaderPropertyDefineOptions(PropType prop, std::string first, std::string option2="", std::string option3="", std::string option4="", std::string option5="", std::string option6="", std::string option7="", std::string option8="", std::string option9="", std::string option10="")
Create a new instance of this class.
IdxList operator()(const typename PropType::element_type::ValueType &in) const
Converts the specified property value to an index list.
IdxList operator()(const WPVBaseTypes::PV_SELECTION &in) const
Converts the specified property value to an index list.
Contains some utility functions related to the WGEShaderPropertyDefineOptions class.
PropType m_property
The property controlling this instance and the active options list.
WGEShaderPropertyDefineOptions< PropType >::IdxList IdxList
The type of the index-list to create.
WGEShaderPropertyDefineOptions< WPropSelection >::IdxList IdxList
The type of the index-list to create.
This class represents a subset of a WItemSelection.
Definition: WItemSelector.h:56
boost::tuple< std::string, std::string, std::string > NameDescriptionDefineTuple
This tuple contains name, description and define-name of an option.
boost::shared_ptr< WPropertyGroup > SPtr
shared pointer to object of this type
This GLSL preprocessor is able to set one define from a list of defines depending on the active optio...
Class converts the specified property value to an index list.
std::vector< size_t > IdxList
The type of the index list.
PropType getProperty() const
Returns the property associated with this instance.
WGEShaderPropertyDefineOptions< WPropSelection >::SPtr createSelection(std::string propName, std::string propDescription, WProperties::SPtr propGroup, std::vector< NameDescriptionDefineTuple > defines)
A little bit more comfortable way to create a list of shader-defines and the corresponding property...
boost::shared_ptr< WGEShaderPropertyDefineOptions > SPtr
Convenience typedef for a boost_shared_ptr< WGEShaderPropertyDefineOptions >.