blob: eca1afcc54dc958c8cd34ea2adfd819ce1089902 [file] [log] [blame]
Doris Liu766431a2016-02-04 22:17:11 +00001/*
2 * Copyright (C) 2016 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#include "PropertyValuesAnimatorSet.h"
18#include "RenderNode.h"
19
20namespace android {
21namespace uirenderer {
22
23void PropertyValuesAnimatorSet::addPropertyAnimator(PropertyValuesHolder* propertyValuesHolder,
24 Interpolator* interpolator, nsecs_t startDelay,
25 nsecs_t duration, int repeatCount) {
26
27 PropertyAnimator* animator = new PropertyAnimator(propertyValuesHolder,
28 interpolator, startDelay, duration, repeatCount);
29 mAnimators.emplace_back(animator);
30 setListener(new PropertyAnimatorSetListener(this));
31}
32
33PropertyValuesAnimatorSet::PropertyValuesAnimatorSet()
34 : BaseRenderNodeAnimator(1.0f) {
35 setStartValue(0);
36 mLastFraction = 0.0f;
37 setInterpolator(new LinearInterpolator());
38}
39
40void PropertyValuesAnimatorSet::onFinished(BaseRenderNodeAnimator* animator) {
41 if (mOneShotListener.get()) {
42 mOneShotListener->onAnimationFinished(animator);
43 mOneShotListener = nullptr;
44 }
45}
46
47float PropertyValuesAnimatorSet::getValue(RenderNode* target) const {
48 return mLastFraction;
49}
50
51void PropertyValuesAnimatorSet::setValue(RenderNode* target, float value) {
52 mLastFraction = value;
53}
54
55void PropertyValuesAnimatorSet::onPlayTimeChanged(nsecs_t playTime) {
56 for (size_t i = 0; i < mAnimators.size(); i++) {
57 mAnimators[i]->setCurrentPlayTime(playTime);
58 }
59}
60
61void PropertyValuesAnimatorSet::reset() {
62 // TODO: implement reset through adding a play state because we need to support reset() even
63 // during an animation run.
64}
65
66void PropertyValuesAnimatorSet::start(AnimationListener* listener) {
67 init();
68 mOneShotListener = listener;
69 BaseRenderNodeAnimator::start();
70}
71
72void PropertyValuesAnimatorSet::reverse(AnimationListener* listener) {
73// TODO: implement reverse
74}
75
76void PropertyValuesAnimatorSet::init() {
77 if (mInitialized) {
78 return;
79 }
80 nsecs_t maxDuration = 0;
81 for (size_t i = 0; i < mAnimators.size(); i++) {
82 if (maxDuration < mAnimators[i]->getTotalDuration()) {
83 maxDuration = mAnimators[i]->getTotalDuration();
84 }
85 }
86 mDuration = maxDuration;
87 mInitialized = true;
88}
89
90uint32_t PropertyValuesAnimatorSet::dirtyMask() {
91 return RenderNode::DISPLAY_LIST;
92}
93
94PropertyAnimator::PropertyAnimator(PropertyValuesHolder* holder, Interpolator* interpolator,
95 nsecs_t startDelay, nsecs_t duration, int repeatCount)
96 : mPropertyValuesHolder(holder), mInterpolator(interpolator), mStartDelay(startDelay),
97 mDuration(duration) {
98 if (repeatCount < 0) {
99 mRepeatCount = UINT32_MAX;
100 } else {
101 mRepeatCount = repeatCount;
102 }
103 mTotalDuration = ((nsecs_t) mRepeatCount + 1) * mDuration + mStartDelay;
104}
105
106void PropertyAnimator::setCurrentPlayTime(nsecs_t playTime) {
107 if (playTime >= mStartDelay && playTime < mTotalDuration) {
108 nsecs_t currentIterationPlayTime = (playTime - mStartDelay) % mDuration;
109 mLatestFraction = currentIterationPlayTime / (float) mDuration;
110 } else if (mLatestFraction < 1.0f && playTime >= mTotalDuration) {
111 mLatestFraction = 1.0f;
112 } else {
113 return;
114 }
115
116 setFraction(mLatestFraction);
117}
118
119void PropertyAnimator::setFraction(float fraction) {
120 float interpolatedFraction = mInterpolator->interpolate(mLatestFraction);
121 mPropertyValuesHolder->setFraction(interpolatedFraction);
122}
123
124void PropertyAnimatorSetListener::onAnimationFinished(BaseRenderNodeAnimator* animator) {
125 mSet->onFinished(animator);
126}
127
128}
129}