Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "controller/SlsAnimator.hxx"
21 : #include "view/SlideSorterView.hxx"
22 : #include "View.hxx"
23 : #include <boost/bind.hpp>
24 :
25 : namespace sd { namespace slidesorter { namespace controller {
26 :
27 : namespace {
28 : static const sal_Int32 gnResolution = 25;
29 : }
30 : /** Handle one animation function by using a timer for frequent calls to
31 : the animations operator().
32 : */
33 : class Animator::Animation
34 : {
35 : public:
36 : Animation (
37 : const Animator::AnimationFunctor& rAnimation,
38 : const double nStartOffset,
39 : const double nDuration,
40 : const double nGlobalTime,
41 : const Animator::AnimationId nAnimationId,
42 : const Animator::FinishFunctor& rFinishFunctor);
43 : ~Animation (void);
44 : /** Run next animation step. If animation has reached its end it is
45 : expired.
46 : */
47 : bool Run (const double nGlobalTime);
48 :
49 : /** Typically called when an animation has finished, but also from
50 : Animator::Disposed(). The finish functor is called and the
51 : animation is marked as expired to prevent another run.
52 : */
53 : void Expire (void);
54 : bool IsExpired (void);
55 :
56 : Animator::AnimationFunctor maAnimation;
57 : Animator::FinishFunctor maFinishFunctor;
58 : const Animator::AnimationId mnAnimationId;
59 : const double mnDuration;
60 : const double mnEnd;
61 : const double mnGlobalTimeAtStart;
62 : bool mbIsExpired;
63 : };
64 :
65 :
66 :
67 :
68 0 : Animator::Animator (SlideSorter& rSlideSorter)
69 : : mrSlideSorter(rSlideSorter),
70 : maTimer(),
71 : mbIsDisposed(false),
72 : maAnimations(),
73 : maElapsedTime(),
74 : mpDrawLock(),
75 0 : mnNextAnimationId(0)
76 : {
77 0 : maTimer.SetTimeout(gnResolution);
78 0 : maTimer.SetTimeoutHdl(LINK(this,Animator,TimeoutHandler));
79 0 : }
80 :
81 :
82 :
83 :
84 0 : Animator::~Animator (void)
85 : {
86 0 : if ( ! mbIsDisposed)
87 : {
88 : OSL_ASSERT(mbIsDisposed);
89 0 : Dispose();
90 : }
91 0 : }
92 :
93 :
94 :
95 :
96 0 : void Animator::Dispose (void)
97 : {
98 0 : mbIsDisposed = true;
99 :
100 0 : AnimationList aCopy (maAnimations);
101 0 : AnimationList::const_iterator iAnimation;
102 0 : for (iAnimation=aCopy.begin(); iAnimation!=aCopy.end(); ++iAnimation)
103 0 : (*iAnimation)->Expire();
104 :
105 0 : maTimer.Stop();
106 0 : if (mpDrawLock)
107 : {
108 0 : mpDrawLock->Dispose();
109 0 : mpDrawLock.reset();
110 0 : }
111 0 : }
112 :
113 :
114 :
115 :
116 0 : Animator::AnimationId Animator::AddAnimation (
117 : const AnimationFunctor& rAnimation,
118 : const sal_Int32 nStartOffset,
119 : const sal_Int32 nDuration,
120 : const FinishFunctor& rFinishFunctor)
121 : {
122 : // When the animator is already disposed then ignore this call
123 : // silently (well, we show an assertion, but do not throw an exception.)
124 : OSL_ASSERT( ! mbIsDisposed);
125 0 : if (mbIsDisposed)
126 0 : return -1;
127 :
128 : boost::shared_ptr<Animation> pAnimation (
129 : new Animation(
130 : rAnimation,
131 : nStartOffset / 1000.0,
132 : nDuration / 1000.0,
133 0 : maElapsedTime.getElapsedTime(),
134 : ++mnNextAnimationId,
135 0 : rFinishFunctor));
136 0 : maAnimations.push_back(pAnimation);
137 :
138 0 : RequestNextFrame();
139 :
140 0 : return pAnimation->mnAnimationId;
141 : }
142 :
143 :
144 :
145 :
146 0 : void Animator::RemoveAnimation (const Animator::AnimationId nId)
147 : {
148 : OSL_ASSERT( ! mbIsDisposed);
149 :
150 : const AnimationList::iterator iAnimation (::std::find_if(
151 : maAnimations.begin(),
152 : maAnimations.end(),
153 : ::boost::bind(
154 : ::std::equal_to<Animator::AnimationId>(),
155 : nId,
156 0 : ::boost::bind(&Animation::mnAnimationId, _1))));
157 0 : if (iAnimation != maAnimations.end())
158 : {
159 : OSL_ASSERT((*iAnimation)->mnAnimationId == nId);
160 0 : (*iAnimation)->Expire();
161 0 : maAnimations.erase(iAnimation);
162 : }
163 :
164 0 : if (maAnimations.empty())
165 : {
166 : // Reset the animation id when we can.
167 0 : mnNextAnimationId = 0;
168 :
169 : // No more animations => we do not have to suppress painting
170 : // anymore.
171 0 : mpDrawLock.reset();
172 : }
173 0 : }
174 :
175 :
176 :
177 :
178 0 : void Animator::RemoveAllAnimations (void)
179 : {
180 : ::std::for_each(
181 : maAnimations.begin(),
182 : maAnimations.end(),
183 : ::boost::bind(
184 : &Animation::Expire,
185 0 : _1));
186 0 : maAnimations.clear();
187 0 : mnNextAnimationId = 0;
188 :
189 : // No more animations => we do not have to suppress painting
190 : // anymore.
191 0 : mpDrawLock.reset();
192 0 : }
193 :
194 :
195 :
196 :
197 0 : bool Animator::ProcessAnimations (const double nTime)
198 : {
199 0 : bool bExpired (false);
200 :
201 : OSL_ASSERT( ! mbIsDisposed);
202 0 : if (mbIsDisposed)
203 0 : return bExpired;
204 :
205 :
206 0 : AnimationList aCopy (maAnimations);
207 0 : AnimationList::const_iterator iAnimation;
208 0 : for (iAnimation=aCopy.begin(); iAnimation!=aCopy.end(); ++iAnimation)
209 : {
210 0 : bExpired |= (*iAnimation)->Run(nTime);
211 : }
212 :
213 0 : return bExpired;
214 : }
215 :
216 :
217 :
218 :
219 0 : void Animator::CleanUpAnimationList (void)
220 : {
221 : OSL_ASSERT( ! mbIsDisposed);
222 0 : if (mbIsDisposed)
223 0 : return;
224 :
225 0 : AnimationList aActiveAnimations;
226 :
227 0 : AnimationList::const_iterator iAnimation;
228 0 : for (iAnimation=maAnimations.begin(); iAnimation!=maAnimations.end(); ++iAnimation)
229 : {
230 0 : if ( ! (*iAnimation)->IsExpired())
231 0 : aActiveAnimations.push_back(*iAnimation);
232 : }
233 :
234 0 : maAnimations.swap(aActiveAnimations);
235 : }
236 :
237 :
238 :
239 :
240 0 : void Animator::RequestNextFrame (const double nFrameStart)
241 : {
242 : (void)nFrameStart;
243 0 : if ( ! maTimer.IsActive())
244 : {
245 : // Prevent redraws except for the ones in TimeoutHandler. While the
246 : // Animator is active it will schedule repaints regularly. Repaints
247 : // in between would only lead to visual artifacts.
248 0 : mpDrawLock.reset(new view::SlideSorterView::DrawLock(mrSlideSorter));
249 0 : maTimer.Start();
250 : }
251 0 : }
252 :
253 :
254 :
255 :
256 0 : IMPL_LINK_NOARG(Animator, TimeoutHandler)
257 : {
258 0 : if (mbIsDisposed)
259 0 : return 0;
260 :
261 0 : if (ProcessAnimations(maElapsedTime.getElapsedTime()))
262 0 : CleanUpAnimationList();
263 :
264 : // Unlock the draw lock. This should lead to a repaint.
265 0 : mpDrawLock.reset();
266 :
267 0 : if (!maAnimations.empty())
268 0 : RequestNextFrame();
269 :
270 0 : return 0;
271 : }
272 :
273 :
274 :
275 :
276 : //===== Animator::Animation ===================================================
277 :
278 0 : Animator::Animation::Animation (
279 : const Animator::AnimationFunctor& rAnimation,
280 : const double nStartOffset,
281 : const double nDuration,
282 : const double nGlobalTime,
283 : const Animator::AnimationId nId,
284 : const Animator::FinishFunctor& rFinishFunctor)
285 : : maAnimation(rAnimation),
286 : maFinishFunctor(rFinishFunctor),
287 : mnAnimationId(nId),
288 : mnDuration(nDuration),
289 : mnEnd(nGlobalTime + nDuration + nStartOffset),
290 : mnGlobalTimeAtStart(nGlobalTime + nStartOffset),
291 0 : mbIsExpired(false)
292 : {
293 0 : Run(nGlobalTime);
294 0 : }
295 :
296 :
297 :
298 :
299 0 : Animator::Animation::~Animation (void)
300 : {
301 0 : }
302 :
303 :
304 :
305 :
306 0 : bool Animator::Animation::Run (const double nGlobalTime)
307 : {
308 0 : if ( ! mbIsExpired)
309 : {
310 0 : if (mnDuration > 0)
311 : {
312 0 : if (nGlobalTime >= mnEnd)
313 : {
314 0 : maAnimation(1.0);
315 0 : Expire();
316 : }
317 0 : else if (nGlobalTime >= mnGlobalTimeAtStart)
318 : {
319 0 : maAnimation((nGlobalTime - mnGlobalTimeAtStart) / mnDuration);
320 : }
321 : }
322 0 : else if (mnDuration < 0)
323 : {
324 : // Animations without end have to be expired by their owner.
325 0 : maAnimation(nGlobalTime);
326 : }
327 : }
328 :
329 0 : return mbIsExpired;
330 : }
331 :
332 :
333 :
334 :
335 0 : void Animator::Animation::Expire (void)
336 : {
337 0 : if ( ! mbIsExpired)
338 : {
339 0 : mbIsExpired = true;
340 0 : if (maFinishFunctor)
341 0 : maFinishFunctor();
342 : }
343 0 : }
344 :
345 :
346 :
347 :
348 0 : bool Animator::Animation::IsExpired (void)
349 : {
350 0 : return mbIsExpired;
351 : }
352 :
353 :
354 :
355 :
356 9 : } } } // end of namespace ::sd::slidesorter::controller
357 :
358 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|