# 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-2009 Torus Knot Software Ltd
    00008
    00009 Permission is hereby granted, free of charge, to any person obtaining a copy
    00010 of this software and associated documentation files (the "Software"), to deal
    00011 in the Software without restriction, including without limitation the rights
    00012 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    00013 copies of the Software, and to permit persons to whom the Software is
    00014 furnished to do so, subject to the following conditions:
    00015
    00016 The above copyright notice and this permission notice shall be included in
    00017 all copies or substantial portions of the Software.
    00018
    00019 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    00020 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    00021 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    00022 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    00023 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    00024 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    00025 THE SOFTWARE.
    00026 -----------------------------------------------------------------------------
    00027 */
    00028 #ifndef __Controller_H__
    00029 #define __Controller_H__
    00030
    00031 #include "OgrePrerequisites.h"
    00032
    00033 #include "OgreSharedPtr.h"
    00034
    00035 namespace Ogre {
    00036
    00054     template <typename T>
    00055     class ControllerFunction : public ControllerAlloc
    00056     {
    00057     protected:
    00059         bool mDeltaInput;
    00060         T mDeltaCount;
    00061
    00064         T getAdjustedInput(T input)
    00065         {
    00066             if (mDeltaInput)
    00067             {
    00068                 mDeltaCount += input;
    00069                 // Wrap
    00070                 while (mDeltaCount >= 1.0)
    00071                     mDeltaCount -= 1.0;
    00072                 while (mDeltaCount < 0.0)
    00073                     mDeltaCount += 1.0;
    00074
    00075                 return mDeltaCount;
    00076             }
    00077             else
    00078             {
    00079                 return input;
    00080             }
    00081         }
    00082
    00083     public:
    00089         ControllerFunction(bool deltaInput)
    00090         {
    00091             mDeltaInput = deltaInput;
    00092             mDeltaCount = 0;
    00093         }
    00094
    00095         virtual ~ControllerFunction() {}
    00096
    00097         virtual T calculate(T sourceValue) = 0;
    00098     };
    00099
    00100
    00103     template <typename T>
    00104     class ControllerValue : public ControllerAlloc
    00105     {
    00106
    00107     public:
    00108         virtual ~ControllerValue() { }
    00109         virtual T getValue(void) const = 0;
    00110         virtual void setValue(T value) = 0;
    00111
    00112     };
    00113
    00134     template <typename T>
    00135     class Controller : public ControllerAlloc
    00136     {
    00137     protected:
    00139         SharedPtr< ControllerValue<T> > mSource;
    00141         SharedPtr< ControllerValue<T> > mDest;
    00143         SharedPtr< ControllerFunction<T> > mFunc;
    00145         bool mEnabled;
    00146
    00147
    00148     public:
    00149
    00155         Controller(const SharedPtr< ControllerValue<T> >& src,
    00156             const SharedPtr< ControllerValue<T> >& dest, const SharedPtr< ControllerFunction<T> >& func)
    00157             : mSource(src), mDest(dest), mFunc(func)
    00158         {
    00159             mEnabled = true;
    00160         }
    00161
    00164         virtual ~Controller() {}
    00165
    00166
    00168         void setSource(const SharedPtr< ControllerValue<T> >& src)
    00169         {
    00170             mSource = src;
    00171         }
    00173         const SharedPtr< ControllerValue<T> >& getSource(void) const
    00174         {
    00175             return mSource;
    00176         }
    00178         void setDestination(const SharedPtr< ControllerValue<T> >& dest)
    00179         {
    00180             mDest = dest;
    00181         }
    00182
    00184         const SharedPtr< ControllerValue<T> >& getDestination(void) const
    00185         {
    00186             return mDest;
    00187         }
    00188
    00190         bool getEnabled(void) const
    00191         {
    00192             return mEnabled;
    00193         }
    00194
    00196         void setEnabled(bool enabled)
    00197         {
    00198             mEnabled = enabled;
    00199         }
    00200
    00203         void setFunction(const SharedPtr< ControllerFunction<T> >& func)
    00204         {
    00205             mFunc = func;
    00206         }
    00207
    00210         const SharedPtr< ControllerFunction<T> >& getFunction(void) const
    00211         {
    00212             return mFunc;
    00213         }
    00214
    00220         void update(void)
    00221         {
    00222             if(mEnabled)
    00223                 mDest->setValue(mFunc->calculate(mSource->getValue()));
    00224         }
    00225
    00226     };
    00227
    00231 }
    00232
    00233 #endif

Copyright © 2008 Torus Knot Software Ltd
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Wed Nov 3 2010 19:24:51