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_COMPONENTS_TWEEN_TWEEN_ELEMENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TWEEN_TWEEN_ELEMENT_H
18 
19 #include "base/utils/linear_map.h"
20 #include "base/utils/utils.h"
21 #include "core/animation/animation.h"
22 #include "core/animation/animator.h"
23 #include "core/animation/property_animatable.h"
24 #include "core/components/common/layout/position_param.h"
25 #include "core/components/common/properties/tween_option.h"
26 #include "core/components/display/render_display.h"
27 #include "core/components/transform/render_transform.h"
28 #include "core/components/transform/transform_component.h"
29 #include "core/pipeline/base/composed_element.h"
30 
31 namespace OHOS::Ace {
32 
33 class TweenElement : public ComposedElement, public FlushEvent {
34     DECLARE_ACE_TYPE(TweenElement, ComposedElement, FlushEvent);
35 
36 public:
TweenElement(const ComposeId & id)37     explicit TweenElement(const ComposeId& id) : ComposedElement(id) {}
38     ~TweenElement() override;
39 
40     void PerformBuild() override;
41     void Update() override;
42     bool CanUpdate(const RefPtr<Component>& newComponent) override;
43     void OnPostFlush() override;
44     void OnPostAnimationFlush() override;
45     void OnPreFlush() override;
46     bool ApplyKeyframes();
47     void ApplyOptions();
48     const TweenOption& GetOption() const;
49     void SetOption(const TweenOption& option);
50     void SetWrapHidden(bool hidden);
51     void AddPreFlush();
52     void SkipPostFlush();
53     void SetOpacity(uint8_t opacity);
54     void SetController(const RefPtr<Animator>& controller);
55     RefPtr<RenderNode> GetContentRender() const;
56     RefPtr<Element> GetContentElement() const;
57     RefPtr<Element> GetContentParent() const;
58     const RefPtr<Animator>& GetController() const;
59     void SetTouchable(bool enable);
60 
Dump()61     void Dump() override {}
62 
63 protected:
64     RefPtr<Component> BuildChild() override;
65 
66 private:
67     bool ApplyKeyframes(RefPtr<Animator>& controller, TweenOption& option, BaseId::IdType& prepareId);
68     void ApplyOptions(RefPtr<Animator>& controller, TweenOption& option);
69     bool IsNeedAnimation(RefPtr<Animator>& controller, TweenOption& option);
70     void AddPrepareListener(RefPtr<Animator>& controller, const WeakPtr<RenderTransform>& weakTransform,
71         BaseId::IdType& prepareId);
72     void CreateTranslateAnimation(const RefPtr<RenderTransform>& renderTransformNode, TweenOption& option);
73     void CreateScaleAnimation(const RefPtr<RenderTransform>& renderTransformNode, TweenOption& option);
74     void CreateRotateAnimation(const RefPtr<RenderTransform>& renderTransformNode, TweenOption& option);
75     void CreateTransformOriginAnimation(const RefPtr<RenderTransform>& renderTransformNode, TweenOption& option);
76     void CreateColorAnimation(const RefPtr<PropertyAnimatable>& animatable, TweenOption& option);
77     void CreatePropertyAnimationFloat(const RefPtr<PropertyAnimatable>& animatable, TweenOption& option);
78     void ApplyOperation(RefPtr<Animator>& controller, AnimationOperation& operation);
79     template<class U, class V>
80     static bool CreatePropertyAnimation(const RefPtr<PropertyAnimatable>& propertyAnimatable,
81         PropertyAnimatableType propertyType, const TweenOption& option, RefPtr<Animation<V>>& animation);
82     template<class U>
83     static bool AddToAnimator(
84         const std::map<PropertyAnimatableType, U>& animations, RefPtr<Animator>& controller, TweenOption& option);
85 
86     bool isDelegatedController_ = false;
87     bool isComponentController_ = false;
88     bool needUpdateTweenOption_ = false;
89     bool needUpdateKeyframes_ = false;
90     bool needUpdateTweenOptionCustom_ = false;
91     RefPtr<Animator> controller_;
92     RefPtr<Animator> controllerCustom_;
93     BaseId::IdType prepareIdCustom_ = -1;
94     BaseId::IdType prepareId_ = -1;
95     AnimationOperation operation_ = AnimationOperation::NONE;
96     AnimationOperation operationCustom_ = AnimationOperation::NONE;
97     TweenOption option_;
98     TweenOption optionCustom_;
99     uint64_t currentTimestamp_ = 0;
100     const static LinearEnumMapNode<AnimationType,
101         void (*)(const RefPtr<Animation<float>>&, WeakPtr<RenderTransform>&, TweenOption&)>
102         transformFloatAnimationAddMap_[];
103     Shadow shadow_;
104     PositionParam positionParam_;
105     bool skipPostFlush_ = false;
106     WeakPtr<TransformComponent> transform_;
107     WeakPtr<DisplayComponent> display_;
108 };
109 
110 } // namespace OHOS::Ace
111 
112 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TWEEN_TWEEN_ELEMENT_H
113