OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
WGEShaderDefineOptions.cpp
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 #include <stdarg.h>
26 
27 #include <algorithm>
28 
29 #include "../../common/exceptions/WPreconditionNotMet.h"
30 
31 #include "WGEShaderDefineOptions.h"
32 
34  std::string option2, std::string option3, std::string option4, std::string option5,
35  std::string option6, std::string option7, std::string option8, std::string option9,
36  std::string option10 ):
38  m_options( 1, first ),
39  m_idx( 1, 0 )
40 {
41  // init
42  if( !option2.empty() )
43  {
44  m_options.push_back( option2 );
45  }
46  if( !option3.empty() )
47  {
48  m_options.push_back( option3 );
49  }
50  if( !option4.empty() )
51  {
52  m_options.push_back( option4 );
53  }
54  if( !option5.empty() )
55  {
56  m_options.push_back( option5 );
57  }
58  if( !option6.empty() )
59  {
60  m_options.push_back( option6 );
61  }
62  if( !option7.empty() )
63  {
64  m_options.push_back( option7 );
65  }
66  if( !option8.empty() )
67  {
68  m_options.push_back( option8 );
69  }
70  if( !option9.empty() )
71  {
72  m_options.push_back( option9 );
73  }
74  if( !option10.empty() )
75  {
76  m_options.push_back( option10 );
77  }
78 }
79 
80 WGEShaderDefineOptions::WGEShaderDefineOptions( std::vector< std::string > options ):
82  m_options( options ),
83  m_idx( 1, 0 )
84 {
85  WPrecond( options.size() >= 1, "You need to specify at least one option." );
86 }
87 
89 {
90  // cleanup
91 }
92 
93 std::string WGEShaderDefineOptions::process( const std::string& /*file*/, const std::string& code ) const
94 {
95  if( !getActive() )
96  {
97  return code;
98  }
99 
100  // add a define for every active option
101  std::stringstream ss;
102  for( IdxList::const_iterator iter = m_idx.begin(); iter != m_idx.end(); ++iter )
103  {
104  ss << "#define " + getOptionName( *iter ) << std::endl;
105  }
106 
107  // add the original code again
108  ss << code;
109  return ss.str();
110 }
111 
113 {
114  return m_idx;
115 }
116 
117 std::string WGEShaderDefineOptions::getOptionName( size_t idx ) const
118 {
119  WPrecond( idx < m_options.size(), "Index invalid." );
120  return m_options[ idx ];
121 }
122 
123 void WGEShaderDefineOptions::activateOption( size_t idx, bool exclusive )
124 {
125  WPrecond( idx < m_options.size(), "Index invalid." );
126 
127  if( exclusive )
128  {
129  m_idx.clear();
130  }
131 
132  // is the option already active?
133  if( std::find( m_idx.begin(), m_idx.end(), idx ) == m_idx.end() )
134  {
135  m_idx.push_back( idx );
136  updated();
137  }
138 }
139 
141 {
142  IdxList::iterator iter = std::find( m_idx.begin(), m_idx.end(), idx );
143  if( iter != m_idx.end() )
144  {
145  m_idx.erase( iter );
146  updated();
147  }
148 }
149 
151 {
152  // simply add all
153  for( size_t i = 0; i < m_options.size(); ++i )
154  {
155  m_idx.push_back( i );
156  }
157 
158  updated();
159 }
160 
162 {
163  // clear active list
164  m_idx.clear();
165  updated();
166 }
167 
168 void WGEShaderDefineOptions::addOption( std::string opt )
169 {
170  WPrecond( !opt.empty(), "Options need to have a non-empty name." );
171  if( std::find( m_options.begin(), m_options.end(), opt ) == m_options.end() )
172  {
173  m_options.push_back( opt );
174 
175  // signal update
176  updated();
177  }
178 }
179 
181 {
182  if( m_idx != newList )
183  {
184  m_idx = newList;
185  updated();
186  }
187 }
188