1 /*
2 * Copyright (c) 2025 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 "modifier_ng/geometry/rs_transform_modifier.h"
17
18 #include "platform/common/rs_log.h"
19
20 namespace OHOS::Rosen::ModifierNG {
SetSandBox(std::optional<Vector2f> parentPosition)21 void RSTransformModifier::SetSandBox(std::optional<Vector2f> parentPosition)
22 {
23 SetterOptional<RSAnimatableProperty>(RSPropertyType::SANDBOX, parentPosition);
24 }
25
GetSandBox() const26 std::optional<Vector2f> RSTransformModifier::GetSandBox() const
27 {
28 return GetterOptional<Vector2f>(RSPropertyType::SANDBOX);
29 }
30
SetPositionZ(float positionZ)31 void RSTransformModifier::SetPositionZ(float positionZ)
32 {
33 Setter(RSPropertyType::POSITION_Z, positionZ);
34 }
35
GetPositionZ() const36 float RSTransformModifier::GetPositionZ() const
37 {
38 return Getter(RSPropertyType::POSITION_Z, 0.f);
39 }
40
SetPositionZApplicableCamera3D(bool isApplicable)41 void RSTransformModifier::SetPositionZApplicableCamera3D(bool isApplicable)
42 {
43 Setter<RSProperty>(RSPropertyType::POSITION_Z_APPLICABLE_CAMERA3D, isApplicable);
44 }
45
GetPositionZApplicableCamera3D() const46 bool RSTransformModifier::GetPositionZApplicableCamera3D() const
47 {
48 return Getter(RSPropertyType::POSITION_Z_APPLICABLE_CAMERA3D, true);
49 }
50
SetPivot(const Vector2f & pivot,bool animatable)51 void RSTransformModifier::SetPivot(const Vector2f& pivot, bool animatable)
52 {
53 if (animatable) {
54 Setter(RSPropertyType::PIVOT, pivot);
55 } else {
56 Setter<RSProperty>(RSPropertyType::PIVOT, pivot);
57 }
58 }
59
GetPivot() const60 Vector2f RSTransformModifier::GetPivot() const
61 {
62 return Getter(RSPropertyType::PIVOT, Vector2f { 0.5f, 0.5f });
63 }
64
SetPivotZ(float pivotZ)65 void RSTransformModifier::SetPivotZ(float pivotZ)
66 {
67 Setter(RSPropertyType::PIVOT_Z, pivotZ);
68 }
69
GetPivotZ() const70 float RSTransformModifier::GetPivotZ() const
71 {
72 return Getter(RSPropertyType::PIVOT_Z, 0.f);
73 }
74
SetQuaternion(const Quaternion & quaternion)75 void RSTransformModifier::SetQuaternion(const Quaternion& quaternion)
76 {
77 Setter(RSPropertyType::QUATERNION, quaternion);
78 }
79
GetQuaternion() const80 Quaternion RSTransformModifier::GetQuaternion() const
81 {
82 return Getter(RSPropertyType::QUATERNION, Quaternion());
83 }
84
SetRotation(float degree)85 void RSTransformModifier::SetRotation(float degree)
86 {
87 Setter(RSPropertyType::ROTATION, degree);
88 }
89
GetRotation() const90 float RSTransformModifier::GetRotation() const
91 {
92 return Getter(RSPropertyType::ROTATION, 0.f);
93 }
94
SetRotationX(float degree)95 void RSTransformModifier::SetRotationX(float degree)
96 {
97 Setter(RSPropertyType::ROTATION_X, degree);
98 }
99
GetRotationX() const100 float RSTransformModifier::GetRotationX() const
101 {
102 return Getter(RSPropertyType::ROTATION_X, 0.f);
103 }
104
SetRotationY(float degree)105 void RSTransformModifier::SetRotationY(float degree)
106 {
107 Setter(RSPropertyType::ROTATION_Y, degree);
108 }
109
GetRotationY() const110 float RSTransformModifier::GetRotationY() const
111 {
112 return Getter(RSPropertyType::ROTATION_Y, 0.f);
113 }
114
SetCameraDistance(float cameraDistance)115 void RSTransformModifier::SetCameraDistance(float cameraDistance)
116 {
117 Setter(RSPropertyType::CAMERA_DISTANCE, cameraDistance);
118 }
119
GetCameraDistance() const120 float RSTransformModifier::GetCameraDistance() const
121 {
122 return Getter(RSPropertyType::CAMERA_DISTANCE, 0.f);
123 }
124
SetTranslate(const Vector2f & translate)125 void RSTransformModifier::SetTranslate(const Vector2f& translate)
126 {
127 Setter(RSPropertyType::TRANSLATE, translate);
128 }
129
GetTranslate() const130 Vector2f RSTransformModifier::GetTranslate() const
131 {
132 return Getter(RSPropertyType::TRANSLATE, Vector2f());
133 }
134
SetTranslateZ(float translate)135 void RSTransformModifier::SetTranslateZ(float translate)
136 {
137 Setter(RSPropertyType::TRANSLATE_Z, translate);
138 }
139
GetTranslateZ() const140 float RSTransformModifier::GetTranslateZ() const
141 {
142 return Getter(RSPropertyType::TRANSLATE_Z, 0.f);
143 }
144
SetScale(const Vector2f & scale)145 void RSTransformModifier::SetScale(const Vector2f& scale)
146 {
147 Setter(RSPropertyType::SCALE, scale);
148 }
149
GetScale() const150 Vector2f RSTransformModifier::GetScale() const
151 {
152 return Getter(RSPropertyType::SCALE, Vector2f());
153 }
154
SetScaleZ(float scaleZ)155 void RSTransformModifier::SetScaleZ(float scaleZ)
156 {
157 Setter(RSPropertyType::SCALE_Z, scaleZ);
158 }
159
GetScaleZ() const160 float RSTransformModifier::GetScaleZ() const
161 {
162 return Getter(RSPropertyType::SCALE_Z, 1.f);
163 }
164
SetSkew(const Vector3f & skew)165 void RSTransformModifier::SetSkew(const Vector3f& skew)
166 {
167 Setter(RSPropertyType::SKEW, skew);
168 }
169
GetSkew() const170 Vector3f RSTransformModifier::GetSkew() const
171 {
172 return Getter(RSPropertyType::SKEW, Vector3f());
173 }
174
SetPersp(const Vector4f & persp)175 void RSTransformModifier::SetPersp(const Vector4f& persp)
176 {
177 Setter(RSPropertyType::PERSP, persp);
178 }
179
GetPersp() const180 Vector4f RSTransformModifier::GetPersp() const
181 {
182 return Getter(RSPropertyType::PERSP, Vector4f());
183 }
184
ApplyGeometry(const std::shared_ptr<RSObjAbsGeometry> & geometry)185 void RSTransformModifier::ApplyGeometry(const std::shared_ptr<RSObjAbsGeometry>& geometry)
186 {
187 if (geometry == nullptr) {
188 RS_LOGE("RSTransformModifier::ApplyGeometry, geometry null");
189 return;
190 }
191 if (auto pivotPtr = GetProperty(RSPropertyType::PIVOT)) {
192 auto pivot = GetterWithoutCheck<Vector2f>(pivotPtr);
193 geometry->SetPivot(pivot.x_, pivot.y_);
194 }
195 if (auto pivotZPtr = GetProperty(RSPropertyType::PIVOT_Z)) {
196 geometry->SetPivotZ(GetterWithoutCheck<float>(pivotZPtr));
197 }
198 if (auto quaternionPtr = GetProperty(RSPropertyType::QUATERNION)) {
199 geometry->SetQuaternion(GetterWithoutCheck<Quaternion>(quaternionPtr));
200 }
201 if (auto rotationPtr = GetProperty(RSPropertyType::ROTATION)) {
202 geometry->SetRotation(GetterWithoutCheck<float>(rotationPtr) + geometry->GetRotation());
203 }
204 if (auto rotationXPtr = GetProperty(RSPropertyType::ROTATION_X)) {
205 geometry->SetRotationX(GetterWithoutCheck<float>(rotationXPtr) + geometry->GetRotationX());
206 }
207 if (auto rotationYPtr = GetProperty(RSPropertyType::ROTATION_Y)) {
208 geometry->SetRotationY(GetterWithoutCheck<float>(rotationYPtr) + geometry->GetRotationY());
209 }
210 if (auto cameraDistancePtr = GetProperty(RSPropertyType::CAMERA_DISTANCE)) {
211 geometry->SetCameraDistance(GetterWithoutCheck<float>(cameraDistancePtr));
212 }
213 if (auto scalePtr = GetProperty(RSPropertyType::SCALE)) {
214 auto scale = GetterWithoutCheck<Vector2f>(scalePtr);
215 scale *= Vector2f(geometry->GetScaleX(), geometry->GetScaleY());
216 geometry->SetScale(scale.x_, scale.y_);
217 }
218 if (auto scaleZPtr = GetProperty(RSPropertyType::SCALE_Z)) {
219 geometry->SetScaleZ(GetterWithoutCheck<float>(scaleZPtr) * geometry->GetScaleZ());
220 }
221 if (auto skewPtr = GetProperty(RSPropertyType::SKEW)) {
222 auto skew = GetterWithoutCheck<Vector3f>(skewPtr);
223 skew += Vector3f(geometry->GetSkewX(), geometry->GetSkewY(), geometry->GetSkewZ());
224 geometry->SetSkew(skew.x_, skew.y_, skew.z_);
225 }
226 if (auto perspPtr = GetProperty(RSPropertyType::PERSP)) {
227 auto persp = GetterWithoutCheck<Vector4f>(perspPtr);
228 geometry->SetPersp(persp.x_, persp.y_, persp.z_, persp.w_);
229 }
230 if (auto translatePtr = GetProperty(RSPropertyType::TRANSLATE)) {
231 auto translate = GetterWithoutCheck<Vector2f>(translatePtr);
232 translate += Vector2f(geometry->GetTranslateX(), geometry->GetTranslateY());
233 geometry->SetTranslateX(translate.x_);
234 geometry->SetTranslateY(translate.y_);
235 }
236 if (auto translateZPtr = GetProperty(RSPropertyType::TRANSLATE_Z)) {
237 geometry->SetTranslateZ(GetterWithoutCheck<float>(translateZPtr) + geometry->GetTranslateZ());
238 }
239 }
240 } // namespace OHOS::Rosen::ModifierNG
241