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