xref: /trunk/main/svx/source/sdr/animation/animationstate.cxx (revision cf6516809c57e1bb0a940545cca99cdad54d4ce2)
1*f6e50924SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*f6e50924SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*f6e50924SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*f6e50924SAndrew Rist  * distributed with this work for additional information
6*f6e50924SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*f6e50924SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*f6e50924SAndrew Rist  * "License"); you may not use this file except in compliance
9*f6e50924SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*f6e50924SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*f6e50924SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*f6e50924SAndrew Rist  * software distributed under the License is distributed on an
15*f6e50924SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*f6e50924SAndrew Rist  * KIND, either express or implied.  See the License for the
17*f6e50924SAndrew Rist  * specific language governing permissions and limitations
18*f6e50924SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*f6e50924SAndrew Rist  *************************************************************/
21*f6e50924SAndrew Rist 
22*f6e50924SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_svx.hxx"
26cdf0e10cSrcweir #include <svx/sdr/animation/animationstate.hxx>
27cdf0e10cSrcweir #include <tools/debug.hxx>
28cdf0e10cSrcweir #include <svx/sdr/contact/viewobjectcontact.hxx>
29cdf0e10cSrcweir #include <svx/sdr/animation/objectanimator.hxx>
30cdf0e10cSrcweir #include <svx/sdr/contact/objectcontact.hxx>
31cdf0e10cSrcweir #include <svx/sdr/contact/viewcontact.hxx>
32cdf0e10cSrcweir #include <drawinglayer/primitive2d/animatedprimitive2d.hxx>
33cdf0e10cSrcweir #include <drawinglayer/animation/animationtiming.hxx>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
36cdf0e10cSrcweir 
37cdf0e10cSrcweir namespace sdr
38cdf0e10cSrcweir {
39cdf0e10cSrcweir     namespace animation
40cdf0e10cSrcweir     {
getSmallestNextTime(double fCurrentTime)41cdf0e10cSrcweir         double PrimitiveAnimation::getSmallestNextTime(double fCurrentTime)
42cdf0e10cSrcweir         {
43cdf0e10cSrcweir             double fRetval(0.0);
44cdf0e10cSrcweir 
45cdf0e10cSrcweir             if(maAnimatedPrimitives.hasElements())
46cdf0e10cSrcweir             {
47cdf0e10cSrcweir                 const sal_Int32 nCount(maAnimatedPrimitives.getLength());
48cdf0e10cSrcweir 
49cdf0e10cSrcweir                 for(sal_Int32 a(0L); a < nCount; a++)
50cdf0e10cSrcweir                 {
51cdf0e10cSrcweir                     const drawinglayer::primitive2d::Primitive2DReference xRef(maAnimatedPrimitives[a]);
52cdf0e10cSrcweir                     const drawinglayer::primitive2d::AnimatedSwitchPrimitive2D* pCandidate = dynamic_cast< const drawinglayer::primitive2d::AnimatedSwitchPrimitive2D* >(xRef.get());
53cdf0e10cSrcweir                     OSL_ENSURE(pCandidate, "PrimitiveAnimation::getSmallestNextTime: wrong primitive in animated list (!)");
54cdf0e10cSrcweir 
55cdf0e10cSrcweir                     if(pCandidate)
56cdf0e10cSrcweir                     {
57cdf0e10cSrcweir                         const drawinglayer::animation::AnimationEntry& rAnimEntry = pCandidate->getAnimationEntry();
58cdf0e10cSrcweir                         const double fNextTime(rAnimEntry.getNextEventTime(fCurrentTime));
59cdf0e10cSrcweir 
60cdf0e10cSrcweir                         if(!::basegfx::fTools::equalZero(fNextTime))
61cdf0e10cSrcweir                         {
62cdf0e10cSrcweir                             if(::basegfx::fTools::equalZero(fRetval))
63cdf0e10cSrcweir                             {
64cdf0e10cSrcweir                                 fRetval = fNextTime;
65cdf0e10cSrcweir                             }
66cdf0e10cSrcweir                             else if(::basegfx::fTools::less(fNextTime, fRetval))
67cdf0e10cSrcweir                             {
68cdf0e10cSrcweir                                 fRetval = fNextTime;
69cdf0e10cSrcweir                             }
70cdf0e10cSrcweir                         }
71cdf0e10cSrcweir                     }
72cdf0e10cSrcweir                 }
73cdf0e10cSrcweir             }
74cdf0e10cSrcweir 
75cdf0e10cSrcweir             return fRetval;
76cdf0e10cSrcweir         }
77cdf0e10cSrcweir 
prepareNextEvent()78cdf0e10cSrcweir         void PrimitiveAnimation::prepareNextEvent()
79cdf0e10cSrcweir         {
80cdf0e10cSrcweir             const double fCurrentTime(mrVOContact.GetObjectContact().getPrimitiveAnimator().GetTime());
81cdf0e10cSrcweir             const double fNextTime(getSmallestNextTime(fCurrentTime));
82cdf0e10cSrcweir 
83cdf0e10cSrcweir             // getSmallestNextTime will be zero when animation ended. If not zero, a next step
84cdf0e10cSrcweir             // exists
85cdf0e10cSrcweir             if(!::basegfx::fTools::equalZero(fNextTime))
86cdf0e10cSrcweir             {
87cdf0e10cSrcweir                 // next time point exists, use it
88cdf0e10cSrcweir                 sal_uInt32 nNextTime;
89cdf0e10cSrcweir 
90cdf0e10cSrcweir                 if(fNextTime >= (double)0xffffff00)
91cdf0e10cSrcweir                 {
92cdf0e10cSrcweir                     // take care for very late points in time, e.g. when a text animation stops
93cdf0e10cSrcweir                     // in a defined AnimationEntryFixed with endless (0xffffffff) duration
94cdf0e10cSrcweir                     nNextTime = GetTime() + (1000 * 60 * 60); // one hour, works with vcl timers, 0xffffff00 was too much...
95cdf0e10cSrcweir                 }
96cdf0e10cSrcweir                 else
97cdf0e10cSrcweir                 {
98cdf0e10cSrcweir                     nNextTime = (sal_uInt32)fNextTime;
99cdf0e10cSrcweir                 }
100cdf0e10cSrcweir 
101cdf0e10cSrcweir                 // ensure step forward in integer timing, the floating step difference maybe smaller than 1.0. Use
102cdf0e10cSrcweir                 // at least 25ms for next step
103cdf0e10cSrcweir                 const sal_uInt32 nMinimumStepTime((sal_uInt32)fCurrentTime + 25L);
104cdf0e10cSrcweir 
105cdf0e10cSrcweir                 if(nNextTime <= nMinimumStepTime)
106cdf0e10cSrcweir                 {
107cdf0e10cSrcweir                     nNextTime = nMinimumStepTime;
108cdf0e10cSrcweir                 }
109cdf0e10cSrcweir 
110cdf0e10cSrcweir                 // set time and reactivate by re-adding to the scheduler
111cdf0e10cSrcweir                 SetTime(nNextTime);
112cdf0e10cSrcweir                 mrVOContact.GetObjectContact().getPrimitiveAnimator().InsertEvent(this);
113cdf0e10cSrcweir             }
114cdf0e10cSrcweir         }
115cdf0e10cSrcweir 
PrimitiveAnimation(sdr::contact::ViewObjectContact & rVOContact,const drawinglayer::primitive2d::Primitive2DSequence & rAnimatedPrimitives)116cdf0e10cSrcweir         PrimitiveAnimation::PrimitiveAnimation(sdr::contact::ViewObjectContact& rVOContact, const drawinglayer::primitive2d::Primitive2DSequence& rAnimatedPrimitives)
117cdf0e10cSrcweir         :   Event(0L),
118cdf0e10cSrcweir             mrVOContact(rVOContact),
119cdf0e10cSrcweir             maAnimatedPrimitives(rAnimatedPrimitives)
120cdf0e10cSrcweir         {
121cdf0e10cSrcweir             // setup initially
122cdf0e10cSrcweir             prepareNextEvent();
123cdf0e10cSrcweir         }
124cdf0e10cSrcweir 
~PrimitiveAnimation()125cdf0e10cSrcweir         PrimitiveAnimation::~PrimitiveAnimation()
126cdf0e10cSrcweir         {
127cdf0e10cSrcweir             // ensure that Event member is removed from PrimitiveAnimator
128cdf0e10cSrcweir             mrVOContact.GetObjectContact().getPrimitiveAnimator().RemoveEvent(this);
129cdf0e10cSrcweir         }
130cdf0e10cSrcweir 
131cdf0e10cSrcweir         // execute event, from base class Event
Trigger(sal_uInt32)132cdf0e10cSrcweir         void PrimitiveAnimation::Trigger(sal_uInt32 /*nTime*/)
133cdf0e10cSrcweir         {
134cdf0e10cSrcweir             // schedule a repaint of associated object
135cdf0e10cSrcweir             mrVOContact.ActionChanged();
136cdf0e10cSrcweir 
137cdf0e10cSrcweir             // re-setup
138cdf0e10cSrcweir             prepareNextEvent();
139cdf0e10cSrcweir         }
140cdf0e10cSrcweir     } // end of namespace animation
141cdf0e10cSrcweir } // end of namespace sdr
142cdf0e10cSrcweir 
143cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
144cdf0e10cSrcweir // eof
145