• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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_CONTROLLED_ANIMATOR_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_CONTROLLED_ANIMATOR_H
18 
19 #include <chrono>
20 #include <vector>
21 
22 #include "ui/base/ace_type.h"
23 
24 #include "base/thread/cancelable_callback.h"
25 #include "core/animation/animation_pub.h"
26 namespace OHOS::Ace {
27 
28 using AnimatorEvent = std::function<void()>;
29 using PictureInfo = std::pair<float, int32_t>;
30 using IndexChangeListener = std::function<void(int32_t)>;
31 
32 class ControlledAnimator : public AceType {
33     DECLARE_ACE_TYPE(ControlledAnimator, AceType);
34 public:
35     ControlledAnimator() = default;
36 
37     ~ControlledAnimator() = default;
38 
39     enum class ControlStatus {
40         IDLE,    // when animation not start or been cancel.
41         RUNNING, // play in reverse / forward direction.
42         PAUSED,  // paused by call Pause API.
43         STOPPED, // stopped by call Finish/Stop API or has played to the end.
44     };
45 
46     void SetControlStatus(ControlStatus controlStatus);
47     ControlStatus GetControlStatus() const;
48     void SetFillMode(FillMode fillMode);
49     FillMode GetFillMode() const;
50     void SetRunningIdx(int32_t runningIdx);
51     void SetDuration(int32_t duration);
52     int32_t GetDuration() const;
53     void SetIteration(int32_t iteration);
54     int32_t GetIteration() const;
55     void ClearInterpolators();
56     void AddInterpolator(const std::vector<PictureInfo>& pictureInfos);
57     void AddListener(IndexChangeListener listener);
58     // register callBack
59     void AddStartListener(AnimatorEvent startEvent);
60     void AddStopListener(AnimatorEvent stopEvent);
61     void AddPauseListener(AnimatorEvent pauseEvent);
62     void AddRepeatListener(AnimatorEvent repeatEvent);
63     void AddInnerRepeatListener(AnimatorEvent innerRepeatEvent);
64     void RemoveRepeatListener();
65     void RemoveInnerRepeatListener();
66     void AddCancelListener(AnimatorEvent cancelEvent);
67     void ClearAllListeners();
68     // behavioral control
69     void Forward();
70     void Backward();
71     void Cancel();
72     void Pause();
73     void Finish();
74     void MovePictureToRightPosition(bool checkWithFillMode = true);
75     int32_t CalFinishIdx(bool checkWithFillMode = true);
76 
77 private:
78     ControlStatus controlStatus_ = ControlStatus::IDLE;
79     bool isReverse_ = false;
80     FillMode fillMode_ = FillMode::FORWARDS;
81     int32_t iteration_ = 1;
82     // the total playback duration.
83     int32_t duration_ = 0;
84     // the playback duration ratio for each picture.
85     std::vector<PictureInfo> pictureInfos_;
86     int32_t runningIdx_ = 0;
87     bool needFireRepeatEvent_ = false;
88     bool isFirstRun_ = true;
89 
90     AnimatorEvent startEvent_;
91     AnimatorEvent stopEvent_;
92     AnimatorEvent pauseEvent_;
93     //  User-defined callback triggered when the animation repeats.
94     AnimatorEvent repeatEvent_;
95     //  Callback used within imageAnimatorPattern to update the duration for the next cycle.
96     AnimatorEvent innerRepeatEvent_;
97     AnimatorEvent cancelEvent_;
98 
99     void PostPlayTask(int32_t idx, int32_t iteration, int32_t idxOffset = 1, int32_t elapsedTime = 0.0);
100     IndexChangeListener playbackListener_;
101     CancelableCallback<void()> currentPostTask_;
102     // Records the start time of the current playback task.
103     std::chrono::time_point<std::chrono::steady_clock> currentTaskStartTime_;
104     // Tracks whether the playback was in forward order when paused (true for Backward, false for Forward).
105     bool elapsedTimeReversedStatus_ = false;
106     // Stores the elapsed time of the current playback, used to calculate remaining time.
107     int32_t elapsedTime_ = 0.0f;
108 };
109 
110 } // namespace OHOS::Ace
111 
112 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_CONTROLLED_ANIMATOR_H
113