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 private: 57 void KeyframesAddKeyFrame(const std::string& keyStyle, const std::string& value, const std::string& timeStr); 58 template<class T> 59 void AddAnimatable(const T& value, double time, AnimatableType); 60 RefPtr<ThemeConstants> GetThemeConstants() const; 61 Color ParseColor(const std::string& value, uint32_t maskAlpha = COLOR_ALPHA_MASK) const; 62 double ParseDouble(const std::string& value) const; 63 Dimension ParseDimension(const std::string& value) const; 64 65 template<typename T> ParseThemeReference(const std::string & value,std::function<T ()> && noRefFunc,std::function<T (uint32_t refId)> && idRefFunc,const T & errorValue)66 T ParseThemeReference(const std::string& value, std::function<T()>&& noRefFunc, 67 std::function<T(uint32_t refId)>&& idRefFunc, const T& errorValue) const 68 { 69 const auto& parseResult = ThemeUtils::ParseThemeIdReference(value, GetThemeConstants()); 70 if (!parseResult.parseSuccess) { 71 return noRefFunc(); 72 } 73 auto themeConstants = GetThemeConstants(); 74 if (!themeConstants) { 75 return errorValue; 76 } 77 // Refer to a theme id resource. 78 if (parseResult.isIdRef) { 79 return idRefFunc(parseResult.id); 80 } 81 // Refer to a theme attribute. 82 auto themeStyle = themeConstants->GetThemeStyle(); 83 if (!themeStyle) { 84 return errorValue; 85 } 86 return themeStyle->GetAttr<T>(parseResult.refAttr, errorValue); 87 } 88 89 private: 90 std::string animationName_; 91 PropAnimationMap propAnimationMap_; 92 TransformConvertor transformConvertor_; 93 std::map<std::string, Shadow> shadowKeyframes_; 94 bool isRightToLeft = false; 95 RefPtr<ThemeConstants> themeConstants_; 96 }; 97 98 } // namespace OHOS::Ace 99 100 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_ANIMATION_UTIL_H 101