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

Revision 1030, 7.1 KB checked in by gumbau, 18 years ago (diff)

Ogre Stuff initial import

Line 
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 T calculate(T sourceValue) = 0;
88    };
89
90
91    /** Can either be used as an input or output value.
92    */
93        template <typename T>
94    class ControllerValue
95    {
96
97    public:
98        virtual ~ControllerValue() { }
99        virtual T getValue(void) const = 0;
100        virtual void setValue(T value) = 0;
101
102    };
103
104    /** Instances of this class 'control' the value of another object in the system.
105        @remarks
106            Controller classes are used to manage the values of object automatically based
107            on the value of some input. For example, a Controller could animate a texture
108            by controlling the current frame of the texture based on time, or a different Controller
109            could change the colour of a material used for a spaceship shield mesh based on the remaining
110            shield power level of the ship.
111        @par
112            The Controller is an intentionally abstract concept - it can generate values
113            based on input and a function, which can either be one of the standard ones
114            supplied, or a function can be 'plugged in' for custom behaviour - see the ControllerFunction class for details.
115            Both the input and output values are via ControllerValue objects, meaning that any value can be both
116            input and output of the controller.
117        @par
118            Whilst this is very flexible, it can be a little bit confusing so to make it simpler the most often used
119            controller setups are available by calling methods on the ControllerManager object.
120        @see
121            ControllerFunction
122
123    */
124        template <typename T>
125    class Controller
126    {
127    protected:
128        /// Source value
129        SharedPtr< ControllerValue<T> > mSource;
130        /// Destination value
131        SharedPtr< ControllerValue<T> > mDest;
132        /// Function
133        SharedPtr< ControllerFunction<T> > mFunc;
134                /// Controller is enabled or not
135        bool mEnabled;
136
137
138    public:
139
140        /** Usual constructor.
141            @remarks
142                Requires source and destination values, and a function object. None of these are destroyed
143                with the Controller when it is deleted (they can be shared) so you must delete these as appropriate.
144        */
145        Controller(const SharedPtr< ControllerValue<T> >& src,
146                        const SharedPtr< ControllerValue<T> >& dest, const SharedPtr< ControllerFunction<T> >& func)
147                        : mSource(src), mDest(dest), mFunc(func)
148                {
149                        mEnabled = true;
150                }
151
152        /** Default d-tor.
153        */
154                virtual ~Controller() {}
155
156
157                /// Sets the input controller value
158        void setSource(const SharedPtr< ControllerValue<T> >& src)
159                {
160                        mSource = src;
161                }
162                /// Gets the input controller value
163        const SharedPtr< ControllerValue<T> >& getSource(void) const
164                {
165                        return mSource;
166                }
167                /// Sets the output controller value
168        void setDestination(const SharedPtr< ControllerValue<T> >& dest)
169                {
170                        mDest = dest;
171                }
172
173                /// Gets the output controller value
174        const SharedPtr< ControllerValue<T> >& getDestination(void) const
175                {
176                        return mDest;
177                }
178
179        /// Returns true if this controller is currently enabled
180        bool getEnabled(void) const
181                {
182                        return mEnabled;
183                }
184
185        /// Sets whether this controller is enabled
186        void setEnabled(bool enabled)
187                {
188                        mEnabled = enabled;
189                }
190
191        /** Sets the function object to be used by this controller.
192        */
193        void setFunction(const SharedPtr< ControllerFunction<T> >& func)
194                {
195                        mFunc = func;
196                }
197
198        /** Returns a pointer to the function object used by this controller.
199        */
200        const SharedPtr< ControllerFunction<T> >& getFunction(void) const
201                {
202                        return mFunc;
203                }
204
205                /** Tells this controller to map it's input controller value
206                    to it's output controller value, via the controller function.
207                @remarks
208                        This method is called automatically every frame by ControllerManager.
209                */
210                void update(void)
211                {
212                        if(mEnabled)
213                                mDest->setValue(mFunc->calculate(mSource->getValue()));
214                }
215
216    };
217
218
219}
220
221#endif
Note: See TracBrowser for help on using the repository browser.