LibreOffice Module drawinglayer (master) 1
animationtiming.cxx
Go to the documentation of this file.
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
24
26{
27
28
30 {
31 }
32
34 {
35 }
36
37
38 AnimationEntryFixed::AnimationEntryFixed(double fDuration, double fState)
39 : mfDuration(fDuration),
40 mfState(fState)
41 {
42 }
43
45 {
46 }
47
48 std::unique_ptr<AnimationEntry> AnimationEntryFixed::clone() const
49 {
50 return std::make_unique<AnimationEntryFixed>(mfDuration, mfState);
51 }
52
53 bool AnimationEntryFixed::operator==(const AnimationEntry& rCandidate) const
54 {
55 const AnimationEntryFixed* pCompare = dynamic_cast< const AnimationEntryFixed* >(&rCandidate);
56
57 return (pCompare
60 }
61
63 {
64 return mfDuration;
65 }
66
67 double AnimationEntryFixed::getStateAtTime(double /*fTime*/) const
68 {
69 return mfState;
70 }
71
72 double AnimationEntryFixed::getNextEventTime(double fTime) const
73 {
75 {
76 return mfDuration;
77 }
78 else
79 {
80 return 0.0;
81 }
82 }
83
84
85 AnimationEntryLinear::AnimationEntryLinear(double fDuration, double fFrequency, double fStart, double fStop)
86 : mfDuration(fDuration),
87 mfFrequency(fFrequency),
88 mfStart(fStart),
89 mfStop(fStop)
90 {
91 }
92
94 {
95 }
96
97 std::unique_ptr<AnimationEntry> AnimationEntryLinear::clone() const
98 {
99 return std::make_unique<AnimationEntryLinear>(mfDuration, mfFrequency, mfStart, mfStop);
100 }
101
103 {
104 const AnimationEntryLinear* pCompare = dynamic_cast< const AnimationEntryLinear* >(&rCandidate);
105
106 return (pCompare
109 && basegfx::fTools::equal(mfStop, pCompare->mfStop));
110 }
111
113 {
114 return mfDuration;
115 }
116
117 double AnimationEntryLinear::getStateAtTime(double fTime) const
118 {
120 {
121 const double fFactor(fTime / mfDuration);
122
123 if(fFactor > 1.0)
124 {
125 return mfStop;
126 }
127 else
128 {
129 return mfStart + ((mfStop - mfStart) * fFactor);
130 }
131 }
132 else
133 {
134 return mfStart;
135 }
136 }
137
138 double AnimationEntryLinear::getNextEventTime(double fTime) const
139 {
141 {
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 fTime += mfFrequency;
146
148 {
149 fTime = mfDuration;
150 }
151
152 return fTime;
153 }
154 else
155 {
156 return 0.0;
157 }
158 }
159
160
161 AnimationEntryList::Entries::size_type AnimationEntryList::impGetIndexAtTime(double fTime, double &rfAddedTime) const
162 {
163 Entries::size_type nIndex(0);
164
165 while(nIndex < maEntries.size() && basegfx::fTools::lessOrEqual(rfAddedTime + maEntries[nIndex]->getDuration(), fTime))
166 {
167 rfAddedTime += maEntries[nIndex++]->getDuration();
168 }
169
170 return nIndex;
171 }
172
174 : mfDuration(0.0)
175 {
176 }
177
179 {
180 }
181
182 std::unique_ptr<AnimationEntry> AnimationEntryList::clone() const
183 {
184 std::unique_ptr<AnimationEntryList> pNew(std::make_unique<AnimationEntryList>());
185
186 for(const auto &i : maEntries)
187 {
188 pNew->append(*i);
189 }
190
191 return pNew;
192 }
193
194 bool AnimationEntryList::operator==(const AnimationEntry& rCandidate) const
195 {
196 const AnimationEntryList* pCompare = dynamic_cast< const AnimationEntryList* >(&rCandidate);
197
198 if(pCompare && mfDuration == pCompare->mfDuration)
199 {
200 for(size_t a(0); a < maEntries.size(); a++)
201 {
202 if(!(*maEntries[a] == *pCompare->maEntries[a]))
203 {
204 return false;
205 }
206 }
207
208 return true;
209 }
210
211 return false;
212 }
213
215 {
216 const double fDuration(rCandidate.getDuration());
217
218 if(!basegfx::fTools::equalZero(fDuration))
219 {
220 maEntries.push_back(rCandidate.clone());
221 mfDuration += fDuration;
222 }
223 }
224
226 {
227 return mfDuration;
228 }
229
230 double AnimationEntryList::getStateAtTime(double fTime) const
231 {
233 {
234 double fAddedTime(0.0);
235 const auto nIndex(impGetIndexAtTime(fTime, fAddedTime));
236
237 if(nIndex < maEntries.size())
238 {
239 return maEntries[nIndex]->getStateAtTime(fTime - fAddedTime);
240 }
241 }
242
243 return 0.0;
244 }
245
246 double AnimationEntryList::getNextEventTime(double fTime) const
247 {
248 double fNewTime(0.0);
249
251 {
252 double fAddedTime(0.0);
253 const auto nIndex(impGetIndexAtTime(fTime, fAddedTime));
254
255 if(nIndex < maEntries.size())
256 {
257 fNewTime = maEntries[nIndex]->getNextEventTime(fTime - fAddedTime) + fAddedTime;
258 }
259 }
260
261 return fNewTime;
262 }
263
264
266 : mnRepeat(nRepeat)
267 {
268 }
269
271 {
272 }
273
274 std::unique_ptr<AnimationEntry> AnimationEntryLoop::clone() const
275 {
276 std::unique_ptr<AnimationEntryLoop> pNew(std::make_unique<AnimationEntryLoop>(mnRepeat));
277
278 for(const auto &i : maEntries)
279 {
280 pNew->append(*i);
281 }
282
283 return pNew;
284 }
285
286 bool AnimationEntryLoop::operator==(const AnimationEntry& rCandidate) const
287 {
288 const AnimationEntryLoop* pCompare = dynamic_cast< const AnimationEntryLoop* >(&rCandidate);
289
290 return (pCompare
291 && mnRepeat == pCompare->mnRepeat
292 && AnimationEntryList::operator==(rCandidate));
293 }
294
296 {
297 return (mfDuration * static_cast<double>(mnRepeat));
298 }
299
300 double AnimationEntryLoop::getStateAtTime(double fTime) const
301 {
303 {
304 const sal_uInt32 nCurrentLoop(static_cast<sal_uInt32>(fTime / mfDuration));
305
306 if(nCurrentLoop > mnRepeat)
307 {
308 return 1.0;
309 }
310 else
311 {
312 const double fTimeAtLoopStart(static_cast<double>(nCurrentLoop) * mfDuration);
313 const double fRelativeTime(fTime - fTimeAtLoopStart);
314 return AnimationEntryList::getStateAtTime(fRelativeTime);
315 }
316 }
317
318 return 0.0;
319 }
320
321 double AnimationEntryLoop::getNextEventTime(double fTime) const
322 {
323 double fNewTime(0.0);
324
326 {
327 const sal_uInt32 nCurrentLoop(static_cast<sal_uInt32>(fTime / mfDuration));
328
329 if(nCurrentLoop <= mnRepeat)
330 {
331 const double fTimeAtLoopStart(static_cast<double>(nCurrentLoop) * mfDuration);
332 const double fRelativeTime(fTime - fTimeAtLoopStart);
333 const double fNextEventAtLoop(AnimationEntryList::getNextEventTime(fRelativeTime));
334
335 if(!basegfx::fTools::equalZero(fNextEventAtLoop))
336 {
337 fNewTime = fNextEventAtLoop + fTimeAtLoopStart;
338 }
339 }
340 }
341
342 return fNewTime;
343 }
344} // end of namespace
345
346/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
double mfDuration
virtual double getStateAtTime(double fTime) const override
virtual double getDuration() const override
virtual std::unique_ptr< AnimationEntry > clone() const override
AnimationEntryFixed(double fDuration, double fState)
virtual double getNextEventTime(double fTime) const override
virtual bool operator==(const AnimationEntry &rCandidate) const override
virtual double getStateAtTime(double fTime) const override
virtual std::unique_ptr< AnimationEntry > clone() const override
virtual bool operator==(const AnimationEntry &rCandidate) const override
virtual double getDuration() const override
AnimationEntryLinear(double fDuration, double fFrequency, double fStart, double fStop)
virtual double getNextEventTime(double fTime) const override
virtual std::unique_ptr< AnimationEntry > clone() const override
void append(const AnimationEntry &rCandidate)
Entries::size_type impGetIndexAtTime(double fTime, double &rfAddedTime) const
virtual double getNextEventTime(double fTime) const override
virtual double getStateAtTime(double fTime) const override
virtual double getDuration() const override
virtual bool operator==(const AnimationEntry &rCandidate) const override
virtual std::unique_ptr< AnimationEntry > clone() const override
virtual double getDuration() const override
virtual bool operator==(const AnimationEntry &rCandidate) const override
virtual double getNextEventTime(double fTime) const override
virtual double getStateAtTime(double fTime) const override
virtual std::unique_ptr< AnimationEntry > clone() const =0
virtual double getDuration() const =0
sal_Int32 nIndex
uno_Any a
bool lessOrEqual(const T &rfValA, const T &rfValB)
bool more(const T &rfValA, const T &rfValB)
bool equalZero(const T &rfVal)
bool equal(T const &rfValA, T const &rfValB)
bool less(const T &rfValA, const T &rfValB)
int i