xref: /trunk/main/slideshow/source/engine/shapes/intrinsicanimationactivity.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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 #include <canvas/debug.hxx>
32*cdf0e10cSrcweir #include <tools/diagnose_ex.h>
33*cdf0e10cSrcweir #include <canvas/verbosetrace.hxx>
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #include "drawshapesubsetting.hxx"
36*cdf0e10cSrcweir #include "subsettableshapemanager.hxx"
37*cdf0e10cSrcweir #include "eventqueue.hxx"
38*cdf0e10cSrcweir #include "eventmultiplexer.hxx"
39*cdf0e10cSrcweir #include "intrinsicanimationactivity.hxx"
40*cdf0e10cSrcweir #include "intrinsicanimationeventhandler.hxx"
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir #include <boost/noncopyable.hpp>
43*cdf0e10cSrcweir #include <boost/enable_shared_from_this.hpp>
44*cdf0e10cSrcweir #include <boost/weak_ptr.hpp>
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir namespace slideshow
47*cdf0e10cSrcweir {
48*cdf0e10cSrcweir     namespace internal
49*cdf0e10cSrcweir     {
50*cdf0e10cSrcweir         /** Activity for intrinsic shape animations
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir             This is an Activity interface implementation for intrinsic
53*cdf0e10cSrcweir             shape animations. Intrinsic shape animations are
54*cdf0e10cSrcweir             animations directly within a shape, e.g. drawing layer
55*cdf0e10cSrcweir             animations, or GIF animations.
56*cdf0e10cSrcweir          */
57*cdf0e10cSrcweir         class IntrinsicAnimationActivity : public Activity,
58*cdf0e10cSrcweir                                            public boost::enable_shared_from_this<IntrinsicAnimationActivity>,
59*cdf0e10cSrcweir                                            private boost::noncopyable
60*cdf0e10cSrcweir         {
61*cdf0e10cSrcweir         public:
62*cdf0e10cSrcweir             /** Create an IntrinsicAnimationActivity.
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir                 @param rContext
65*cdf0e10cSrcweir                 Common slideshow objects
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir                 @param rDrawShape
68*cdf0e10cSrcweir                 Shape to control the intrinsic animation for
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir                 @param rWakeupEvent
71*cdf0e10cSrcweir                 Externally generated wakeup event, to set this
72*cdf0e10cSrcweir                 activity to sleep during inter-frame intervals. Must
73*cdf0e10cSrcweir                 come frome the outside, since wakeup event and this
74*cdf0e10cSrcweir                 object have mutual references to each other.
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir                 @param rTimeouts
77*cdf0e10cSrcweir                 Vector of timeout values, to wait before the next
78*cdf0e10cSrcweir                 frame is shown.
79*cdf0e10cSrcweir              */
80*cdf0e10cSrcweir             IntrinsicAnimationActivity( const SlideShowContext&         rContext,
81*cdf0e10cSrcweir                                         const DrawShapeSharedPtr&       rDrawShape,
82*cdf0e10cSrcweir                                         const WakeupEventSharedPtr&     rWakeupEvent,
83*cdf0e10cSrcweir                                         const ::std::vector<double>&    rTimeouts,
84*cdf0e10cSrcweir                                         ::std::size_t                   nNumLoops,
85*cdf0e10cSrcweir                                         CycleMode                       eCycleMode );
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir             virtual void dispose();
88*cdf0e10cSrcweir             virtual double calcTimeLag() const;
89*cdf0e10cSrcweir             virtual bool perform();
90*cdf0e10cSrcweir             virtual bool isActive() const;
91*cdf0e10cSrcweir             virtual void dequeued();
92*cdf0e10cSrcweir             virtual void end();
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir             bool enableAnimations();
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir         private:
97*cdf0e10cSrcweir             SlideShowContext                        maContext;
98*cdf0e10cSrcweir             boost::weak_ptr<DrawShape>              mpDrawShape;
99*cdf0e10cSrcweir             WakeupEventSharedPtr                    mpWakeupEvent;
100*cdf0e10cSrcweir             IntrinsicAnimationEventHandlerSharedPtr mpListener;
101*cdf0e10cSrcweir             ::std::vector<double>                   maTimeouts;
102*cdf0e10cSrcweir             CycleMode                               meCycleMode;
103*cdf0e10cSrcweir             ::std::size_t                           mnCurrIndex;
104*cdf0e10cSrcweir             ::std::size_t                           mnNumLoops;
105*cdf0e10cSrcweir             ::std::size_t                           mnLoopCount;
106*cdf0e10cSrcweir             bool                                    mbIsActive;
107*cdf0e10cSrcweir         };
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir         //////////////////////////////////////////////////////////////////////
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir         class IntrinsicAnimationListener : public IntrinsicAnimationEventHandler,
112*cdf0e10cSrcweir                                            private boost::noncopyable
113*cdf0e10cSrcweir         {
114*cdf0e10cSrcweir         public:
115*cdf0e10cSrcweir             explicit IntrinsicAnimationListener( IntrinsicAnimationActivity& rActivity ) :
116*cdf0e10cSrcweir                 mrActivity( rActivity )
117*cdf0e10cSrcweir             {}
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir         private:
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir             virtual bool enableAnimations() { return mrActivity.enableAnimations(); }
122*cdf0e10cSrcweir             virtual bool disableAnimations() { mrActivity.end(); return true; }
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir             IntrinsicAnimationActivity& mrActivity;
125*cdf0e10cSrcweir         };
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir         //////////////////////////////////////////////////////////////////////
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir         IntrinsicAnimationActivity::IntrinsicAnimationActivity( const SlideShowContext&         rContext,
130*cdf0e10cSrcweir                                                                 const DrawShapeSharedPtr&       rDrawShape,
131*cdf0e10cSrcweir                                                                 const WakeupEventSharedPtr&     rWakeupEvent,
132*cdf0e10cSrcweir                                                                 const ::std::vector<double>&    rTimeouts,
133*cdf0e10cSrcweir                                                                 ::std::size_t                   nNumLoops,
134*cdf0e10cSrcweir                                                                 CycleMode                       eCycleMode ) :
135*cdf0e10cSrcweir             maContext( rContext ),
136*cdf0e10cSrcweir             mpDrawShape( rDrawShape ),
137*cdf0e10cSrcweir             mpWakeupEvent( rWakeupEvent ),
138*cdf0e10cSrcweir             mpListener( new IntrinsicAnimationListener(*this) ),
139*cdf0e10cSrcweir             maTimeouts( rTimeouts ),
140*cdf0e10cSrcweir             meCycleMode( eCycleMode ),
141*cdf0e10cSrcweir             mnCurrIndex(0),
142*cdf0e10cSrcweir             mnNumLoops(nNumLoops),
143*cdf0e10cSrcweir             mnLoopCount(0),
144*cdf0e10cSrcweir             mbIsActive(false)
145*cdf0e10cSrcweir         {
146*cdf0e10cSrcweir             ENSURE_OR_THROW( rContext.mpSubsettableShapeManager,
147*cdf0e10cSrcweir                               "IntrinsicAnimationActivity::IntrinsicAnimationActivity(): Invalid shape manager" );
148*cdf0e10cSrcweir             ENSURE_OR_THROW( rDrawShape,
149*cdf0e10cSrcweir                               "IntrinsicAnimationActivity::IntrinsicAnimationActivity(): Invalid draw shape" );
150*cdf0e10cSrcweir             ENSURE_OR_THROW( rWakeupEvent,
151*cdf0e10cSrcweir                               "IntrinsicAnimationActivity::IntrinsicAnimationActivity(): Invalid wakeup event" );
152*cdf0e10cSrcweir             ENSURE_OR_THROW( !rTimeouts.empty(),
153*cdf0e10cSrcweir                               "IntrinsicAnimationActivity::IntrinsicAnimationActivity(): Empty timeout vector" );
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir             maContext.mpSubsettableShapeManager->addIntrinsicAnimationHandler(
156*cdf0e10cSrcweir                 mpListener );
157*cdf0e10cSrcweir         }
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir         void IntrinsicAnimationActivity::dispose()
160*cdf0e10cSrcweir         {
161*cdf0e10cSrcweir             end();
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir             if( mpWakeupEvent )
164*cdf0e10cSrcweir                 mpWakeupEvent->dispose();
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir             maContext.dispose();
167*cdf0e10cSrcweir             mpDrawShape.reset();
168*cdf0e10cSrcweir             mpWakeupEvent.reset();
169*cdf0e10cSrcweir             maTimeouts.clear();
170*cdf0e10cSrcweir             mnCurrIndex = 0;
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir             maContext.mpSubsettableShapeManager->removeIntrinsicAnimationHandler(
173*cdf0e10cSrcweir                 mpListener );
174*cdf0e10cSrcweir         }
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir         double IntrinsicAnimationActivity::calcTimeLag() const
177*cdf0e10cSrcweir         {
178*cdf0e10cSrcweir             return 0.0;
179*cdf0e10cSrcweir         }
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir         bool IntrinsicAnimationActivity::perform()
182*cdf0e10cSrcweir         {
183*cdf0e10cSrcweir             if( !isActive() )
184*cdf0e10cSrcweir                 return false;
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir             DrawShapeSharedPtr pDrawShape( mpDrawShape.lock() );
187*cdf0e10cSrcweir             if( !pDrawShape || !mpWakeupEvent )
188*cdf0e10cSrcweir             {
189*cdf0e10cSrcweir                 // event or draw shape vanished, no sense living on ->
190*cdf0e10cSrcweir                 // commit suicide.
191*cdf0e10cSrcweir                 dispose();
192*cdf0e10cSrcweir                 return false;
193*cdf0e10cSrcweir             }
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir             // mnNumLoops == 0 means infinite looping
196*cdf0e10cSrcweir             if( mnNumLoops != 0 &&
197*cdf0e10cSrcweir                 mnLoopCount >= mnNumLoops )
198*cdf0e10cSrcweir             {
199*cdf0e10cSrcweir                 // #i55294# After finishing the loops, display the first frame
200*cdf0e10cSrcweir                 pDrawShape->setIntrinsicAnimationFrame( 0 );
201*cdf0e10cSrcweir                 maContext.mpSubsettableShapeManager->notifyShapeUpdate( pDrawShape );
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir                 end();
204*cdf0e10cSrcweir 
205*cdf0e10cSrcweir                 return false;
206*cdf0e10cSrcweir             }
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir             ::std::size_t       nNewIndex = 0;
209*cdf0e10cSrcweir             const ::std::size_t nNumFrames(maTimeouts.size());
210*cdf0e10cSrcweir             switch( meCycleMode )
211*cdf0e10cSrcweir             {
212*cdf0e10cSrcweir                 case CYCLE_LOOP:
213*cdf0e10cSrcweir                 {
214*cdf0e10cSrcweir                     pDrawShape->setIntrinsicAnimationFrame( mnCurrIndex );
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir                     mpWakeupEvent->start();
217*cdf0e10cSrcweir                     mpWakeupEvent->setNextTimeout( maTimeouts[mnCurrIndex] );
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir                     mnLoopCount += (mnCurrIndex + 1) / nNumFrames;
220*cdf0e10cSrcweir                     nNewIndex = (mnCurrIndex + 1) % nNumFrames;
221*cdf0e10cSrcweir                     break;
222*cdf0e10cSrcweir                 }
223*cdf0e10cSrcweir 
224*cdf0e10cSrcweir                 case CYCLE_PINGPONGLOOP:
225*cdf0e10cSrcweir                 {
226*cdf0e10cSrcweir                     ::std::size_t nTrueIndex( mnCurrIndex < nNumFrames ?
227*cdf0e10cSrcweir                                               mnCurrIndex :
228*cdf0e10cSrcweir                                               2*nNumFrames - mnCurrIndex - 1 );
229*cdf0e10cSrcweir                     pDrawShape->setIntrinsicAnimationFrame( nTrueIndex );
230*cdf0e10cSrcweir 
231*cdf0e10cSrcweir                     mpWakeupEvent->start();
232*cdf0e10cSrcweir                     mpWakeupEvent->setNextTimeout( maTimeouts[nTrueIndex] );
233*cdf0e10cSrcweir 
234*cdf0e10cSrcweir                     mnLoopCount += (mnCurrIndex + 1) / (2*nNumFrames);
235*cdf0e10cSrcweir                     nNewIndex = (mnCurrIndex + 1) % 2*nNumFrames;
236*cdf0e10cSrcweir                     break;
237*cdf0e10cSrcweir                 }
238*cdf0e10cSrcweir             }
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir             maContext.mrEventQueue.addEvent( mpWakeupEvent );
241*cdf0e10cSrcweir             maContext.mpSubsettableShapeManager->notifyShapeUpdate( pDrawShape );
242*cdf0e10cSrcweir             mnCurrIndex = nNewIndex;
243*cdf0e10cSrcweir 
244*cdf0e10cSrcweir             return false; // don't reinsert, WakeupEvent will perform
245*cdf0e10cSrcweir                           // that after the given timeout
246*cdf0e10cSrcweir         }
247*cdf0e10cSrcweir 
248*cdf0e10cSrcweir         bool IntrinsicAnimationActivity::isActive() const
249*cdf0e10cSrcweir         {
250*cdf0e10cSrcweir             return mbIsActive;
251*cdf0e10cSrcweir         }
252*cdf0e10cSrcweir 
253*cdf0e10cSrcweir         void IntrinsicAnimationActivity::dequeued()
254*cdf0e10cSrcweir         {
255*cdf0e10cSrcweir             // not used here
256*cdf0e10cSrcweir         }
257*cdf0e10cSrcweir 
258*cdf0e10cSrcweir         void IntrinsicAnimationActivity::end()
259*cdf0e10cSrcweir         {
260*cdf0e10cSrcweir             // there is no dedicated end state, just become inactive:
261*cdf0e10cSrcweir             mbIsActive = false;
262*cdf0e10cSrcweir         }
263*cdf0e10cSrcweir 
264*cdf0e10cSrcweir         bool IntrinsicAnimationActivity::enableAnimations()
265*cdf0e10cSrcweir         {
266*cdf0e10cSrcweir             mbIsActive = true;
267*cdf0e10cSrcweir             return maContext.mrActivitiesQueue.addActivity(
268*cdf0e10cSrcweir                 shared_from_this() );
269*cdf0e10cSrcweir         }
270*cdf0e10cSrcweir 
271*cdf0e10cSrcweir         //////////////////////////////////////////////////////////////////////
272*cdf0e10cSrcweir 
273*cdf0e10cSrcweir         ActivitySharedPtr createIntrinsicAnimationActivity(
274*cdf0e10cSrcweir             const SlideShowContext&         rContext,
275*cdf0e10cSrcweir             const DrawShapeSharedPtr&       rDrawShape,
276*cdf0e10cSrcweir             const WakeupEventSharedPtr&     rWakeupEvent,
277*cdf0e10cSrcweir             const ::std::vector<double>&    rTimeouts,
278*cdf0e10cSrcweir             ::std::size_t                   nNumLoops,
279*cdf0e10cSrcweir             CycleMode                       eCycleMode )
280*cdf0e10cSrcweir         {
281*cdf0e10cSrcweir             return ActivitySharedPtr(
282*cdf0e10cSrcweir                 new IntrinsicAnimationActivity(rContext,
283*cdf0e10cSrcweir                                                rDrawShape,
284*cdf0e10cSrcweir                                                rWakeupEvent,
285*cdf0e10cSrcweir                                                rTimeouts,
286*cdf0e10cSrcweir                                                nNumLoops,
287*cdf0e10cSrcweir                                                eCycleMode) );
288*cdf0e10cSrcweir         }
289*cdf0e10cSrcweir     }
290*cdf0e10cSrcweir }
291