1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #ifndef INCLUDED_SLIDESHOW_SETACTIVITY_HXX
24 #define INCLUDED_SLIDESHOW_SETACTIVITY_HXX
25 
26 // must be first
27 #include <canvas/debug.hxx>
28 #include <tools/diagnose_ex.h>
29 #include <canvas/verbosetrace.hxx>
30 
31 #include "animationactivity.hxx"
32 #include "animation.hxx"
33 #include "animatableshape.hxx"
34 #include "shapeattributelayer.hxx"
35 #include "activitiesfactory.hxx"
36 
37 namespace slideshow {
38 namespace internal {
39 
40 /** Templated setter for animation values
41 
42     This template class implements the AnimationActivity
43     interface, but only the perform() and
44     setAttributeLayer() methods are functional. To be used for set animations.
45 
46     @see AnimationSetNode.
47 */
48 template <class AnimationT>
49 class SetActivity : public AnimationActivity
50 {
51 public:
52     typedef ::boost::shared_ptr< AnimationT >   AnimationSharedPtrT;
53     typedef typename AnimationT::ValueType      ValueT;
54 
SetActivity(const ActivitiesFactory::CommonParameters & rParms,const AnimationSharedPtrT & rAnimation,const ValueT & rToValue)55     SetActivity( const ActivitiesFactory::CommonParameters& rParms,
56                  const AnimationSharedPtrT&                 rAnimation,
57                  const ValueT&                              rToValue )
58         : mpAnimation( rAnimation ),
59           mpShape(),
60           mpAttributeLayer(),
61           mpEndEvent( rParms.mpEndEvent ),
62           mrEventQueue( rParms.mrEventQueue ),
63           maToValue( rToValue ),
64           mbIsActive(true)
65     {
66         ENSURE_OR_THROW( mpAnimation, "Invalid animation" );
67     }
68 
dispose()69     virtual void dispose()
70     {
71         mbIsActive = false;
72         mpAnimation.reset();
73         mpShape.reset();
74         mpAttributeLayer.reset();
75         // discharge end event:
76         if (mpEndEvent && mpEndEvent->isCharged())
77             mpEndEvent->dispose();
78         mpEndEvent.reset();
79     }
80 
calcTimeLag() const81     virtual double calcTimeLag() const
82     {
83         return 0.0;
84     }
85 
perform()86     virtual bool perform()
87     {
88         if (! isActive())
89             return false;
90         // we're going inactive immediately:
91         mbIsActive = false;
92 
93         if (mpAnimation && mpAttributeLayer && mpShape) {
94             mpAnimation->start( mpShape, mpAttributeLayer );
95             (*mpAnimation)(maToValue);
96             mpAnimation->end();
97         }
98         // fire end event, if any
99         if (mpEndEvent)
100             mrEventQueue.addEvent( mpEndEvent );
101 
102         return false; // don't reinsert
103     }
104 
isActive() const105     virtual bool isActive() const
106     {
107         return mbIsActive;
108     }
109 
dequeued()110     virtual void dequeued()
111     {
112     }
113 
end()114     virtual void end()
115     {
116         perform();
117     }
118 
setTargets(const AnimatableShapeSharedPtr & rShape,const ShapeAttributeLayerSharedPtr & rAttrLayer)119     virtual void setTargets( const AnimatableShapeSharedPtr&        rShape,
120                              const ShapeAttributeLayerSharedPtr&    rAttrLayer )
121     {
122         ENSURE_OR_THROW( rShape, "Invalid shape" );
123         ENSURE_OR_THROW( rAttrLayer, "Invalid attribute layer" );
124 
125         mpShape = rShape;
126         mpAttributeLayer = rAttrLayer;
127     }
128 
129 private:
130     AnimationSharedPtrT             mpAnimation;
131     AnimatableShapeSharedPtr        mpShape;
132     ShapeAttributeLayerSharedPtr    mpAttributeLayer;
133     EventSharedPtr                  mpEndEvent;
134     EventQueue&                     mrEventQueue;
135     ValueT                          maToValue;
136     bool                            mbIsActive;
137 };
138 
makeSetActivity(const ActivitiesFactory::CommonParameters & rParms,const::boost::shared_ptr<AnimationT> & rAnimation,const typename AnimationT::ValueType & rToValue)139 template <class AnimationT> AnimationActivitySharedPtr makeSetActivity(
140     const ActivitiesFactory::CommonParameters& rParms,
141     const ::boost::shared_ptr< AnimationT >&   rAnimation,
142     const typename AnimationT::ValueType&      rToValue )
143 {
144     return AnimationActivitySharedPtr(
145         new SetActivity<AnimationT>(rParms,rAnimation,rToValue) );
146 }
147 
148 } // namespace internal
149 } // namespace presentation
150 
151 #endif /* INCLUDED_SLIDESHOW_SETACTIVITY_HXX */
152