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_LIST_INTERACTIVE_EFFECT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_LIST_INTERACTIVE_EFFECT_H
18 
19 #include "base/memory/ace_type.h"
20 #include "core/animation/animator.h"
21 #include "core/animation/keyframe_animation.h"
22 #include "core/components/list/list_item_theme.h"
23 #include "core/pipeline/base/render_node.h"
24 #include "core/pipeline/pipeline_context.h"
25 
26 namespace OHOS::Ace {
27 
28 const double DEFAULT_FOCUS_DURATION = 100.0;
29 const double DEFAULT_CLICK_DURATION = 200.0;
30 const double PRESS_ANIMATION_DURATION = 100.0;
31 const float ANIMATION_ZERO_TIME = 0.0f;
32 const float ANIMATION_HALF_TIME = 0.5f;
33 const float ANIMATION_END_TIME = 1.0f;
34 
35 enum class ItemState {
36     FOCUS = 0, // center position of list on watch
37     BLUR, // not FOCUS, and not neighbor to FOCUS on watch
38     NEARBY, // neighbor to FOCUS on watch
39     CLICK, // instant state when item clicked
40     NONE,
41 };
42 
43 class InteractiveEffect : public AceType {
44     DECLARE_ACE_TYPE(InteractiveEffect, AceType);
45 
46 public:
47     explicit InteractiveEffect(const WeakPtr<PipelineContext>& context);
48     ~InteractiveEffect() = default;
49     void ShowAnimation(ItemState state);
50     void TouchDownAnimation();
51     void TouchUpAnimation();
52     void CancelTouchAnimation();
53     void Initialize(const RefPtr<ThemeManager>& themeManager);
54     void UpdateContext(const WeakPtr<PipelineContext>& context);
55     void FinishPreviousAnimation();
56 
GetScale()57     double GetScale() const
58     {
59         return scale_;
60     }
SetScale(double scale)61     void SetScale(double scale)
62     {
63         scale_ = scale;
64     }
GetAlpha()65     double GetAlpha() const
66     {
67         return alpha_;
68     }
SetAlpha(double alpha)69     void SetAlpha(double alpha)
70     {
71         alpha_ = alpha;
72     }
GetOpacity()73     double GetOpacity() const
74     {
75         return opacity_;
76     }
SetOpacity(double opacity)77     void SetOpacity(double opacity)
78     {
79         opacity_ = opacity;
80     }
SetItemNode(const WeakPtr<RenderNode> & item)81     void SetItemNode(const WeakPtr<RenderNode>& item)
82     {
83         item_ = item;
84     }
SetListNode(const WeakPtr<RenderNode> & list)85     void SetListNode(const WeakPtr<RenderNode>& list)
86     {
87         list_ = list;
88     }
89 
GetAnimator()90     const RefPtr<Animator>& GetAnimator() const
91     {
92         return controller_;
93     }
94 
HandleOnFocus()95     virtual void HandleOnFocus() {}
HandleOnBlur()96     virtual void HandleOnBlur() {}
97 
98     // default click effect.
HandleOnClick()99     virtual void HandleOnClick()
100     {
101         if (theme_) {
102             alphaBegin_ = theme_->GetClickAlphaBegin();
103             alphaEnd_ = theme_->GetClickAlphaEnd();
104             clickDuration_ = theme_->GetClickAnimationDuration(); // 200.0
105         }
106     }
107 
BuildStateAnimation()108     virtual void BuildStateAnimation() {}
109     virtual void BuildClickAnimation();
110 
111 protected:
112     void MarkItemRender();
113     bool NeedClickAnimation();
114     void BuildClickAlphaAnimation(const RefPtr<KeyframeAnimation<double>>& alphaAnimation);
115     void CreateDoubleAnimation(RefPtr<KeyframeAnimation<double>>& doubleAnimation, double beginValue, double endValue);
116     void StartTouchAnimation(RefPtr<Animator> controller,
117         RefPtr<KeyframeAnimation<double>>& doubleAnimation, int32_t startDelay = 0);
118 
119     double focusDuration_ = DEFAULT_FOCUS_DURATION; // focus animation duration, ms
120     double clickDuration_ = DEFAULT_CLICK_DURATION; // click animation duration, ms
121     double scaleBegin_ = 1.0;
122     double scaleEnd_ = 1.0;
123     double opacityBegin_ = 0;
124     double opacityEnd_ = 1.0;
125     double alphaBegin_ = 0.0;
126     double alphaEnd_ = 0.1;
127     double alpha_ = 0.0;   // alpha factor in argb
128     double scale_ = 1.0;   // for canvas scale
129     double opacity_ = 1.0; // for opacity layer
130     WeakPtr<RenderNode> item_;
131     WeakPtr<RenderNode> list_;
132     RefPtr<PipelineContext> context_;
133     RefPtr<Animator> controller_;
134     RefPtr<ListItemTheme> theme_;
135 };
136 
137 } // namespace OHOS::Ace
138 
139 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_LIST_INTERACTIVE_EFFECT_H
140