OgreController.h

Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of OGRE
00004     (Object-oriented Graphics Rendering Engine)
00005 For the latest info, see http://www.ogre3d.org/
00006 
00007 Copyright (c) 2000-2006 Torus Knot Software Ltd
00008 Also see acknowledgements in Readme.html
00009 
00010 This program is free software; you can redistribute it and/or modify it under
00011 the terms of the GNU Lesser General Public License as published by the Free Software
00012 Foundation; either version 2 of the License, or (at your option) any later
00013 version.
00014 
00015 This program is distributed in the hope that it will be useful, but WITHOUT
00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public License along with
00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00022 http://www.gnu.org/copyleft/lesser.txt.
00023 
00024 You may alternatively use this source under the terms of a specific version of
00025 the OGRE Unrestricted License provided you have obtained such a license from
00026 Torus Knot Software Ltd.
00027 -----------------------------------------------------------------------------
00028 */
00029 #ifndef __Controller_H__
00030 #define __Controller_H__
00031 
00032 #include "OgrePrerequisites.h"
00033 
00034 #include "OgreSharedPtr.h"
00035 
00036 namespace Ogre {
00037 
00038 
00039     
00040     
00050     template <typename T>
00051     class ControllerFunction : public ControllerAlloc
00052     {
00053     protected:
00055         bool mDeltaInput;
00056         T mDeltaCount;
00057 
00060         T getAdjustedInput(T input)
00061         {
00062             if (mDeltaInput)
00063             {
00064                 mDeltaCount += input;
00065                 // Wrap
00066                 while (mDeltaCount >= 1.0)
00067                     mDeltaCount -= 1.0;
00068                 while (mDeltaCount < 0.0)
00069                     mDeltaCount += 1.0;
00070 
00071                 return mDeltaCount;
00072             }
00073             else
00074             {
00075                 return input;
00076             }
00077         }
00078 
00079     public:
00085         ControllerFunction(bool deltaInput)
00086         {
00087             mDeltaInput = deltaInput;
00088             mDeltaCount = 0;
00089         }
00090 
00091         virtual ~ControllerFunction() {}
00092 
00093         virtual T calculate(T sourceValue) = 0;
00094     };
00095 
00096 
00099     template <typename T>
00100     class ControllerValue : public ControllerAlloc
00101     {
00102 
00103     public:
00104         virtual ~ControllerValue() { }
00105         virtual T getValue(void) const = 0;
00106         virtual void setValue(T value) = 0;
00107 
00108     };
00109 
00130     template <typename T>
00131     class Controller : public ControllerAlloc
00132     {
00133     protected:
00135         SharedPtr< ControllerValue<T> > mSource;
00137         SharedPtr< ControllerValue<T> > mDest;
00139         SharedPtr< ControllerFunction<T> > mFunc;
00141         bool mEnabled;
00142 
00143 
00144     public:
00145 
00151         Controller(const SharedPtr< ControllerValue<T> >& src, 
00152             const SharedPtr< ControllerValue<T> >& dest, const SharedPtr< ControllerFunction<T> >& func)
00153             : mSource(src), mDest(dest), mFunc(func)
00154         {
00155             mEnabled = true;
00156         }
00157 
00160         virtual ~Controller() {}
00161 
00162 
00164         void setSource(const SharedPtr< ControllerValue<T> >& src)
00165         {
00166             mSource = src;
00167         }
00169         const SharedPtr< ControllerValue<T> >& getSource(void) const
00170         {
00171             return mSource;
00172         }
00174         void setDestination(const SharedPtr< ControllerValue<T> >& dest)
00175         {
00176             mDest = dest;
00177         }
00178 
00180         const SharedPtr< ControllerValue<T> >& getDestination(void) const
00181         {
00182             return mDest;
00183         }
00184 
00186         bool getEnabled(void) const
00187         {
00188             return mEnabled;
00189         }
00190 
00192         void setEnabled(bool enabled)
00193         {
00194             mEnabled = enabled;
00195         }
00196 
00199         void setFunction(const SharedPtr< ControllerFunction<T> >& func)
00200         {
00201             mFunc = func;
00202         }
00203 
00206         const SharedPtr< ControllerFunction<T> >& getFunction(void) const
00207         {
00208             return mFunc;
00209         }
00210 
00216         void update(void)
00217         {
00218             if(mEnabled)
00219                 mDest->setValue(mFunc->calculate(mSource->getValue()));
00220         }
00221 
00222     };
00223 
00224 
00225 }
00226 
00227 #endif

Copyright © 2008 Torus Knot Software Ltd
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
Last modified Sun Sep 27 22:02:22 2009