Coverage Report

Created: 2025-07-07 10:01

/src/libreoffice/drawinglayer/source/animation/animationtiming.cxx
Line
Count
Source (jump to first uncovered line)
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 <memory>
21
22
#include <drawinglayer/animation/animationtiming.hxx>
23
#include <basegfx/numeric/ftools.hxx>
24
25
namespace drawinglayer::animation
26
{
27
28
29
        AnimationEntry::AnimationEntry()
30
0
        {
31
0
        }
32
33
        AnimationEntry::~AnimationEntry()
34
0
        {
35
0
        }
36
37
38
        AnimationEntryFixed::AnimationEntryFixed(double fDuration, double fState)
39
0
        :   mfDuration(fDuration),
40
0
            mfState(fState)
41
0
        {
42
0
        }
43
44
        AnimationEntryFixed::~AnimationEntryFixed()
45
        {
46
        }
47
48
        std::unique_ptr<AnimationEntry> AnimationEntryFixed::clone() const
49
0
        {
50
0
            return std::make_unique<AnimationEntryFixed>(mfDuration, mfState);
51
0
        }
52
53
        bool AnimationEntryFixed::operator==(const AnimationEntry& rCandidate) const
54
0
        {
55
0
            const AnimationEntryFixed* pCompare = dynamic_cast< const AnimationEntryFixed* >(&rCandidate);
56
57
0
            return (pCompare
58
0
                && basegfx::fTools::equal(mfDuration, pCompare->mfDuration)
59
0
                && basegfx::fTools::equal(mfState, pCompare->mfState));
60
0
        }
61
62
        double AnimationEntryFixed::getDuration() const
63
0
        {
64
0
            return mfDuration;
65
0
        }
66
67
        double AnimationEntryFixed::getStateAtTime(double /*fTime*/) const
68
0
        {
69
0
            return mfState;
70
0
        }
71
72
        double AnimationEntryFixed::getNextEventTime(double fTime) const
73
0
        {
74
0
            if(basegfx::fTools::less(fTime, mfDuration))
75
0
            {
76
0
                return mfDuration;
77
0
            }
78
0
            else
79
0
            {
80
0
                return 0.0;
81
0
            }
82
0
        }
83
84
85
        AnimationEntryLinear::AnimationEntryLinear(double fDuration, double fFrequency, double fStart, double fStop)
86
0
        :   mfDuration(fDuration),
87
0
            mfFrequency(fFrequency),
88
0
            mfStart(fStart),
89
0
            mfStop(fStop)
90
0
        {
91
0
        }
92
93
        AnimationEntryLinear::~AnimationEntryLinear()
94
        {
95
        }
96
97
        std::unique_ptr<AnimationEntry> AnimationEntryLinear::clone() const
98
0
        {
99
0
            return std::make_unique<AnimationEntryLinear>(mfDuration, mfFrequency, mfStart, mfStop);
100
0
        }
101
102
        bool AnimationEntryLinear::operator==(const AnimationEntry& rCandidate) const
103
0
        {
104
0
            const AnimationEntryLinear* pCompare = dynamic_cast< const AnimationEntryLinear* >(&rCandidate);
105
106
0
            return (pCompare
107
0
                && basegfx::fTools::equal(mfDuration, pCompare->mfDuration)
108
0
                && basegfx::fTools::equal(mfStart, pCompare->mfStart)
109
0
                && basegfx::fTools::equal(mfStop, pCompare->mfStop));
110
0
        }
111
112
        double AnimationEntryLinear::getDuration() const
113
0
        {
114
0
            return mfDuration;
115
0
        }
116
117
        double AnimationEntryLinear::getStateAtTime(double fTime) const
118
0
        {
119
0
            if(mfDuration > 0.0)
120
0
            {
121
0
                const double fFactor(fTime / mfDuration);
122
123
0
                if(fFactor > 1.0)
124
0
                {
125
0
                    return mfStop;
126
0
                }
127
0
                else
128
0
                {
129
0
                    return mfStart + ((mfStop - mfStart) * fFactor);
130
0
                }
131
0
            }
132
0
            else
133
0
            {
134
0
                return mfStart;
135
0
            }
136
0
        }
137
138
        double AnimationEntryLinear::getNextEventTime(double fTime) const
139
0
        {
140
0
            if(basegfx::fTools::less(fTime, mfDuration))
141
0
            {
142
                // use the simple solution: just add the frequency. More correct (but also more
143
                // complicated) would be to calculate the slice of time we are in and when this
144
                // slice will end. For the animations, this makes no quality difference.
145
0
                fTime += mfFrequency;
146
147
0
                if(basegfx::fTools::more(fTime, mfDuration))
148
0
                {
149
0
                    fTime = mfDuration;
150
0
                }
151
152
0
                return fTime;
153
0
            }
154
0
            else
155
0
            {
156
0
                return 0.0;
157
0
            }
158
0
        }
159
160
161
        AnimationEntryList::Entries::size_type AnimationEntryList::impGetIndexAtTime(double fTime, double &rfAddedTime) const
162
0
        {
163
0
            Entries::size_type nIndex(0);
164
165
0
            while(nIndex < maEntries.size() && basegfx::fTools::lessOrEqual(rfAddedTime + maEntries[nIndex]->getDuration(), fTime))
166
0
            {
167
0
                rfAddedTime += maEntries[nIndex++]->getDuration();
168
0
            }
169
170
0
            return nIndex;
171
0
        }
172
173
        AnimationEntryList::AnimationEntryList()
174
0
        :   mfDuration(0.0)
175
0
        {
176
0
        }
177
178
        AnimationEntryList::~AnimationEntryList()
179
0
        {
180
0
        }
181
182
        std::unique_ptr<AnimationEntry> AnimationEntryList::clone() const
183
0
        {
184
0
            std::unique_ptr<AnimationEntryList> pNew(std::make_unique<AnimationEntryList>());
185
186
0
            for(const auto &i : maEntries)
187
0
            {
188
0
                pNew->append(*i);
189
0
            }
190
191
0
            return pNew;
192
0
        }
193
194
        bool AnimationEntryList::operator==(const AnimationEntry& rCandidate) const
195
0
        {
196
0
            const AnimationEntryList* pCompare = dynamic_cast<const AnimationEntryList*>(&rCandidate);
197
198
0
            if (pCompare && mfDuration == pCompare->mfDuration) 
199
0
            {
200
0
                return std::equal(maEntries.cbegin(), maEntries.cend(),
201
0
                                pCompare->maEntries.cbegin(), pCompare->maEntries.cend(),
202
0
                                [](const auto& lhs, const auto& rhs) {
203
0
                                     return *lhs == *rhs; 
204
0
                                });
205
0
            }
206
207
0
            return false;
208
0
        }
209
210
        void AnimationEntryList::append(const AnimationEntry& rCandidate)
211
0
        {
212
0
            const double fDuration(rCandidate.getDuration());
213
214
0
            if(!basegfx::fTools::equalZero(fDuration))
215
0
            {
216
0
                maEntries.push_back(rCandidate.clone());
217
0
                mfDuration += fDuration;
218
0
            }
219
0
        }
220
221
        double AnimationEntryList::getDuration() const
222
0
        {
223
0
            return mfDuration;
224
0
        }
225
226
        double AnimationEntryList::getStateAtTime(double fTime) const
227
0
        {
228
0
            if(!basegfx::fTools::equalZero(mfDuration))
229
0
            {
230
0
                double fAddedTime(0.0);
231
0
                const auto nIndex(impGetIndexAtTime(fTime, fAddedTime));
232
233
0
                if(nIndex < maEntries.size())
234
0
                {
235
0
                    return maEntries[nIndex]->getStateAtTime(fTime - fAddedTime);
236
0
                }
237
0
            }
238
239
0
            return 0.0;
240
0
        }
241
242
        double AnimationEntryList::getNextEventTime(double fTime) const
243
0
        {
244
0
            double fNewTime(0.0);
245
246
0
            if(!basegfx::fTools::equalZero(mfDuration))
247
0
            {
248
0
                double fAddedTime(0.0);
249
0
                const auto nIndex(impGetIndexAtTime(fTime, fAddedTime));
250
251
0
                if(nIndex < maEntries.size())
252
0
                {
253
0
                    fNewTime = maEntries[nIndex]->getNextEventTime(fTime - fAddedTime) + fAddedTime;
254
0
                }
255
0
            }
256
257
0
            return fNewTime;
258
0
        }
259
260
261
        AnimationEntryLoop::AnimationEntryLoop(sal_uInt32 nRepeat)
262
0
        :   mnRepeat(nRepeat)
263
0
        {
264
0
        }
265
266
        AnimationEntryLoop::~AnimationEntryLoop()
267
        {
268
        }
269
270
        std::unique_ptr<AnimationEntry> AnimationEntryLoop::clone() const
271
0
        {
272
0
            std::unique_ptr<AnimationEntryLoop> pNew(std::make_unique<AnimationEntryLoop>(mnRepeat));
273
274
0
            for(const auto &i : maEntries)
275
0
            {
276
0
                pNew->append(*i);
277
0
            }
278
279
0
            return pNew;
280
0
        }
281
282
        bool AnimationEntryLoop::operator==(const AnimationEntry& rCandidate) const
283
0
        {
284
0
            const AnimationEntryLoop* pCompare = dynamic_cast< const AnimationEntryLoop* >(&rCandidate);
285
286
0
            return (pCompare
287
0
                && mnRepeat == pCompare->mnRepeat
288
0
                && AnimationEntryList::operator==(rCandidate));
289
0
        }
290
291
        double AnimationEntryLoop::getDuration() const
292
0
        {
293
0
            return (mfDuration * static_cast<double>(mnRepeat));
294
0
        }
295
296
        double AnimationEntryLoop::getStateAtTime(double fTime) const
297
0
        {
298
0
            if(mnRepeat && !basegfx::fTools::equalZero(mfDuration))
299
0
            {
300
0
                const sal_uInt32 nCurrentLoop(static_cast<sal_uInt32>(fTime / mfDuration));
301
302
0
                if(nCurrentLoop > mnRepeat)
303
0
                {
304
0
                    return 1.0;
305
0
                }
306
0
                else
307
0
                {
308
0
                    const double fTimeAtLoopStart(static_cast<double>(nCurrentLoop) * mfDuration);
309
0
                    const double fRelativeTime(fTime - fTimeAtLoopStart);
310
0
                    return AnimationEntryList::getStateAtTime(fRelativeTime);
311
0
                }
312
0
            }
313
314
0
            return 0.0;
315
0
        }
316
317
        double AnimationEntryLoop::getNextEventTime(double fTime) const
318
0
        {
319
0
            double fNewTime(0.0);
320
321
0
            if(mnRepeat && !basegfx::fTools::equalZero(mfDuration))
322
0
            {
323
0
                const sal_uInt32 nCurrentLoop(static_cast<sal_uInt32>(fTime / mfDuration));
324
325
0
                if(nCurrentLoop <= mnRepeat)
326
0
                {
327
0
                    const double fTimeAtLoopStart(static_cast<double>(nCurrentLoop) * mfDuration);
328
0
                    const double fRelativeTime(fTime - fTimeAtLoopStart);
329
0
                    const double fNextEventAtLoop(AnimationEntryList::getNextEventTime(fRelativeTime));
330
331
0
                    if(!basegfx::fTools::equalZero(fNextEventAtLoop))
332
0
                    {
333
0
                        fNewTime = fNextEventAtLoop + fTimeAtLoopStart;
334
0
                    }
335
0
                }
336
0
            }
337
338
0
            return fNewTime;
339
0
        }
340
} // end of namespace
341
342
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */