OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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  /**
60  * Convenience typedef for a boost_shared_ptr< WGEShaderPropertyDefineOptions >.
61  */
62  typedef boost::shared_ptr< WGEShaderPropertyDefineOptions > SPtr;
63 
64  /**
65  * Convenience typedef for a boost_shared_ptr< const WGEShaderPropertyDefineOptions >.
66  */
67  typedef boost::shared_ptr< const WGEShaderPropertyDefineOptions > ConstSPtr;
68 
69  /**
70  * Create a new instance of this class. The first option is mandatory and is set as default. The specified property controls the activations.
71  *
72  * \param prop the property controlling this thing.
73  *
74  * \param first fist option. Is default.
75  * \param option2 another option
76  * \param option3 another option
77  * \param option4 another option
78  * \param option5 another option
79  * \param option6 another option
80  * \param option7 another option
81  * \param option8 another option
82  * \param option9 another option
83  * \param option10 another option
84  */
85  WGEShaderPropertyDefineOptions( PropType prop, std::string first,
86  std::string option2 = "", std::string option3 = "", std::string option4 = "", std::string option5 = "",
87  std::string option6 = "", std::string option7 = "", std::string option8 = "", std::string option9 = "",
88  std::string option10 = "" );
89 
90  /**
91  * Create a new instance of this class. The first option is mandatory and is set as default. The specified property controls the activations.
92  *
93  * \param prop the property controlling this thing.
94  *
95  * \param options the list of options. Must have a size greater 0.
96  */
97  WGEShaderPropertyDefineOptions( PropType prop, std::vector< std::string > options );
98 
99  /**
100  * Destructor.
101  */
103 
104  /**
105  * Returns the property associated with this instance.
106  *
107  * \return
108  */
109  PropType getProperty() const;
110 
111 protected:
112 
113 private:
114 
115  /**
116  * The property controlling this instance and the active options list.
117  */
118  PropType m_property;
119 
120  /**
121  * The connection associated with the properties update condition.
122  */
123  boost::signals2::connection m_connection;
124 
125  /**
126  * Called by the property update mechanism. This handles the new value in the property.
127  */
128  void propUpdated();
129 };
130 
131 /**
132  * Contains some utility functions related to the WGEShaderPropertyDefineOptions class.
133  */
134 namespace WGEShaderPropertyDefineOptionsTools
135 {
136  /**
137  * This tuple contains name, description and define-name of an option.
138  */
139  typedef boost::tuple< std::string, std::string, std::string > NameDescriptionDefineTuple;
140 
141  /**
142  * A little bit more comfortable way to create a list of shader-defines and the corresponding property.
143  *
144  * \param propName the name of the property to create
145  * \param propDescription the description of the property to create
146  * \param propGroup the owning group of the property
147  * \param defines the list of names, descriptions and defines
148  *
149  * \return a WGEShaderPropertyDefineOptions instance associated with a new property. This can be acquired using getProperty().
150  */
151  WGEShaderPropertyDefineOptions< WPropSelection >::SPtr createSelection( std::string propName, std::string propDescription,
152  WProperties::SPtr propGroup,
153  std::vector< NameDescriptionDefineTuple > defines );
154 }
155 
156 /**
157  * Class converts the specified property value to an index list. The generic case for all int-castable property types is trivial. WPropSelection
158  * is a specialization of this class.
159  *
160  * \tparam PropType The property. WPropInt for example.
161  */
162 template< typename PropType >
164 {
165 public:
166  /**
167  * The type of the index-list to create.
168  */
170 
171  /**
172  * Converts the specified property value to an index list.
173  *
174  * \param in the value to convert to an index list
175  *
176  * \return the new index list
177  */
178  IdxList operator()( const typename PropType::element_type::ValueType& in ) const
179  {
180  return IdxList( 1, typename IdxList::value_type( in ) );
181  }
182 };
183 
184 /**
185  * Class converts the specified property value to an index list. The generic case for all int-castable property types is trivial. This is the
186  * specialization for WPropSelection which allows multiple options to be active if the selection has multiple selected items.
187  *
188  * \tparam PropType The property. WPropInt for example.
189  */
190 template<>
192 {
193 public:
194  /**
195  * The type of the index-list to create.
196  */
198 
199  /**
200  * Converts the specified property value to an index list.
201  *
202  * \param in the value to convert to an index list
203  *
204  * \return the new index list
205  */
207  {
208  return in.getIndexList();
209  }
210 };
211 
212 template< typename PropType, typename PropIndexAdapter >
214  std::string option2, std::string option3, std::string option4, std::string option5,
215  std::string option6, std::string option7, std::string option8, std::string option9,
216  std::string option10 ):
217  WGEShaderDefineOptions( first, option2, option3, option4, option5, option6, option7, option8, option9, option10 ),
218  m_property( prop )
219 {
220  // if the prop changes -> update options
221  m_connection = m_property->getValueChangeCondition()->subscribeSignal(
223  );
224  propUpdated();
225 }
226 
227 template< typename PropType, typename PropIndexAdapter >
229  WGEShaderDefineOptions( options ),
230  m_property( prop )
231 {
232  // if the prop changes -> update options
233  m_connection = m_property->getValueChangeCondition()->subscribeSignal(
235  );
236  propUpdated();
237 }
238 
239 template< typename PropType, typename PropIndexAdapter >
241 {
242  // cleanup
243 }
244 
245 template< typename PropType, typename PropIndexAdapter >
247 {
248  PropIndexAdapter functor;
249  setActivationList( functor( m_property->get() ) );
250 }
251 
252 template< typename PropType, typename PropIndexAdapter >
254 {
255  return m_property;
256 }
257 
258 #endif // WGESHADERPROPERTYDEFINEOPTIONS_H
259