• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_COMPONENTS_NG_PROPERTIES_TRANSITION_PROPERTY_H
17 #define FOUNDATION_ACE_FRAMEWORKS_COMPONENTS_NG_PROPERTIES_TRANSITION_PROPERTY_H
18 
19 #include <iomanip>
20 #include <ios>
21 #include <optional>
22 #include <sstream>
23 #include <string>
24 
25 #include "base/geometry/calc_dimension.h"
26 #include "core/animation/animation_pub.h"
27 #include "core/components/common/properties/animation_option.h"
28 #include "core/components_ng/property/property.h"
29 
30 
31 namespace OHOS::Ace::NG {
32 
33 struct TranslateOptions {
34     CalcDimension x;
35     CalcDimension y;
36     CalcDimension z;
37     TranslateOptions() = default;
TranslateOptionsTranslateOptions38     TranslateOptions(const CalcDimension& x, const CalcDimension& y, const CalcDimension& z) : x(x), y(y), z(z) {}
39     // for inner construct, default unit is PX
TranslateOptionsTranslateOptions40     TranslateOptions(float x, float y, float z) : x(x), y(y), z(z) {}
41     bool operator==(const TranslateOptions& other) const
42     {
43         return x == other.x && y == other.y && z == other.z;
44     }
ToStringTranslateOptions45     std::string ToString() const
46     {
47         return "translate:[" + x.ToString() + ", " + y.ToString() + ", " + z.ToString() + "]";
48     }
49 };
50 struct ScaleOptions {
51     float xScale = 1.0f;
52     float yScale = 1.0f;
53     float zScale = 1.0f;
54     CalcDimension centerX;
55     CalcDimension centerY;
ScaleOptionsScaleOptions56     ScaleOptions(float xScale, float yScale, float zScale, const CalcDimension& centerX, const CalcDimension& centerY)
57         : xScale(xScale), yScale(yScale), zScale(zScale), centerX(centerX), centerY(centerY)
58     {}
59     ScaleOptions() = default;
60     bool operator==(const ScaleOptions& other) const
61     {
62         return NearEqual(xScale, other.xScale) && NearEqual(yScale, other.yScale) && NearEqual(zScale, other.zScale) &&
63                NearEqual(centerX, other.centerX) && NearEqual(centerY, other.centerY);
64     }
ToStringScaleOptions65     std::string ToString() const
66     {
67         return "scale:[" + std::to_string(xScale) + "," + std::to_string(yScale) + "," + std::to_string(zScale) + "," +
68                centerX.ToString() + "," + centerY.ToString() + "]";
69     }
70 };
71 struct RotateOptions {
72     float xDirection = 0.0f;
73     float yDirection = 0.0f;
74     float zDirection = 0.0f;
75     // angle in degree unit
76     float angle = 0.0f;
77     CalcDimension centerX;
78     CalcDimension centerY;
79     CalcDimension centerZ;
80     // camera distance value
81     float perspective = 0.0f;
82 
83     RotateOptions(float xDirection, float yDirection, float zDirection, float angle, const CalcDimension& centerX,
84         const CalcDimension& centerY, const CalcDimension& centerZ = CalcDimension(0.0f, DimensionUnit::VP),
xDirectionRotateOptions85         const float perspective = 0.0f) : xDirection(xDirection), yDirection(yDirection), zDirection(zDirection),
86         angle(angle), centerX(centerX), centerY(centerY), centerZ(centerZ), perspective(perspective) {}
87     RotateOptions() = default;
88     bool operator==(const RotateOptions& other) const
89     {
90         return NearEqual(angle, other.angle) && NearEqual(xDirection, other.xDirection) &&
91                NearEqual(yDirection, other.yDirection) && NearEqual(zDirection, other.zDirection) &&
92                NearEqual(centerX, other.centerX) && NearEqual(centerY, other.centerY) &&
93                NearEqual(centerZ, other.centerZ) && NearEqual(perspective, other.perspective);
94     }
ToStringRotateOptions95     std::string ToString() const
96     {
97         return "rotate:[" + std::to_string(xDirection) + "," + std::to_string(yDirection) + "," +
98                std::to_string(zDirection) + "," + centerX.ToString() + "," + centerY.ToString() +
99                "," + centerZ.ToString() + ", angle:" + std::to_string(angle) + ", perspective:" +
100                std::to_string(perspective) + "]";
101     }
102 };
103 struct TransitionOptions {
104     TransitionType Type = TransitionType::ALL;
105     ACE_DEFINE_PROPERTY_GROUP_ITEM(Opacity, float);
106     ACE_DEFINE_PROPERTY_GROUP_ITEM(Translate, TranslateOptions);
107     ACE_DEFINE_PROPERTY_GROUP_ITEM(Scale, ScaleOptions);
108     ACE_DEFINE_PROPERTY_GROUP_ITEM(Rotate, RotateOptions);
GetDefaultTransitionTransitionOptions109     static TransitionOptions GetDefaultTransition(TransitionType type)
110     {
111         TransitionOptions options;
112         options.Type = type;
113         options.UpdateOpacity(0.0f);
114         return options;
115     }
116     bool operator==(const TransitionOptions& other) const
117     {
118         return NearEqual(Type, other.Type) && NearEqual(propOpacity, other.propOpacity) &&
119                NearEqual(propTranslate, other.propTranslate) && NearEqual(propScale, other.propScale) &&
120                NearEqual(propRotate, other.propRotate);
121     }
ToStringTransitionOptions122     std::string ToString() const
123     {
124         std::stringstream ss;
125         ss << "type:"
126            << (Type == TransitionType::ALL ? "all" : (Type == TransitionType::APPEARING ? "appear" : "disappear"))
127            << ", opacity:" << (HasOpacity() ? std::to_string(GetOpacityValue()) : "none") << ", "
128            << (HasTranslate() ? GetTranslate()->ToString() : "translate: none") << ", "
129            << (HasScale() ? GetScale()->ToString() : "scale: none") << ", "
130            << (HasRotate() ? GetRotate()->ToString() : "rotate: none");
131         return ss.str();
132     }
133 };
134 
135 enum class ChainedTransitionEffectType {
136     IDENTITY = 0,
137     OPACITY,
138     SLIDE_SWITCH,
139     MOVE,
140     TRANSLATE,
141     ROTATE,
142     SCALE,
143     ASYMMETRIC,
144 };
145 
146 enum class TransitionEdge {
147     TOP = 0,
148     BOTTOM,
149     START,
150     END,
151 };
152 
153 class ChainedTransitionEffect : public AceType {
154     DECLARE_ACE_TYPE(ChainedTransitionEffect, AceType);
155 
156 public:
ChainedTransitionEffect(ChainedTransitionEffectType type)157     explicit ChainedTransitionEffect(ChainedTransitionEffectType type) : type_(type) {}
158 
GetType()159     ChainedTransitionEffectType GetType() const
160     {
161         return type_;
162     }
GetNext()163     const RefPtr<ChainedTransitionEffect>& GetNext() const
164     {
165         return next_;
166     }
GetAnimationOption()167     const std::shared_ptr<AnimationOption>& GetAnimationOption() const
168     {
169         return animationOption_;
170     }
SetNext(const RefPtr<ChainedTransitionEffect> & next)171     void SetNext(const RefPtr<ChainedTransitionEffect>& next)
172     {
173         next_ = next;
174     }
SetAnimationOption(const std::shared_ptr<AnimationOption> & option)175     void SetAnimationOption(const std::shared_ptr<AnimationOption>& option)
176     {
177         animationOption_ = option;
178     }
179     virtual std::string ToString() = 0;
180 
181 protected:
AnimationOptionToString()182     std::string AnimationOptionToString() const
183     {
184         if (animationOption_) {
185             return "{duration:" + std::to_string(animationOption_->GetDuration()) +
186                    ", delay:" + std::to_string(animationOption_->GetDelay()) + ", curve:" +
187                    (animationOption_->GetCurve() ? animationOption_->GetCurve()->ToString() : std::string("null")) +
188                    "}";
189         }
190         return "null";
191     }
192     ChainedTransitionEffectType type_;
193     std::shared_ptr<AnimationOption> animationOption_;
194     RefPtr<ChainedTransitionEffect> next_;
195 };
196 
197 class ChainedTranslateEffect final : public ChainedTransitionEffect {
198     DECLARE_ACE_TYPE(ChainedTranslateEffect, ChainedTransitionEffect);
199 
200 public:
ChainedTranslateEffect(const TranslateOptions & option)201     explicit ChainedTranslateEffect(const TranslateOptions& option)
202         : ChainedTransitionEffect(ChainedTransitionEffectType::TRANSLATE), effect_(option)
203     {}
204 
GetEffect()205     const TranslateOptions& GetEffect() const
206     {
207         return effect_;
208     }
ToString()209     std::string ToString() override
210     {
211         std::string ans = "{type: translate";
212         ans += ", effect: " + effect_.ToString();
213         ans += ", animation: " + AnimationOptionToString();
214         ans += ", successor: " + (next_ ? next_->ToString() : std::string("null")) + "}";
215         return ans;
216     }
217 
218 private:
219     TranslateOptions effect_;
220 };
221 
222 class ChainedRotateEffect final : public ChainedTransitionEffect {
223     DECLARE_ACE_TYPE(ChainedRotateEffect, ChainedTransitionEffect);
224 
225 public:
ChainedRotateEffect(const RotateOptions & option)226     explicit ChainedRotateEffect(const RotateOptions& option)
227         : ChainedTransitionEffect(ChainedTransitionEffectType::ROTATE), effect_(option)
228     {}
229 
GetEffect()230     const RotateOptions& GetEffect() const
231     {
232         return effect_;
233     }
ToString()234     std::string ToString() override
235     {
236         std::string ans = "{type: rotate";
237         ans += ", effect: " + effect_.ToString();
238         ans += ", animation: " + AnimationOptionToString();
239         ans += ", successor: " + (next_ ? next_->ToString() : std::string("null")) + "}";
240         return ans;
241     }
242 
243 private:
244     RotateOptions effect_;
245 };
246 
247 class ChainedScaleEffect final : public ChainedTransitionEffect {
248     DECLARE_ACE_TYPE(ChainedScaleEffect, ChainedTransitionEffect);
249 
250 public:
ChainedScaleEffect(const ScaleOptions & option)251     explicit ChainedScaleEffect(const ScaleOptions& option)
252         : ChainedTransitionEffect(ChainedTransitionEffectType::SCALE), effect_(option)
253     {}
254 
GetEffect()255     const ScaleOptions& GetEffect() const
256     {
257         return effect_;
258     }
ToString()259     std::string ToString() override
260     {
261         std::string ans = "{type: scale";
262         ans += ", effect: " + effect_.ToString();
263         ans += ", animation: " + AnimationOptionToString();
264         ans += ", successor: " + (next_ ? next_->ToString() : std::string("null")) + "}";
265         return ans;
266     }
267 
268 private:
269     ScaleOptions effect_;
270 };
271 
272 class ChainedOpacityEffect final : public ChainedTransitionEffect {
273     DECLARE_ACE_TYPE(ChainedOpacityEffect, ChainedTransitionEffect);
274 
275 public:
ChainedOpacityEffect(float opacity)276     explicit ChainedOpacityEffect(float opacity)
277         : ChainedTransitionEffect(ChainedTransitionEffectType::OPACITY), opacity_(opacity)
278     {}
279 
GetEffect()280     const float& GetEffect() const
281     {
282         return opacity_;
283     }
ToString()284     std::string ToString() override
285     {
286         std::string ans = "{type: opacity";
287         ans += ", effect: " + std::to_string(opacity_);
288         ans += ", animation: " + AnimationOptionToString();
289         ans += ", successor: " + (next_ ? next_->ToString() : std::string("null")) + "}";
290         return ans;
291     }
292 
293 private:
294     float opacity_;
295 };
296 
297 class ChainedMoveEffect final : public ChainedTransitionEffect {
298     DECLARE_ACE_TYPE(ChainedMoveEffect, ChainedTransitionEffect);
299 
300 public:
ChainedMoveEffect(TransitionEdge edge)301     explicit ChainedMoveEffect(TransitionEdge edge)
302         : ChainedTransitionEffect(ChainedTransitionEffectType::MOVE), edge_(edge)
303     {}
304 
GetEffect()305     const TransitionEdge& GetEffect() const
306     {
307         return edge_;
308     }
ToString()309     std::string ToString() override
310     {
311         const static std::string edgeName[] = { "top", "bottom", "start", "end" };
312         std::string ans = "{type: move";
313         ans += ", effect: " + edgeName[static_cast<int>(edge_)];
314         ans += ", animation: " + AnimationOptionToString();
315         ans += ", successor: " + (next_ ? next_->ToString() : std::string("null")) + "}";
316         return ans;
317     }
318 
319 private:
320     TransitionEdge edge_;
321 };
322 
323 class ChainedSlideSwitchEffect final : public ChainedTransitionEffect {
324     DECLARE_ACE_TYPE(ChainedSlideSwitchEffect, ChainedTransitionEffect);
325 
326 public:
ChainedSlideSwitchEffect()327     explicit ChainedSlideSwitchEffect() : ChainedTransitionEffect(ChainedTransitionEffectType::SLIDE_SWITCH) {}
ToString()328     std::string ToString() override
329     {
330         std::string ans = "{type: slideSwitch";
331         ans += ", animation: " + AnimationOptionToString();
332         ans += ", successor: " + (next_ ? next_->ToString() : std::string("null")) + "}";
333         return ans;
334     }
335 };
336 
337 class ChainedIdentityEffect final : public ChainedTransitionEffect {
338     DECLARE_ACE_TYPE(ChainedIdentityEffect, ChainedTransitionEffect);
339 
340 public:
ChainedIdentityEffect()341     explicit ChainedIdentityEffect() : ChainedTransitionEffect(ChainedTransitionEffectType::IDENTITY) {}
ToString()342     std::string ToString() override
343     {
344         std::string ans = "{type: identity";
345         ans += ", animation: " + AnimationOptionToString();
346         ans += ", successor: " + (next_ ? next_->ToString() : std::string("null")) + "}";
347         return ans;
348     }
349 };
350 
351 class ChainedAsymmetricEffect final : public ChainedTransitionEffect {
352     DECLARE_ACE_TYPE(ChainedAsymmetricEffect, ChainedTransitionEffect);
353 
354 public:
ChainedAsymmetricEffect(const RefPtr<ChainedTransitionEffect> & appear,const RefPtr<ChainedTransitionEffect> & disappear)355     explicit ChainedAsymmetricEffect(
356         const RefPtr<ChainedTransitionEffect>& appear, const RefPtr<ChainedTransitionEffect>& disappear)
357         : ChainedTransitionEffect(ChainedTransitionEffectType::ASYMMETRIC), appearEffect_(appear),
358           disappearEffect_(disappear)
359     {}
360 
GetAppearEffect()361     const RefPtr<ChainedTransitionEffect>& GetAppearEffect() const
362     {
363         return appearEffect_;
364     }
GetDisappearEffect()365     const RefPtr<ChainedTransitionEffect>& GetDisappearEffect() const
366     {
367         return disappearEffect_;
368     }
ToString()369     std::string ToString() override
370     {
371         std::string ans = "{type: asymmetric";
372         ans += ", effect: {appear: " + (appearEffect_ ? appearEffect_->ToString() : "null") +
373                ", disappear: " + (disappearEffect_ ? disappearEffect_->ToString() : "null") + "}";
374         ans += ", animation: " + AnimationOptionToString();
375         ans += ", successor: " + (next_ ? next_->ToString() : std::string("null")) + "}";
376         return ans;
377     }
378 
379 private:
380     RefPtr<ChainedTransitionEffect> appearEffect_;
381     RefPtr<ChainedTransitionEffect> disappearEffect_;
382 };
383 } // namespace OHOS::Ace::NG
384 #endif // FOUNDATION_ACE_FRAMEWORKS_COMPONENTS_NG_PROPERTIES_TRANSITION_PROPERTY_H
385