• 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_COMPONENTS_BASE_PROPERTIES_TWEEN_OPTION_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_PROPERTIES_TWEEN_OPTION_H
18 
19 #include <list>
20 #include <map>
21 
22 #include "base/geometry/dimension.h"
23 #include "base/geometry/dimension_offset.h"
24 #include "base/geometry/transform_util.h"
25 #include "core/animation/animation.h"
26 #include "core/animation/animation_pub.h"
27 #include "core/animation/curve.h"
28 #include "core/animation/curves.h"
29 #include "core/animation/keyframe_animation.h"
30 #include "core/animation/property_animatable.h"
31 #include "core/animation/property_animation.h"
32 #include "core/components/common/properties/color.h"
33 #include "core/components/common/properties/decoration.h"
34 #include "core/components/common/properties/motion_path_option.h"
35 
36 namespace OHOS::Ace {
37 
38 class TweenOption final {
39 public:
SetCurve(const RefPtr<Curve> & curve)40     void SetCurve(const RefPtr<Curve>& curve)
41     {
42         if (!curve) {
43             LOGE("set curve failed. curve is null.");
44             return;
45         }
46         curve_ = curve;
47     }
48 
GetCurve()49     const RefPtr<Curve>& GetCurve() const
50     {
51         return curve_;
52     }
53 
SetDelay(int32_t delay)54     void SetDelay(int32_t delay)
55     {
56         delay_ = delay;
57     }
58 
GetDelay()59     int32_t GetDelay() const
60     {
61         return delay_;
62     }
63 
SetIteration(int32_t iteration)64     void SetIteration(int32_t iteration)
65     {
66         iteration_ = iteration;
67     }
68 
GetIteration()69     int32_t GetIteration() const
70     {
71         return iteration_;
72     }
73 
SetFillMode(FillMode fillMode)74     void SetFillMode(FillMode fillMode)
75     {
76         fillMode_ = fillMode;
77     }
78 
GetFillMode()79     FillMode GetFillMode() const
80     {
81         return fillMode_;
82     }
83 
SetTempo(float tempo)84     void SetTempo(float tempo)
85     {
86         if (tempo < 0.0f) {
87             return;
88         }
89         tempo_ = tempo;
90     }
91 
GetTempo()92     float GetTempo() const
93     {
94         return tempo_;
95     }
96 
SetDuration(int32_t duration)97     void SetDuration(int32_t duration)
98     {
99         duration_ = duration;
100         changeDuration_ = true;
101     }
102 
103     // Duration in millisecond.
GetDuration()104     int32_t GetDuration() const
105     {
106         return duration_;
107     }
108 
SetAnimationDirection(AnimationDirection direction)109     void SetAnimationDirection(AnimationDirection direction)
110     {
111         direction_ = direction;
112     }
113 
GetAnimationDirection()114     AnimationDirection GetAnimationDirection() const
115     {
116         return direction_;
117     }
118 
SetAnimatables(const PropAnimationMap & animatables)119     void SetAnimatables(const PropAnimationMap& animatables)
120     {
121         propAnimations_ = animatables;
122     }
123 
GetAnimatables()124     const PropAnimationMap& GetAnimatables() const
125     {
126         return propAnimations_;
127     }
128 
SetTransformOrigin(const Dimension & originX,const Dimension & originY)129     void SetTransformOrigin(const Dimension& originX, const Dimension& originY)
130     {
131         auto keyFrameAnimation = AceType::MakeRefPtr<KeyframeAnimation<DimensionOffset>>();
132         auto begin = AceType::MakeRefPtr<Keyframe<DimensionOffset>>(0.0f, DimensionOffset(originX, originY));
133         auto end = AceType::MakeRefPtr<Keyframe<DimensionOffset>>(1.0f, DimensionOffset(originX, originY));
134         keyFrameAnimation->AddKeyframe(begin);
135         keyFrameAnimation->AddKeyframe(end);
136         transformOriginAnimation_ = keyFrameAnimation;
137         SetTransformOriginChanged(true);
138     }
139 
GetTransformOriginX()140     Dimension GetTransformOriginX() const
141     {
142         if (transformOriginAnimation_) {
143             return transformOriginAnimation_->GetValue().GetX();
144         }
145 
146         return 0.5_pct;
147     }
148 
GetTransformOriginY()149     Dimension GetTransformOriginY() const
150     {
151         if (transformOriginAnimation_) {
152             return transformOriginAnimation_->GetValue().GetY();
153         }
154 
155         return 0.5_pct;
156     }
157 
GetTransformOriginAnimation()158     const RefPtr<Animation<DimensionOffset>>& GetTransformOriginAnimation() const
159     {
160         return transformOriginAnimation_;
161     }
162 
SetIsBackground(bool isBackground)163     void SetIsBackground(bool isBackground)
164     {
165         isBackground_ = isBackground;
166     }
167 
GetIsBackground()168     bool GetIsBackground() const
169     {
170         return isBackground_;
171     }
172 
GetMotionPathOption()173     const MotionPathOption& GetMotionPathOption() const
174     {
175         return motionPathOption_;
176     }
177 
SetMotionPathOption(const MotionPathOption & option)178     void SetMotionPathOption(const MotionPathOption& option)
179     {
180         motionPathOption_ = option;
181     }
182 
SetTranslateAnimations(AnimationType type,const RefPtr<Animation<DimensionOffset>> & transformOffsetAnimation)183     void SetTranslateAnimations(AnimationType type, const RefPtr<Animation<DimensionOffset>>& transformOffsetAnimation)
184     {
185         if (!transformOffsetAnimation) {
186             LOGE("input translateAnimation is null.");
187             return;
188         }
189         transformOffsetAnimations_[type] = transformOffsetAnimation;
190     }
191 
GetTranslateAnimations()192     const std::unordered_map<AnimationType, RefPtr<Animation<DimensionOffset>>>& GetTranslateAnimations() const
193     {
194         return transformOffsetAnimations_;
195     }
196 
SetTransformFloatAnimation(AnimationType type,const RefPtr<Animation<float>> & transformFloatAnimation)197     void SetTransformFloatAnimation(AnimationType type, const RefPtr<Animation<float>>& transformFloatAnimation)
198     {
199         if (!transformFloatAnimation) {
200             LOGE("input transformFloatAnimation is null.");
201             return;
202         }
203         transformFloatAnimations_[type] = transformFloatAnimation;
204     }
205 
GetMaxScaleXY()206     double GetMaxScaleXY()
207     {
208         return maxScaleXY_;
209     }
210 
SetMaxScaleXY(double maxScaleXY)211     void SetMaxScaleXY(double maxScaleXY)
212     {
213         maxScaleXY_ = maxScaleXY;
214     }
215 
GetTransformFloatAnimation()216     const std::unordered_map<AnimationType, RefPtr<Animation<float>>>& GetTransformFloatAnimation() const
217     {
218         return transformFloatAnimations_;
219     }
220 
SetOpacityAnimation(const RefPtr<Animation<float>> & opacityAnimation)221     void SetOpacityAnimation(const RefPtr<Animation<float>>& opacityAnimation)
222     {
223         if (!opacityAnimation) {
224             LOGE("input opacityAnimation is null.");
225             return;
226         }
227         opacityAnimation_ = opacityAnimation;
228     }
229 
GetOpacityAnimation()230     const RefPtr<Animation<float>>& GetOpacityAnimation() const
231     {
232         return opacityAnimation_;
233     }
234 
SetColorAnimation(const RefPtr<Animation<Color>> & colorAnimation)235     void SetColorAnimation(const RefPtr<Animation<Color>>& colorAnimation)
236     {
237         if (!colorAnimation) {
238             LOGE("input colorAnimation is null.");
239             return;
240         }
241         colorAnimation_ = colorAnimation;
242     }
243 
GetColorAnimation()244     RefPtr<Animation<Color>>& GetColorAnimation()
245     {
246         return colorAnimation_;
247     }
248 
SetPropertyAnimationFloat(PropertyAnimatableType property,const RefPtr<Animation<float>> & animation)249     void SetPropertyAnimationFloat(PropertyAnimatableType property, const RefPtr<Animation<float>>& animation)
250     {
251         if (!animation) {
252             LOGE("Set float property animation failed. animation is null. property: %{public}d", property);
253             return;
254         }
255         LOGD("set float property animation. property: %{public}d", property);
256         floatAnimationMap_[property] = animation;
257     }
258 
AddTransformAnimation(const RefPtr<Animation<TransformOperation>> & transformAnimation)259     void AddTransformAnimation(const RefPtr<Animation<TransformOperation>>& transformAnimation)
260     {
261         if (transformAnimation) {
262             transformAnimations_.push_back(transformAnimation);
263         }
264     }
265 
GetTransformAnimations()266     std::list<RefPtr<Animation<TransformOperation>>>& GetTransformAnimations()
267     {
268         return transformAnimations_;
269     }
270 
GetFloatPropertyAnimation()271     PropertyAnimationFloatMap& GetFloatPropertyAnimation()
272     {
273         return floatAnimationMap_;
274     }
275 
SetTransformOriginChanged(bool change)276     void SetTransformOriginChanged(bool change)
277     {
278         changeTransformOrigin_ = change;
279     }
280 
HasTransformOriginChanged()281     bool HasTransformOriginChanged() const
282     {
283         return changeTransformOrigin_;
284     }
285 
HasTransformOffsetChanged()286     bool HasTransformOffsetChanged() const
287     {
288         return !transformOffsetAnimations_.empty();
289     }
290 
HasTransformFloatChanged()291     bool HasTransformFloatChanged() const
292     {
293         return !transformFloatAnimations_.empty();
294     }
295 
HasTransformChanged()296     bool HasTransformChanged() const
297     {
298         return !transformAnimations_.empty();
299     }
300 
HasDurationChanged()301     bool HasDurationChanged() const
302     {
303         return changeDuration_;
304     }
305 
IsValid()306     bool IsValid() const
307     {
308         return (opacityAnimation_ || colorAnimation_ || !floatAnimationMap_.empty() ||
309                 !transformFloatAnimations_.empty() || !transformOffsetAnimations_.empty() ||
310                 !transformAnimations_.empty() || !propAnimations_.empty());
311     }
312 
ClearListeners()313     void ClearListeners()
314     {
315         if (opacityAnimation_) {
316             opacityAnimation_->ClearListeners();
317         }
318         if (colorAnimation_) {
319             colorAnimation_->ClearListeners();
320         }
321         ClearListeners(floatAnimationMap_);
322         for (auto&& [type, animation] : transformOffsetAnimations_) {
323             if (animation) {
324                 animation->ClearListeners();
325             }
326         }
327         for (auto&& [type, animation] : transformFloatAnimations_) {
328             if (animation) {
329                 animation->ClearListeners();
330             }
331         }
332 
333         for (auto&& animation : transformAnimations_) {
334             if (animation) {
335                 animation->ClearListeners();
336             }
337         }
338     }
339 
SetAllowRunningAsynchronously(bool allowRunningAsynchronously)340     void SetAllowRunningAsynchronously(bool allowRunningAsynchronously)
341     {
342         allowRunningAsynchronously_ = allowRunningAsynchronously;
343     }
344 
GetAllowRunningAsynchronously()345     bool GetAllowRunningAsynchronously()
346     {
347         return allowRunningAsynchronously_;
348     }
349 
350 private:
351     template<class T, class U>
ClearListeners(const std::map<T,RefPtr<Animation<U>>> & animations)352     void ClearListeners(const std::map<T, RefPtr<Animation<U>>>& animations)
353     {
354         for (auto&& [type, animation] : animations) {
355             if (animation) {
356                 animation->ClearListeners();
357             }
358         }
359     }
360 
361 private:
362     FillMode fillMode_ = FillMode::NONE;
363     AnimationDirection direction_ = AnimationDirection::NORMAL;
364     RefPtr<Curve> curve_; // use animation's curve as default.
365     RefPtr<Animation<float>> opacityAnimation_;
366     RefPtr<Animation<Color>> colorAnimation_;
367     RefPtr<Animation<DimensionOffset>> transformOriginAnimation_;
368     PropAnimationMap propAnimations_;
369     PropertyAnimationFloatMap floatAnimationMap_;
370     std::unordered_map<AnimationType, RefPtr<Animation<DimensionOffset>>> transformOffsetAnimations_;
371     std::unordered_map<AnimationType, RefPtr<Animation<float>>> transformFloatAnimations_;
372     std::list<RefPtr<Animation<TransformOperation>>> transformAnimations_;
373     int32_t duration_ = 0;
374     int32_t delay_ = 0;
375     int32_t iteration_ = 1;
376     bool isBackground_ = true;
377     bool changeTransformOrigin_ = false;
378     bool changeDuration_ = false;
379     double maxScaleXY_ = -1.0;
380     float tempo_ = 1.0f;
381     MotionPathOption motionPathOption_;
382     bool allowRunningAsynchronously_ = false;
383 };
384 
385 } // namespace OHOS::Ace
386 
387 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_PROPERTIES_TWEEN_OPTION_H
388