• 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/log/ace_trace.h"
22 #include "base/utils/macros.h"
23 #include "core/animation/animation_pub.h"
24 #include "core/animation/interpolator.h"
25 #include "core/animation/motion.h"
26 #include "core/animation/scheduler.h"
27 #include "core/animation/status_listener.h"
28 #include "core/animation/time_event.h"
29 #include "core/components/common/properties/animation_option.h"
30 
31 namespace OHOS::Ace {
32 
33 class ACE_FORCE_EXPORT_WITH_PREVIEW Animator : public AceType, public StatusListenable {
34     DECLARE_ACE_TYPE(Animator, AceType);
35 
36 public:
37     enum class Status {
38         IDLE,     // when animation not start or been cancel.
39         RUNNING,  // play in reverse / forward direction.
40         PAUSED,   // paused by call Pause API.
41         STOPPED,  // stopped by call Finish/Stop API or has played to the end.
42     };
43 
44     // Adjust global animation duration, default scale is 1.0f.
45     static void SetDurationScale(float scale);
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     Animator(const WeakPtr<PipelineBase>& context, const char* name = nullptr);
53 
54     Animator(const char* name = nullptr);
55 
56     ~Animator() override;
57 
58     void AttachScheduler(const WeakPtr<PipelineBase>& context);
59     void AttachSchedulerOnContainer();
60     bool HasScheduler() const;
61 
62     // Every interpolate animation needs to add itself into animator and use the controller to drive.
63     void AddInterpolator(const RefPtr<Interpolator>& animation);
64     void RemoveInterpolator(const RefPtr<Interpolator>& animation);
65     void ClearInterpolators();
66 
67     // Controller (A) can be added to other controller (B) as a proxy when (B) is running.
68     void AddProxyController(const RefPtr<Animator>& proxy);
69     void RemoveProxyController(const RefPtr<Animator>& proxy);
70     void ClearProxyControllers();
71 
72     // Can use these APIs to check controller's current state.
73     Status GetStatus() const;
74     bool IsStopped() const;
75     bool IsRunning() const;
76     bool IsPending() const;
77 
78     // Sets the total running time of the animator to drive the animation.
79     void SetDuration(int32_t duration);
80     int32_t GetDuration() const;
81     void SetStartDelay(int32_t startDelay);
82 
83     // At last will run iteration loops. repeatTimes equals 0 as default.
84     bool SetIteration(int32_t iteration);
85 
86     // fillmode used to decided the attr of last frame or first frame.
87     void SetFillMode(FillMode fillMode);
88 
89     // tempo is used to control speed of animation.
90     void SetTempo(float tempo);
91 
92     // init animation parameters with animationOption
93     void ApplyOption(const AnimationOption& option);
94 
95     // Whether the animation should be played in reverse in turn.
96     void SetAnimationDirection(AnimationDirection direction);
97 
98     // Set Whether or not the animator is allowed to run asynchronously off of the UI thread.
99     void SetAllowRunningAsynchronously(bool runAsync);
100 
101     // Get whether or not the animator is allowed to run asynchronously off of the UI thread.
102     bool GetAllowRunningAsynchronously();
103 
104     // Update the played time, will not trigger OnFrame callback.
105     void UpdatePlayedTime(int32_t playedTime, bool checkReverse = false);
106     int64_t GetPlayedTime() const;
107 
108     // Trigger one frame callback by the given time.
109     void TriggerFrame(int32_t playedTime, bool checkReverse = false);
110 
111     // Motion not support set duration & repeat & Reverse.
112     void PlayMotion(const RefPtr<Motion>& motion);
113 
114     // Play the Animation based current direction.
115     void Play();
116 
117     // Reverse the Animation based current direction.
118     void Reverse();
119 
120     // Means play forward and set direction reverse false.
121     void Forward();
122 
123     // Means play backward and set direction reverse true.
124     void Backward();
125 
126     // Stop at the current frame(Continueable stop).
127     void Pause();
128 
129     // Resume animation from the pause frame.
130     void Resume();
131 
132     // Stop at the current frame(Unrecoverable stop).
133     void Stop();
134 
135     // Stop at the end frame.
136     void Finish();
137 
138     // Stop at the start frame.
139     void Cancel();
140 
141     // Reset isReverse_ value.
142     void ResetIsReverse();
143 
144     // Get Controller Id.
145     int32_t GetId() const;
146 
147     // Get AnimationScale
148     float GetAnimationScale() const;
149 
150     // Get FillMode
151     FillMode GetFillMode() const;
152 
153     // Get Iteration
154     int32_t GetIteration() const;
155 
156 private:
157     // Screen refresh callback. duration is in millisecond.
158     void OnFrame(int64_t duration);
159 
160     // Callback the played time to the interpolator animation.
161     void NotifyInterpolator(int32_t playedTime);
162 
163     // Callback the played time to the motion animation.
164     void NotifyMotion(int32_t playedTime);
165 
166     void StartInner(bool alwaysNotify);
167 
168     AnimationOption GetAnimationOption();
169 
170     bool IsSupportedRunningAsynchronously();
171 
172     bool StartAsync();
173 
174     bool StartInnerAsync();
175 
176     void StopInnerAsync();
177 
178     // Calculate played loops and remaining in playedTime
179     int32_t GetPlayedLoopsAndRemaining(int32_t& playedTime);
180 
181     bool GetInitAnimationDirection();
182 
183     // update repeatTimesLeft_ and returns true if run out of repeat times.
184     bool UpdateRepeatTimesLeftAndCheckFinished(int32_t playedLoops);
185 
186     void ToggleDirection();
187     float GetNormalizedTime(float playedTime, bool needStop) const;
188 
189     void UpdateScaledTime();
190     void UpdateIteration(int32_t iteration);
191 
192     void Copy(const RefPtr<Animator>& controller);
193 
194     std::list<RefPtr<Interpolator>> interpolators_;
195     std::list<RefPtr<Animator>> proxyControllers_;
196     RefPtr<Scheduler> scheduler_;
197     RefPtr<Motion> motion_;
198     FillMode fillMode_ = FillMode::FORWARDS;
199     AnimationDirection direction_ = AnimationDirection::NORMAL;
200     int32_t duration_ = 0;        // millisecond.
201     int64_t elapsedTime_ = 0;     // millisecond. in range: 0 ~ startDelay_ + INTERPOLATE_DURATION_MAX
202     int32_t startDelay_ = 0;      // millisecond.
203     int32_t repeatTimes_ = 0;     // user configured repeat times.
204     int32_t iteration_ = 1;       // user configured iteration times.
205     int32_t repeatTimesLeft_ = 0; // repeat times for controller to play
206     int32_t scaledDuration_ = 0;
207     int32_t scaledStartDelay_ = 0;
208     int asyncRunningAnimationCount_ = 0;
209     bool isReverse_ = false;
210     bool isResume_ = false;
211     bool isCurDirection_ = false;
212     bool toggleDirectionPending_ = false;
213     bool allowRunningAsynchronously_ = false;
214     Status status_ = Status::IDLE;
215     int32_t controllerId_ = 0;
216     static float scale_;
217     float tempo_ = 1.0f;
218     bool isBothBackwards = false;
219     std::shared_ptr<AceAsyncScopedTrace> asyncTrace_;
220     std::string animatorName_ = "Animator";
221 };
222 
223 } // namespace OHOS::Ace
224 
225 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_ANIMATOR_H
226