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 "view/SlsInsertAnimator.hxx"
21 : #include "controller/SlideSorterController.hxx"
22 : #include "controller/SlsAnimationFunction.hxx"
23 : #include "view/SlideSorterView.hxx"
24 : #include "view/SlsLayouter.hxx"
25 : #include "model/SlideSorterModel.hxx"
26 : #include "model/SlsPageEnumerationProvider.hxx"
27 :
28 : #include <set>
29 : #include <boost/bind.hpp>
30 : #include <boost/enable_shared_from_this.hpp>
31 :
32 : namespace sd { namespace slidesorter { namespace view {
33 :
34 : namespace {
35 :
36 : class PageObjectRun;
37 :
38 0 : class AnimatorAccess
39 : {
40 : public:
41 : virtual void AddRun (const ::boost::shared_ptr<PageObjectRun>& rRun) = 0;
42 : virtual void RemoveRun (const ::boost::shared_ptr<PageObjectRun>& rRun) = 0;
43 : virtual model::SlideSorterModel& GetModel (void) const = 0;
44 : virtual view::SlideSorterView& GetView (void) const = 0;
45 : virtual ::boost::shared_ptr<controller::Animator> GetAnimator (void) = 0;
46 : virtual VclPtr<sd::Window> GetContentWindow (void) = 0;
47 :
48 : protected:
49 0 : ~AnimatorAccess() {}
50 : };
51 :
52 : /** Controller of the position offsets of all page objects in one row or one
53 : column.
54 : */
55 : class PageObjectRun : public ::boost::enable_shared_from_this<PageObjectRun>
56 : {
57 : public:
58 : PageObjectRun (
59 : AnimatorAccess& rAnimatorAccess,
60 : const sal_Int32 nRunIndex,
61 : const sal_Int32 nStartIndex,
62 : const sal_Int32 nEndIndex);
63 : ~PageObjectRun();
64 :
65 : void operator () (const double nTime);
66 :
67 : void UpdateOffsets(
68 : const InsertPosition& rInsertPosition,
69 : const view::Layouter& GetLayouter);
70 : void ResetOffsets (const controller::Animator::AnimationMode eMode);
71 :
72 : /// Index of the row or column that this run represents.
73 : sal_Int32 mnRunIndex;
74 : /// The index at which to make place for the insertion indicator (-1 for
75 : /// no indicator).
76 : sal_Int32 mnLocalInsertIndex;
77 : /// Index of the first page in the run.
78 : sal_Int32 mnStartIndex;
79 : /// Index of the last page in the run.
80 : sal_Int32 mnEndIndex;
81 : /// Offset of each item in the run at the start of the current animation.
82 : ::std::vector<Point> maStartOffset;
83 : /// Target offset of each item in the run at the end of the current animation.
84 : ::std::vector<Point> maEndOffset;
85 : /// Time at which the current animation started.
86 : double mnStartTime;
87 :
88 : class Comparator
89 : {
90 0 : public: bool operator() (
91 : const ::boost::shared_ptr<PageObjectRun>& rpRunA,
92 : const ::boost::shared_ptr<PageObjectRun>& rpRunB) const
93 : {
94 0 : return rpRunA->mnRunIndex < rpRunB->mnRunIndex;
95 : }
96 : };
97 : private:
98 : controller::Animator::AnimationId mnAnimationId;
99 : AnimatorAccess& mrAnimatorAccess;
100 : ::boost::function<double(double)> maAccelerationFunction;
101 :
102 : void RestartAnimation();
103 : };
104 : typedef ::boost::shared_ptr<PageObjectRun> SharedPageObjectRun;
105 :
106 0 : Point Blend (const Point& rPointA, const Point& rPointB, const double nT)
107 : {
108 : return Point(
109 0 : sal_Int32(rPointA.X() * (1-nT) + rPointB.X() * nT),
110 0 : sal_Int32(rPointA.Y() * (1-nT) + rPointB.Y() * nT));
111 : }
112 :
113 : } // end of anonymous namespace
114 :
115 : class InsertAnimator::Implementation : public AnimatorAccess
116 : {
117 : public:
118 : Implementation (SlideSorter& rSlideSorter);
119 : virtual ~Implementation();
120 :
121 : void SetInsertPosition (
122 : const InsertPosition& rInsertPosition,
123 : const controller::Animator::AnimationMode eAnimationMode);
124 :
125 : virtual void AddRun (const ::boost::shared_ptr<PageObjectRun>& rRun) SAL_OVERRIDE;
126 : virtual void RemoveRun (const ::boost::shared_ptr<PageObjectRun>& rRun) SAL_OVERRIDE;
127 :
128 0 : virtual model::SlideSorterModel& GetModel() const SAL_OVERRIDE { return mrModel; }
129 0 : virtual view::SlideSorterView& GetView() const SAL_OVERRIDE { return mrView; }
130 0 : virtual ::boost::shared_ptr<controller::Animator> GetAnimator() SAL_OVERRIDE { return mpAnimator; }
131 0 : virtual VclPtr<sd::Window> GetContentWindow() SAL_OVERRIDE { return mrSlideSorter.GetContentWindow(); }
132 :
133 : private:
134 : model::SlideSorterModel& mrModel;
135 : view::SlideSorterView& mrView;
136 : SlideSorter& mrSlideSorter;
137 : ::boost::shared_ptr<controller::Animator> mpAnimator;
138 : typedef ::std::set<SharedPageObjectRun, PageObjectRun::Comparator> RunContainer;
139 : RunContainer maRuns;
140 : InsertPosition maInsertPosition;
141 :
142 : SharedPageObjectRun GetRun (
143 : view::Layouter& rLayouter,
144 : const InsertPosition& rInsertPosition,
145 : const bool bCreate = true);
146 : RunContainer::const_iterator FindRun (const sal_Int32 nRunIndex) const;
147 : };
148 :
149 : //===== InsertAnimator ========================================================
150 :
151 0 : InsertAnimator::InsertAnimator (SlideSorter& rSlideSorter)
152 0 : : mpImplementation(new Implementation(rSlideSorter))
153 : {
154 0 : }
155 :
156 0 : void InsertAnimator::SetInsertPosition (const InsertPosition& rInsertPosition)
157 : {
158 0 : mpImplementation->SetInsertPosition(rInsertPosition, controller::Animator::AM_Animated);
159 0 : }
160 :
161 0 : void InsertAnimator::Reset (const controller::Animator::AnimationMode eMode)
162 : {
163 0 : mpImplementation->SetInsertPosition(InsertPosition(), eMode);
164 0 : }
165 :
166 : //===== InsertAnimator::Implementation ========================================
167 :
168 0 : InsertAnimator::Implementation::Implementation (SlideSorter& rSlideSorter)
169 0 : : mrModel(rSlideSorter.GetModel()),
170 0 : mrView(rSlideSorter.GetView()),
171 : mrSlideSorter(rSlideSorter),
172 0 : mpAnimator(rSlideSorter.GetController().GetAnimator()),
173 : maRuns(),
174 0 : maInsertPosition()
175 : {
176 0 : }
177 :
178 0 : InsertAnimator::Implementation::~Implementation()
179 : {
180 0 : SetInsertPosition(InsertPosition(), controller::Animator::AM_Immediate);
181 0 : }
182 :
183 0 : void InsertAnimator::Implementation::SetInsertPosition (
184 : const InsertPosition& rInsertPosition,
185 : const controller::Animator::AnimationMode eMode)
186 : {
187 0 : if (maInsertPosition == rInsertPosition)
188 0 : return;
189 :
190 0 : SharedPageObjectRun pOldRun (GetRun(mrView.GetLayouter(), maInsertPosition));
191 0 : SharedPageObjectRun pCurrentRun (GetRun(mrView.GetLayouter(), rInsertPosition));
192 0 : maInsertPosition = rInsertPosition;
193 :
194 : // When the new insert position is in a different run then move the page
195 : // objects in the old run to their default positions.
196 0 : if (pOldRun != pCurrentRun)
197 : {
198 0 : if (pOldRun)
199 0 : pOldRun->ResetOffsets(eMode);
200 : }
201 :
202 0 : if (pCurrentRun)
203 : {
204 0 : pCurrentRun->UpdateOffsets(rInsertPosition, mrView.GetLayouter());
205 0 : }
206 : }
207 :
208 0 : SharedPageObjectRun InsertAnimator::Implementation::GetRun (
209 : view::Layouter& rLayouter,
210 : const InsertPosition& rInsertPosition,
211 : const bool bCreate)
212 : {
213 0 : const sal_Int32 nRow (rInsertPosition.GetRow());
214 0 : if (nRow < 0)
215 0 : return SharedPageObjectRun();
216 :
217 0 : RunContainer::const_iterator iRun (maRuns.end());
218 0 : if (rLayouter.GetColumnCount() == 1)
219 : {
220 : // There is only one run that contains all slides.
221 0 : if (maRuns.empty() && bCreate)
222 : maRuns.insert(SharedPageObjectRun(new PageObjectRun(
223 : *this,
224 : 0,
225 : 0,
226 0 : mrModel.GetPageCount()-1)));
227 0 : iRun = maRuns.begin();
228 : }
229 : else
230 : {
231 0 : iRun = FindRun(nRow);
232 0 : if (iRun == maRuns.end() && bCreate)
233 : {
234 : // Create a new run.
235 0 : const sal_Int32 nStartIndex (rLayouter.GetIndex(nRow, 0));
236 0 : const sal_Int32 nEndIndex (rLayouter.GetIndex(nRow, rLayouter.GetColumnCount()-1));
237 0 : if (nStartIndex <= nEndIndex)
238 : {
239 : iRun = maRuns.insert(SharedPageObjectRun(new PageObjectRun(
240 : *this,
241 : nRow,
242 : nStartIndex,
243 0 : nEndIndex))).first;
244 : OSL_ASSERT(iRun != maRuns.end());
245 : }
246 : }
247 : }
248 :
249 0 : if (iRun != maRuns.end())
250 0 : return *iRun;
251 : else
252 0 : return SharedPageObjectRun();
253 : }
254 :
255 : InsertAnimator::Implementation::RunContainer::const_iterator
256 0 : InsertAnimator::Implementation::FindRun (const sal_Int32 nRunIndex) const
257 : {
258 : return std::find_if(
259 : maRuns.begin(),
260 : maRuns.end(),
261 : ::boost::bind(
262 : ::std::equal_to<sal_Int32>(),
263 : ::boost::bind(&PageObjectRun::mnRunIndex, _1),
264 0 : nRunIndex));
265 : }
266 :
267 0 : void InsertAnimator::Implementation::AddRun (const ::boost::shared_ptr<PageObjectRun>& rRun)
268 : {
269 0 : if (rRun)
270 : {
271 0 : maRuns.insert(rRun);
272 : }
273 : else
274 : {
275 : OSL_ASSERT(rRun);
276 : }
277 0 : }
278 :
279 0 : void InsertAnimator::Implementation::RemoveRun (const ::boost::shared_ptr<PageObjectRun>& rRun)
280 : {
281 0 : if (rRun)
282 : {
283 : // Do not remove runs that show the space for the insertion indicator.
284 0 : if (rRun->mnLocalInsertIndex == -1)
285 : {
286 0 : InsertAnimator::Implementation::RunContainer::const_iterator iRun (FindRun(rRun->mnRunIndex));
287 0 : if (iRun != maRuns.end())
288 : {
289 : OSL_ASSERT(*iRun == rRun);
290 0 : maRuns.erase(iRun);
291 : }
292 : }
293 : }
294 : else
295 : {
296 : OSL_ASSERT(rRun);
297 : }
298 0 : }
299 :
300 : //===== PageObjectRun =========================================================
301 :
302 0 : PageObjectRun::PageObjectRun (
303 : AnimatorAccess& rAnimatorAccess,
304 : const sal_Int32 nRunIndex,
305 : const sal_Int32 nStartIndex,
306 : const sal_Int32 nEndIndex)
307 : : mnRunIndex(nRunIndex),
308 : mnLocalInsertIndex(-1),
309 : mnStartIndex(nStartIndex),
310 : mnEndIndex(nEndIndex),
311 : maStartOffset(),
312 : maEndOffset(),
313 : mnStartTime(-1),
314 : mnAnimationId(controller::Animator::NotAnAnimationId),
315 : mrAnimatorAccess(rAnimatorAccess),
316 : maAccelerationFunction(
317 : controller::AnimationParametricFunction(
318 0 : controller::AnimationBezierFunction (0.1,0.7)))
319 : {
320 0 : maStartOffset.resize(nEndIndex - nStartIndex + 1);
321 0 : maEndOffset.resize(nEndIndex - nStartIndex + 1);
322 0 : }
323 :
324 0 : PageObjectRun::~PageObjectRun()
325 : {
326 0 : }
327 :
328 0 : void PageObjectRun::UpdateOffsets(
329 : const InsertPosition& rInsertPosition,
330 : const view::Layouter& rLayouter)
331 : {
332 0 : const bool bIsVertical (rLayouter.GetColumnCount()==1);
333 : const sal_Int32 nLocalInsertIndex(bIsVertical
334 : ? rInsertPosition.GetRow()
335 0 : : rInsertPosition.GetColumn());
336 0 : if (nLocalInsertIndex != mnLocalInsertIndex)
337 : {
338 0 : mnLocalInsertIndex = nLocalInsertIndex;
339 :
340 0 : model::SlideSorterModel& rModel (mrAnimatorAccess.GetModel());
341 0 : const sal_Int32 nRunLength (mnEndIndex - mnStartIndex + 1);
342 0 : for (sal_Int32 nIndex=0; nIndex<nRunLength; ++nIndex)
343 : {
344 0 : model::SharedPageDescriptor pDescriptor(rModel.GetPageDescriptor(nIndex+mnStartIndex));
345 0 : if (pDescriptor)
346 0 : maStartOffset[nIndex] = pDescriptor->GetVisualState().GetLocationOffset();
347 0 : maEndOffset[nIndex] = nIndex < mnLocalInsertIndex
348 : ? rInsertPosition.GetLeadingOffset()
349 0 : : rInsertPosition.GetTrailingOffset();
350 0 : if (bIsVertical)
351 0 : maEndOffset[nIndex].X() = 0;
352 : else
353 0 : maEndOffset[nIndex].Y() = 0;
354 0 : }
355 0 : RestartAnimation();
356 : }
357 0 : }
358 :
359 0 : void PageObjectRun::ResetOffsets (const controller::Animator::AnimationMode eMode)
360 : {
361 0 : mnLocalInsertIndex = -1;
362 0 : const sal_Int32 nRunLength (mnEndIndex - mnStartIndex + 1);
363 0 : model::SlideSorterModel& rModel (mrAnimatorAccess.GetModel());
364 0 : view::SlideSorterView& rView (mrAnimatorAccess.GetView());
365 0 : for (sal_Int32 nIndex=0; nIndex<nRunLength; ++nIndex)
366 : {
367 0 : model::SharedPageDescriptor pDescriptor(rModel.GetPageDescriptor(nIndex+mnStartIndex));
368 0 : if (pDescriptor)
369 : {
370 0 : if (eMode == controller::Animator::AM_Animated)
371 0 : maStartOffset[nIndex] = pDescriptor->GetVisualState().GetLocationOffset();
372 : else
373 : {
374 0 : const Rectangle aOldBoundingBox (pDescriptor->GetBoundingBox());
375 0 : pDescriptor->GetVisualState().SetLocationOffset(Point(0,0));
376 0 : rView.RequestRepaint(aOldBoundingBox);
377 0 : rView.RequestRepaint(pDescriptor);
378 : }
379 : }
380 0 : maEndOffset[nIndex] = Point(0,0);
381 0 : }
382 0 : if (eMode == controller::Animator::AM_Animated)
383 0 : RestartAnimation();
384 : else
385 0 : mrAnimatorAccess.RemoveRun(shared_from_this());
386 0 : }
387 :
388 0 : void PageObjectRun::RestartAnimation()
389 : {
390 : // Stop the current animation.
391 0 : if (mnAnimationId != controller::Animator::NotAnAnimationId)
392 : {
393 0 : mrAnimatorAccess.GetAnimator()->RemoveAnimation(mnAnimationId);
394 : }
395 :
396 : // Restart the animation.
397 0 : mrAnimatorAccess.AddRun(shared_from_this());
398 0 : mnAnimationId = mrAnimatorAccess.GetAnimator()->AddAnimation(
399 : ::boost::ref(*this),
400 : 0,
401 : 300,
402 : ::boost::bind(
403 : &AnimatorAccess::RemoveRun,
404 : ::boost::ref(mrAnimatorAccess),
405 0 : shared_from_this()));
406 0 : }
407 :
408 0 : void PageObjectRun::operator () (const double nGlobalTime)
409 : {
410 0 : if (mnStartTime < 0)
411 0 : mnStartTime = nGlobalTime;
412 :
413 0 : double nLocalTime (nGlobalTime - mnStartTime);
414 0 : if (nLocalTime > 1.0)
415 0 : nLocalTime = 1.0;
416 0 : nLocalTime = maAccelerationFunction(nLocalTime);
417 :
418 0 : model::SlideSorterModel& rModel (mrAnimatorAccess.GetModel());
419 0 : view::SlideSorterView& rView (mrAnimatorAccess.GetView());
420 0 : for (sal_Int32 nIndex=mnStartIndex; nIndex<=mnEndIndex; ++nIndex)
421 : {
422 0 : model::SharedPageDescriptor pDescriptor (rModel.GetPageDescriptor(nIndex));
423 0 : if ( ! pDescriptor)
424 0 : continue;
425 0 : const Rectangle aOldBoundingBox (pDescriptor->GetBoundingBox());
426 0 : pDescriptor->GetVisualState().SetLocationOffset(
427 : Blend(
428 0 : maStartOffset[nIndex-mnStartIndex],
429 0 : maEndOffset[nIndex-mnStartIndex],
430 0 : nLocalTime));
431 :
432 : // Request a repaint of the old and new bounding box (which largely overlap.)
433 0 : rView.RequestRepaint(aOldBoundingBox);
434 0 : rView.RequestRepaint(pDescriptor);
435 0 : }
436 :
437 : // Call Flush to make
438 : // a) animations a bit more smooth and
439 : // b) on Mac without the Flush a Reset of the page locations is not properly
440 : // visualized when the mouse leaves the window during drag-and-drop.
441 0 : mrAnimatorAccess.GetContentWindow()->Flush();
442 0 : }
443 :
444 66 : } } } // end of namespace ::sd::slidesorter::view
445 :
446 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|