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/motion_path_evaluator.h"
17
18 #include "base/utils/utils.h"
19 #ifndef NEW_SKIA
20 #include "frameworks/core/components/common/painter/flutter_svg_painter.h"
21 #else
22 #include "frameworks/core/components/common/painter/rosen_svg_painter.h"
23 #endif
24
25 namespace OHOS::Ace {
26 namespace {
27
28 const char START_X[] = "start.x";
29 const char START_Y[] = "start.y";
30 const char END_X[] = "end.x";
31 const char END_Y[] = "end.y";
32
ReplaceAll(std::string str,const std::string & from,const std::string & to)33 std::string ReplaceAll(std::string str, const std::string& from, const std::string& to)
34 {
35 size_t startPos = 0;
36 size_t fromLength = from.length();
37 size_t toLength = to.length();
38 while ((startPos = str.find(from, startPos)) != std::string::npos) {
39 str.replace(startPos, fromLength, to);
40 startPos += toLength;
41 }
42 return str;
43 }
44
Preprocess(std::string path,const Offset & start,const Offset & end)45 std::string Preprocess(std::string path, const Offset& start, const Offset& end)
46 {
47 path = ReplaceAll(path, START_X, std::to_string(start.GetX()));
48 path = ReplaceAll(path, START_Y, std::to_string(start.GetY()));
49 path = ReplaceAll(path, END_X, std::to_string(end.GetX()));
50 path = ReplaceAll(path, END_Y, std::to_string(end.GetY()));
51 return path;
52 }
53
54 } // namespace
55
MotionPathEvaluator(const MotionPathOption & option,const Offset & start,const Offset & end,PositionType type)56 MotionPathEvaluator::MotionPathEvaluator(
57 const MotionPathOption& option, const Offset& start, const Offset& end, PositionType type)
58 : motionPathOption_(option), startPoint_(start), endPoint_(end), positionType_(type)
59 {
60 auto path = Preprocess(motionPathOption_.GetPath(), startPoint_, endPoint_);
61 motionPathOption_.SetPath(path);
62 }
63
Evaluate(float fraction)64 MotionPathPosition MotionPathEvaluator::Evaluate(float fraction)
65 {
66 if (NearEqual(fraction, 1.0f)) {
67 fraction = 1.0f;
68 }
69 if (!motionPathOption_.IsValid()) {
70 return MotionPathPosition { .offset = startPoint_ * (1.0f - fraction) + endPoint_ * fraction, .rotate = 0.0f };
71 }
72 auto progress = motionPathOption_.GetBegin() * (1.0f - fraction) + motionPathOption_.GetEnd() * fraction;
73 #ifndef FLUTTER_2_5
74 MotionPathPosition position;
75 #ifndef NEW_SKIA
76 if (FlutterSvgPainter::GetMotionPathPosition(motionPathOption_.GetPath(), progress, position)) {
77 #else
78 if (RosenSvgPainter::GetMotionPathPosition(motionPathOption_.GetPath(), progress, position)) {
79 #endif
80 if (positionType_ == PositionType::PTOFFSET) {
81 position.offset += startPoint_;
82 }
83 return position;
84 }
85 #endif
86 return MotionPathPosition { .offset = Offset(), .rotate = 0.0f };
87 }
88
89 double DoubleEvaluator::Evaluate(const double& start, const double& end, float fraction)
90 {
91 if (motionPathEvaluator_) {
92 auto position = motionPathEvaluator_->Evaluate(fraction);
93
94 if (isXAxis_) {
95 return position.offset.GetX();
96 } else {
97 return position.offset.GetY();
98 }
99 }
100 return 0.0;
101 }
102
103 DimensionOffset DimensionOffsetEvaluator::Evaluate(
104 const DimensionOffset& start, const DimensionOffset& end, float fraction)
105 {
106 if (motionPathEvaluator_) {
107 auto position = motionPathEvaluator_->Evaluate(fraction);
108
109 return DimensionOffset(Dimension(position.offset.GetX()), Dimension(position.offset.GetY()));
110 }
111
112 return DimensionOffset();
113 }
114
115 float RotateEvaluator::Evaluate(const float& start, const float& end, float fraction)
116 {
117 if (motionPathEvaluator_) {
118 auto position = motionPathEvaluator_->Evaluate(fraction);
119 return position.rotate;
120 }
121 return 0.0f;
122 }
123
124 TransformOperations TransformOperationsEvaluator::Evaluate(
125 const TransformOperations& start, const TransformOperations& end, float fraction)
126 {
127 TransformOperations result;
128 if (motionPathEvaluator_) {
129 auto position = motionPathEvaluator_->Evaluate(fraction);
130 TransformOperation rotation;
131 rotation.type_ = TransformOperationType::ROTATE;
132 rotation.rotateOperation_ = RotateOperation(0.0f, 0.0f, 1.0f, position.rotate);
133 result.GetOperations().push_back(rotation);
134 }
135 return result;
136 }
137
138 } // namespace OHOS::Ace
139