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_ANIMATION_INTERPOLATOR_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_INTERPOLATOR_H
18 
19 #include "core/animation/animation_pub.h"
20 #include "core/animation/curves.h"
21 #include "core/animation/scheduler.h"
22 #include "core/animation/time_event.h"
23 #include "core/components/common/properties/animation_option.h"
24 
25 namespace OHOS::Ace {
26 
27 class ACE_FORCE_EXPORT Interpolator : public TimeEvent {
28     DECLARE_ACE_TYPE(Interpolator, TimeEvent);
29 
30 public:
31     Interpolator() = default;
32     ~Interpolator() override = default;
33 
34     // interpolate animations must have duration, and accept normalized timestamp.
35     // so far, do not support play in sequence, always return 1.0, besides picture animation.
GetDuration()36     float GetDuration() const
37     {
38         return duration_;
39     }
40 
SetDuration(float duration)41     void SetDuration(float duration)
42     {
43         duration_ = duration;
44     }
45 
46     // Interpolator use OnNormalizedTimestampChanged instead.
OnTimestampChanged(float timestamp,float normalizedTime,bool reverse)47     void OnTimestampChanged(float timestamp, float normalizedTime, bool reverse) final
48     {
49         // just pass normalized time to subclass.
50         OnNormalizedTimestampChanged(normalizedTime, reverse);
51     }
52 
53     // when a Interpolator starts, animator will notify it through this interface.
54     virtual void OnNormalizedTimestampChanged(float normalized, bool reverse) = 0;
55 
56     virtual void OnInitNotify(float normalizedTime, bool reverse) = 0;
57 
IsSupportedRunningAsynchronously()58     virtual bool IsSupportedRunningAsynchronously()
59     {
60         return true;
61     }
62 
63     virtual bool RunAsync(const WeakPtr<Scheduler>& weakScheduler, const AnimationOption& option,
64         const std::function<void()> prepareCallback = nullptr, const std::function<void()> finishCallback = nullptr)
65     {
66         auto scheduler = weakScheduler.Upgrade();
67         if (scheduler == nullptr) {
68             LOGE("run async failed, scheduler is null.");
69             return false;
70         }
71 
72         if (prepareCallback != nullptr) {
73             prepareCallback();
74         }
75         OnNormalizedTimestampChanged(NORMALIZED_DURATION_MIN, false);
76         return scheduler->Animate(
77             option, GetCurve(),
78             [weak = AceType::WeakClaim(this), callback = prepareCallback]() -> void {
79                 auto animation = weak.Upgrade();
80                 if (animation == nullptr) {
81                     LOGE("property change failed, animation is null.");
82                     return;
83                 }
84 
85                 if (callback != nullptr) {
86                     callback();
87                 }
88 
89                 animation->OnNormalizedTimestampChanged(NORMALIZED_DURATION_MAX, false);
90             },
91             finishCallback);
92     }
93 
94 protected:
GetCurve()95     virtual RefPtr<Curve> GetCurve()
96     {
97         return Curves::EASE_IN_OUT;
98     }
99 
100     // so far, do not support play multi interpolate animations in sequence. so always set it to 1.0
101     // but do not change it to const, picture animation's duration may be not equals 1.0
102     // and this variable will change to modifiable when support play animations in sequence.
103     float duration_ { NORMALIZED_DURATION_MAX };
104 };
105 
106 } // namespace OHOS::Ace
107 
108 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_INTERPOLATOR_H
109