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 #include "core/components/common/properties/animatable_path.h"
17 
18 #include "include/core/SkString.h"
19 #include "include/utils/SkParsePath.h"
20 
21 #include "core/event/ace_event_helper.h"
22 
23 namespace OHOS::Ace {
24 
operator =(const AnimatablePath & newValue)25 AnimatablePath& AnimatablePath::operator=(const AnimatablePath& newValue)
26 {
27     SetAnimationOption(newValue.GetAnimationOption());
28     auto context = context_.Upgrade();
29     if (!context || !animationCallback_) {
30         SetValue(newValue.GetValue());
31         return *this;
32     }
33     AnimationOption explicitAnim = context->GetExplicitAnimationOption();
34     if (explicitAnim.IsValid()) {
35         SetAnimationOption(explicitAnim);
36         AnimateTo(newValue.GetValue());
37     } else if (animationOption_.IsValid()) {
38         AnimateTo(newValue.GetValue());
39     } else {
40         SetValue(newValue.GetValue());
41     }
42     isFirstAssign_ = false;
43     return *this;
44 }
45 
AnimateTo(std::string endValue)46 void AnimatablePath::AnimateTo(std::string endValue)
47 {
48     if (isFirstAssign_) {
49         isFirstAssign_ = false;
50         SetValue(endValue);
51         return;
52     }
53     if (path_ == endValue) {
54         return;
55     }
56     pathFrom_ = FormatPathString(path_);
57     pathTo_ = FormatPathString(endValue);
58     if (pathFrom_ == pathTo_) {
59         return;
60     }
61 
62     ResetController();
63 
64     SkPath skPathFrom;
65     SkPath skPathTo;
66     SkParsePath::FromSVGString(pathFrom_.c_str(), &skPathFrom);
67     SkParsePath::FromSVGString(pathTo_.c_str(), &skPathTo);
68     if (!skPathTo.isInterpolatable(skPathFrom)) {
69         isFirstAssign_ = false;
70         SetValue(endValue);
71         return;
72     }
73 
74     if (!animationController_) {
75         animationController_ = CREATE_ANIMATOR(context_);
76     }
77     RefPtr<CurveAnimation<double>> animation =
78         AceType::MakeRefPtr<CurveAnimation<double>>(0.0, 1.0, animationOption_.GetCurve());
79     animation->AddListener(std::bind(&AnimatablePath::OnAnimationCallback, this, std::placeholders::_1));
80 
81     animationController_->AddInterpolator(animation);
82     auto onFinishEvent = animationOption_.GetOnFinishEvent();
83     if (onFinishEvent) {
84         animationController_->AddStopListener([onFinishEvent, weakContext = context_] {
85             auto context = weakContext.Upgrade();
86             if (context) {
87                 context->PostAsyncEvent(onFinishEvent, "ArkUIAnimatablePathFinishEvent");
88             } else {
89                 LOGE("the context is null");
90             }
91         });
92     }
93     animationController_->SetDuration(animationOption_.GetDuration());
94     animationController_->SetStartDelay(animationOption_.GetDelay());
95     animationController_->SetIteration(animationOption_.GetIteration());
96     animationController_->SetTempo(animationOption_.GetTempo());
97     animationController_->SetAnimationDirection(animationOption_.GetAnimationDirection());
98     animationController_->Play();
99 }
100 
ResetController()101 void AnimatablePath::ResetController()
102 {
103     if (animationController_) {
104         if (!animationController_->IsStopped()) {
105             animationController_->Stop();
106         }
107         animationController_->ClearInterpolators();
108         animationController_->ClearAllListeners();
109         animationController_.Reset();
110     }
111 }
112 
OnAnimationCallback(double value)113 void AnimatablePath::OnAnimationCallback(double value)
114 {
115     SkPath skPathFrom;
116     SkPath skPathTo;
117     SkParsePath::FromSVGString(pathFrom_.c_str(), &skPathFrom);
118     SkParsePath::FromSVGString(pathTo_.c_str(), &skPathTo);
119 
120     SkPath out;
121     SkString outString;
122     if (skPathTo.interpolate(skPathFrom, value, &out)) {
123         SkParsePath::ToSVGString(out, &outString);
124         SetValue(outString.c_str());
125     } else {
126         SetValue(pathTo_);
127     }
128 
129     if (animationCallback_) {
130         animationCallback_();
131     }
132 }
133 
134 // Format path string before judging whether interpolation can be done
135 // e.g. 'M0 20 L50 50 L50 100 Z' --> 'M0 20L50 50L50 100L0 20Z'
FormatPathString(const std::string & path)136 std::string AnimatablePath::FormatPathString(const std::string& path)
137 {
138     SkPath skPath;
139     SkString outString;
140     SkParsePath::FromSVGString(path.c_str(), &skPath);
141     SkParsePath::ToSVGString(skPath, &outString);
142     return outString.c_str();
143 }
144 
145 }
146