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_CORE_ANIMATION_ANIMATION_UTIL_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_ANIMATION_UTIL_H
18 
19 #include <string>
20 
21 #include "core/animation/animatable_data.h"
22 #include "core/components/common/properties/shadow.h"
23 #include "core/components/common/properties/tween_option.h"
24 #include "core/components/declaration/common/declaration.h"
25 #include "frameworks/bridge/common/utils/transform_convertor.h"
26 
27 namespace OHOS::Ace {
28 
29 class AnimationUtil final {
30 public:
GetPropAnimationMap()31     const PropAnimationMap& GetPropAnimationMap() const
32     {
33         return propAnimationMap_;
34     }
35 
GetTransformConvertor()36     const TransformConvertor& GetTransformConvertor() const
37     {
38         return transformConvertor_;
39     }
40 
GetAnimationName()41     const std::string& GetAnimationName() const
42     {
43         return animationName_;
44     }
45 
Clear()46     void Clear()
47     {
48         transformConvertor_.ClearAnimations();
49         shadowKeyframes_.clear();
50         propAnimationMap_.clear();
51     }
52 
53     void ParseAnimationStyle(const std::vector<std::unordered_map<std::string, std::string>>& animationKeyframes,
54         const RefPtr<Declaration>& declaration, const RefPtr<ThemeConstants> themeConstants);
55 
56     static AnimationOption CreateKeyboardAnimationOption(const KeyboardAnimationConfig& config, float keyboardHeight);
57 
58 private:
59     void KeyframesAddKeyFrame(const std::string& keyStyle, const std::string& value, const std::string& timeStr);
60     template<class T>
61     void AddAnimatable(const T& value, double time, AnimatableType);
62     RefPtr<ThemeConstants> GetThemeConstants() const;
63     Color ParseColor(const std::string& value, uint32_t maskAlpha = COLOR_ALPHA_MASK) const;
64     double ParseDouble(const std::string& value) const;
65     Dimension ParseDimension(const std::string& value) const;
66 
67     template<typename T>
ParseThemeReference(const std::string & value,std::function<T ()> && noRefFunc,std::function<T (uint32_t refId)> && idRefFunc,const T & errorValue)68     T ParseThemeReference(const std::string& value, std::function<T()>&& noRefFunc,
69         std::function<T(uint32_t refId)>&& idRefFunc, const T& errorValue) const
70     {
71         const auto& parseResult = ThemeUtils::ParseThemeIdReference(value, GetThemeConstants());
72         if (!parseResult.parseSuccess) {
73             return noRefFunc();
74         }
75         auto themeConstants = GetThemeConstants();
76         if (!themeConstants) {
77             return errorValue;
78         }
79         // Refer to a theme id resource.
80         if (parseResult.isIdRef) {
81             return idRefFunc(parseResult.id);
82         }
83         // Refer to a theme attribute.
84         auto themeStyle = themeConstants->GetThemeStyle();
85         if (!themeStyle) {
86             return errorValue;
87         }
88         return themeStyle->GetAttr<T>(parseResult.refAttr, errorValue);
89     }
90 
91 private:
92     std::string animationName_;
93     PropAnimationMap propAnimationMap_;
94     TransformConvertor transformConvertor_;
95     std::map<std::string, Shadow> shadowKeyframes_;
96     bool isRightToLeft = false;
97     RefPtr<ThemeConstants> themeConstants_;
98 };
99 
100 } // namespace OHOS::Ace
101 
102 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_ANIMATION_UTIL_H
103