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/transform/transform_component.h"
17 
18 #include "core/components/transform/transform_element.h"
19 
20 namespace OHOS::Ace {
21 
CreateElement()22 RefPtr<Element> TransformComponent::CreateElement()
23 {
24     return AceType::MakeRefPtr<TransformElement>();
25 }
26 
CreateRenderNode()27 RefPtr<RenderNode> TransformComponent::CreateRenderNode()
28 {
29     return RenderTransform::Create();
30 }
31 
32 // translate
Translate(const Dimension & x,const Dimension & y,const AnimationOption & animationOption)33 void TransformComponent::Translate(const Dimension& x, const Dimension& y, const AnimationOption& animationOption)
34 {
35     Translate(x, y, Dimension(), animationOption);
36 }
37 
Translate(const Dimension & x,const Dimension & y,const Dimension & z,const AnimationOption & animationOption)38 void TransformComponent::Translate(
39     const Dimension& x, const Dimension& y, const Dimension& z, const AnimationOption& animationOption)
40 {
41     TransformOperation operation;
42     operation.type_ = TransformOperationType::TRANSLATE;
43     operation.translateOperation_ = TranslateOperation(x, y, z);
44     transformEffects.AddTransformOperation(operation, animationOption);
45 }
46 
TranslateX(const Dimension & x,const AnimationOption & animationOption)47 void TransformComponent::TranslateX(const Dimension& x, const AnimationOption& animationOption)
48 {
49     Translate(x, Dimension {}, Dimension {}, animationOption);
50 }
51 
TranslateY(const Dimension & y,const AnimationOption & animationOption)52 void TransformComponent::TranslateY(const Dimension& y, const AnimationOption& animationOption)
53 {
54     Translate(Dimension {}, y, Dimension {}, animationOption);
55 }
56 
TranslateZ(const Dimension & z,const AnimationOption & animationOption)57 void TransformComponent::TranslateZ(const Dimension& z, const AnimationOption& animationOption)
58 {
59     Translate(Dimension {}, Dimension {}, z);
60 }
61 
62 // scale
Scale(float value,const AnimationOption & animationOption)63 void TransformComponent::Scale(float value, const AnimationOption& animationOption)
64 {
65     Scale(value, value, 1.0f, animationOption);
66 }
67 
Scale(float x,float y,const AnimationOption & animationOption)68 void TransformComponent::Scale(float x, float y, const AnimationOption& animationOption)
69 {
70     Scale(x, y, 1.0f, animationOption);
71 }
72 
Scale(float x,float y,float z,const AnimationOption & animationOption)73 void TransformComponent::Scale(float x, float y, float z, const AnimationOption& animationOption)
74 {
75     TransformOperation operation;
76     operation.type_ = TransformOperationType::SCALE;
77     operation.scaleOperation_ = ScaleOperation(x, y, z);
78     transformEffects.AddTransformOperation(operation, animationOption);
79 }
80 
ScaleX(float x,const AnimationOption & animationOption)81 void TransformComponent::ScaleX(float x, const AnimationOption& animationOption)
82 {
83     Scale(x, 1.0f, 1.0f, animationOption);
84 }
85 
ScaleY(float y,const AnimationOption & animationOption)86 void TransformComponent::ScaleY(float y, const AnimationOption& animationOption)
87 {
88     Scale(1.0f, y, 1.0f, animationOption);
89 }
90 
ScaleZ(float z,const AnimationOption & animationOption)91 void TransformComponent::ScaleZ(float z, const AnimationOption& animationOption)
92 {
93     Scale(1.0f, 1.0f, z, animationOption);
94 }
95 
96 // rotate
Rotate(float dx,float dy,float dz,float angle,const AnimationOption & animationOption)97 void TransformComponent::Rotate(float dx, float dy, float dz, float angle, const AnimationOption& animationOption)
98 {
99     TransformOperation operation;
100     operation.type_ = TransformOperationType::ROTATE;
101     operation.rotateOperation_ = RotateOperation(dx, dy, dz, angle);
102     transformEffects.AddTransformOperation(operation, animationOption);
103 }
104 
RotateX(float angle,const AnimationOption & animationOption)105 void TransformComponent::RotateX(float angle, const AnimationOption& animationOption)
106 {
107     Rotate(1.0f, 0.0f, 0.0f, angle, animationOption);
108 }
109 
RotateY(float angle,const AnimationOption & animationOption)110 void TransformComponent::RotateY(float angle, const AnimationOption& animationOption)
111 {
112     Rotate(0.0f, 1.0f, 0.0f, angle, animationOption);
113 }
114 
RotateZ(float angle,const AnimationOption & animationOption)115 void TransformComponent::RotateZ(float angle, const AnimationOption& animationOption)
116 {
117     Rotate(0.0f, 0.0f, 1.0f, angle, animationOption);
118 }
119 
120 // skew
Skew(float xAngle,float yAngle,const AnimationOption & animationOption)121 void TransformComponent::Skew(float xAngle, float yAngle, const AnimationOption& animationOption)
122 {
123     TransformOperation operation;
124     operation.type_ = TransformOperationType::SKEW;
125     operation.skewOperation_ = SkewOperation(xAngle, yAngle);
126     transformEffects.AddTransformOperation(operation, animationOption);
127 }
128 
SkewX(float angle,const AnimationOption & animationOption)129 void TransformComponent::SkewX(float angle, const AnimationOption& animationOption)
130 {
131     Skew(angle, 0.0f, animationOption);
132 }
133 
SkewY(float angle,const AnimationOption & animationOption)134 void TransformComponent::SkewY(float angle, const AnimationOption& animationOption)
135 {
136     Skew(0.0f, angle, animationOption);
137 }
138 
Perspective(const Dimension & value,const AnimationOption & animationOption)139 void TransformComponent::Perspective(const Dimension& value, const AnimationOption& animationOption)
140 {
141     TransformOperation operation;
142     operation.type_ = TransformOperationType::PERSPECTIVE;
143     operation.perspectiveOperation_ = PerspectiveOperation(value);
144     transformEffects.AddTransformOperation(operation, animationOption);
145 }
146 
Matrix(float a,float b,float c,float d,float dx,float dy,const AnimationOption & animationOption)147 void TransformComponent::Matrix(
148     float a, float b, float c, float d, float dx, float dy, const AnimationOption& animationOption)
149 {
150     return Matrix3d(a, b, 0, 0, c, d, 0, 0, 0, 0, 1, 0, dx, dy, 0, 1);
151 }
152 
Matrix3d(float a1,float b1,float c1,float d1,float a2,float b2,float c2,float d2,float a3,float b3,float c3,float d3,float a4,float b4,float c4,float d4,const AnimationOption & animationOption)153 void TransformComponent::Matrix3d(float a1, float b1, float c1, float d1, float a2, float b2, float c2, float d2,
154     float a3, float b3, float c3, float d3, float a4, float b4, float c4, float d4,
155     const AnimationOption& animationOption)
156 {
157     TransformOperation operation;
158     operation.type_ = TransformOperationType::MATRIX;
159     operation.matrix4_ = Matrix4(a1, a2, a3, a4, b1, b2, b3, b4, c1, c2, c3, c4, d1, d2, d3, d4);
160     transformEffects.AddTransformOperation(operation, animationOption);
161 }
162 
Matrix3d(Matrix4 m,const AnimationOption & animationOption)163 void TransformComponent::Matrix3d(Matrix4 m, const AnimationOption& animationOption)
164 {
165     TransformOperation operation;
166     operation.type_ = TransformOperationType::MATRIX;
167     operation.matrix4_ = m;
168     transformEffects.AddTransformOperation(operation, animationOption);
169 }
170 
SetRotateTransition(TransitionType type,double x,double y,double z,double angle)171 void TransformComponent::SetRotateTransition(TransitionType type, double x, double y, double z, double angle)
172 {
173     if (type == TransitionType::ALL) {
174         SetRotateTransition(TransitionType::APPEARING, x, y, z, angle);
175         SetRotateTransition(TransitionType::DISAPPEARING, x, y, z, angle);
176         hasDisappearTransition_ = true;
177         hasAppearTransition_ = true;
178         return;
179     }
180 
181     TransformOperation operation;
182     operation.type_ = TransformOperationType::ROTATE;
183     operation.rotateOperation_ = RotateOperation(x, y, z, angle);
184     if (type == TransitionType::APPEARING) {
185         transformEffectsAppearing_.emplace_back(operation);
186         hasAppearTransition_ = true;
187     } else if (type == TransitionType::DISAPPEARING) {
188         transformEffectsDisappearing_.emplace_back(operation);
189         hasDisappearTransition_ = true;
190     }
191 }
192 
SetTranslateTransition(TransitionType type,const Dimension & x,const Dimension & y,const Dimension & z)193 void TransformComponent::SetTranslateTransition(
194     TransitionType type, const Dimension& x, const Dimension& y, const Dimension& z)
195 {
196     if (type == TransitionType::ALL) {
197         SetTranslateTransition(TransitionType::APPEARING, x, y, z);
198         SetTranslateTransition(TransitionType::DISAPPEARING, x, y, z);
199         hasDisappearTransition_ = true;
200         hasAppearTransition_ = true;
201         return;
202     }
203 
204     TransformOperation operation;
205     operation.type_ = TransformOperationType::TRANSLATE;
206     operation.translateOperation_ = TranslateOperation(x, y, z);
207     if (type == TransitionType::APPEARING) {
208         transformEffectsAppearing_.emplace_back(operation);
209         hasAppearTransition_ = true;
210     } else if (type == TransitionType::DISAPPEARING) {
211         transformEffectsDisappearing_.emplace_back(operation);
212         hasDisappearTransition_ = true;
213     }
214 }
215 
SetScaleTransition(TransitionType type,float x,float y,float z)216 void TransformComponent::SetScaleTransition(TransitionType type, float x, float y, float z)
217 {
218     if (type == TransitionType::ALL) {
219         SetScaleTransition(TransitionType::APPEARING, x, y, z);
220         SetScaleTransition(TransitionType::DISAPPEARING, x, y, z);
221         hasDisappearTransition_ = true;
222         hasAppearTransition_ = true;
223         return;
224     }
225     TransformOperation operation;
226     operation.type_ = TransformOperationType::SCALE;
227     operation.scaleOperation_ = ScaleOperation(x, y, z);
228     if (type == TransitionType::APPEARING) {
229         transformEffectsAppearing_.emplace_back(operation);
230         hasAppearTransition_ = true;
231     } else if (type == TransitionType::DISAPPEARING) {
232         transformEffectsDisappearing_.emplace_back(operation);
233         hasDisappearTransition_ = true;
234     }
235 }
236 
237 } // namespace OHOS::Ace
238