Update ooo320-m1
[ooovba.git] / slideshow / source / engine / activities / activitybase.cxx
blob97eeb6c4302dc82746b2164a2fdfd500ecc783f7
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: activitybase.cxx,v $
10 * $Revision: 1.12 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_slideshow.hxx"
34 // must be first
35 #include <canvas/debug.hxx>
36 #include <tools/diagnose_ex.h>
37 #include <canvas/verbosetrace.hxx>
38 #include <canvas/canvastools.hxx>
40 #include <activitybase.hxx>
43 namespace slideshow
45 namespace internal
47 // TODO(P1): Elide some virtual function calls, by templifying this
48 // static hierarchy
50 ActivityBase::ActivityBase( const ActivityParameters& rParms ) :
51 mpEndEvent( rParms.mrEndEvent ),
52 mrEventQueue( rParms.mrEventQueue ),
53 mpShape(),
54 mpAttributeLayer(),
55 maRepeats( rParms.mrRepeats ),
56 mnAccelerationFraction( rParms.mnAccelerationFraction ),
57 mnDecelerationFraction( rParms.mnDecelerationFraction ),
58 mbAutoReverse( rParms.mbAutoReverse ),
59 mbFirstPerformCall( true ),
60 mbIsActive( true ) {}
62 void ActivityBase::dispose()
64 // deactivate
65 mbIsActive = false;
67 // dispose event
68 if( mpEndEvent )
69 mpEndEvent->dispose();
71 // release references
72 mpEndEvent.reset();
73 mpShape.reset();
74 mpAttributeLayer.reset();
77 double ActivityBase::calcTimeLag() const
79 // TODO(Q1): implement different init process!
80 if (isActive() && mbFirstPerformCall)
82 mbFirstPerformCall = false;
84 // notify derived classes that we're
85 // starting now
86 const_cast<ActivityBase *>(this)->startAnimation();
88 return 0.0;
91 bool ActivityBase::perform()
93 // still active?
94 if( !isActive() )
95 return false; // no, early exit.
97 OSL_ASSERT( ! mbFirstPerformCall );
99 return true;
102 bool ActivityBase::isActive() const
104 return mbIsActive;
107 void ActivityBase::setTargets( const AnimatableShapeSharedPtr& rShape,
108 const ShapeAttributeLayerSharedPtr& rAttrLayer )
110 ENSURE_OR_THROW( rShape,
111 "ActivityBase::setTargets(): Invalid shape" );
112 ENSURE_OR_THROW( rAttrLayer,
113 "ActivityBase::setTargets(): Invalid attribute layer" );
115 mpShape = rShape;
116 mpAttributeLayer = rAttrLayer;
119 void ActivityBase::endActivity()
121 // this is a regular activity end
122 mbIsActive = false;
124 // Activity is ending, queue event, then
125 if( mpEndEvent )
126 mrEventQueue.addEvent( mpEndEvent );
128 // release references
129 mpEndEvent.reset();
132 void ActivityBase::dequeued()
134 // xxx todo:
135 // // ignored here, if we're still active. Discrete
136 // // activities are dequeued after every perform() call,
137 // // thus, the call is only significant when isActive() ==
138 // // false.
139 if( !isActive() )
140 endAnimation();
143 void ActivityBase::end()
145 if (!isActive() || isDisposed())
146 return;
147 // assure animation is started:
148 if (mbFirstPerformCall) {
149 mbFirstPerformCall = false;
150 // notify derived classes that we're starting now
151 this->startAnimation();
154 performEnd(); // calling private virtual
155 endAnimation();
156 endActivity();
159 double ActivityBase::calcAcceleratedTime( double nT ) const
161 // Handle acceleration/deceleration
162 // ================================
164 // clamp nT to permissible [0,1] range
165 nT = ::canvas::tools::clamp( nT, 0.0, 1.0 );
167 // take acceleration/deceleration into account. if the sum
168 // of mnAccelerationFraction and mnDecelerationFraction
169 // exceeds 1.0, ignore both (that's according to SMIL spec)
170 if( (mnAccelerationFraction > 0.0 ||
171 mnDecelerationFraction > 0.0) &&
172 mnAccelerationFraction + mnDecelerationFraction <= 1.0 )
175 // calc accelerated/decelerated time.
177 // We have three intervals:
178 // 1 [0,a]
179 // 2 [a,d]
180 // 3 [d,1] (with a and d being acceleration/deceleration
181 // fraction, resp.)
183 // The change rate during interval 1 is constantly
184 // increasing, reaching 1 at a. It then stays at 1,
185 // starting a linear decrease at d, ending with 0 at
186 // time 1. The integral of this function is the
187 // required new time nT'.
189 // As we arbitrarily assumed 1 as the upper value of
190 // the change rate, the integral must be normalized to
191 // reach nT'=1 at the end of the interval. This
192 // normalization constant is:
194 // c = 1 - 0.5a - 0.5d
196 // The integral itself then amounts to:
198 // 0.5 nT^2 / a + (nT-a) + (nT - 0.5 nT^2 / d)
200 // (where each of the three summands correspond to the
201 // three intervals above, and are applied only if nT
202 // has reached the corresponding interval)
204 // The graph of the change rate is a trapezoid:
206 // |
207 // 1| /--------------\
208 // | / \
209 // | / \
210 // | / \
211 // -----------------------------
212 // 0 a d 1
214 //*/
215 const double nC( 1.0 - 0.5*mnAccelerationFraction - 0.5*mnDecelerationFraction );
217 // this variable accumulates the new time value
218 double nTPrime(0.0);
220 if( nT < mnAccelerationFraction )
222 nTPrime += 0.5*nT*nT/mnAccelerationFraction; // partial first interval
224 else
226 nTPrime += 0.5*mnAccelerationFraction; // full first interval
228 if( nT <= 1.0-mnDecelerationFraction )
230 nTPrime += nT-mnAccelerationFraction; // partial second interval
232 else
234 nTPrime += 1.0 - mnAccelerationFraction - mnDecelerationFraction; // full second interval
236 const double nTRelative( nT - 1.0 + mnDecelerationFraction );
238 nTPrime += nTRelative - 0.5*nTRelative*nTRelative / mnDecelerationFraction;
242 // normalize, and assign to work variable
243 nT = nTPrime / nC;
246 return nT;