fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / Dynamics / Animation / OSGTimeSensor.cpp
blobd465102f3237a924dd993314b787820186287453
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2006 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
18 * *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
37 \*---------------------------------------------------------------------------*/
39 //---------------------------------------------------------------------------
40 // Includes
41 //---------------------------------------------------------------------------
43 #include <cstdlib>
44 #include <cstdio>
46 #include "OSGConfig.h"
48 #include "OSGTimeSensor.h"
50 OSG_BEGIN_NAMESPACE
52 // Documentation for this class is emitted in the
53 // OSGTimeSensorBase.cpp file.
54 // To modify it, please change the .fcd file (OSGTimeSensor.fcd) and
55 // regenerate the base file.
57 /***************************************************************************\
58 * Class variables *
59 \***************************************************************************/
61 /***************************************************************************\
62 * Class methods *
63 \***************************************************************************/
65 void TimeSensor::initMethod(InitPhase ePhase)
67 Inherited::initMethod(ePhase);
69 if(ePhase == TypeObject::SystemPost)
75 /***************************************************************************\
76 * Instance methods *
77 \***************************************************************************/
79 /*-------------------------------------------------------------------------*\
80 - private -
81 \*-------------------------------------------------------------------------*/
83 /*----------------------- constructors & destructors ----------------------*/
85 TimeSensor::TimeSensor(void) :
86 Inherited()
90 TimeSensor::TimeSensor(const TimeSensor &source) :
91 Inherited(source)
95 TimeSensor::~TimeSensor(void)
99 /*----------------------------- class specific ----------------------------*/
101 void TimeSensor::changed(ConstFieldMaskArg whichField,
102 UInt32 origin,
103 BitVector details)
105 if(0x0000 != (whichField & ChangeFractionByFieldMask))
107 if(_sfEnabled .getValue() == false &&
108 _sfChangeFractionBy.getValue() >= -1.0 &&
109 _sfChangeFractionBy.getValue() <= 1.0 )
111 Time tVal = _sfFraction.getValue();
113 tVal += _sfChangeFractionBy.getValue();
115 if(tVal > 1.0)
116 tVal -= 1.0;
118 if(tVal < 0.0)
119 tVal += 1.0;
121 this->setFraction(tVal);
126 Inherited::changed(whichField, origin, details);
129 void TimeSensor::dump( UInt32 uiIndent,
130 const BitVector bvFlags ) const
132 SLOG;
133 indentLog(uiIndent, PLOG);
134 PLOG << "TimeSensor (" << this->getId()
135 << " - " << this
136 << ")\n";
138 SLOG;
139 indentLog(uiIndent + 4, PLOG);
140 PLOG << "enabled [" << _sfEnabled.getValue()
141 << "] active [" << _sfIsActive.getValue()
142 << "] loop [" << _sfLoop.getValue()
143 << "]\n";
145 SLOG;
146 indentLog(uiIndent + 4, PLOG);
147 PLOG << "fraction [" << _sfFraction.getValue()
148 << "] startTime [" << _sfStartTime.getValue()
149 << "] stopTime [" << _sfStopTime.getValue()
150 << "] cycleInterval [" << _sfCycleInterval.getValue()
151 << "] cycleTime [" << _sfCycleTime.getValue()
152 << "] time [" << _sfTime.getValue()
153 << "]" << std::endl;
156 void TimeSensor::frame(Time tTime, UInt32 uiFrame)
158 Real64 dFraction;
160 bool bDoTimeRange = bool(_sfStartTime.getValue() <
161 _sfStopTime .getValue());
163 bool bDoCycle = false;
165 if(bDoTimeRange)
167 dFraction =
168 Real64( tTime - _sfStartTime.getValue()) /
169 Real64(_sfStopTime.getValue() - _sfStartTime.getValue());
171 else
173 if(_sfCycleTime.getValue() > 0.0)
175 dFraction =
176 Real64(tTime - _sfCycleTime.getValue()) /
177 Real64(_sfCycleInterval.getValue());
179 bDoCycle = true;
181 else
183 dFraction =
184 Real64(tTime - _sfStartTime.getValue()) /
185 Real64(_sfCycleInterval.getValue());
189 if(_sfEnabled.getValue() == false)
191 if(_sfIsActive.getValue() == true)
193 setTime (tTime);
194 setIsActive(false);
195 setFraction(Real32(dFraction));
198 else
200 if(dFraction < 0.0) // before start
202 if(bDoCycle == true)
204 if(_sfIsActive.getValue() == true)
206 if(_sfLoop.getValue() == false)
208 setIsActive (false);
209 setTime (tTime);
210 setFraction (1.0 );
211 setCycleTime(tTime);
213 else
215 setCycleTime(_sfCycleTime .getValue() -
216 _sfCycleInterval.getValue());
218 setTime (tTime);
219 setFraction (dFraction + 1.0);
222 else
227 else if(dFraction > 1.0) // after end time
229 if(_sfIsActive.getValue() == true)
231 if(_sfLoop.getValue() == false)
233 setIsActive (false);
234 setTime (0.f );
235 setFraction (1.0 );
236 setCycleTime(0.f );
237 setEnabled (false);
239 else
241 setCycleTime(_sfCycleTime .getValue() +
242 _sfCycleInterval.getValue());
244 setTime (tTime);
245 setFraction (dFraction - 1.0);
248 else
250 if(bDoTimeRange == false)
252 setFraction (0.f);
253 setCycleTime(tTime -
254 (_sfFraction .getValue() *
255 _sfCycleInterval.getValue() ));
257 setIsActive (true);
258 setTime (tTime);
262 else // within cycle
264 if (_sfIsActive.getValue() == false)
266 if(bDoTimeRange == true)
268 setCycleTime(_sfStartTime.getValue());
269 setIsActive (true);
270 setTime (tTime);
271 setFraction (dFraction);
273 else
275 setCycleTime(tTime -
276 (_sfFraction .getValue() *
277 _sfCycleInterval.getValue() ));
279 if(_sfCycleTime.getValue() >
280 TypeTraits<Time>::getDefaultEps())
282 setIsActive(true);
285 setTime(tTime);
288 else
290 if(tTime >= _sfStopTime.getValue() &&
291 bDoTimeRange == true )
293 setIsActive(false);
295 else
297 if(tTime >= _sfStartTime.getValue())
299 setTime (tTime );
300 setFraction(dFraction);
308 OSG_END_NAMESPACE