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-2014 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 #include "OgreStableHeaders.h"
29 #include "OgreControllerManager.h"
30 
31 #include "OgrePredefinedControllers.h"
32 
33 namespace Ogre {
34     //-----------------------------------------------------------------------
35     template<> ControllerManager* Singleton<ControllerManager>::msSingleton = 0;
getSingletonPtr(void)36     ControllerManager* ControllerManager::getSingletonPtr(void)
37     {
38         return msSingleton;
39     }
getSingleton(void)40     ControllerManager& ControllerManager::getSingleton(void)
41     {
42         assert( msSingleton );  return ( *msSingleton );
43     }
44     //-----------------------------------------------------------------------
ControllerManager()45     ControllerManager::ControllerManager()
46         : mFrameTimeController(OGRE_NEW FrameTimeControllerValue())
47         , mPassthroughFunction(OGRE_NEW PassthroughControllerFunction())
48         , mLastFrameNumber(0)
49     {
50 
51     }
52     //-----------------------------------------------------------------------
~ControllerManager()53     ControllerManager::~ControllerManager()
54     {
55         clearControllers();
56     }
57     //-----------------------------------------------------------------------
createController(const ControllerValueRealPtr & src,const ControllerValueRealPtr & dest,const ControllerFunctionRealPtr & func)58     Controller<Real>* ControllerManager::createController(
59         const ControllerValueRealPtr& src, const ControllerValueRealPtr& dest,
60         const ControllerFunctionRealPtr& func)
61     {
62         Controller<Real>* c = OGRE_NEW Controller<Real>(src, dest, func);
63 
64         mControllers.insert(c);
65         return c;
66     }
67     //-----------------------------------------------------------------------
createFrameTimePassthroughController(const ControllerValueRealPtr & dest)68     Controller<Real>* ControllerManager::createFrameTimePassthroughController(
69         const ControllerValueRealPtr& dest)
70     {
71         return createController(getFrameTimeSource(), dest, getPassthroughControllerFunction());
72     }
73     //-----------------------------------------------------------------------
updateAllControllers(void)74     void ControllerManager::updateAllControllers(void)
75     {
76         // Only update once per frame
77         unsigned long thisFrameNumber = Root::getSingleton().getNextFrameNumber();
78         if (thisFrameNumber != mLastFrameNumber)
79         {
80             ControllerList::const_iterator ci;
81             for (ci = mControllers.begin(); ci != mControllers.end(); ++ci)
82             {
83                 (*ci)->update();
84             }
85             mLastFrameNumber = thisFrameNumber;
86         }
87     }
88     //-----------------------------------------------------------------------
clearControllers(void)89     void ControllerManager::clearControllers(void)
90     {
91         ControllerList::iterator ci;
92         for (ci = mControllers.begin(); ci != mControllers.end(); ++ci)
93         {
94             OGRE_DELETE *ci;
95         }
96         mControllers.clear();
97     }
98     //-----------------------------------------------------------------------
getFrameTimeSource(void) const99     const ControllerValueRealPtr& ControllerManager::getFrameTimeSource(void) const
100     {
101         return mFrameTimeController;
102     }
103     //-----------------------------------------------------------------------
getPassthroughControllerFunction(void) const104     const ControllerFunctionRealPtr& ControllerManager::getPassthroughControllerFunction(void) const
105     {
106         return mPassthroughFunction;
107     }
108     //-----------------------------------------------------------------------
createTextureAnimator(TextureUnitState * layer,Real sequenceTime)109     Controller<Real>* ControllerManager::createTextureAnimator(TextureUnitState* layer, Real sequenceTime)
110     {
111         return createController(mFrameTimeController, TextureFrameControllerValue::create(layer),
112                                 AnimationControllerFunction::create(sequenceTime));
113     }
114     //-----------------------------------------------------------------------
createTextureUVScroller(TextureUnitState * layer,Real speed)115     Controller<Real>* ControllerManager::createTextureUVScroller(TextureUnitState* layer, Real speed)
116     {
117         Controller<Real>* ret = 0;
118 
119         if (speed != 0)
120         {
121             // We do both scrolls with a single controller
122             // Create function: use -speed since we're altering texture coords so they have reverse effect
123             ret = createController(mFrameTimeController, TexCoordModifierControllerValue::create(layer, true, true),
124                                    ScaleControllerFunction::create(-speed, true));
125         }
126 
127         return ret;
128     }
129     //-----------------------------------------------------------------------
createTextureUScroller(TextureUnitState * layer,Real uSpeed)130     Controller<Real>* ControllerManager::createTextureUScroller(TextureUnitState* layer, Real uSpeed)
131     {
132         Controller<Real>* ret = 0;
133 
134         if (uSpeed != 0)
135         {
136             // Create function: use -speed since we're altering texture coords so they have reverse effect
137             ret = createController(mFrameTimeController, TexCoordModifierControllerValue::create(layer, true),
138                                    ScaleControllerFunction::create(-uSpeed, true));
139         }
140 
141         return ret;
142     }
143     //-----------------------------------------------------------------------
createTextureVScroller(TextureUnitState * layer,Real vSpeed)144     Controller<Real>* ControllerManager::createTextureVScroller(TextureUnitState* layer, Real vSpeed)
145     {
146         Controller<Real>* ret = 0;
147 
148         if (vSpeed != 0)
149         {
150             // Set up a second controller for v scroll
151             // Create function: use -speed since we're altering texture coords so they have reverse effect
152             ret = createController(mFrameTimeController, TexCoordModifierControllerValue::create(layer, false, true),
153                                    ScaleControllerFunction::create(-vSpeed, true));
154         }
155 
156         return ret;
157     }
158     //-----------------------------------------------------------------------
createTextureRotater(TextureUnitState * layer,Real speed)159     Controller<Real>* ControllerManager::createTextureRotater(TextureUnitState* layer, Real speed)
160     {
161         // Target value is texture coord rotation
162         // Function is simple scale (seconds * speed)
163         // Use -speed since altering texture coords has the reverse visible effect
164         return createController(mFrameTimeController,
165                                 TexCoordModifierControllerValue::create(layer, false, false, false, false, true),
166                                 ScaleControllerFunction::create(-speed, true));
167     }
168     //-----------------------------------------------------------------------
createTextureWaveTransformer(TextureUnitState * layer,TextureUnitState::TextureTransformType ttype,WaveformType waveType,Real base,Real frequency,Real phase,Real amplitude)169     Controller<Real>* ControllerManager::createTextureWaveTransformer(TextureUnitState* layer,
170         TextureUnitState::TextureTransformType ttype, WaveformType waveType, Real base, Real frequency, Real phase, Real amplitude)
171     {
172         ControllerValueRealPtr val;
173 
174         switch (ttype)
175         {
176         case TextureUnitState::TT_TRANSLATE_U:
177             // Target value is a u scroll
178             val = TexCoordModifierControllerValue::create(layer, true);
179             break;
180         case TextureUnitState::TT_TRANSLATE_V:
181             // Target value is a v scroll
182             val = TexCoordModifierControllerValue::create(layer, false, true);
183             break;
184         case TextureUnitState::TT_SCALE_U:
185             // Target value is a u scale
186             val = TexCoordModifierControllerValue::create(layer, false, false, true);
187             break;
188         case TextureUnitState::TT_SCALE_V:
189             // Target value is a v scale
190             val = TexCoordModifierControllerValue::create(layer, false, false, false, true);
191             break;
192         case TextureUnitState::TT_ROTATE:
193             // Target value is texture coord rotation
194             val = TexCoordModifierControllerValue::create(layer, false, false, false, false, true);
195             break;
196         }
197         // Create new wave function for alterations
198         return createController(mFrameTimeController, val,
199                                 WaveformControllerFunction::create(waveType, base, frequency, phase, amplitude, true));
200     }
201     //-----------------------------------------------------------------------
createGpuProgramTimerParam(GpuProgramParametersSharedPtr params,size_t paramIndex,Real timeFactor)202     Controller<Real>* ControllerManager::createGpuProgramTimerParam(
203         GpuProgramParametersSharedPtr params, size_t paramIndex, Real timeFactor)
204     {
205         return createController(mFrameTimeController, FloatGpuParameterControllerValue::create(params, paramIndex),
206                                 ScaleControllerFunction::create(timeFactor, true));
207     }
208     //-----------------------------------------------------------------------
destroyController(Controller<Real> * controller)209     void ControllerManager::destroyController(Controller<Real>* controller)
210     {
211         ControllerList::iterator i = mControllers.find(controller);
212         if (i != mControllers.end())
213         {
214             mControllers.erase(i);
215             OGRE_DELETE controller;
216         }
217     }
218     //-----------------------------------------------------------------------
getTimeFactor(void) const219     Real ControllerManager::getTimeFactor(void) const {
220         return static_cast<const FrameTimeControllerValue*>(mFrameTimeController.get())->getTimeFactor();
221     }
222     //-----------------------------------------------------------------------
setTimeFactor(Real tf)223     void ControllerManager::setTimeFactor(Real tf) {
224         static_cast<FrameTimeControllerValue*>(mFrameTimeController.get())->setTimeFactor(tf);
225     }
226     //-----------------------------------------------------------------------
getFrameDelay(void) const227     Real ControllerManager::getFrameDelay(void) const {
228         return static_cast<const FrameTimeControllerValue*>(mFrameTimeController.get())->getFrameDelay();
229     }
230     //-----------------------------------------------------------------------
setFrameDelay(Real fd)231     void ControllerManager::setFrameDelay(Real fd) {
232         static_cast<FrameTimeControllerValue*>(mFrameTimeController.get())->setFrameDelay(fd);
233     }
234     //-----------------------------------------------------------------------
getElapsedTime(void) const235     Real ControllerManager::getElapsedTime(void) const
236     {
237         return static_cast<const FrameTimeControllerValue*>(mFrameTimeController.get())->getElapsedTime();
238     }
239     //-----------------------------------------------------------------------
setElapsedTime(Real elapsedTime)240     void ControllerManager::setElapsedTime(Real elapsedTime)
241     {
242         static_cast<FrameTimeControllerValue*>(mFrameTimeController.get())->setElapsedTime(elapsedTime);
243     }
244 }
245