# 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-2005 The OGRE Team
    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 */
    00025 #ifndef __Controller_H__
    00026 #define __Controller_H__
    00027
    00028 #include "OgrePrerequisites.h"
    00029
    00030 #include "OgreSharedPtr.h"
    00031
    00032 namespace Ogre {
    00033
    00034
    00035
    00036
    00046     template <typename T>
    00047     class ControllerFunction
    00048     {
    00049     protected:
    00051         bool mDeltaInput;
    00052         T mDeltaCount;
    00053
    00056         T getAdjustedInput(T input)
    00057         {
    00058             if (mDeltaInput)
    00059             {
    00060                 mDeltaCount += input;
    00061                 // Wrap
    00062                 while (mDeltaCount >= 1.0)
    00063                     mDeltaCount -= 1.0;
    00064                 while (mDeltaCount < 0.0)
    00065                     mDeltaCount += 1.0;
    00066
    00067                 return mDeltaCount;
    00068             }
    00069             else
    00070             {
    00071                 return input;
    00072             }
    00073         }
    00074
    00075     public:
    00081         ControllerFunction(bool deltaInput)
    00082         {
    00083             mDeltaInput = deltaInput;
    00084             mDeltaCount = 0;
    00085         }
    00086
    00087         virtual ~ControllerFunction() {}
    00088
    00089         virtual T calculate(T sourceValue) = 0;
    00090     };
    00091
    00092
    00095     template <typename T>
    00096     class ControllerValue
    00097     {
    00098
    00099     public:
    00100         virtual ~ControllerValue() { }
    00101         virtual T getValue(void) const = 0;
    00102         virtual void setValue(T value) = 0;
    00103
    00104     };
    00105
    00126     template <typename T>
    00127     class Controller
    00128     {
    00129     protected:
    00131         SharedPtr< ControllerValue<T> > mSource;
    00133         SharedPtr< ControllerValue<T> > mDest;
    00135         SharedPtr< ControllerFunction<T> > mFunc;
    00137         bool mEnabled;
    00138
    00139
    00140     public:
    00141
    00147         Controller(const SharedPtr< ControllerValue<T> >& src,
    00148             const SharedPtr< ControllerValue<T> >& dest, const SharedPtr< ControllerFunction<T> >& func)
    00149             : mSource(src), mDest(dest), mFunc(func)
    00150         {
    00151             mEnabled = true;
    00152         }
    00153
    00156         virtual ~Controller() {}
    00157
    00158
    00160         void setSource(const SharedPtr< ControllerValue<T> >& src)
    00161         {
    00162             mSource = src;
    00163         }
    00165         const SharedPtr< ControllerValue<T> >& getSource(void) const
    00166         {
    00167             return mSource;
    00168         }
    00170         void setDestination(const SharedPtr< ControllerValue<T> >& dest)
    00171         {
    00172             mDest = dest;
    00173         }
    00174
    00176         const SharedPtr< ControllerValue<T> >& getDestination(void) const
    00177         {
    00178             return mDest;
    00179         }
    00180
    00182         bool getEnabled(void) const
    00183         {
    00184             return mEnabled;
    00185         }
    00186
    00188         void setEnabled(bool enabled)
    00189         {
    00190             mEnabled = enabled;
    00191         }
    00192
    00195         void setFunction(const SharedPtr< ControllerFunction<T> >& func)
    00196         {
    00197             mFunc = func;
    00198         }
    00199
    00202         const SharedPtr< ControllerFunction<T> >& getFunction(void) const
    00203         {
    00204             return mFunc;
    00205         }
    00206
    00212         void update(void)
    00213         {
    00214             if(mEnabled)
    00215                 mDest->setValue(mFunc->calculate(mSource->getValue()));
    00216         }
    00217
    00218     };
    00219
    00220
    00221 }
    00222
    00223 #endif

Copyright © 2000-2005 by The OGRE Team
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
Last modified Sun Feb 12 12:59:43 2006