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 #ifndef FOUNDATION_ACE_FRAMEWORKS_BRIDGE_COMMON_UTILS_TRANSFORM_CONVERTOR_H
17 #define FOUNDATION_ACE_FRAMEWORKS_BRIDGE_COMMON_UTILS_TRANSFORM_CONVERTOR_H
18 
19 #include <unordered_map>
20 #include <list>
21 
22 #include "base/geometry/transform_util.h"
23 #include "core/animation/animation_pub.h"
24 #include "core/animation/keyframe_animation.h"
25 #include "core/components/common/properties/tween_option.h"
26 
27 namespace OHOS::Ace {
28 
29 class TransformConvertor {
30 public:
31     static const std::vector<std::string> TransformKey;
32 
33     template<class T>
34     using TransformUnorderedMap = std::unordered_map<AnimationType, RefPtr<KeyframeAnimation<T>>>;
35 
36     template<class T>
37     using TransformList = std::list<RefPtr<KeyframeAnimation<T>>>;
38 
39     TransformConvertor() = default;
40     ~TransformConvertor() = default;
41     void Convert(const std::string& key, const std::string& value, double time);
42     void InsertIdentityKeyframe(double time);
43     void AddAnimationToTweenOption(TweenOption& option) const;
44     void ApplyCurve(const RefPtr<Curve>& curve);
45     void ClearAnimations();
46 
47 private:
48     void AddKeyframe(AnimationType type, const RefPtr<Keyframe<TransformOperation>>& keyframe);
49     void AddKeyframe(AnimationType type, double time, const TranslateOperation& translate);
50     void AddKeyframe(AnimationType type, double time, const RotateOperation& rotate);
51     void AddKeyframe(AnimationType type, double time, const SkewOperation& skew);
52     void AddKeyframe(AnimationType type, double time, const ScaleOperation& scale);
53     void AddKeyframe(AnimationType type, double time, const Matrix4& matrix);
54     void AddKeyframe(AnimationType type, double time, const PerspectiveOperation& distance);
55 
56 private:
57     TransformUnorderedMap<TransformOperation> operationMap_;
58     TransformList<TransformOperation> operationList_;
59     std::list<float> noneKeyframeTimes_;
60 
61     static const std::unordered_map<std::string, void (*)(const std::string&, const double&, TransformConvertor&)>
62         TransformConvertorMap;
63 };
64 
65 } // namespace OHOS::Ace
66 
67 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_COMMON_UTILS_TRANSFORM_CONVERTOR_H
68