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