• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_ANIMATOR_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_ANIMATOR_H
18 
19 #include <list>
20 
21 #include "base/utils/macros.h"
22 #include "core/animation/animation_pub.h"
23 #include "core/animation/interpolator.h"
24 #include "core/animation/motion.h"
25 #include "core/animation/scheduler.h"
26 #include "core/animation/status_listener.h"
27 #include "core/animation/time_event.h"
28 #include "core/components/common/properties/animation_option.h"
29 
30 namespace OHOS::Ace {
31 
32 class ACE_EXPORT Animator : public AceType, public StatusListenable {
33     DECLARE_ACE_TYPE(Animator, AceType);
34 
35 public:
36     enum class Status {
37         IDLE,     // when animation not start or been cancel.
38         RUNNING,  // play in reverse / forward direction.
39         PAUSED,   // paused by call Pause API.
40         STOPPED,  // stopped by call Finish/Stop API or has played to the end.
41     };
42 
43     // Adjust global animation duration, default scale is 1.0f.
44     static void SetDurationScale(float scale);
45     static float GetDurationScale();
46 
47     // Animator can play animations.
48     // So far, animation has two types: Interpolator and Motion(physical-based animation).
49     // But only one type will be played at a time. When playing one, the other's setting will be cleared.
50     // 1. Interpolator: Play/Reverse/Stop/Finish will work
51     // 2. Motion: PlayMotion will work
52     explicit Animator(const WeakPtr<PipelineContext>& context);
53 
54     Animator();
55 
56     ~Animator() override;
57 
58     void AttachScheduler(const WeakPtr<PipelineContext>& context);
59     bool HasScheduler() const;
60 
61     // Every interpolate animation needs to add itself into animator and use the controller to drive.
62     void AddInterpolator(const RefPtr<Interpolator>& animation);
63     void RemoveInterpolator(const RefPtr<Interpolator>& animation);
64     void ClearInterpolators();
65 
66     // Controller (A) can be added to other controller (B) as a proxy when (B) is running.
67     void AddProxyController(const RefPtr<Animator>& proxy);
68     void RemoveProxyController(const RefPtr<Animator>& proxy);
69     void ClearProxyControllers();
70 
71     // Can use these APIs to check controller's current state.
72     Status GetStatus() const;
73     bool IsStopped() const;
74     bool IsRunning() const;
75     bool IsPending() const;
76 
77     // Sets the total running time of the animator to drive the animation.
78     void SetDuration(int32_t duration);
79     int32_t GetDuration() const;
80     void SetStartDelay(int32_t startDelay);
81 
82     // At last will run iteration loops. repeatTimes equals 0 as default.
83     bool SetIteration(int32_t iteration);
84 
85     // fillmode used to decided the attr of last frame or first frame.
86     void SetFillMode(FillMode fillMode);
87 
88     // tempo is used to control speed of animation.
89     void SetTempo(float tempo);
90 
91     // init animation parameters with animationOption
92     void ApplyOption(const AnimationOption& option);
93 
94     // Whether the animation should be played in reverse in turn.
95     void SetAnimationDirection(AnimationDirection direction);
96 
97     // Set Whether or not the animator is allowed to run asynchronously off of the UI thread.
98     void SetAllowRunningAsynchronously(bool runAsync);
99 
100     // Get whether or not the animator is allowed to run asynchronously off of the UI thread.
101     bool GetAllowRunningAsynchronously();
102 
103     // Update the played time, will not trigger OnFrame callback.
104     void UpdatePlayedTime(int32_t playedTime, bool checkReverse = false);
105     int64_t GetPlayedTime() const;
106 
107     // Trigger one frame callback by the given time.
108     void TriggerFrame(int32_t playedTime, bool checkReverse = false);
109 
110     // Motion not support set duration & repeat & Reverse.
111     void PlayMotion(const RefPtr<Motion>& motion);
112 
113     // Play the Animation based current direction.
114     void Play();
115 
116     // Reverse the Animation based current direction.
117     void Reverse();
118 
119     // Means play forward and set direction reverse false.
120     void Forward();
121 
122     // Means play backward and set direction reverse true.
123     void Backward();
124 
125     // Stop at the current frame(Continueable stop).
126     void Pause();
127 
128     // Resume animtion from the pause frame.
129     void Resume();
130 
131     // Stop at the current frame(Unrecoverable stop).
132     void Stop();
133 
134     // Stop at the end frame.
135     void Finish();
136 
137     // Stop at the start frame.
138     void Cancel();
139 
140     // Get Controller Id.
141     int32_t GetId() const;
142 
143 private:
144     // Screen refresh callback. duration is in millisecond.
145     void OnFrame(int64_t duration);
146 
147     // Callback the played time to the interpolator animtion.
148     void NotifyInterpolator(int32_t playedTime);
149 
150     // Callback the played time to the motion animtion.
151     void NotifyMotion(int32_t playedTime);
152 
153     void StartInner(bool alwaysNotify);
154 
155     AnimationOption GetAnimationOption();
156 
157     bool IsSupportedRunningAsynchronously();
158 
159     bool StartAsync();
160 
161     bool StartInnerAsync();
162 
163     void StopInnerAsync();
164 
165     // Calculate played loops and remaining in playedTime
166     int32_t GetPlayedLoopsAndRemaining(int32_t& playedTime);
167 
168     bool GetInitAnimationDirection();
169 
170     // update repeatTimesLeft_ and returns true if run out of repeat times.
171     bool UpdateRepeatTimesLeftAndCheckFinished(int32_t playedLoops);
172 
173     void ToggleDirection();
174     float GetNormalizedTime(float playedTime, bool needStop) const;
175 
176     void UpdateScaledTime();
177     void UpdateIteration(int32_t iteration);
178 
179     void Copy(const RefPtr<Animator>& controller);
180 
181     std::list<RefPtr<Interpolator>> interpolators_;
182     std::list<RefPtr<Animator>> proxyControllers_;
183     RefPtr<Scheduler> scheduler_;
184     RefPtr<Motion> motion_;
185     FillMode fillMode_ = FillMode::FORWARDS;
186     AnimationDirection direction_ = AnimationDirection::NORMAL;
187     int32_t duration_ = 0;        // millisecond.
188     int64_t elapsedTime_ = 0;     // millisecond. in range: 0 ~ startDelay_ + INTERPOLATE_DURATION_MAX
189     int32_t startDelay_ = 0;      // millisecond.
190     int32_t repeatTimes_ = 0;     // user configured repeat times.
191     int32_t iteration_ = 1;       // user configured iteration times.
192     int32_t repeatTimesLeft_ = 0; // repeat times for controller to play
193     int32_t scaledDuration_ = 0;
194     int32_t scaledStartDelay_ = 0;
195     int asyncRunningAnimationCount_ = 0;
196     bool isReverse_ = false;
197     bool isResume_ = false;
198     bool isCurDirection_ = false;
199     bool toggleDirectionPending_ = false;
200     bool allowRunningAsynchronously_ = false;
201     Status status_ = Status::IDLE;
202     int32_t controllerId_ = 0;
203     static float scale_;
204     float tempo_ = 1.0f;
205     bool isBothBackwards = false;
206 };
207 
208 } // namespace OHOS::Ace
209 
210 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_ANIMATOR_H
211