1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: PresenterAnimator.cxx,v $
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>
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(),
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
));
81 void PresenterAnimator::Process (void)
83 ::osl::MutexGuard
aGuard (m_aMutex
);
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
)
99 maActiveAnimations
.erase(maActiveAnimations
.begin());
101 const double nTotalDuration (double(pAnimation
->GetEndTime() - pAnimation
->GetStartTime()));
102 double nProgress (nTotalDuration
> 0 ? (nCurrentTime
- pAnimation
->GetStartTime()) / nTotalDuration
: 1);
105 else if (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(),
118 pAnimation
->RunEndCallbacks();
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
;
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),
175 } } // end of namespace ::sdext::presenter