1 /*
2  * Copyright (c) 2021 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_PROPERTIES_ANIMATION_OPTION_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_PROPERTIES_ANIMATION_OPTION_H
18 
19 #include <functional>
20 #include <string>
21 #include <unordered_map>
22 
23 #include "core/animation/animation_pub.h"
24 #include "core/animation/curve.h"
25 #include "core/components_ng/manager/display_sync/ui_display_sync.h"
26 
27 namespace OHOS::Ace {
28 
29 class AnimationOption final {
30 public:
31     AnimationOption() = default;
AnimationOption(const RefPtr<Curve> & curve,int32_t duration)32     AnimationOption(const RefPtr<Curve>& curve, int32_t duration) : duration_(duration), curve_(curve) {}
33     ~AnimationOption() = default;
34 
SetDuration(int32_t duration)35     void SetDuration(int32_t duration)
36     {
37         duration_ = duration;
38     }
39 
GetDuration()40     int32_t GetDuration() const
41     {
42         return duration_;
43     }
44 
SetDelay(int32_t delay)45     void SetDelay(int32_t delay)
46     {
47         delay_ = delay;
48     }
49 
GetDelay()50     int32_t GetDelay() const
51     {
52         return delay_;
53     }
54 
SetCurve(const RefPtr<Curve> & curve)55     void SetCurve(const RefPtr<Curve>& curve)
56     {
57         if (!curve) {
58             LOGE("set curve failed. curve is null.");
59             return;
60         }
61         curve_ = curve;
62     }
63 
SetIteration(int32_t iteration)64     void SetIteration(int32_t iteration)
65     {
66         if (iteration < 0 && iteration != ANIMATION_REPEAT_INFINITE) {
67             return;
68         }
69         iteration_ = iteration;
70     }
71 
GetIteration()72     int32_t GetIteration() const
73     {
74         return iteration_;
75     }
76 
SetTempo(float tempo)77     void SetTempo(float tempo)
78     {
79         if (tempo < 0.0f) {
80             return;
81         }
82         tempo_ = tempo;
83     }
84 
GetTempo()85     float GetTempo() const
86     {
87         return tempo_;
88     }
89 
SetAnimationDirection(AnimationDirection direction)90     void SetAnimationDirection(AnimationDirection direction)
91     {
92         direction_ = direction;
93     }
94 
GetAnimationDirection()95     AnimationDirection GetAnimationDirection() const
96     {
97         return direction_;
98     }
99 
GetCurve()100     const RefPtr<Curve>& GetCurve() const
101     {
102         return curve_;
103     }
104 
SetFillMode(const FillMode & fillMode)105     void SetFillMode(const FillMode& fillMode)
106     {
107         fillMode_ = fillMode;
108     }
109 
GetFillMode()110     FillMode GetFillMode() const
111     {
112         return fillMode_;
113     }
114 
SetOnFinishEvent(const std::function<void ()> & onFinishEvent)115     void SetOnFinishEvent(const std::function<void()>& onFinishEvent)
116     {
117         onFinishEvent_ = onFinishEvent;
118     }
119 
GetOnFinishEvent()120     const std::function<void()>& GetOnFinishEvent() const
121     {
122         return onFinishEvent_;
123     }
124 
IsValid()125     bool IsValid() const
126     {
127         return (GetDuration() > 0 || GetAllowRunningAsynchronously());
128     }
129 
SetAllowRunningAsynchronously(bool runAsync)130     void SetAllowRunningAsynchronously(bool runAsync)
131     {
132         allowRunningAsynchronously_ = runAsync;
133     }
134 
GetAllowRunningAsynchronously()135     bool GetAllowRunningAsynchronously() const
136     {
137         return allowRunningAsynchronously_;
138     }
139 
SetFinishCallbackType(FinishCallbackType finishCallbackType)140     void SetFinishCallbackType(FinishCallbackType finishCallbackType)
141     {
142         finishCallbackType_ = finishCallbackType;
143     }
144 
GetFinishCallbackType()145     FinishCallbackType GetFinishCallbackType() const
146     {
147         return finishCallbackType_;
148     }
149 
SetFrameRateRange(const RefPtr<FrameRateRange> & rateRange)150     void SetFrameRateRange(const RefPtr<FrameRateRange>& rateRange)
151     {
152         rateRange_ = rateRange;
153     }
154 
GetFrameRateRange()155     const RefPtr<FrameRateRange>& GetFrameRateRange() const
156     {
157         return rateRange_;
158     }
159 
ToString()160     std::string ToString() const
161     {
162         std::string result = std::string("duration:").append(std::to_string(duration_))
163             .append(", curve:").append(curve_ ? curve_->ToString() : "");
164         if (iteration_ != 1) {
165             result.append(", iteration:").append(std::to_string(iteration_));
166         }
167         if (delay_) {
168             result.append(", delay:").append(std::to_string(delay_));
169         }
170         if (!NearEqual(tempo_, 1.0f)) {
171             result.append(", tempo:").append(std::to_string(tempo_));
172         }
173         return result;
174     }
175 
176 private:
177     int32_t duration_ = 0;
178     int32_t delay_ = 0;
179     int32_t iteration_ = 1;
180     float tempo_ = 1.0f;
181     FillMode fillMode_ = FillMode::FORWARDS;
182     bool allowRunningAsynchronously_ = false;
183     RefPtr<Curve> curve_;
184     std::function<void()> onFinishEvent_;
185     AnimationDirection direction_ = AnimationDirection::NORMAL;
186     FinishCallbackType finishCallbackType_ = FinishCallbackType::REMOVED;
187     RefPtr<FrameRateRange> rateRange_;
188 };
189 
190 } // namespace OHOS::Ace
191 
192 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_PROPERTIES_ANIMATION_OPTION_H
193