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