summaryrefslogtreecommitdiff
path: root/sd/source/ui/slidesorter/view/SlsInsertAnimator.cxx
blob: 7677b9015d0e0c67430de2d15beba6468bee9561 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include "view/SlsInsertAnimator.hxx"
#include "controller/SlideSorterController.hxx"
#include "controller/SlsAnimationFunction.hxx"
#include "view/SlideSorterView.hxx"
#include "view/SlsLayouter.hxx"
#include "model/SlideSorterModel.hxx"
#include "model/SlsPageEnumerationProvider.hxx"

#include <memory>
#include <set>

namespace sd { namespace slidesorter { namespace view {

namespace {

class PageObjectRun;

class AnimatorAccess
{
public:
    virtual void AddRun (const std::shared_ptr<PageObjectRun>& rRun) = 0;
    virtual void RemoveRun (const std::shared_ptr<PageObjectRun>& rRun) = 0;
    virtual model::SlideSorterModel& GetModel (void) const = 0;
    virtual view::SlideSorterView& GetView (void) const = 0;
    virtual std::shared_ptr<controller::Animator> GetAnimator (void) = 0;
    virtual VclPtr<sd::Window> GetContentWindow (void) = 0;

protected:
    ~AnimatorAccess() {}
};

/** Controller of the position offsets of all page objects in one row or one
    column.
*/
class PageObjectRun : public std::enable_shared_from_this<PageObjectRun>
{
public:
    PageObjectRun (
        AnimatorAccess& rAnimatorAccess,
        const sal_Int32 nRunIndex,
        const sal_Int32 nStartIndex,
        const sal_Int32 nEndIndex);

    void operator () (const double nTime);

    void UpdateOffsets(
        const InsertPosition& rInsertPosition,
        const view::Layouter& GetLayouter);
    void ResetOffsets (const controller::Animator::AnimationMode eMode);

    /// Index of the row or column that this run represents.
    sal_Int32 mnRunIndex;
    /// The index at which to make place for the insertion indicator (-1 for
    /// no indicator).
    sal_Int32 mnLocalInsertIndex;
    /// Index of the first page in the run.
    sal_Int32 mnStartIndex;
    /// Index of the last page in the run.
    sal_Int32 mnEndIndex;
    /// Offset of each item in the run at the start of the current animation.
    ::std::vector<Point> maStartOffset;
    /// Target offset of each item in the run at the end of the current animation.
    ::std::vector<Point> maEndOffset;
    /// Time at which the current animation started.
    double mnStartTime;

    class Comparator
    {
        public: bool operator() (
            const std::shared_ptr<PageObjectRun>& rpRunA,
            const std::shared_ptr<PageObjectRun>& rpRunB) const
        {
            return rpRunA->mnRunIndex < rpRunB->mnRunIndex;
        }
    };
private:
    controller::Animator::AnimationId mnAnimationId;
    AnimatorAccess& mrAnimatorAccess;
    ::std::function<double (double)> maAccelerationFunction;

    void RestartAnimation();
};
typedef std::shared_ptr<PageObjectRun> SharedPageObjectRun;

Point Blend (const Point& rPointA, const Point& rPointB, const double nT)
{
    return Point(
        sal_Int32(rPointA.X() * (1-nT) + rPointB.X() * nT),
        sal_Int32(rPointA.Y() * (1-nT) + rPointB.Y() * nT));
}

} // end of anonymous namespace

class InsertAnimator::Implementation : public AnimatorAccess
{
public:
    explicit Implementation (SlideSorter& rSlideSorter);
    virtual ~Implementation();

    void SetInsertPosition (
        const InsertPosition& rInsertPosition,
        const controller::Animator::AnimationMode eAnimationMode);

    virtual void AddRun (const std::shared_ptr<PageObjectRun>& rRun) override;
    virtual void RemoveRun (const std::shared_ptr<PageObjectRun>& rRun) override;

    virtual model::SlideSorterModel& GetModel() const override { return mrModel; }
    virtual view::SlideSorterView& GetView() const override { return mrView; }
    virtual std::shared_ptr<controller::Animator> GetAnimator() override { return mpAnimator; }
    virtual VclPtr<sd::Window> GetContentWindow() override { return mrSlideSorter.GetContentWindow(); }

private:
    model::SlideSorterModel& mrModel;
    view::SlideSorterView& mrView;
    SlideSorter& mrSlideSorter;
    std::shared_ptr<controller::Animator> mpAnimator;
    typedef ::std::set<SharedPageObjectRun, PageObjectRun::Comparator> RunContainer;
    RunContainer maRuns;
    InsertPosition maInsertPosition;

    SharedPageObjectRun GetRun (
        view::Layouter& rLayouter,
        const InsertPosition& rInsertPosition);
    RunContainer::const_iterator FindRun (const sal_Int32 nRunIndex) const;
};

//===== InsertAnimator ========================================================

InsertAnimator::InsertAnimator (SlideSorter& rSlideSorter)
    : mpImplementation(new Implementation(rSlideSorter))
{
}

void InsertAnimator::SetInsertPosition (const InsertPosition& rInsertPosition)
{
    mpImplementation->SetInsertPosition(rInsertPosition, controller::Animator::AM_Animated);
}

void InsertAnimator::Reset (const controller::Animator::AnimationMode eMode)
{
    mpImplementation->SetInsertPosition(InsertPosition(), eMode);
}

//===== InsertAnimator::Implementation ========================================

InsertAnimator::Implementation::Implementation (SlideSorter& rSlideSorter)
    : mrModel(rSlideSorter.GetModel()),
      mrView(rSlideSorter.GetView()),
      mrSlideSorter(rSlideSorter),
      mpAnimator(rSlideSorter.GetController().GetAnimator()),
      maRuns(),
      maInsertPosition()
{
}

InsertAnimator::Implementation::~Implementation()
{
    SetInsertPosition(InsertPosition(), controller::Animator::AM_Immediate);
}

void InsertAnimator::Implementation::SetInsertPosition (
    const InsertPosition& rInsertPosition,
    const controller::Animator::AnimationMode eMode)
{
    if (maInsertPosition == rInsertPosition)
        return;

    SharedPageObjectRun pOldRun (GetRun(mrView.GetLayouter(), maInsertPosition));
    SharedPageObjectRun pCurrentRun (GetRun(mrView.GetLayouter(), rInsertPosition));
    maInsertPosition = rInsertPosition;

    // When the new insert position is in a different run then move the page
    // objects in the old run to their default positions.
    if (pOldRun != pCurrentRun)
    {
        if (pOldRun)
            pOldRun->ResetOffsets(eMode);
    }

    if (pCurrentRun)
    {
        pCurrentRun->UpdateOffsets(rInsertPosition, mrView.GetLayouter());
    }
}

SharedPageObjectRun InsertAnimator::Implementation::GetRun (
    view::Layouter& rLayouter,
    const InsertPosition& rInsertPosition)
{
    const sal_Int32 nRow (rInsertPosition.GetRow());
    if (nRow < 0)
        return SharedPageObjectRun();

    RunContainer::const_iterator iRun (maRuns.end());
    if (rLayouter.GetColumnCount() == 1)
    {
        // There is only one run that contains all slides.
        if (maRuns.empty())
            maRuns.insert(std::make_shared<PageObjectRun>(
                *this,
                0,
                0,
                mrModel.GetPageCount()-1));
        iRun = maRuns.begin();
    }
    else
    {
        iRun = FindRun(nRow);
        if (iRun == maRuns.end())
        {
            // Create a new run.
            const sal_Int32 nStartIndex (rLayouter.GetIndex(nRow, 0));
            const sal_Int32 nEndIndex (rLayouter.GetIndex(nRow, rLayouter.GetColumnCount()-1));
            if (nStartIndex <= nEndIndex)
            {
                iRun = maRuns.insert(std::make_shared<PageObjectRun>(
                    *this,
                    nRow,
                    nStartIndex,
                    nEndIndex)).first;
                OSL_ASSERT(iRun != maRuns.end());
            }
        }
    }

    if (iRun != maRuns.end())
        return *iRun;
    else
        return SharedPageObjectRun();
}

InsertAnimator::Implementation::RunContainer::const_iterator
    InsertAnimator::Implementation::FindRun (const sal_Int32 nRunIndex) const
{
    return std::find_if(
        maRuns.begin(),
        maRuns.end(),
        [nRunIndex] (std::shared_ptr<PageObjectRun> const& rRun)
            { return rRun->mnRunIndex == nRunIndex; });
}

void InsertAnimator::Implementation::AddRun (const std::shared_ptr<PageObjectRun>& rRun)
{
    if (rRun)
    {
        maRuns.insert(rRun);
    }
    else
    {
        OSL_ASSERT(rRun);
    }
}

void InsertAnimator::Implementation::RemoveRun (const std::shared_ptr<PageObjectRun>& rRun)
{
    if (rRun)
    {
        // Do not remove runs that show the space for the insertion indicator.
        if (rRun->mnLocalInsertIndex == -1)
        {
            InsertAnimator::Implementation::RunContainer::const_iterator iRun (FindRun(rRun->mnRunIndex));
            if (iRun != maRuns.end())
            {
                OSL_ASSERT(*iRun == rRun);
                maRuns.erase(iRun);
            }
        }
    }
    else
    {
        OSL_ASSERT(rRun);
    }
}

//===== PageObjectRun =========================================================

PageObjectRun::PageObjectRun (
    AnimatorAccess& rAnimatorAccess,
    const sal_Int32 nRunIndex,
    const sal_Int32 nStartIndex,
    const sal_Int32 nEndIndex)
    : mnRunIndex(nRunIndex),
      mnLocalInsertIndex(-1),
      mnStartIndex(nStartIndex),
      mnEndIndex(nEndIndex),
      maStartOffset(),
      maEndOffset(),
      mnStartTime(-1),
      mnAnimationId(controller::Animator::NotAnAnimationId),
      mrAnimatorAccess(rAnimatorAccess),
      maAccelerationFunction(
          controller::AnimationParametricFunction(
              controller::AnimationBezierFunction (0.1,0.7)))
{
    maStartOffset.resize(nEndIndex - nStartIndex + 1);
    maEndOffset.resize(nEndIndex - nStartIndex + 1);
}

void PageObjectRun::UpdateOffsets(
    const InsertPosition& rInsertPosition,
    const view::Layouter& rLayouter)
{
    const bool bIsVertical (rLayouter.GetColumnCount()==1);
    const sal_Int32 nLocalInsertIndex(bIsVertical
        ? rInsertPosition.GetRow()
        : rInsertPosition.GetColumn());
    if (nLocalInsertIndex != mnLocalInsertIndex)
    {
        mnLocalInsertIndex = nLocalInsertIndex;

        model::SlideSorterModel& rModel (mrAnimatorAccess.GetModel());
        const sal_Int32 nRunLength (mnEndIndex - mnStartIndex + 1);
        for (sal_Int32 nIndex=0; nIndex<nRunLength; ++nIndex)
        {
            model::SharedPageDescriptor pDescriptor(rModel.GetPageDescriptor(nIndex+mnStartIndex));
            if (pDescriptor)
                maStartOffset[nIndex] = pDescriptor->GetVisualState().GetLocationOffset();
            maEndOffset[nIndex] = nIndex < mnLocalInsertIndex
                ? rInsertPosition.GetLeadingOffset()
                : rInsertPosition.GetTrailingOffset();
            if (bIsVertical)
                maEndOffset[nIndex].X() = 0;
            else
                maEndOffset[nIndex].Y() = 0;
        }
        RestartAnimation();
    }
}

void PageObjectRun::ResetOffsets (const controller::Animator::AnimationMode eMode)
{
    mnLocalInsertIndex = -1;
    const sal_Int32 nRunLength (mnEndIndex - mnStartIndex + 1);
    model::SlideSorterModel& rModel (mrAnimatorAccess.GetModel());
    view::SlideSorterView& rView (mrAnimatorAccess.GetView());
    for (sal_Int32 nIndex=0; nIndex<nRunLength; ++nIndex)
    {
        model::SharedPageDescriptor pDescriptor(rModel.GetPageDescriptor(nIndex+mnStartIndex));
        if (pDescriptor)
        {
            if (eMode == controller::Animator::AM_Animated)
                maStartOffset[nIndex] = pDescriptor->GetVisualState().GetLocationOffset();
            else
            {
                const ::tools::Rectangle aOldBoundingBox (pDescriptor->GetBoundingBox());
                pDescriptor->GetVisualState().SetLocationOffset(Point(0,0));
                rView.RequestRepaint(aOldBoundingBox);
                rView.RequestRepaint(pDescriptor);
            }
        }
        maEndOffset[nIndex] = Point(0,0);
    }
    if (eMode == controller::Animator::AM_Animated)
        RestartAnimation();
    else
        mrAnimatorAccess.RemoveRun(shared_from_this());
}

void PageObjectRun::RestartAnimation()
{
    // Stop the current animation.
    if (mnAnimationId != controller::Animator::NotAnAnimationId)
    {
        mrAnimatorAccess.GetAnimator()->RemoveAnimation(mnAnimationId);
    }

    // Restart the animation.
    mrAnimatorAccess.AddRun(shared_from_this());
    auto sharedThis(shared_from_this());
    mnAnimationId = mrAnimatorAccess.GetAnimator()->AddAnimation(
        [this] (double const val) { (*this)(val); },
        [sharedThis] () { sharedThis->mrAnimatorAccess.RemoveRun(sharedThis); }
        );
}

void PageObjectRun::operator () (const double nGlobalTime)
{
    if (mnStartTime < 0)
        mnStartTime = nGlobalTime;

    double nLocalTime (nGlobalTime - mnStartTime);
    if (nLocalTime > 1.0)
        nLocalTime = 1.0;
    nLocalTime = maAccelerationFunction(nLocalTime);

    model::SlideSorterModel& rModel (mrAnimatorAccess.GetModel());
    view::SlideSorterView& rView (mrAnimatorAccess.GetView());
    for (sal_Int32 nIndex=mnStartIndex; nIndex<=mnEndIndex; ++nIndex)
    {
        model::SharedPageDescriptor pDescriptor (rModel.GetPageDescriptor(nIndex));
        if ( ! pDescriptor)
            continue;
        const ::tools::Rectangle aOldBoundingBox (pDescriptor->GetBoundingBox());
        pDescriptor->GetVisualState().SetLocationOffset(
            Blend(
                maStartOffset[nIndex-mnStartIndex],
                maEndOffset[nIndex-mnStartIndex],
                nLocalTime));

        // Request a repaint of the old and new bounding box (which largely overlap.)
        rView.RequestRepaint(aOldBoundingBox);
        rView.RequestRepaint(pDescriptor);
    }

    // Call Flush to make
    // a) animations a bit more smooth and
    // b) on Mac without the Flush a Reset of the page locations is not properly
    // visualized when the mouse leaves the window during drag-and-drop.
    mrAnimatorAccess.GetContentWindow()->Flush();
}

} } } // end of namespace ::sd::slidesorter::view

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */