Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/OgreMain/include/OgreController.h @ 3

Last change on this file since 3 was 3, checked in by anonymous, 17 years ago

=update

File size: 7.1 KB
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-2006 Torus Knot Software Ltd
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
24You may alternatively use this source under the terms of a specific version of
25the OGRE Unrestricted License provided you have obtained such a license from
26Torus Knot Software Ltd.
27-----------------------------------------------------------------------------
28*/
29#ifndef __Controller_H__
30#define __Controller_H__
31
32#include "OgrePrerequisites.h"
33
34#include "OgreSharedPtr.h"
35
36namespace Ogre {
37
38
39       
40       
41        /** Subclasses of this class are responsible for performing a function on an input value for a Controller.
42        @remarks
43            This abstract class provides the interface that needs to be supported for a custom function which
44            can be 'plugged in' to a Controller instance, which controls some object value based on an input value.
45            For example, the WaveControllerFunction class provided by Ogre allows you to use various waveforms to
46            translate an input value to an output value.
47        @par
48            You are free to create your own subclasses in order to define any function you wish.
49    */
50        template <typename T>
51    class ControllerFunction
52    {
53    protected:
54        /// If true, function will add imput values together and wrap at 1.0 before evaluating
55        bool mDeltaInput;
56        T mDeltaCount;
57
58        /** Gets the input value as adjusted by any delta.
59        */
60        T getAdjustedInput(T input)
61        {
62            if (mDeltaInput)
63            {
64                mDeltaCount += input;
65                // Wrap
66                while (mDeltaCount >= 1.0)
67                    mDeltaCount -= 1.0;
68                while (mDeltaCount < 0.0)
69                    mDeltaCount += 1.0;
70
71                return mDeltaCount;
72            }
73            else
74                        {
75                return input;
76            }
77        }
78
79    public:
80        /** Constructor.
81            @param
82                deltaInput If true, signifies that the input will be a delta value such that the function should
83                add it to an internal counter before calculating the output.
84        */
85        ControllerFunction(bool deltaInput)
86        {
87            mDeltaInput = deltaInput;
88            mDeltaCount = 0;
89        }
90
91                virtual ~ControllerFunction() {}
92
93        virtual T calculate(T sourceValue) = 0;
94    };
95
96
97    /** Can either be used as an input or output value.
98    */
99        template <typename T>
100    class ControllerValue
101    {
102
103    public:
104        virtual ~ControllerValue() { }
105        virtual T getValue(void) const = 0;
106        virtual void setValue(T value) = 0;
107
108    };
109
110    /** Instances of this class 'control' the value of another object in the system.
111        @remarks
112            Controller classes are used to manage the values of object automatically based
113            on the value of some input. For example, a Controller could animate a texture
114            by controlling the current frame of the texture based on time, or a different Controller
115            could change the colour of a material used for a spaceship shield mesh based on the remaining
116            shield power level of the ship.
117        @par
118            The Controller is an intentionally abstract concept - it can generate values
119            based on input and a function, which can either be one of the standard ones
120            supplied, or a function can be 'plugged in' for custom behaviour - see the ControllerFunction class for details.
121            Both the input and output values are via ControllerValue objects, meaning that any value can be both
122            input and output of the controller.
123        @par
124            Whilst this is very flexible, it can be a little bit confusing so to make it simpler the most often used
125            controller setups are available by calling methods on the ControllerManager object.
126        @see
127            ControllerFunction
128
129    */
130        template <typename T>
131    class Controller
132    {
133    protected:
134        /// Source value
135        SharedPtr< ControllerValue<T> > mSource;
136        /// Destination value
137        SharedPtr< ControllerValue<T> > mDest;
138        /// Function
139        SharedPtr< ControllerFunction<T> > mFunc;
140                /// Controller is enabled or not
141        bool mEnabled;
142
143
144    public:
145
146        /** Usual constructor.
147            @remarks
148                Requires source and destination values, and a function object. None of these are destroyed
149                with the Controller when it is deleted (they can be shared) so you must delete these as appropriate.
150        */
151        Controller(const SharedPtr< ControllerValue<T> >& src, 
152                        const SharedPtr< ControllerValue<T> >& dest, const SharedPtr< ControllerFunction<T> >& func)
153                        : mSource(src), mDest(dest), mFunc(func)
154                {
155                        mEnabled = true;
156                }
157
158        /** Default d-tor.
159        */
160                virtual ~Controller() {}
161
162
163                /// Sets the input controller value
164        void setSource(const SharedPtr< ControllerValue<T> >& src)
165                {
166                        mSource = src;
167                }
168                /// Gets the input controller value
169        const SharedPtr< ControllerValue<T> >& getSource(void) const
170                {
171                        return mSource;
172                }
173                /// Sets the output controller value
174        void setDestination(const SharedPtr< ControllerValue<T> >& dest)
175                {
176                        mDest = dest;
177                }
178
179                /// Gets the output controller value
180        const SharedPtr< ControllerValue<T> >& getDestination(void) const
181                {
182                        return mDest;
183                }
184
185        /// Returns true if this controller is currently enabled
186        bool getEnabled(void) const
187                {
188                        return mEnabled;
189                }
190
191        /// Sets whether this controller is enabled
192        void setEnabled(bool enabled)
193                {
194                        mEnabled = enabled;
195                }
196
197        /** Sets the function object to be used by this controller.
198        */
199        void setFunction(const SharedPtr< ControllerFunction<T> >& func)
200                {
201                        mFunc = func;
202                }
203
204        /** Returns a pointer to the function object used by this controller.
205        */
206        const SharedPtr< ControllerFunction<T> >& getFunction(void) const
207                {
208                        return mFunc;
209                }
210
211                /** Tells this controller to map it's input controller value
212                    to it's output controller value, via the controller function.
213                @remarks
214                        This method is called automatically every frame by ControllerManager.
215                */
216                void update(void)
217                {
218                        if(mEnabled)
219                                mDest->setValue(mFunc->calculate(mSource->getValue()));
220                }
221
222    };
223
224
225}
226
227#endif
Note: See TracBrowser for help on using the repository browser.