Crazy Eddie's GUI System  0.8.4
TplInterpolators.h
00001 /***********************************************************************
00002     created:    23/11/2010
00003     author:     Martin Preisler
00004 
00005     purpose:    Provides templated finger saving interpolators
00006 *************************************************************************/
00007 /***************************************************************************
00008  *   Copyright (C) 2004 - 2010 Paul D Turner & The CEGUI Development Team
00009  *
00010  *   Permission is hereby granted, free of charge, to any person obtaining
00011  *   a copy of this software and associated documentation files (the
00012  *   "Software"), to deal in the Software without restriction, including
00013  *   without limitation the rights to use, copy, modify, merge, publish,
00014  *   distribute, sublicense, and/or sell copies of the Software, and to
00015  *   permit persons to whom the Software is furnished to do so, subject to
00016  *   the following conditions:
00017  *
00018  *   The above copyright notice and this permission notice shall be
00019  *   included in all copies or substantial portions of the Software.
00020  *
00021  *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00022  *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00023  *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00024  *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
00025  *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00026  *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00027  *   OTHER DEALINGS IN THE SOFTWARE.
00028  ***************************************************************************/
00029 #ifndef _CEGUITplInterpolators_h_
00030 #define _CEGUITplInterpolators_h_
00031 
00032 #include "CEGUI/Base.h"
00033 #include "CEGUI/Interpolator.h"
00034 #include "CEGUI/PropertyHelper.h"
00035 
00036 // Start of CEGUI namespace section
00037 namespace CEGUI
00038 {
00039 
00040 class CEGUIEXPORT TplInterpolatorBase : public Interpolator
00041 {
00042 public:
00043     TplInterpolatorBase(const String& type):
00044         d_type(type)
00045     {}
00046     
00048     virtual ~TplInterpolatorBase() {}
00049     
00051     virtual const String& getType() const
00052     {
00053         return d_type;
00054     }
00055     
00056 private:
00057     const String d_type;
00058 };
00059 
00066 template<typename T>
00067 class TplLinearInterpolator : public TplInterpolatorBase
00068 {
00069 public:
00070     typedef PropertyHelper<T> Helper;
00071     
00072     TplLinearInterpolator(const String& type):
00073         TplInterpolatorBase(type)
00074     {}
00075     
00077     virtual ~TplLinearInterpolator() {}
00078     
00080     virtual String interpolateAbsolute(const String& value1,
00081                                        const String& value2,
00082                                        float position)
00083     {
00084         typename Helper::return_type val1 = Helper::fromString(value1);
00085         typename Helper::return_type val2 = Helper::fromString(value2);
00086 
00087         const T result = static_cast<const T>(val1 * (1.0f - position) + val2 * (position));
00088 
00089         return Helper::toString(result);
00090     }
00091     
00093     virtual String interpolateRelative(const String& base,
00094                                        const String& value1,
00095                                        const String& value2,
00096                                        float position)
00097     {
00098         typename Helper::return_type bas = Helper::fromString(base);
00099         typename Helper::return_type val1 = Helper::fromString(value1);
00100         typename Helper::return_type val2 = Helper::fromString(value2);
00101 
00102         const T result = static_cast<const T>(bas + (val1 * (1.0f - position) + val2 * (position)));
00103 
00104         return Helper::toString(result);
00105     }
00106     
00108     virtual String interpolateRelativeMultiply(const String& base,
00109                                                const String& value1,
00110                                                const String& value2,
00111                                                float position)
00112     {
00113         typename Helper::return_type bas = Helper::fromString(base);
00114         typename PropertyHelper<float>::return_type val1 = PropertyHelper<float>::fromString(value1);
00115         typename PropertyHelper<float>::return_type val2 = PropertyHelper<float>::fromString(value2);
00116 
00117         const float mul = val1 * (1.0f - position) + val2 * (position);
00118 
00119         const T result = static_cast<const T>(bas * mul);
00120 
00121         return Helper::toString(result);
00122     }
00123 };
00124 
00132 template<typename T>
00133 class TplDiscreteInterpolator : public TplInterpolatorBase
00134 {
00135 public:
00136     typedef PropertyHelper<T> Helper;
00137     
00138     TplDiscreteInterpolator(const String& type):
00139         TplInterpolatorBase(type)
00140     {}
00141     
00143     virtual ~TplDiscreteInterpolator() {}
00144     
00146     virtual String interpolateAbsolute(const String& value1,
00147                                        const String& value2,
00148                                        float position)
00149     {
00150         typename Helper::return_type val1 = Helper::fromString(value1);
00151         typename Helper::return_type val2 = Helper::fromString(value2);
00152 
00153         typename Helper::return_type result = position < 0.5 ? val1 : val2;;
00154 
00155         return Helper::toString(result);
00156     }
00157     
00159     virtual String interpolateRelative(const String& /*base*/,
00160                                        const String& value1,
00161                                        const String& value2,
00162                                        float position)
00163     {
00164         //typename Helper::return_type bas = Helper::fromString(base);
00165         typename Helper::return_type val1 = Helper::fromString(value1);
00166         typename Helper::return_type val2 = Helper::fromString(value2);
00167         
00168         typename Helper::return_type result = position < 0.5 ? val1 : val2;
00169         
00170         return Helper::toString(result);
00171     }
00172     
00174     virtual String interpolateRelativeMultiply(const String& base,
00175                                                const String& /*value1*/,
00176                                                const String& /*value2*/,
00177                                                float /*position*/)
00178     {
00179         typename Helper::return_type bas = Helper::fromString(base);
00180         /*const float val1 = PropertyHelper<float>::fromString(value1);
00181         const float val2 = PropertyHelper<float>::fromString(value2);
00182 
00183         const float mul = val1 * (1.0f - position) + val2 * (position);*/
00184 
00185         // there is nothing we can do, we have no idea what operators T has overloaded
00186         return Helper::toString(bas);
00187     }
00188 };
00189 
00198 template<typename T>
00199 class TplDiscreteRelativeInterpolator : public TplDiscreteInterpolator<T>
00200 {
00201 public:
00202     typedef PropertyHelper<T> Helper;
00203     
00204     TplDiscreteRelativeInterpolator(const String& type):
00205         TplDiscreteInterpolator<T>(type)
00206     {}
00207     
00209     virtual ~TplDiscreteRelativeInterpolator() {}
00210     
00212     virtual String interpolateRelative(const String& base,
00213                                        const String& value1,
00214                                        const String& value2,
00215                                        float position)
00216     {
00217         typename Helper::return_type bas = Helper::fromString(base);
00218         typename Helper::return_type val1 = Helper::fromString(value1);
00219         typename Helper::return_type val2 = Helper::fromString(value2);
00220         
00221         typename Helper::return_type result = bas + (position < 0.5 ? val1 : val2);
00222         
00223         return Helper::toString(result);
00224     }
00225 };
00226 
00227 } // End of  CEGUI namespace section
00228 
00229 #endif  // end of guard _CEGUITplInterpolators_h_
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends