1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_slideshow.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir // must be first
32*cdf0e10cSrcweir #include <canvas/debug.hxx>
33*cdf0e10cSrcweir #include <tools/diagnose_ex.h>
34*cdf0e10cSrcweir #include <canvas/verbosetrace.hxx>
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir #include <discreteactivitybase.hxx>
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir namespace slideshow
40*cdf0e10cSrcweir {
41*cdf0e10cSrcweir     namespace internal
42*cdf0e10cSrcweir     {
43*cdf0e10cSrcweir         DiscreteActivityBase::DiscreteActivityBase( const ActivityParameters& rParms ) :
44*cdf0e10cSrcweir             ActivityBase( rParms ),
45*cdf0e10cSrcweir             mpWakeupEvent( rParms.mpWakeupEvent ),
46*cdf0e10cSrcweir             maDiscreteTimes( rParms.maDiscreteTimes ),
47*cdf0e10cSrcweir             mnSimpleDuration( rParms.mnMinDuration ),
48*cdf0e10cSrcweir             mnCurrPerformCalls( 0 )
49*cdf0e10cSrcweir         {
50*cdf0e10cSrcweir             ENSURE_OR_THROW( mpWakeupEvent,
51*cdf0e10cSrcweir                               "DiscreteActivityBase::DiscreteActivityBase(): Invalid wakeup event" );
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir             ENSURE_OR_THROW( !maDiscreteTimes.empty(),
54*cdf0e10cSrcweir                               "DiscreteActivityBase::DiscreteActivityBase(): time vector is empty, why do you create me?" );
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir #ifdef DBG_UTIL
57*cdf0e10cSrcweir             // check parameters: rDiscreteTimes must be sorted in
58*cdf0e10cSrcweir             // ascending order, and contain values only from the range
59*cdf0e10cSrcweir             // [0,1]
60*cdf0e10cSrcweir             for( ::std::size_t i=1, len=maDiscreteTimes.size(); i<len; ++i )
61*cdf0e10cSrcweir             {
62*cdf0e10cSrcweir                 if( maDiscreteTimes[i] < 0.0 ||
63*cdf0e10cSrcweir                     maDiscreteTimes[i] > 1.0 ||
64*cdf0e10cSrcweir                     maDiscreteTimes[i-1] < 0.0 ||
65*cdf0e10cSrcweir                     maDiscreteTimes[i-1] > 1.0 )
66*cdf0e10cSrcweir                 {
67*cdf0e10cSrcweir                     ENSURE_OR_THROW( false, "DiscreteActivityBase::DiscreteActivityBase(): time values not within [0,1] range!" );
68*cdf0e10cSrcweir                 }
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir                 if( maDiscreteTimes[i-1] > maDiscreteTimes[i] )
71*cdf0e10cSrcweir                     ENSURE_OR_THROW( false, "DiscreteActivityBase::DiscreteActivityBase(): time vector is not sorted in ascending order!" );
72*cdf0e10cSrcweir             }
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir             // TODO(E2): check this also in production code?
75*cdf0e10cSrcweir #endif
76*cdf0e10cSrcweir         }
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir         void DiscreteActivityBase::startAnimation()
79*cdf0e10cSrcweir         {
80*cdf0e10cSrcweir             // start timer on wakeup event
81*cdf0e10cSrcweir             mpWakeupEvent->start();
82*cdf0e10cSrcweir         }
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir         sal_uInt32 DiscreteActivityBase::calcFrameIndex( sal_uInt32 	nCurrCalls,
85*cdf0e10cSrcweir                                                          ::std::size_t 	nVectorSize ) const
86*cdf0e10cSrcweir         {
87*cdf0e10cSrcweir             if( isAutoReverse() )
88*cdf0e10cSrcweir             {
89*cdf0e10cSrcweir                 // every full repeat run consists of one
90*cdf0e10cSrcweir                 // forward and one backward traversal.
91*cdf0e10cSrcweir                 sal_uInt32 nFrameIndex( nCurrCalls % (2*nVectorSize) );
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir                 // nFrameIndex values >= nVectorSize belong to
94*cdf0e10cSrcweir                 // the backward traversal
95*cdf0e10cSrcweir                 if( nFrameIndex >= nVectorSize )
96*cdf0e10cSrcweir                     nFrameIndex = 2*nVectorSize - nFrameIndex; // invert sweep
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir                 return nFrameIndex;
99*cdf0e10cSrcweir             }
100*cdf0e10cSrcweir             else
101*cdf0e10cSrcweir             {
102*cdf0e10cSrcweir                 return nCurrCalls % nVectorSize ;
103*cdf0e10cSrcweir             }
104*cdf0e10cSrcweir         }
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir         sal_uInt32 DiscreteActivityBase::calcRepeatCount( sal_uInt32 	nCurrCalls,
107*cdf0e10cSrcweir                                                           ::std::size_t	nVectorSize ) const
108*cdf0e10cSrcweir         {
109*cdf0e10cSrcweir             if( isAutoReverse() )
110*cdf0e10cSrcweir                 return nCurrCalls / (2*nVectorSize); // we've got 2 cycles per repeat
111*cdf0e10cSrcweir             else
112*cdf0e10cSrcweir                 return nCurrCalls / nVectorSize;
113*cdf0e10cSrcweir         }
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir         bool DiscreteActivityBase::perform()
116*cdf0e10cSrcweir         {
117*cdf0e10cSrcweir             // call base class, for start() calls and end handling
118*cdf0e10cSrcweir             if( !ActivityBase::perform() )
119*cdf0e10cSrcweir                 return false; // done, we're ended
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir             const ::std::size_t nVectorSize( maDiscreteTimes.size() );
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir             // actually perform something
124*cdf0e10cSrcweir             // ==========================
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir             // TODO(Q3): Refactor this mess
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir             // call derived class with current frame index (modulo
129*cdf0e10cSrcweir             // vector size, to cope with repeats)
130*cdf0e10cSrcweir             perform( calcFrameIndex( mnCurrPerformCalls, nVectorSize ),
131*cdf0e10cSrcweir                      calcRepeatCount( mnCurrPerformCalls, nVectorSize ) );
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir             // calc next index
134*cdf0e10cSrcweir             ++mnCurrPerformCalls;
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir             // calc currently reached repeat count
137*cdf0e10cSrcweir             double nCurrRepeat( double(mnCurrPerformCalls) / nVectorSize );
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir             // if auto-reverse is specified, halve the
140*cdf0e10cSrcweir             // effective repeat count, since we pass every
141*cdf0e10cSrcweir             // repeat run twice: once forward, once backward.
142*cdf0e10cSrcweir             if( isAutoReverse() )
143*cdf0e10cSrcweir                 nCurrRepeat /= 2.0;
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir             // schedule next frame, if either repeat is indefinite
146*cdf0e10cSrcweir             // (repeat forever), or we've not yet reached the requested
147*cdf0e10cSrcweir             // repeat count
148*cdf0e10cSrcweir             if( !isRepeatCountValid() ||
149*cdf0e10cSrcweir                 nCurrRepeat < getRepeatCount() )
150*cdf0e10cSrcweir             {
151*cdf0e10cSrcweir                 // add wake-up event to queue (modulo
152*cdf0e10cSrcweir                 // vector size, to cope with repeats).
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir                 // repeat is handled locally, only apply acceleration/deceleration.
155*cdf0e10cSrcweir                 // Scale time vector with simple duration, offset with full repeat
156*cdf0e10cSrcweir                 // times.
157*cdf0e10cSrcweir                 //
158*cdf0e10cSrcweir                 // Somewhat condensed, the argument for setNextTimeout below could
159*cdf0e10cSrcweir                 // be written as
160*cdf0e10cSrcweir                 //
161*cdf0e10cSrcweir                 // mnSimpleDuration*(nFullRepeats + calcAcceleratedTime( currentRepeatTime )),
162*cdf0e10cSrcweir                 //
163*cdf0e10cSrcweir                 // with currentRepeatTime = maDiscreteTimes[ currentRepeatIndex ]
164*cdf0e10cSrcweir                 //
165*cdf0e10cSrcweir                 // Note that calcAcceleratedTime() is only applied to the current repeat's value,
166*cdf0e10cSrcweir                 // not to the total resulting time. This is in accordance with the SMIL spec.
167*cdf0e10cSrcweir                 //
168*cdf0e10cSrcweir                 mpWakeupEvent->setNextTimeout(
169*cdf0e10cSrcweir                     mnSimpleDuration*(
170*cdf0e10cSrcweir                         calcRepeatCount(
171*cdf0e10cSrcweir                             mnCurrPerformCalls,
172*cdf0e10cSrcweir                             nVectorSize ) +
173*cdf0e10cSrcweir                         calcAcceleratedTime(
174*cdf0e10cSrcweir                             maDiscreteTimes[
175*cdf0e10cSrcweir                                 calcFrameIndex(
176*cdf0e10cSrcweir                                     mnCurrPerformCalls,
177*cdf0e10cSrcweir                                     nVectorSize ) ] ) ) );
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir                 getEventQueue().addEvent( mpWakeupEvent );
180*cdf0e10cSrcweir             }
181*cdf0e10cSrcweir             else
182*cdf0e10cSrcweir             {
183*cdf0e10cSrcweir                 // release event reference (relation to wakeup event
184*cdf0e10cSrcweir                 // is circular!)
185*cdf0e10cSrcweir                 mpWakeupEvent.reset();
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir                 // done with this activity
188*cdf0e10cSrcweir                 endActivity();
189*cdf0e10cSrcweir             }
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir             return false; // remove from queue, will be added back by the wakeup event.
192*cdf0e10cSrcweir         }
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir         void DiscreteActivityBase::dispose()
195*cdf0e10cSrcweir         {
196*cdf0e10cSrcweir             // dispose event
197*cdf0e10cSrcweir             if( mpWakeupEvent )
198*cdf0e10cSrcweir                 mpWakeupEvent->dispose();
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir             // release references
201*cdf0e10cSrcweir             mpWakeupEvent.reset();
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir             ActivityBase::dispose();
204*cdf0e10cSrcweir         }
205*cdf0e10cSrcweir     }
206*cdf0e10cSrcweir }
207