soui 5.0.0.1
Soui5 Doc
 
Loading...
Searching...
No Matches
SValueAnimator.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef __SVALUEANIMATOR__H__
18#define __SVALUEANIMATOR__H__
19
20/**
21 * @file SValueAnimator.h
22 * @brief Provides a simple timing engine for running animations which calculate animated values and set them on target objects.
23 *
24 * @details This class provides a simple timing engine for running animations which calculate animated values and set them on target objects.
25 * There is a single timing pulse that all animations use. It runs in a custom handler to ensure that property changes happen on the UI thread.
26 * By default, SValueAnimator uses non-linear time interpolation, via the AccelerateDecelerateInterpolator class, which accelerates into and decelerates
27 * out of an animation. This behavior can be changed by calling setInterpolator(TimeInterpolator).
28 */
29
30#include <interface/SValueAnimator-i.h>
32#include <sobject/Sobject.hpp>
33
34SNSBEGIN
35
36/**
37 * @class SValueAnimator
38 * @brief A simple timing engine for running animations which calculate animated values and set them on target objects.
39 *
40 * @details This class provides a simple timing engine for running animations which calculate animated values and set them on target objects.
41 * There is a single timing pulse that all animations use. It runs in a custom handler to ensure that property changes happen on the UI thread.
42 * By default, SValueAnimator uses non-linear time interpolation, via the AccelerateDecelerateInterpolator class, which accelerates into and decelerates
43 * out of an animation. This behavior can be changed by calling setInterpolator(TimeInterpolator).
44 */
45class SOUI_EXP SValueAnimator
46 : public TObjRefImpl<SObjectImpl<IValueAnimator>>
48 DEF_SOBJECT(SObjectImpl<IValueAnimator>, L"valueAnimator")
49
50 protected:
51 /**
52 * @brief The first time that the animation's animateFrame() method is called.
53 * @details This time is used to determine elapsed time (and therefore the elapsed fraction) in subsequent calls to animateFrame().
54 * Whenever mStartTime is set, you must also update mStartTimeCommitted.
55 */
56 uint64_t mStartTime;
57
58 /**
59 * @brief Flag indicating whether the start time has been firmly committed.
60 * @details When true, the start time has been firmly committed as a chosen reference point in time by which the progress of the animation
61 * will be evaluated. When false, the start time may be updated when the first animation frame is committed so as to compensate for jank
62 * that may have occurred between when the start time was initialized and when the frame was actually drawn.
63 */
65
66 /**
67 * @brief Set when setCurrentPlayTime() is called.
68 * @details If negative, animation is not currently seeked to a value.
69 */
71
72 /**
73 * @brief Flag to indicate whether this animator is playing in reverse mode.
74 * @details This flag is different than mPlayingBackwards, which indicates merely whether the current iteration of the animator is playing in reverse.
75 * It is used in corner cases to determine proper end behavior.
76 */
78
79 /**
80 * @brief Tracks the overall fraction of the animation, ranging from 0 to mRepeatCount + 1.
81 */
83
84 /**
85 * @brief Tracks current elapsed/eased fraction, for querying in getAnimatedFraction().
86 * @details This is calculated by interpolating the fraction (range: [0, 1]) in the current iteration.
87 */
89
90 /**
91 * @brief Tracks the time (in milliseconds) when the last frame arrived.
92 */
94
95 /**
96 * @brief Tracks the time (in milliseconds) when the first frame arrived.
97 * @details Note the frame may arrive during the start delay.
98 */
100
101 /**
102 * @brief Additional playing state to indicate whether an animator has been start()'d.
103 * @details There is some lag between a call to start() and the first animation frame. We should still note that the animation has been started,
104 * even if its first animation frame has not yet happened, and reflect that state in isRunning(). Note that delayed animations are different:
105 * they are not started until their first animation frame, which occurs after their delay elapses.
106 */
108
109 /**
110 * @brief Additional playing state to indicate whether an animator has been start()'d, whether or not there is a nonzero startDelay.
111 */
113
114 /**
115 * @brief Tracks whether we've notified listeners of the onAnimationStart() event.
116 * @details This can be complex to keep track of since we notify listeners at different times depending on startDelay and whether start() was
117 * called before end().
118 */
120
121 /**
122 * @brief Flag that denotes whether the animation is set up and ready to go.
123 * @details Used to set up animation that has not yet been started.
124 */
126
127 /**
128 * @brief Flag that tracks whether animation has been requested to end.
129 */
131
132 /**
133 * @brief How long the animation should last in milliseconds.
134 */
136
137 /**
138 * @brief The amount of time in milliseconds to delay starting the animation after start() is called.
139 * @details Note that this start delay is unscaled. When there is a duration scale set on the animator, the scaling factor will be applied to this delay.
140 */
142
143 /**
144 * @brief The number of times the animation will repeat.
145 * @details The default is 0, which means the animation will play only once.
146 */
148
149 /**
150 * @brief Scaling factor for the duration.
151 */
153
154 /**
155 * @brief The type of repetition that will occur when repeatMode is nonzero.
156 * @details RESTART means the animation will start from the beginning on every new cycle. REVERSE means the animation will reverse directions on each iteration.
157 */
158 RepeatMode mRepeatMode;
159
160 /**
161 * @brief The time interpolator to be used.
162 * @details The elapsed fraction of the animation will be passed through this interpolator to calculate the interpolated fraction, which is then used to calculate the animated values.
163 */
165
166 /**
167 * @brief The set of listeners to be sent update events through the life of an animation.
168 */
169 SArray<IAnimatorUpdateListener *> mUpdateListeners;
170 SArray<IAnimatorListener *> mListeners;
171
172 /**
173 * @brief The container managing the timeline handlers.
174 */
175 ITimelineHandlersMgr *mContainer;
176
177 public:
178 /**
179 * @brief Creates a new SValueAnimator object.
180 * @details This default constructor is primarily for use internally; the factory methods which take parameters are more generally useful.
181 */
183
184 /**
185 * @brief Destructor.
186 */
188
189 /**
190 * @brief Sets the length of the animation.
191 * @param duration The length of the animation, in milliseconds. This value cannot be negative.
192 * @return SValueAnimator The object called with setDuration(). This return value makes it easier to compose statements together that construct
193 * and then set the duration, as in SValueAnimator.ofInt(0, 10).setDuration(500).start().
194 */
195 STDMETHOD_(void, setDuration)(THIS_ long duration) OVERRIDE;
196
197 private:
198 /**
199 * @brief Gets the scaled duration of the animation.
200 * @return The scaled duration of the animation.
201 */
202 long getScaledDuration();
203
204 public:
205 /**
206 * @brief Gets the length of the animation.
207 * @return The length of the animation, in milliseconds.
208 */
209 STDMETHOD_(long, getDuration)(THIS) SCONST OVERRIDE;
210
211 /**
212 * @brief Gets the total duration of the animation, including any repetitions.
213 * @return The total duration of the animation, in milliseconds.
214 */
215 STDMETHOD_(long, getTotalDuration)(THIS) SCONST OVERRIDE;
216
217 /**
218 * @brief Sets the position of the animation to the specified point in time.
219 * @param playTime The time, in milliseconds, to which the animation is advanced or rewound.
220 */
221 STDMETHOD_(void, setCurrentPlayTime)(THIS_ long playTime) OVERRIDE;
222
223 /**
224 * @brief Sets the position of the animation to the specified fraction.
225 * @param fraction The fraction to which the animation is advanced or rewound. Values outside the range of 0 to the maximum fraction for the animator
226 * will be clamped to the correct range.
227 */
228 STDMETHOD_(void, setCurrentFraction)(THIS_ float fraction) OVERRIDE;
229
230 private:
231 /**
232 * @brief Calculates current iteration based on the overall fraction.
233 * @param fraction The overall fraction of the animation.
234 * @return The current iteration.
235 */
236 int getCurrentIteration(float fraction);
237
238 /**
239 * @brief Calculates the fraction of the current iteration.
240 * @param fraction The overall fraction of the animation.
241 * @param inReverse Whether the animation is playing backwards.
242 * @return The fraction of the current iteration.
243 */
244 float getCurrentIterationFraction(float fraction, bool inReverse);
245
246 /**
247 * @brief Clamps fraction into the correct range: [0, mRepeatCount + 1].
248 * @param fraction The fraction to be clamped.
249 * @return The clamped fraction.
250 */
251 float clampFraction(float fraction);
252
253 /**
254 * @brief Calculates the direction of animation playing (i.e., forward or backward).
255 * @param iteration The current iteration.
256 * @param inReverse Whether the animation is being reversed.
257 * @return TRUE if the animation should play backward, FALSE otherwise.
258 */
259 bool shouldPlayBackward(int iteration, bool inReverse);
260
261 public:
262 /**
263 * @brief Gets the current position of the animation in time.
264 * @return The current position in time of the animation.
265 */
266 STDMETHOD_(long, getCurrentPlayTime)(THIS) OVERRIDE;
267
268 /**
269 * @brief Gets the amount of time, in milliseconds, to delay starting the animation after start() is called.
270 * @return The number of milliseconds to delay running the animation.
271 */
272 STDMETHOD_(long, getStartDelay)(THIS) SCONST OVERRIDE;
273
274 /**
275 * @brief Sets the amount of time, in milliseconds, to delay starting the animation after start() is called.
276 * @param startDelay The amount of the delay, in milliseconds.
277 */
278 STDMETHOD_(void, setStartDelay)(THIS_ long startDelay) OVERRIDE;
279
280 /**
281 * @brief Sets how many times the animation should be repeated.
282 * @param value The number of times the animation should be repeated. If the repeat count is 0, the animation is never repeated.
283 * If the repeat count is greater than 0 or INFINITE, the repeat mode will be taken into account. The repeat count is 0 by default.
284 */
285 STDMETHOD_(void, setRepeatCount)(THIS_ int value) OVERRIDE;
286
287 /**
288 * @brief Defines how many times the animation should repeat.
289 * @return The number of times the animation should repeat, or INFINITE.
290 */
291 STDMETHOD_(int, getRepeatCount)(THIS) SCONST OVERRIDE;
292
293 /**
294 * @brief Defines what this animation should do when it reaches the end.
295 * @param value Either RESTART or REVERSE. Defaults to RESTART.
296 */
297 STDMETHOD_(void, setRepeatMode)(THIS_ RepeatMode value) OVERRIDE;
298
299 /**
300 * @brief Defines what this animation should do when it reaches the end.
301 * @return Either REVERSE or RESTART.
302 */
303 STDMETHOD_(RepeatMode, getRepeatMode)(THIS) SCONST OVERRIDE;
304
305 /**
306 * @brief Adds a listener to the set of listeners that are sent update events through the life of an animation.
307 * @param listener The listener to be added to the current set of listeners for this animation.
308 */
309 STDMETHOD_(void, addUpdateListener)(THIS_ IAnimatorUpdateListener *listener) OVERRIDE;
310
311 /**
312 * @brief Removes all listeners from the set listening to frame updates for this animation.
313 */
314 STDMETHOD_(void, removeAllUpdateListeners)(THIS) OVERRIDE;
315
316 /**
317 * @brief Removes a listener from the set listening to frame updates for this animation.
318 * @param listener The listener to be removed from the current set of update listeners for this animation.
319 */
320 STDMETHOD_(void, removeUpdateListener)(THIS_ IAnimatorUpdateListener *listener) OVERRIDE;
321
322 /**
323 * @brief Sets the time interpolator used in calculating the elapsed fraction of this animation.
324 * @param value The interpolator to be used by this animation. A value of nullptr will result in linear interpolation.
325 */
326 STDMETHOD_(void, setInterpolator)(THIS_ IInterpolator *value) OVERRIDE;
327
328 /**
329 * @brief Returns the timing interpolator that this SValueAnimator uses.
330 * @return The timing interpolator for this SValueAnimator.
331 */
332 STDMETHOD_(IInterpolator *, getInterpolator)(THIS) SCONST OVERRIDE;
333
334 /**
335 * @brief Adds an animator listener.
336 * @param p The listener to be added.
337 */
338 STDMETHOD_(void, addListener)(THIS_ IAnimatorListener *p) OVERRIDE;
339
340 /**
341 * @brief Removes an animator listener.
342 * @param p The listener to be removed.
343 */
344 STDMETHOD_(void, removeListener)(THIS_ IAnimatorListener *p) OVERRIDE;
345
346 private:
347 /**
348 * @brief Notifies start listeners.
349 */
350 void notifyStartListeners();
351
352 /**
353 * @brief Starts the animation playing.
354 * @param playBackwards Whether the SValueAnimator should start playing in reverse.
355 */
356 void start(bool playBackwards);
357
358 public:
359 /**
360 * @brief Starts the animation playing.
361 * @param pContainer The container managing the timeline handlers.
362 */
363 STDMETHOD_(void, start)(THIS_ ITimelineHandlersMgr *pContainer) OVERRIDE;
364
365 /**
366 * @brief Ends the animation.
367 */
368 STDMETHOD_(void, end)(THIS) OVERRIDE;
369
370 /**
371 * @brief Checks if the animation is running.
372 * @return TRUE if the animation is running, FALSE otherwise.
373 */
374 STDMETHOD_(BOOL, isRunning)(THIS) SCONST OVERRIDE;
375
376 /**
377 * @brief Checks if the animation has been started.
378 * @return TRUE if the animation has been started, FALSE otherwise.
379 */
380 STDMETHOD_(BOOL, isStarted)(THIS) SCONST OVERRIDE;
381
382 /**
383 * @brief Plays the SValueAnimator in reverse.
384 * @details If the animation is already running, it will stop itself and play backwards from the point reached when reverse was called.
385 * If the animation is not currently running, then it will start from the end and play backwards. This behavior is only set for the current
386 * animation; future playing of the animation will use the default behavior of playing forward.
387 */
388 STDMETHOD_(void, reverse)(THIS) OVERRIDE;
389
390 /**
391 * @brief Creates a clone of the current animation.
392 * @return A clone of the current animation.
393 */
394 STDMETHOD_(IValueAnimator *, clone)(THIS) SCONST OVERRIDE;
395
396 /**
397 * @brief Copies the properties of another animation to this animation.
398 * @param pSrc The source animation to copy properties from.
399 */
400 STDMETHOD_(void, copy)(THIS_ const IValueAnimator *pSrc) OVERRIDE;
401
402 private:
403 /**
404 * @brief Checks if the animation can reverse.
405 * @return TRUE if the animation can reverse, FALSE otherwise.
406 */
407 bool canReverse();
408
409 /**
410 * @brief Ends the animation by removing it from the animations list.
411 * @details Must be called on the UI thread.
412 */
413 void endAnimation();
414
415 /**
416 * @brief Starts the animation by adding it to the active animations list.
417 * @details Must be called on the UI thread.
418 */
419 void startAnimation();
420
421 /**
422 * @brief Checks if the animation is pulsing internally.
423 * @details This is different than isRunning() in that the latter tracks the time after start()
424 * is called (or after start delay if any), which may be before the animation loop starts.
425 * @return TRUE if the animation is pulsing, FALSE otherwise.
426 */
427 bool isPulsingInternal();
428
429 public:
430 /**
431 * @brief Applies an adjustment to the animation to compensate for jank between when the animation first ran and when the frame was drawn.
432 * @param frameTime The current frame time.
433 */
434 STDMETHOD_(void, commitAnimationFrame)(THIS_ long frameTime) OVERRIDE;
435
436 private:
437 /**
438 * @brief Processes a single animation frame for a given animation.
439 * @param currentTime The current time, as tracked by the static timing handler.
440 * @return TRUE if the animation's duration, including any repetitions due to repeatCount, has been exceeded and the animation should be ended.
441 */
442 bool animateBasedOnTime(uint64_t currentTime);
443
444 /**
445 * @brief Animates the animation based on the current and last play times.
446 * @param currentPlayTime The current play time.
447 * @param lastPlayTime The last play time.
448 * @param inReverse Whether the animation is playing in reverse.
449 */
450 void animateBasedOnPlayTime(long currentPlayTime, long lastPlayTime, bool inReverse);
451
452 /**
453 * @brief Skips the animation value to the end/start, depending on whether the play direction is forward or backward.
454 * @param inReverse Whether the end value is based on a reverse direction.
455 */
456 void skipToEndValue(bool inReverse);
457
458 /**
459 * @brief Checks if the animation is initialized.
460 * @return TRUE if the animation is initialized, FALSE otherwise.
461 */
462 bool isInitialized();
463
464 /**
465 * @brief Processes a frame of the animation, adjusting the start time if needed.
466 * @param frameTime The frame time.
467 * @return TRUE if the animation has ended.
468 */
469 bool doAnimationFrame(uint64_t frameTime);
470
471 public:
472 /**
473 * @brief Returns the current animation fraction.
474 * @details This is the elapsed/interpolated fraction used in the most recent frame update on the animation.
475 * @return The elapsed/interpolated fraction of the animation.
476 */
477 STDMETHOD_(float, getAnimatedFraction)(THIS) SCONST OVERRIDE;
478
479 /**
480 * @brief Handles the next frame of the animation.
481 */
482 STDMETHOD_(void, OnNextFrame)(THIS_) OVERRIDE;
483
484 private:
485 /**
486 * @brief This method is called with the elapsed fraction of the animation during every animation frame.
487 * @details This function turns the elapsed fraction into an interpolated fraction and then into an animated value (from the evaluator).
488 * The function is called mostly during animation updates, but it is also called when the end() function is called, to set the value on the property.
489 * @param fraction The elapsed fraction of the animation.
490 */
491 void animateValue(float fraction);
492
493 /**
494 * @brief Removes the animation callback.
495 */
496 void removeAnimationCallback();
497
498 /**
499 * @brief Adds the animation callback.
500 */
501 void addAnimationCallback();
502};
503
504/**
505 * @class TValueAnimator
506 * @brief Template class for creating value animators.
507 * @tparam T The type of the animated value.
508 */
509template <class T>
511 protected:
512 /**
513 * @brief Type evaluator for the animated value.
514 */
516
517 /**
518 * @brief Current animated value.
519 */
521
522 public:
523 /**
524 * @brief Constructor.
525 * @param from Initial value.
526 * @param to Final value.
527 */
528 TValueAnimator(T from, T to)
529 : mValueEvaluator(from, to)
530 {
531 }
532
533 /**
534 * @brief Gets the current animated value.
535 * @return The current animated value.
536 */
537 T getValue() const
538 {
539 return mValue;
540 }
541
542 /**
543 * @brief Sets the range of the animation.
544 * @param from Initial value.
545 * @param to Final value.
546 */
547 void setRange(T from, T to)
548 {
549 mValueEvaluator.setRange(from, to);
550 }
551
552 /**
553 * @brief Copies the properties of another animation to this animation.
554 * @param pSrc The source animation to copy properties from.
555 */
556 STDMETHOD_(void, copy)(THIS_ const IValueAnimator *pSrc) OVERRIDE
557 {
559 const TValueAnimator *src = (const TValueAnimator *)pSrc;
560 mValue = src->mValue;
562 }
563
564 protected:
565 /**
566 * @brief Evaluates the animated value based on the given fraction.
567 * @param fraction The elapsed fraction of the animation.
568 */
569 STDMETHOD_(void, onEvaluateValue)(THIS_ float fraction) OVERRIDE
570 {
571 mValue = mValueEvaluator.evaluate(fraction);
572 }
573
574 public:
575 /**
576 * @brief Defines the attributes for TValueAnimator.
577 */
578 SOUI_ATTRS_BEGIN()
579 ATTR_INT(L"duration", mDuration, FALSE)
580 ATTR_INT(L"repeatCount", mRepeatCount, FALSE)
581 ATTR_ENUM_BEGIN(L"repeatMode", RepeatMode, FALSE)
582 ATTR_ENUM_VALUE(L"reverse", REVERSE)
583 ATTR_ENUM_VALUE(L"restart", RESTART)
584 ATTR_ENUM_END(mRepeatMode)
585 ATTR_INTERPOLATOR(L"interpolator", mInterpolator, FALSE)
586 ATTR_CHAIN_PTR(mInterpolator, 0)
587 SOUI_ATTRS_END()
588};
589
590/**
591 * @class SIntAnimator
592 * @brief Animator for integer values.
593 */
594class SOUI_EXP SIntAnimator : public TValueAnimator<int> {
595 DEF_SOBJECT(TValueAnimator<int>, L"IntAnimator")
596
597 public:
598 /**
599 * @brief Constructor.
600 */
602 : TValueAnimator<int>(0, 100)
603 {
604 }
605
606 public:
607 /**
608 * @brief Defines the attributes for SIntAnimator.
609 */
610 SOUI_ATTRS_BEGIN()
611 ATTR_INT(L"valueFrom", mValueEvaluator.mStart, FALSE)
612 ATTR_INT(L"valueTo", mValueEvaluator.mEnd, FALSE)
613 SOUI_ATTRS_END()
614};
615
616/**
617 * @class SSizeAnimator
618 * @brief Animator for SIZE values.
619 */
620class SOUI_EXP SSizeAnimator : public TValueAnimator<SIZE> {
621 DEF_SOBJECT(TValueAnimator<SIZE>, L"SizeAnimator")
622
623 public:
624 /**
625 * @brief Constructor.
626 */
628 : TValueAnimator<SIZE>(CSize(), CSize())
629 {
630 }
631
632 public:
633 /**
634 * @brief Defines the attributes for SSizeAnimator.
635 */
636 SOUI_ATTRS_BEGIN()
637 ATTR_SIZE(L"valueFrom", mValueEvaluator.mStart, FALSE)
638 ATTR_SIZE(L"valueTo", mValueEvaluator.mEnd, FALSE)
639 SOUI_ATTRS_END()
640};
641
642/**
643 * @class SPointAnimator
644 * @brief Animator for POINT values.
645 */
646class SOUI_EXP SPointAnimator : public TValueAnimator<POINT> {
647 DEF_SOBJECT(TValueAnimator<POINT>, L"PointAnimator")
648
649 public:
650 /**
651 * @brief Constructor.
652 */
654 : TValueAnimator<POINT>(CPoint(), CPoint())
655 {
656 }
657
658 public:
659 /**
660 * @brief Defines the attributes for SPointAnimator.
661 */
662 SOUI_ATTRS_BEGIN()
663 ATTR_POINT(L"valueFrom", mValueEvaluator.mStart, FALSE)
664 ATTR_POINT(L"valueTo", mValueEvaluator.mEnd, FALSE)
665 SOUI_ATTRS_END()
666};
667
668/**
669 * @class SRectAnimator
670 * @brief Animator for RECT values.
671 */
672class SOUI_EXP SRectAnimator : public TValueAnimator<RECT> {
673 DEF_SOBJECT(TValueAnimator<RECT>, L"RectAnimator")
674
675 public:
676 /**
677 * @brief Constructor.
678 */
680 : TValueAnimator<RECT>(CRect(), CRect())
681 {
682 }
683
684 public:
685 /**
686 * @brief Defines the attributes for SRectAnimator.
687 */
688 SOUI_ATTRS_BEGIN()
689 ATTR_RECT(L"valueFrom", mValueEvaluator.mStart, FALSE)
690 ATTR_RECT(L"valueTo", mValueEvaluator.mEnd, FALSE)
691 SOUI_ATTRS_END()
692};
693
694/**
695 * @class SFloatAnimator
696 * @brief Animator for float values.
697 */
698class SOUI_EXP SFloatAnimator : public TValueAnimator<float> {
699 DEF_SOBJECT(TValueAnimator<float>, L"FloatAnimator")
700
701 public:
702 /**
703 * @brief Constructor.
704 */
706 : TValueAnimator<float>(0.f, 1.f)
707 {
708 }
709
710 public:
711 /**
712 * @brief Defines the attributes for SFloatAnimator.
713 */
714 SOUI_ATTRS_BEGIN()
715 ATTR_FLOAT(L"valueFrom", mValueEvaluator.mStart, FALSE)
716 ATTR_FLOAT(L"valueTo", mValueEvaluator.mEnd, FALSE)
717 SOUI_ATTRS_END()
718};
719
720/**
721 * @class SColorAnimator
722 * @brief Animator for COLORREF values.
723 */
724class SOUI_EXP SColorAnimator : public TValueAnimator<COLORREF> {
725 DEF_SOBJECT(TValueAnimator<COLORREF>, L"ColorAnimator")
726
727 public:
728 /**
729 * @brief Constructor.
730 */
732 : TValueAnimator<COLORREF>(0, 0)
733 {
734 }
735
736 protected:
737 /**
738 * @brief Handles the "valueFrom" attribute.
739 * @param strValue The attribute value as a string.
740 * @param bLoading TRUE if the attribute is being loaded, FALSE otherwise.
741 * @return HRESULT indicating success or failure.
742 */
743 HRESULT OnAttrFrom(const SStringW &strValue, BOOL bLoading);
744
745 /**
746 * @brief Handles the "valueTo" attribute.
747 * @param strValue The attribute value as a string.
748 * @param bLoading TRUE if the attribute is being loaded, FALSE otherwise.
749 * @return HRESULT indicating success or failure.
750 */
751 HRESULT OnAttrTo(const SStringW &strValue, BOOL bLoading);
752
753 public:
754 /**
755 * @brief Defines the attributes for SColorAnimator.
756 */
757 SOUI_ATTRS_BEGIN()
758 ATTR_CUSTOM(L"valueFrom", OnAttrFrom)
759 ATTR_CUSTOM(L"valueTo", OnAttrTo)
760 SOUI_ATTRS_END()
761};
762
763/**
764 * @class SAnimatorGroup
765 * @brief Manages a group of animations.
766 */
767class SOUI_EXP SAnimatorGroup
768 : public TObjRefImpl<IAnimatorGroup>
769 , public IAnimatorListener {
770 public:
771 /**
772 * @enum AniState
773 * @brief Enumeration of animation states.
774 */
776 {
777 idle,
778 started,
779 running,
780 };
781
782 /**
783 * @brief Constructor.
784 */
786
787 /**
788 * @brief Destructor.
789 */
791
792 /**
793 * @brief Adds an animator to the group.
794 * @param ani Pointer to the animator to add.
795 * @return TRUE if the animator was added successfully, FALSE otherwise.
796 */
797 STDMETHOD_(BOOL, AddAnimator)(THIS_ IValueAnimator *ani) OVERRIDE;
798
799 /**
800 * @brief Removes an animator from the group.
801 * @param ani Pointer to the animator to remove.
802 * @return TRUE if the animator was removed successfully, FALSE otherwise.
803 */
804 STDMETHOD_(BOOL, RemoveAnimator)(THIS_ IValueAnimator *ani) OVERRIDE;
805
806 /**
807 * @brief Sets the listener for the animator group.
808 * @param listener Pointer to the listener.
809 */
810 STDMETHOD_(void, SetListener)(THIS_ IAnimatorGroupListerer *listener) OVERRIDE;
811
812 protected:
813 /**
814 * @brief Called when an animation starts.
815 * @param pAnimator Pointer to the animator that started.
816 */
817 STDMETHOD_(void, onAnimationStart)(THIS_ IValueAnimator *pAnimator);
818
819 /**
820 * @brief Called when an animation repeats.
821 * @param pAnimator Pointer to the animator that repeated.
822 */
823 STDMETHOD_(void, onAnimationRepeat)(THIS_ IValueAnimator *pAnimator)
824 {
825 }
826
827 /**
828 * @brief Called when an animation ends.
829 * @param pAnimator Pointer to the animator that ended.
830 */
831 STDMETHOD_(void, onAnimationEnd)(THIS_ IValueAnimator *pAnimator);
832
833 protected:
834 /**
835 * @brief Map of animators and their states.
836 */
837 typedef SMap<IValueAnimator *, AniState> AnimatorStateMap;
838
839 /**
840 * @brief Map of animators and their states.
841 */
843
844 /**
845 * @brief Listener for the animator group.
846 */
847 IAnimatorGroupListerer *m_listener;
848};
849
850SNSEND
851
852#endif // __SVALUEANIMATOR__H__
Interface for use with the ValueAnimator::setEvaluator(TypeEvaluator) function.
Manages a group of animations.
AniState
Enumeration of animation states.
AnimatorStateMap m_lstAnimator
Map of animators and their states.
SAnimatorGroup()
Constructor.
SMap< IValueAnimator *, AniState > AnimatorStateMap
Map of animators and their states.
IAnimatorGroupListerer * m_listener
Listener for the animator group.
void onAnimationRepeat(IValueAnimator *pAnimator)
Called when an animation repeats.
Smart pointer class for managing COM-style reference-counted objects.
HRESULT OnAttrTo(const SStringW &strValue, BOOL bLoading)
Handles the "valueTo" attribute.
SColorAnimator()
Constructor.
HRESULT OnAttrFrom(const SStringW &strValue, BOOL bLoading)
Handles the "valueFrom" attribute.
SFloatAnimator()
Constructor.
SIntAnimator()
Constructor.
SPointAnimator()
Constructor.
SRectAnimator()
Constructor.
SSizeAnimator()
Constructor.
A class representing an ASCII string.
Definition sstringw.h:96
int getRepeatCount() SCONST OVERRIDE
Defines how many times the animation should repeat.
SAutoRefPtr< IInterpolator > mInterpolator
The time interpolator to be used.
ITimelineHandlersMgr * mContainer
The container managing the timeline handlers.
void setRepeatCount(int value) OVERRIDE
Sets how many times the animation should be repeated.
void addUpdateListener(IAnimatorUpdateListener *listener) OVERRIDE
Adds a listener to the set of listeners that are sent update events through the life of an animation.
float mCurrentFraction
Tracks current elapsed/eased fraction, for querying in getAnimatedFraction().
void commitAnimationFrame(long frameTime) OVERRIDE
Applies an adjustment to the animation to compensate for jank between when the animation first ran an...
BOOL isStarted() SCONST OVERRIDE
Checks if the animation has been started.
bool mStarted
Additional playing state to indicate whether an animator has been start()'d, whether or not there is ...
void setInterpolator(IInterpolator *value) OVERRIDE
Sets the time interpolator used in calculating the elapsed fraction of this animation.
void setStartDelay(long startDelay) OVERRIDE
Sets the amount of time, in milliseconds, to delay starting the animation after start() is called.
void setRepeatMode(RepeatMode value) OVERRIDE
Defines what this animation should do when it reaches the end.
bool mStartListenersCalled
Tracks whether we've notified listeners of the onAnimationStart() event.
uint64_t mLastFrameTime
Tracks the time (in milliseconds) when the last frame arrived.
long getTotalDuration() SCONST OVERRIDE
Gets the total duration of the animation, including any repetitions.
bool mAnimationEndRequested
Flag that tracks whether animation has been requested to end.
float mSeekFraction
Set when setCurrentPlayTime() is called.
void setDuration(long duration) OVERRIDE
Sets the length of the animation.
void setCurrentPlayTime(long playTime) OVERRIDE
Sets the position of the animation to the specified point in time.
void reverse() OVERRIDE
Plays the SValueAnimator in reverse.
void setCurrentFraction(float fraction) OVERRIDE
Sets the position of the animation to the specified fraction.
bool mReversing
Flag to indicate whether this animator is playing in reverse mode.
IInterpolator * getInterpolator() SCONST OVERRIDE
Returns the timing interpolator that this SValueAnimator uses.
float getAnimatedFraction() SCONST OVERRIDE
Returns the current animation fraction.
uint64_t mStartTime
The first time that the animation's animateFrame() method is called.
void end() OVERRIDE
Ends the animation.
uint64_t mFirstFrameTime
Tracks the time (in milliseconds) when the first frame arrived.
BOOL isRunning() SCONST OVERRIDE
Checks if the animation is running.
bool mRunning
Additional playing state to indicate whether an animator has been start()'d.
long mStartDelay
The amount of time in milliseconds to delay starting the animation after start() is called.
RepeatMode getRepeatMode() SCONST OVERRIDE
Defines what this animation should do when it reaches the end.
SValueAnimator()
Creates a new SValueAnimator object.
IValueAnimator * clone() SCONST OVERRIDE
Creates a clone of the current animation.
void addListener(IAnimatorListener *p) OVERRIDE
Adds an animator listener.
void copy(const IValueAnimator *pSrc) OVERRIDE
Copies the properties of another animation to this animation.
SArray< IAnimatorUpdateListener * > mUpdateListeners
The set of listeners to be sent update events through the life of an animation.
float mOverallFraction
Tracks the overall fraction of the animation, ranging from 0 to mRepeatCount + 1.
void removeListener(IAnimatorListener *p) OVERRIDE
Removes an animator listener.
long getStartDelay() SCONST OVERRIDE
Gets the amount of time, in milliseconds, to delay starting the animation after start() is called.
float sDurationScale
Scaling factor for the duration.
long getCurrentPlayTime() OVERRIDE
Gets the current position of the animation in time.
int mRepeatCount
The number of times the animation will repeat.
RepeatMode mRepeatMode
The type of repetition that will occur when repeatMode is nonzero.
long mDuration
How long the animation should last in milliseconds.
void removeUpdateListener(IAnimatorUpdateListener *listener) OVERRIDE
Removes a listener from the set listening to frame updates for this animation.
void removeAllUpdateListeners() OVERRIDE
Removes all listeners from the set listening to frame updates for this animation.
void OnNextFrame() OVERRIDE
Handles the next frame of the animation.
bool mInitialized
Flag that denotes whether the animation is set up and ready to go.
long getDuration() SCONST OVERRIDE
Gets the length of the animation.
bool mStartTimeCommitted
Flag indicating whether the start time has been firmly committed.
void onEvaluateValue(float fraction) OVERRIDE
Evaluates the animated value based on the given fraction.
void setRange(T from, T to)
Sets the range of the animation.
TypeEvaluator< T > mValueEvaluator
Type evaluator for the animated value.
T getValue() const
Gets the current animated value.
TValueAnimator(T from, T to)
Constructor.
void copy(const IValueAnimator *pSrc) OVERRIDE
Copies the properties of another animation to this animation.
T mValue
Current animated value.
Template class for evaluating interpolated values between a start and end value.
时间轴处理接口