source: GTP/trunk/App/Demos/Geom/OgreStuff/include/OgreController.h @ 1812

Revision 1812, 7.2 KB checked in by gumbau, 18 years ago (diff)
RevLine 
[1812]1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4    (Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2005 The OGRE Team
8Also see acknowledgements in Readme.html
9
10This program is free software; you can redistribute it and/or modify it under
11the terms of the GNU Lesser General Public License as published by the Free Software
12Foundation; either version 2 of the License, or (at your option) any later
13version.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18
19You should have received a copy of the GNU Lesser General Public License along with
20this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22http://www.gnu.org/copyleft/lesser.txt.
23-----------------------------------------------------------------------------
24*/
25#ifndef __Controller_H__
26#define __Controller_H__
27
28#include "OgrePrerequisites.h"
29
30#include "OgreSharedPtr.h"
31
32namespace Ogre {
33
34
35       
36       
37        /** Subclasses of this class are responsible for performing a function on an input value for a Controller.
38        @remarks
39            This abstract class provides the interface that needs to be supported for a custom function which
40            can be 'plugged in' to a Controller instance, which controls some object value based on an input value.
41            For example, the WaveControllerFunction class provided by Ogre allows you to use various waveforms to
42            translate an input value to an output value.
43        @par
44            You are free to create your own subclasses in order to define any function you wish.
45    */
46        template <typename T>
47    class ControllerFunction
48    {
49    protected:
50        /// If true, function will add imput values together and wrap at 1.0 before evaluating
51        bool mDeltaInput;
52        T mDeltaCount;
53
54        /** Gets the input value as adjusted by any delta.
55        */
56        T getAdjustedInput(T input)
57        {
58            if (mDeltaInput)
59            {
60                mDeltaCount += input;
61                // Wrap
62                while (mDeltaCount >= 1.0)
63                    mDeltaCount -= 1.0;
64                while (mDeltaCount < 0.0)
65                    mDeltaCount += 1.0;
66
67                return mDeltaCount;
68            }
69            else
70                        {
71                return input;
72            }
73        }
74
75    public:
76        /** Constructor.
77            @param
78                deltaInput If true, signifies that the input will be a delta value such that the function should
79                add it to an internal counter before calculating the output.
80        */
81        ControllerFunction(bool deltaInput)
82        {
83            mDeltaInput = deltaInput;
84            mDeltaCount = 0;
85        }
86
87                virtual ~ControllerFunction() {}
88
89        virtual T calculate(T sourceValue) = 0;
90    };
91
92
93    /** Can either be used as an input or output value.
94    */
95        template <typename T>
96    class ControllerValue
97    {
98
99    public:
100        virtual ~ControllerValue() { }
101        virtual T getValue(void) const = 0;
102        virtual void setValue(T value) = 0;
103
104    };
105
106    /** Instances of this class 'control' the value of another object in the system.
107        @remarks
108            Controller classes are used to manage the values of object automatically based
109            on the value of some input. For example, a Controller could animate a texture
110            by controlling the current frame of the texture based on time, or a different Controller
111            could change the colour of a material used for a spaceship shield mesh based on the remaining
112            shield power level of the ship.
113        @par
114            The Controller is an intentionally abstract concept - it can generate values
115            based on input and a function, which can either be one of the standard ones
116            supplied, or a function can be 'plugged in' for custom behaviour - see the ControllerFunction class for details.
117            Both the input and output values are via ControllerValue objects, meaning that any value can be both
118            input and output of the controller.
119        @par
120            Whilst this is very flexible, it can be a little bit confusing so to make it simpler the most often used
121            controller setups are available by calling methods on the ControllerManager object.
122        @see
123            ControllerFunction
124
125    */
126        template <typename T>
127    class Controller
128    {
129    protected:
130        /// Source value
131        SharedPtr< ControllerValue<T> > mSource;
132        /// Destination value
133        SharedPtr< ControllerValue<T> > mDest;
134        /// Function
135        SharedPtr< ControllerFunction<T> > mFunc;
136                /// Controller is enabled or not
137        bool mEnabled;
138
139
140    public:
141
142        /** Usual constructor.
143            @remarks
144                Requires source and destination values, and a function object. None of these are destroyed
145                with the Controller when it is deleted (they can be shared) so you must delete these as appropriate.
146        */
147        Controller(const SharedPtr< ControllerValue<T> >& src,
148                        const SharedPtr< ControllerValue<T> >& dest, const SharedPtr< ControllerFunction<T> >& func)
149                        : mSource(src), mDest(dest), mFunc(func)
150                {
151                        mEnabled = true;
152                }
153
154        /** Default d-tor.
155        */
156                virtual ~Controller() {}
157
158
159                /// Sets the input controller value
160        void setSource(const SharedPtr< ControllerValue<T> >& src)
161                {
162                        mSource = src;
163                }
164                /// Gets the input controller value
165        const SharedPtr< ControllerValue<T> >& getSource(void) const
166                {
167                        return mSource;
168                }
169                /// Sets the output controller value
170        void setDestination(const SharedPtr< ControllerValue<T> >& dest)
171                {
172                        mDest = dest;
173                }
174
175                /// Gets the output controller value
176        const SharedPtr< ControllerValue<T> >& getDestination(void) const
177                {
178                        return mDest;
179                }
180
181        /// Returns true if this controller is currently enabled
182        bool getEnabled(void) const
183                {
184                        return mEnabled;
185                }
186
187        /// Sets whether this controller is enabled
188        void setEnabled(bool enabled)
189                {
190                        mEnabled = enabled;
191                }
192
193        /** Sets the function object to be used by this controller.
194        */
195        void setFunction(const SharedPtr< ControllerFunction<T> >& func)
196                {
197                        mFunc = func;
198                }
199
200        /** Returns a pointer to the function object used by this controller.
201        */
202        const SharedPtr< ControllerFunction<T> >& getFunction(void) const
203                {
204                        return mFunc;
205                }
206
207                /** Tells this controller to map it's input controller value
208                    to it's output controller value, via the controller function.
209                @remarks
210                        This method is called automatically every frame by ControllerManager.
211                */
212                void update(void)
213                {
214                        if(mEnabled)
215                                mDest->setValue(mFunc->calculate(mSource->getValue()));
216                }
217
218    };
219
220
221}
222
223#endif
Note: See TracBrowser for help on using the repository browser.