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         typename U::SetterMap setterMap = propertyAnimatable->GetPropertySetterMap(u);
74         typename U::GetterMap getterMap = propertyAnimatable->GetPropertyGetterMap(u);
75 
76         auto setterIter = setterMap.find(property);
77         if (setterIter == setterMap.end()) {
78             LOGW("Create property animation failed. no setter found for property: %{public}d", property);
79             return false;
80         }
81         auto& setter = setterIter->second;
82         if (!setter) {
83             LOGE("Create property animation failed. setter is null for property: %{public}d", property);
84             return false;
85         }
86         auto getterIter = getterMap.find(property);
87         if (getterIter == getterMap.end()) {
88             LOGW("Create property animation failed. no getter found for property: %{public}d", property);
89             return false;
90         }
91         auto& getter = getterIter->second;
92         if (!getter) {
93             LOGE("Create property animation failed. getter is null for property: %{public}d", property);
94             return false;
95         }
96         animation->AddListener(setter);
97         initValue = getter();
98         return true;
99     }
100 
101 private:
GetPropertySetterMap(FloatPropertyAnimatable &)102     const FloatPropertyAnimatable::SetterMap GetPropertySetterMap(FloatPropertyAnimatable&)
103     {
104         return GetFloatPropertySetterMap();
105     };
GetPropertyGetterMap(FloatPropertyAnimatable &)106     const FloatPropertyAnimatable::GetterMap GetPropertyGetterMap(FloatPropertyAnimatable&)
107     {
108         return GetFloatPropertyGetterMap();
109     };
GetPropertySetterMap(ColorPropertyAnimatable &)110     const ColorPropertyAnimatable::SetterMap GetPropertySetterMap(ColorPropertyAnimatable&)
111     {
112         return GetColorPropertySetterMap();
113     };
GetPropertyGetterMap(ColorPropertyAnimatable &)114     const ColorPropertyAnimatable::GetterMap GetPropertyGetterMap(ColorPropertyAnimatable&)
115     {
116         return GetColorPropertyGetterMap();
117     };
GetPropertySetterMap(BackgroundPositionPropertyAnimatable &)118     const BackgroundPositionPropertyAnimatable::SetterMap GetPropertySetterMap(BackgroundPositionPropertyAnimatable&)
119     {
120         return GetBackgroundPositionPropertySetterMap();
121     };
GetPropertyGetterMap(BackgroundPositionPropertyAnimatable &)122     const BackgroundPositionPropertyAnimatable::GetterMap GetPropertyGetterMap(BackgroundPositionPropertyAnimatable&)
123     {
124         return GetBackgroundPositionPropertyGetterMap();
125     };
GetFloatPropertySetterMap()126     virtual FloatPropertyAnimatable::SetterMap GetFloatPropertySetterMap()
127     {
128         return FloatPropertyAnimatable::SetterMap();
129     };
GetFloatPropertyGetterMap()130     virtual FloatPropertyAnimatable::GetterMap GetFloatPropertyGetterMap()
131     {
132         return FloatPropertyAnimatable::GetterMap();
133     };
GetColorPropertySetterMap()134     virtual ColorPropertyAnimatable::SetterMap GetColorPropertySetterMap()
135     {
136         return ColorPropertyAnimatable::SetterMap();
137     };
GetColorPropertyGetterMap()138     virtual ColorPropertyAnimatable::GetterMap GetColorPropertyGetterMap()
139     {
140         return ColorPropertyAnimatable::GetterMap();
141     };
GetBackgroundPositionPropertySetterMap()142     virtual BackgroundPositionPropertyAnimatable::SetterMap GetBackgroundPositionPropertySetterMap()
143     {
144         return BackgroundPositionPropertyAnimatable::SetterMap();
145     };
GetBackgroundPositionPropertyGetterMap()146     virtual BackgroundPositionPropertyAnimatable::GetterMap GetBackgroundPositionPropertyGetterMap()
147     {
148         return BackgroundPositionPropertyAnimatable::GetterMap();
149     };
150 };
151 
152 } // namespace OHOS::Ace
153 
154 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_PROPERTY_ANIMATABLE_H
155