Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 :
21 : // must be first
22 : #include <canvas/debug.hxx>
23 : #include <tools/diagnose_ex.h>
24 : #include <canvas/verbosetrace.hxx>
25 : #include <canvas/canvastools.hxx>
26 :
27 : #include <activitybase.hxx>
28 :
29 :
30 : namespace slideshow
31 : {
32 : namespace internal
33 : {
34 : // TODO(P1): Elide some virtual function calls, by templifying this
35 : // static hierarchy
36 :
37 0 : ActivityBase::ActivityBase( const ActivityParameters& rParms ) :
38 : mpEndEvent( rParms.mrEndEvent ),
39 : mrEventQueue( rParms.mrEventQueue ),
40 : mpShape(),
41 : mpAttributeLayer(),
42 : maRepeats( rParms.mrRepeats ),
43 : mnAccelerationFraction( rParms.mnAccelerationFraction ),
44 : mnDecelerationFraction( rParms.mnDecelerationFraction ),
45 : mbAutoReverse( rParms.mbAutoReverse ),
46 : mbFirstPerformCall( true ),
47 0 : mbIsActive( true ) {}
48 :
49 0 : void ActivityBase::dispose()
50 : {
51 : // deactivate
52 0 : mbIsActive = false;
53 :
54 : // dispose event
55 0 : if( mpEndEvent )
56 0 : mpEndEvent->dispose();
57 :
58 : // release references
59 0 : mpEndEvent.reset();
60 0 : mpShape.reset();
61 0 : mpAttributeLayer.reset();
62 0 : }
63 :
64 0 : double ActivityBase::calcTimeLag() const
65 : {
66 : // TODO(Q1): implement different init process!
67 0 : if (isActive() && mbFirstPerformCall)
68 : {
69 0 : mbFirstPerformCall = false;
70 :
71 : // notify derived classes that we're
72 : // starting now
73 0 : const_cast<ActivityBase *>(this)->startAnimation();
74 : }
75 0 : return 0.0;
76 : }
77 :
78 0 : bool ActivityBase::perform()
79 : {
80 : // still active?
81 0 : if( !isActive() )
82 0 : return false; // no, early exit.
83 :
84 : OSL_ASSERT( ! mbFirstPerformCall );
85 :
86 0 : return true;
87 : }
88 :
89 0 : bool ActivityBase::isActive() const
90 : {
91 0 : return mbIsActive;
92 : }
93 :
94 0 : void ActivityBase::setTargets( const AnimatableShapeSharedPtr& rShape,
95 : const ShapeAttributeLayerSharedPtr& rAttrLayer )
96 : {
97 0 : ENSURE_OR_THROW( rShape,
98 : "ActivityBase::setTargets(): Invalid shape" );
99 0 : ENSURE_OR_THROW( rAttrLayer,
100 : "ActivityBase::setTargets(): Invalid attribute layer" );
101 :
102 0 : mpShape = rShape;
103 0 : mpAttributeLayer = rAttrLayer;
104 0 : }
105 :
106 0 : void ActivityBase::endActivity()
107 : {
108 : // this is a regular activity end
109 0 : mbIsActive = false;
110 :
111 : // Activity is ending, queue event, then
112 0 : if( mpEndEvent )
113 0 : mrEventQueue.addEvent( mpEndEvent );
114 :
115 : // release references
116 0 : mpEndEvent.reset();
117 0 : }
118 :
119 0 : void ActivityBase::dequeued()
120 : {
121 : // xxx todo:
122 : // // ignored here, if we're still active. Discrete
123 : // // activities are dequeued after every perform() call,
124 : // // thus, the call is only significant when isActive() ==
125 : // // false.
126 0 : if( !isActive() )
127 0 : endAnimation();
128 0 : }
129 :
130 0 : void ActivityBase::end()
131 : {
132 0 : if (!isActive() || isDisposed())
133 0 : return;
134 : // assure animation is started:
135 0 : if (mbFirstPerformCall) {
136 0 : mbFirstPerformCall = false;
137 : // notify derived classes that we're starting now
138 0 : this->startAnimation();
139 : }
140 :
141 0 : performEnd(); // calling private virtual
142 0 : endAnimation();
143 0 : endActivity();
144 : }
145 :
146 0 : double ActivityBase::calcAcceleratedTime( double nT ) const
147 : {
148 : // Handle acceleration/deceleration
149 : // ================================
150 :
151 : // clamp nT to permissible [0,1] range
152 0 : nT = ::basegfx::clamp( nT, 0.0, 1.0 );
153 :
154 : // take acceleration/deceleration into account. if the sum
155 : // of mnAccelerationFraction and mnDecelerationFraction
156 : // exceeds 1.0, ignore both (that's according to SMIL spec)
157 0 : if( (mnAccelerationFraction > 0.0 ||
158 0 : mnDecelerationFraction > 0.0) &&
159 0 : mnAccelerationFraction + mnDecelerationFraction <= 1.0 )
160 : {
161 : /*
162 : // calc accelerated/decelerated time.
163 :
164 : // We have three intervals:
165 : // 1 [0,a]
166 : // 2 [a,d]
167 : // 3 [d,1] (with a and d being acceleration/deceleration
168 : // fraction, resp.)
169 :
170 : // The change rate during interval 1 is constantly
171 : // increasing, reaching 1 at a. It then stays at 1,
172 : // starting a linear decrease at d, ending with 0 at
173 : // time 1. The integral of this function is the
174 : // required new time nT'.
175 :
176 : // As we arbitrarily assumed 1 as the upper value of
177 : // the change rate, the integral must be normalized to
178 : // reach nT'=1 at the end of the interval. This
179 : // normalization constant is:
180 :
181 : // c = 1 - 0.5a - 0.5d
182 :
183 : // The integral itself then amounts to:
184 :
185 : // 0.5 nT^2 / a + (nT-a) + (nT - 0.5 nT^2 / d)
186 :
187 : // (where each of the three summands correspond to the
188 : // three intervals above, and are applied only if nT
189 : // has reached the corresponding interval)
190 :
191 : // The graph of the change rate is a trapezoid:
192 :
193 : // |
194 : // 1| /--------------\
195 : // | / \
196 : // | / \
197 : // | / \
198 : // -----------------------------
199 : // 0 a d 1
200 :
201 : //*/
202 0 : const double nC( 1.0 - 0.5*mnAccelerationFraction - 0.5*mnDecelerationFraction );
203 :
204 : // this variable accumulates the new time value
205 0 : double nTPrime(0.0);
206 :
207 0 : if( nT < mnAccelerationFraction )
208 : {
209 0 : nTPrime += 0.5*nT*nT/mnAccelerationFraction; // partial first interval
210 : }
211 : else
212 : {
213 0 : nTPrime += 0.5*mnAccelerationFraction; // full first interval
214 :
215 0 : if( nT <= 1.0-mnDecelerationFraction )
216 : {
217 0 : nTPrime += nT-mnAccelerationFraction; // partial second interval
218 : }
219 : else
220 : {
221 0 : nTPrime += 1.0 - mnAccelerationFraction - mnDecelerationFraction; // full second interval
222 :
223 0 : const double nTRelative( nT - 1.0 + mnDecelerationFraction );
224 :
225 0 : nTPrime += nTRelative - 0.5*nTRelative*nTRelative / mnDecelerationFraction;
226 : }
227 : }
228 :
229 : // normalize, and assign to work variable
230 0 : nT = nTPrime / nC;
231 : }
232 :
233 0 : return nT;
234 : }
235 : }
236 0 : }
237 :
238 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|