update dev300-m58
[ooovba.git] / sdext / source / presenter / PresenterAnimator.cxx
blob5c67a68bef2155c8d170be584ecdcc5f54ca393e
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: PresenterAnimator.cxx,v $
11 * $Revision: 1.5 $
13 * This file is part of OpenOffice.org.
15 * OpenOffice.org is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License version 3
17 * only, as published by the Free Software Foundation.
19 * OpenOffice.org is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License version 3 for more details
23 * (a copy is included in the LICENSE file that accompanied this code).
25 * You should have received a copy of the GNU Lesser General Public License
26 * version 3 along with OpenOffice.org. If not, see
27 * <http://www.openoffice.org/license.html>
28 * for a copy of the LGPLv3 License.
30 ************************************************************************/
32 // MARKER(update_precomp.py): autogen include statement, do not remove
33 #include "precompiled_sdext.hxx"
35 #include "PresenterAnimator.hxx"
37 #include "PresenterTimer.hxx"
38 #include <osl/diagnose.h>
39 #include <osl/time.h>
40 #include <vos/timer.hxx>
41 #include <boost/bind.hpp>
42 #include <boost/function.hpp>
44 namespace sdext { namespace presenter {
48 //===== PresenterAnimator =====================================================
50 PresenterAnimator::PresenterAnimator (void)
51 : maFutureAnimations(),
52 maActiveAnimations(),
53 mnCurrentTaskId(0),
54 mnNextTime(0)
61 PresenterAnimator::~PresenterAnimator (void)
63 PresenterTimer::CancelTask(mnCurrentTaskId);
70 void PresenterAnimator::AddAnimation (const SharedPresenterAnimation& rpAnimation)
72 ::osl::MutexGuard aGuard (m_aMutex);
74 maFutureAnimations.insert(AnimationList::value_type(rpAnimation->GetStartTime(), rpAnimation));
75 ScheduleNextRun();
81 void PresenterAnimator::Process (void)
83 ::osl::MutexGuard aGuard (m_aMutex);
85 mnNextTime = 0;
87 const sal_uInt64 nCurrentTime (GetCurrentTime());
89 ActivateAnimations(nCurrentTime);
91 while ( ! maActiveAnimations.empty())
93 sal_uInt64 nRequestedTime (maActiveAnimations.begin()->first);
94 SharedPresenterAnimation pAnimation (maActiveAnimations.begin()->second);
96 if (nRequestedTime > nCurrentTime)
97 break;
99 maActiveAnimations.erase(maActiveAnimations.begin());
101 const double nTotalDuration (double(pAnimation->GetEndTime() - pAnimation->GetStartTime()));
102 double nProgress (nTotalDuration > 0 ? (nCurrentTime - pAnimation->GetStartTime()) / nTotalDuration : 1);
103 if (nProgress <= 0)
104 nProgress = 0;
105 else if (nProgress >= 1)
106 nProgress = 1;
108 OSL_TRACE("running animation step at %f (requested was %f) %f\n",
109 nCurrentTime/1e6, nRequestedTime/1e6, nProgress);
110 pAnimation->Run(nProgress, nCurrentTime);
112 if (nCurrentTime < pAnimation->GetEndTime())
113 maActiveAnimations.insert(
114 AnimationList::value_type(
115 nCurrentTime + pAnimation->GetStepDuration(),
116 pAnimation));
117 else
118 pAnimation->RunEndCallbacks();
121 ScheduleNextRun();
127 void PresenterAnimator::ActivateAnimations (const sal_uInt64 nCurrentTime)
129 while ( ! maFutureAnimations.empty()
130 && maFutureAnimations.begin()->first <= nCurrentTime)
132 SharedPresenterAnimation pAnimation (maFutureAnimations.begin()->second);
133 maActiveAnimations.insert(*maFutureAnimations.begin());
134 maFutureAnimations.erase(maFutureAnimations.begin());
135 pAnimation->RunStartCallbacks();
142 void PresenterAnimator::ScheduleNextRun (void)
144 sal_uInt64 nStartTime (0);
146 if ( ! maActiveAnimations.empty())
148 nStartTime = maActiveAnimations.begin()->first;
149 if ( ! maFutureAnimations.empty())
150 if (maFutureAnimations.begin()->first < nStartTime)
151 nStartTime = maFutureAnimations.begin()->first;
153 else if ( ! maFutureAnimations.empty())
154 nStartTime = maFutureAnimations.begin()->first;
156 if (nStartTime > 0)
157 ScheduleNextRun(nStartTime);
163 void PresenterAnimator::ScheduleNextRun (const sal_uInt64 nStartTime)
165 if (mnNextTime==0 || nStartTime<mnNextTime)
167 mnNextTime = nStartTime;
168 ::vos::TTimeValue aTimeValue (GetSeconds(mnNextTime), GetNanoSeconds(mnNextTime));
169 PresenterTimer::ScheduleSingleTaskAbsolute (
170 ::boost::bind(&PresenterAnimator::Process, this),
171 aTimeValue);
175 } } // end of namespace ::sdext::presenter