1 | /* |
---|
2 | ----------------------------------------------------------------------------- |
---|
3 | This source file is part of OGRE |
---|
4 | (Object-oriented Graphics Rendering Engine) |
---|
5 | For the latest info, see http://www.ogre3d.org/ |
---|
6 | |
---|
7 | Copyright (c) 2000-2013 Torus Knot Software Ltd |
---|
8 | |
---|
9 | Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
10 | of this software and associated documentation files (the "Software"), to deal |
---|
11 | in the Software without restriction, including without limitation the rights |
---|
12 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
13 | copies of the Software, and to permit persons to whom the Software is |
---|
14 | furnished to do so, subject to the following conditions: |
---|
15 | |
---|
16 | The above copyright notice and this permission notice shall be included in |
---|
17 | all copies or substantial portions of the Software. |
---|
18 | |
---|
19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
22 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
25 | THE SOFTWARE. |
---|
26 | ----------------------------------------------------------------------------- |
---|
27 | */ |
---|
28 | #ifndef __Controller_H__ |
---|
29 | #define __Controller_H__ |
---|
30 | |
---|
31 | #include "OgrePrerequisites.h" |
---|
32 | |
---|
33 | #include "OgreSharedPtr.h" |
---|
34 | |
---|
35 | namespace Ogre { |
---|
36 | |
---|
37 | /** \addtogroup Core |
---|
38 | * @{ |
---|
39 | */ |
---|
40 | /** \addtogroup General |
---|
41 | * @{ |
---|
42 | */ |
---|
43 | |
---|
44 | |
---|
45 | /** Subclasses of this class are responsible for performing a function on an input value for a Controller. |
---|
46 | @remarks |
---|
47 | This abstract class provides the interface that needs to be supported for a custom function which |
---|
48 | can be 'plugged in' to a Controller instance, which controls some object value based on an input value. |
---|
49 | For example, the WaveControllerFunction class provided by Ogre allows you to use various waveforms to |
---|
50 | translate an input value to an output value. |
---|
51 | @par |
---|
52 | You are free to create your own subclasses in order to define any function you wish. |
---|
53 | */ |
---|
54 | template <typename T> |
---|
55 | class ControllerFunction : public ControllerAlloc |
---|
56 | { |
---|
57 | protected: |
---|
58 | /// If true, function will add input values together and wrap at 1.0 before evaluating |
---|
59 | bool mDeltaInput; |
---|
60 | T mDeltaCount; |
---|
61 | |
---|
62 | /** Gets the input value as adjusted by any delta. |
---|
63 | */ |
---|
64 | T getAdjustedInput(T input) |
---|
65 | { |
---|
66 | if (mDeltaInput) |
---|
67 | { |
---|
68 | mDeltaCount += input; |
---|
69 | // Wrap |
---|
70 | while (mDeltaCount >= 1.0) |
---|
71 | mDeltaCount -= 1.0; |
---|
72 | while (mDeltaCount < 0.0) |
---|
73 | mDeltaCount += 1.0; |
---|
74 | |
---|
75 | return mDeltaCount; |
---|
76 | } |
---|
77 | else |
---|
78 | { |
---|
79 | return input; |
---|
80 | } |
---|
81 | } |
---|
82 | |
---|
83 | public: |
---|
84 | /** Constructor. |
---|
85 | @param |
---|
86 | deltaInput If true, signifies that the input will be a delta value such that the function should |
---|
87 | add it to an internal counter before calculating the output. |
---|
88 | */ |
---|
89 | ControllerFunction(bool deltaInput) |
---|
90 | { |
---|
91 | mDeltaInput = deltaInput; |
---|
92 | mDeltaCount = 0; |
---|
93 | } |
---|
94 | |
---|
95 | virtual ~ControllerFunction() {} |
---|
96 | |
---|
97 | virtual T calculate(T sourceValue) = 0; |
---|
98 | }; |
---|
99 | |
---|
100 | |
---|
101 | /** Can either be used as an input or output value. |
---|
102 | */ |
---|
103 | template <typename T> |
---|
104 | class ControllerValue : public ControllerAlloc |
---|
105 | { |
---|
106 | |
---|
107 | public: |
---|
108 | virtual ~ControllerValue() { } |
---|
109 | virtual T getValue(void) const = 0; |
---|
110 | virtual void setValue(T value) = 0; |
---|
111 | |
---|
112 | }; |
---|
113 | |
---|
114 | /** Instances of this class 'control' the value of another object in the system. |
---|
115 | @remarks |
---|
116 | Controller classes are used to manage the values of object automatically based |
---|
117 | on the value of some input. For example, a Controller could animate a texture |
---|
118 | by controlling the current frame of the texture based on time, or a different Controller |
---|
119 | could change the colour of a material used for a spaceship shield mesh based on the remaining |
---|
120 | shield power level of the ship. |
---|
121 | @par |
---|
122 | The Controller is an intentionally abstract concept - it can generate values |
---|
123 | based on input and a function, which can either be one of the standard ones |
---|
124 | supplied, or a function can be 'plugged in' for custom behaviour - see the ControllerFunction class for details. |
---|
125 | Both the input and output values are via ControllerValue objects, meaning that any value can be both |
---|
126 | input and output of the controller. |
---|
127 | @par |
---|
128 | Whilst this is very flexible, it can be a little bit confusing so to make it simpler the most often used |
---|
129 | controller setups are available by calling methods on the ControllerManager object. |
---|
130 | @see |
---|
131 | ControllerFunction |
---|
132 | |
---|
133 | */ |
---|
134 | template <typename T> |
---|
135 | class Controller : public ControllerAlloc |
---|
136 | { |
---|
137 | protected: |
---|
138 | /// Source value |
---|
139 | SharedPtr< ControllerValue<T> > mSource; |
---|
140 | /// Destination value |
---|
141 | SharedPtr< ControllerValue<T> > mDest; |
---|
142 | /// Function |
---|
143 | SharedPtr< ControllerFunction<T> > mFunc; |
---|
144 | /// Controller is enabled or not |
---|
145 | bool mEnabled; |
---|
146 | |
---|
147 | |
---|
148 | public: |
---|
149 | |
---|
150 | /** Usual constructor. |
---|
151 | @remarks |
---|
152 | Requires source and destination values, and a function object. None of these are destroyed |
---|
153 | with the Controller when it is deleted (they can be shared) so you must delete these as appropriate. |
---|
154 | */ |
---|
155 | Controller(const SharedPtr< ControllerValue<T> >& src, |
---|
156 | const SharedPtr< ControllerValue<T> >& dest, const SharedPtr< ControllerFunction<T> >& func) |
---|
157 | : mSource(src), mDest(dest), mFunc(func) |
---|
158 | { |
---|
159 | mEnabled = true; |
---|
160 | } |
---|
161 | |
---|
162 | /** Default d-tor. |
---|
163 | */ |
---|
164 | virtual ~Controller() {} |
---|
165 | |
---|
166 | |
---|
167 | /// Sets the input controller value |
---|
168 | void setSource(const SharedPtr< ControllerValue<T> >& src) |
---|
169 | { |
---|
170 | mSource = src; |
---|
171 | } |
---|
172 | /// Gets the input controller value |
---|
173 | const SharedPtr< ControllerValue<T> >& getSource(void) const |
---|
174 | { |
---|
175 | return mSource; |
---|
176 | } |
---|
177 | /// Sets the output controller value |
---|
178 | void setDestination(const SharedPtr< ControllerValue<T> >& dest) |
---|
179 | { |
---|
180 | mDest = dest; |
---|
181 | } |
---|
182 | |
---|
183 | /// Gets the output controller value |
---|
184 | const SharedPtr< ControllerValue<T> >& getDestination(void) const |
---|
185 | { |
---|
186 | return mDest; |
---|
187 | } |
---|
188 | |
---|
189 | /// Returns true if this controller is currently enabled |
---|
190 | bool getEnabled(void) const |
---|
191 | { |
---|
192 | return mEnabled; |
---|
193 | } |
---|
194 | |
---|
195 | /// Sets whether this controller is enabled |
---|
196 | void setEnabled(bool enabled) |
---|
197 | { |
---|
198 | mEnabled = enabled; |
---|
199 | } |
---|
200 | |
---|
201 | /** Sets the function object to be used by this controller. |
---|
202 | */ |
---|
203 | void setFunction(const SharedPtr< ControllerFunction<T> >& func) |
---|
204 | { |
---|
205 | mFunc = func; |
---|
206 | } |
---|
207 | |
---|
208 | /** Returns a pointer to the function object used by this controller. |
---|
209 | */ |
---|
210 | const SharedPtr< ControllerFunction<T> >& getFunction(void) const |
---|
211 | { |
---|
212 | return mFunc; |
---|
213 | } |
---|
214 | |
---|
215 | /** Tells this controller to map it's input controller value |
---|
216 | to it's output controller value, via the controller function. |
---|
217 | @remarks |
---|
218 | This method is called automatically every frame by ControllerManager. |
---|
219 | */ |
---|
220 | void update(void) |
---|
221 | { |
---|
222 | if(mEnabled) |
---|
223 | mDest->setValue(mFunc->calculate(mSource->getValue())); |
---|
224 | } |
---|
225 | |
---|
226 | }; |
---|
227 | |
---|
228 | /** @} */ |
---|
229 | /** @} */ |
---|
230 | |
---|
231 | } |
---|
232 | |
---|
233 | #endif |
---|