Crazy Eddie's GUI System  0.8.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
TplInterpolators.h
1 /***********************************************************************
2  filename: CEGUITplInterpolators.h
3  created: 23/11/2010
4  author: Martin Preisler
5 
6  purpose: Provides templated finger saving interpolators
7 *************************************************************************/
8 /***************************************************************************
9  * Copyright (C) 2004 - 2010 Paul D Turner & The CEGUI Development Team
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining
12  * a copy of this software and associated documentation files (the
13  * "Software"), to deal in the Software without restriction, including
14  * without limitation the rights to use, copy, modify, merge, publish,
15  * distribute, sublicense, and/or sell copies of the Software, and to
16  * permit persons to whom the Software is furnished to do so, subject to
17  * the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be
20  * included in all copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
25  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
26  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
27  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28  * OTHER DEALINGS IN THE SOFTWARE.
29  ***************************************************************************/
30 #ifndef _CEGUITplInterpolators_h_
31 #define _CEGUITplInterpolators_h_
32 
33 #include "CEGUI/Base.h"
34 #include "CEGUI/Interpolator.h"
35 #include "CEGUI/PropertyHelper.h"
36 
37 // Start of CEGUI namespace section
38 namespace CEGUI
39 {
40 
41 class CEGUIEXPORT TplInterpolatorBase : public Interpolator
42 {
43 public:
44  TplInterpolatorBase(const String& type):
45  d_type(type)
46  {}
47 
49  virtual ~TplInterpolatorBase() {}
50 
52  virtual const String& getType() const
53  {
54  return d_type;
55  }
56 
57 private:
58  const String d_type;
59 };
60 
67 template<typename T>
69 {
70 public:
71  typedef PropertyHelper<T> Helper;
72 
73  TplLinearInterpolator(const String& type):
75  {}
76 
79 
81  virtual String interpolateAbsolute(const String& value1,
82  const String& value2,
83  float position)
84  {
85  typename Helper::return_type val1 = Helper::fromString(value1);
86  typename Helper::return_type val2 = Helper::fromString(value2);
87 
88  const T result = static_cast<const T>(val1 * (1.0f - position) + val2 * (position));
89 
90  return Helper::toString(result);
91  }
92 
94  virtual String interpolateRelative(const String& base,
95  const String& value1,
96  const String& value2,
97  float position)
98  {
99  typename Helper::return_type bas = Helper::fromString(base);
100  typename Helper::return_type val1 = Helper::fromString(value1);
101  typename Helper::return_type val2 = Helper::fromString(value2);
102 
103  const T result = static_cast<const T>(bas + (val1 * (1.0f - position) + val2 * (position)));
104 
105  return Helper::toString(result);
106  }
107 
110  const String& value1,
111  const String& value2,
112  float position)
113  {
114  typename Helper::return_type bas = Helper::fromString(base);
117 
118  const float mul = val1 * (1.0f - position) + val2 * (position);
119 
120  const T result = static_cast<const T>(bas * mul);
121 
122  return Helper::toString(result);
123  }
124 };
125 
133 template<typename T>
135 {
136 public:
137  typedef PropertyHelper<T> Helper;
138 
139  TplDiscreteInterpolator(const String& type):
140  TplInterpolatorBase(type)
141  {}
142 
145 
147  virtual String interpolateAbsolute(const String& value1,
148  const String& value2,
149  float position)
150  {
151  typename Helper::return_type val1 = Helper::fromString(value1);
152  typename Helper::return_type val2 = Helper::fromString(value2);
153 
154  typename Helper::return_type result = position < 0.5 ? val1 : val2;;
155 
156  return Helper::toString(result);
157  }
158 
160  virtual String interpolateRelative(const String& /*base*/,
161  const String& value1,
162  const String& value2,
163  float position)
164  {
165  //typename Helper::return_type bas = Helper::fromString(base);
166  typename Helper::return_type val1 = Helper::fromString(value1);
167  typename Helper::return_type val2 = Helper::fromString(value2);
168 
169  typename Helper::return_type result = position < 0.5 ? val1 : val2;
170 
171  return Helper::toString(result);
172  }
173 
176  const String& /*value1*/,
177  const String& /*value2*/,
178  float /*position*/)
179  {
180  typename Helper::return_type bas = Helper::fromString(base);
181  /*const float val1 = PropertyHelper<float>::fromString(value1);
182  const float val2 = PropertyHelper<float>::fromString(value2);
183 
184  const float mul = val1 * (1.0f - position) + val2 * (position);*/
185 
186  // there is nothing we can do, we have no idea what operators T has overloaded
187  return Helper::toString(bas);
188  }
189 };
190 
199 template<typename T>
201 {
202 public:
203  typedef PropertyHelper<T> Helper;
204 
207  {}
208 
211 
213  virtual String interpolateRelative(const String& base,
214  const String& value1,
215  const String& value2,
216  float position)
217  {
218  typename Helper::return_type bas = Helper::fromString(base);
219  typename Helper::return_type val1 = Helper::fromString(value1);
220  typename Helper::return_type val2 = Helper::fromString(value2);
221 
222  typename Helper::return_type result = bas + (position < 0.5 ? val1 : val2);
223 
224  return Helper::toString(result);
225  }
226 };
227 
228 } // End of CEGUI namespace section
229 
230 #endif // end of guard _CEGUITplInterpolators_h_