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_PROPERTY_ANIMATABLE_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_PROPERTY_ANIMATABLE_H 18 19 #include <map> 20 21 #include "base/memory/ace_type.h" 22 #include "core/animation/animation.h" 23 #include "core/components/common/properties/color.h" 24 #include "core/components/common/properties/decoration.h" 25 26 namespace OHOS::Ace { 27 28 enum class PropertyAnimatableType { 29 PROPERTY_WIDTH, 30 PROPERTY_HEIGHT, 31 PROPERTY_BACK_DECORATION_COLOR, 32 PROPERTY_FRONT_DECORATION_COLOR, 33 PROPERTY_BACKGROUND_POSITION, 34 PROPERTY_OFFSET_X, 35 PROPERTY_OFFSET_Y, 36 PROPERTY_BORDER_RADIUS, 37 }; 38 39 template<class T> 40 class TypePropertyAnimatable { 41 public: 42 using Setter = std::function<void(T value)>; 43 using Getter = std::function<T(void)>; 44 using SetterMap = std::map<PropertyAnimatableType, Setter>; 45 using GetterMap = std::map<PropertyAnimatableType, Getter>; 46 using Type = T; 47 }; 48 49 using FloatPropertyAnimatable = TypePropertyAnimatable<float>; 50 using ColorPropertyAnimatable = TypePropertyAnimatable<Color>; 51 using BackgroundPositionPropertyAnimatable = TypePropertyAnimatable<BackgroundImagePosition>; 52 53 using PropertyAnimationFloat = RefPtr<Animation<float>>; 54 using PropertyAnimationFloatMap = std::map<PropertyAnimatableType, PropertyAnimationFloat>; 55 56 class PropertyAnimatable : public virtual AceType { 57 DECLARE_ACE_TYPE(PropertyAnimatable, AceType); 58 59 public: 60 template<class U, class V> AddPropertyAnimation(const RefPtr<PropertyAnimatable> & propertyAnimatable,PropertyAnimatableType property,RefPtr<Animation<V>> & animation,typename U::Type & initValue)61 static bool AddPropertyAnimation(const RefPtr<PropertyAnimatable>& propertyAnimatable, 62 PropertyAnimatableType property, RefPtr<Animation<V>>& animation, typename U::Type& initValue) 63 { 64 U u {}; 65 if (!propertyAnimatable) { 66 LOGE("Create property animation failed. animatable is null."); 67 return false; 68 } 69 if (!animation) { 70 LOGE("Create property animation failed. animation is null."); 71 return false; 72 } 73 LOGD("Create Property Animation. property: %{public}d.", property); 74 typename U::SetterMap setterMap = propertyAnimatable->GetPropertySetterMap(u); 75 typename U::GetterMap getterMap = propertyAnimatable->GetPropertyGetterMap(u); 76 77 auto setterIter = setterMap.find(property); 78 if (setterIter == setterMap.end()) { 79 LOGW("Create property animation failed. no setter found for property: %{public}d", property); 80 return false; 81 } 82 auto& setter = setterIter->second; 83 if (!setter) { 84 LOGE("Create property animation failed. setter is null for property: %{public}d", property); 85 return false; 86 } 87 auto getterIter = getterMap.find(property); 88 if (getterIter == getterMap.end()) { 89 LOGW("Create property animation failed. no getter found for property: %{public}d", property); 90 return false; 91 } 92 auto& getter = getterIter->second; 93 if (!getter) { 94 LOGE("Create property animation failed. getter is null for property: %{public}d", property); 95 return false; 96 } 97 animation->AddListener(setter); 98 initValue = getter(); 99 return true; 100 } 101 102 private: GetPropertySetterMap(FloatPropertyAnimatable &)103 const FloatPropertyAnimatable::SetterMap GetPropertySetterMap(FloatPropertyAnimatable&) 104 { 105 return GetFloatPropertySetterMap(); 106 }; GetPropertyGetterMap(FloatPropertyAnimatable &)107 const FloatPropertyAnimatable::GetterMap GetPropertyGetterMap(FloatPropertyAnimatable&) 108 { 109 return GetFloatPropertyGetterMap(); 110 }; GetPropertySetterMap(ColorPropertyAnimatable &)111 const ColorPropertyAnimatable::SetterMap GetPropertySetterMap(ColorPropertyAnimatable&) 112 { 113 return GetColorPropertySetterMap(); 114 }; GetPropertyGetterMap(ColorPropertyAnimatable &)115 const ColorPropertyAnimatable::GetterMap GetPropertyGetterMap(ColorPropertyAnimatable&) 116 { 117 return GetColorPropertyGetterMap(); 118 }; GetPropertySetterMap(BackgroundPositionPropertyAnimatable &)119 const BackgroundPositionPropertyAnimatable::SetterMap GetPropertySetterMap(BackgroundPositionPropertyAnimatable&) 120 { 121 return GetBackgroundPositionPropertySetterMap(); 122 }; GetPropertyGetterMap(BackgroundPositionPropertyAnimatable &)123 const BackgroundPositionPropertyAnimatable::GetterMap GetPropertyGetterMap(BackgroundPositionPropertyAnimatable&) 124 { 125 return GetBackgroundPositionPropertyGetterMap(); 126 }; GetFloatPropertySetterMap()127 virtual FloatPropertyAnimatable::SetterMap GetFloatPropertySetterMap() 128 { 129 return FloatPropertyAnimatable::SetterMap(); 130 }; GetFloatPropertyGetterMap()131 virtual FloatPropertyAnimatable::GetterMap GetFloatPropertyGetterMap() 132 { 133 return FloatPropertyAnimatable::GetterMap(); 134 }; GetColorPropertySetterMap()135 virtual ColorPropertyAnimatable::SetterMap GetColorPropertySetterMap() 136 { 137 return ColorPropertyAnimatable::SetterMap(); 138 }; GetColorPropertyGetterMap()139 virtual ColorPropertyAnimatable::GetterMap GetColorPropertyGetterMap() 140 { 141 return ColorPropertyAnimatable::GetterMap(); 142 }; GetBackgroundPositionPropertySetterMap()143 virtual BackgroundPositionPropertyAnimatable::SetterMap GetBackgroundPositionPropertySetterMap() 144 { 145 return BackgroundPositionPropertyAnimatable::SetterMap(); 146 }; GetBackgroundPositionPropertyGetterMap()147 virtual BackgroundPositionPropertyAnimatable::GetterMap GetBackgroundPositionPropertyGetterMap() 148 { 149 return BackgroundPositionPropertyAnimatable::GetterMap(); 150 }; 151 }; 152 153 } // namespace OHOS::Ace 154 155 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_PROPERTY_ANIMATABLE_H 156