OpenWalnut 1.2.5
|
00001 //--------------------------------------------------------------------------- 00002 // 00003 // Project: OpenWalnut ( http://www.openwalnut.org ) 00004 // 00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS 00006 // For more information see http://www.openwalnut.org/copying 00007 // 00008 // This file is part of OpenWalnut. 00009 // 00010 // OpenWalnut is free software: you can redistribute it and/or modify 00011 // it under the terms of the GNU Lesser General Public License as published by 00012 // the Free Software Foundation, either version 3 of the License, or 00013 // (at your option) any later version. 00014 // 00015 // OpenWalnut is distributed in the hope that it will be useful, 00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 // GNU Lesser General Public License for more details. 00019 // 00020 // You should have received a copy of the GNU Lesser General Public License 00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>. 00022 // 00023 //--------------------------------------------------------------------------- 00024 00025 #ifndef WGESHADERPROPERTYDEFINEOPTIONS_H 00026 #define WGESHADERPROPERTYDEFINEOPTIONS_H 00027 00028 #include <string> 00029 #include <vector> 00030 00031 #include <boost/shared_ptr.hpp> 00032 #include "boost/tuple/tuple.hpp" 00033 #include <boost/signals2.hpp> 00034 00035 #include "../../common/WProperties.h" 00036 #include "../../common/WPropertyTypes.h" 00037 #include "../../common/exceptions/WPreconditionNotMet.h" 00038 00039 #include "WGEShaderDefineOptions.h" 00040 00041 template< typename PropType > 00042 class WGEShaderPropertyDefineOptionsIndexAdapter; 00043 00044 00045 /** 00046 * This is a WGEShaderDefineOptions class which additionally uses a property to automatically control the active options. This is very useful if 00047 * you have some WPropInt or WPropSelection which controls some features in your shader. Especially with WPropSelection Instances, you can even 00048 * activate multiple options if your selection allows this ( see WPropertyVariable<>::PropertyConstraint for details ). If used with a WPropBool, 00049 * it is useful to switch on/off an option for example. 00050 * 00051 * \note You can use inherited WGEShaderDefineOptions methods too. This might create some kind of inconsistency since they of course do not 00052 * update the property. 00053 */ 00054 template< typename PropType = WPropSelection, typename PropIndexAdapter = WGEShaderPropertyDefineOptionsIndexAdapter< PropType > > 00055 class WGEShaderPropertyDefineOptions: public WGEShaderDefineOptions 00056 { 00057 public: 00058 00059 /** 00060 * Convenience typedef for a boost_shared_ptr< WGEShaderPropertyDefineOptions >. 00061 */ 00062 typedef boost::shared_ptr< WGEShaderPropertyDefineOptions > SPtr; 00063 00064 /** 00065 * Convenience typedef for a boost_shared_ptr< const WGEShaderPropertyDefineOptions >. 00066 */ 00067 typedef boost::shared_ptr< const WGEShaderPropertyDefineOptions > ConstSPtr; 00068 00069 /** 00070 * Create a new instance of this class. The first option is mandatory and is set as default. The specified property controls the activations. 00071 * 00072 * \param prop the property controlling this thing. 00073 * 00074 * \param first fist option. Is default. 00075 * \param option2 another option 00076 * \param option3 another option 00077 * \param option4 another option 00078 * \param option5 another option 00079 * \param option6 another option 00080 * \param option7 another option 00081 * \param option8 another option 00082 * \param option9 another option 00083 * \param option10 another option 00084 */ 00085 WGEShaderPropertyDefineOptions( PropType prop, std::string first, 00086 std::string option2 = "", std::string option3 = "", std::string option4 = "", std::string option5 = "", 00087 std::string option6 = "", std::string option7 = "", std::string option8 = "", std::string option9 = "", 00088 std::string option10 = "" ); 00089 00090 /** 00091 * Create a new instance of this class. The first option is mandatory and is set as default. The specified property controls the activations. 00092 * 00093 * \param prop the property controlling this thing. 00094 * 00095 * \param options the list of options. Must have a size greater 0. 00096 */ 00097 WGEShaderPropertyDefineOptions( PropType prop, std::vector< std::string > options ); 00098 00099 /** 00100 * Destructor. 00101 */ 00102 virtual ~WGEShaderPropertyDefineOptions(); 00103 00104 /** 00105 * Returns the property associated with this instance. 00106 * 00107 * \return 00108 */ 00109 PropType getProperty() const; 00110 00111 protected: 00112 00113 private: 00114 00115 /** 00116 * The property controlling this instance and the active options list. 00117 */ 00118 PropType m_property; 00119 00120 /** 00121 * The connection associated with the properties update condition. 00122 */ 00123 boost::signals2::connection m_connection; 00124 00125 /** 00126 * Called by the property update mechanism. This handles the new value in the property. 00127 */ 00128 void propUpdated(); 00129 }; 00130 00131 /** 00132 * Contains some utility functions related to the WGEShaderPropertyDefineOptions class. 00133 */ 00134 namespace WGEShaderPropertyDefineOptionsTools 00135 { 00136 /** 00137 * This tuple contains name, description and define-name of an option. 00138 */ 00139 typedef boost::tuple< std::string, std::string, std::string > NameDescriptionDefineTuple; 00140 00141 /** 00142 * A little bit more comfortable way to create a list of shader-defines and the corresponding property. 00143 * 00144 * \param propName the name of the property to create 00145 * \param propDescription the description of the property to create 00146 * \param propGroup the owning group of the property 00147 * \param defines the list of names, descriptions and defines 00148 * 00149 * \return a WGEShaderPropertyDefineOptions instance associated with a new property. This can be acquired using getProperty(). 00150 */ 00151 WGEShaderPropertyDefineOptions< WPropSelection >::SPtr createSelection( std::string propName, std::string propDescription, 00152 WProperties::SPtr propGroup, 00153 std::vector< NameDescriptionDefineTuple > defines ); 00154 } 00155 00156 /** 00157 * Class converts the specified property value to an index list. The generic case for all int-castable property types is trivial. WPropSelection 00158 * is a specialization of this class. 00159 * 00160 * \tparam PropType The property. WPropInt for example. 00161 */ 00162 template< typename PropType > 00163 class WGEShaderPropertyDefineOptionsIndexAdapter 00164 { 00165 public: 00166 /** 00167 * The type of the index-list to create. 00168 */ 00169 typedef typename WGEShaderPropertyDefineOptions< PropType >::IdxList IdxList; 00170 00171 /** 00172 * Converts the specified property value to an index list. 00173 * 00174 * \param in the value to convert to an index list 00175 * 00176 * \return the new index list 00177 */ 00178 IdxList operator()( const typename PropType::element_type::ValueType& in ) const 00179 { 00180 return IdxList( 1, typename IdxList::value_type( in ) ); 00181 } 00182 }; 00183 00184 /** 00185 * Class converts the specified property value to an index list. The generic case for all int-castable property types is trivial. This is the 00186 * specialization for WPropSelection which allows multiple options to be active if the selection has multiple selected items. 00187 * 00188 * \tparam PropType The property. WPropInt for example. 00189 */ 00190 template<> 00191 class WGEShaderPropertyDefineOptionsIndexAdapter< WPropSelection > 00192 { 00193 public: 00194 /** 00195 * The type of the index-list to create. 00196 */ 00197 typedef WGEShaderPropertyDefineOptions< WPropSelection >::IdxList IdxList; 00198 00199 /** 00200 * Converts the specified property value to an index list. 00201 * 00202 * \param in the value to convert to an index list 00203 * 00204 * \return the new index list 00205 */ 00206 IdxList operator()( const WPVBaseTypes::PV_SELECTION& in ) const 00207 { 00208 return in.getIndexList(); 00209 } 00210 }; 00211 00212 template< typename PropType, typename PropIndexAdapter > 00213 WGEShaderPropertyDefineOptions< PropType, PropIndexAdapter >::WGEShaderPropertyDefineOptions( PropType prop, std::string first, 00214 std::string option2, std::string option3, std::string option4, std::string option5, 00215 std::string option6, std::string option7, std::string option8, std::string option9, 00216 std::string option10 ): 00217 WGEShaderDefineOptions( first, option2, option3, option4, option5, option6, option7, option8, option9, option10 ), 00218 m_property( prop ) 00219 { 00220 // if the prop changes -> update options 00221 m_connection = m_property->getValueChangeCondition()->subscribeSignal( 00222 boost::bind( &WGEShaderPropertyDefineOptions< PropType >::propUpdated, this ) 00223 ); 00224 propUpdated(); 00225 } 00226 00227 template< typename PropType, typename PropIndexAdapter > 00228 WGEShaderPropertyDefineOptions< PropType, PropIndexAdapter >::WGEShaderPropertyDefineOptions( PropType prop, std::vector< std::string > options ): 00229 WGEShaderDefineOptions( options ), 00230 m_property( prop ) 00231 { 00232 // if the prop changes -> update options 00233 m_connection = m_property->getValueChangeCondition()->subscribeSignal( 00234 boost::bind( &WGEShaderPropertyDefineOptions< PropType >::propUpdated, this ) 00235 ); 00236 propUpdated(); 00237 } 00238 00239 template< typename PropType, typename PropIndexAdapter > 00240 WGEShaderPropertyDefineOptions< PropType, PropIndexAdapter >::~WGEShaderPropertyDefineOptions() 00241 { 00242 // cleanup 00243 } 00244 00245 template< typename PropType, typename PropIndexAdapter > 00246 void WGEShaderPropertyDefineOptions< PropType, PropIndexAdapter >::propUpdated() 00247 { 00248 PropIndexAdapter functor; 00249 setActivationList( functor( m_property->get() ) ); 00250 } 00251 00252 template< typename PropType, typename PropIndexAdapter > 00253 PropType WGEShaderPropertyDefineOptions< PropType, PropIndexAdapter >::getProperty() const 00254 { 00255 return m_property; 00256 } 00257 00258 #endif // WGESHADERPROPERTYDEFINEOPTIONS_H 00259