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