• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 #include "click_spring_effect.h"
17 
18 #include <unordered_map>
19 
20 namespace OHOS::Ace {
21 namespace {
22 
23 struct SpringEffectProperty {
24     double scale_;
25     double velocity_;
26     RefPtr<SpringProperty> springProperty_;
27 };
28 
29 const static std::unordered_map<ClickSpringEffectType, SpringEffectProperty> CLICK_SPRING_EFFECT_PROPERTIES = {
30     { ClickSpringEffectType::SMALL, { 0.9, 1, AceType::MakeRefPtr<SpringProperty>(0.5, 410.0, 38.0) } },
31     { ClickSpringEffectType::MEDIUM, { 0.95, 0.5, AceType::MakeRefPtr<SpringProperty>(0.5, 350.0, 35.0) } },
32     { ClickSpringEffectType::LARGE, { 0.95, 0, AceType::MakeRefPtr<SpringProperty>(0.5, 240.0, 28.0) } },
33 };
34 
35 } // namespace
36 
ClickSpringEffect(const WeakPtr<PipelineContext> & context)37 ClickSpringEffect::ClickSpringEffect(const WeakPtr<PipelineContext>& context)
38 {
39     controller_ = AceType::MakeRefPtr<Animator>(context);
40 }
41 
FinishPreviousAnimation()42 void ClickSpringEffect::FinishPreviousAnimation()
43 {
44     if (controller_ && !controller_->IsStopped()) {
45         controller_->Finish();
46     }
47 }
48 
ShowAnimation(TouchType touchType,ClickSpringEffectType type)49 void ClickSpringEffect::ShowAnimation(TouchType touchType, ClickSpringEffectType type)
50 {
51     FinishPreviousAnimation();
52     auto propertyPos = CLICK_SPRING_EFFECT_PROPERTIES.find(type);
53     if (propertyPos == CLICK_SPRING_EFFECT_PROPERTIES.end()) {
54         LOGE("can't find type %{public}d", type);
55         return;
56     }
57 
58     auto& effectProperty = propertyPos->second;
59     RefPtr<SpringMotion> springMotion;
60     switch (touchType) {
61         case TouchType::DOWN:
62             springMotion = AceType::MakeRefPtr<SpringMotion>(
63                 GetScale(), effectProperty.scale_, effectProperty.velocity_, effectProperty.springProperty_);
64             break;
65         case TouchType::UP:
66         case TouchType::CANCEL:
67             springMotion = AceType::MakeRefPtr<SpringMotion>(
68                 GetScale(), 1.0, effectProperty.velocity_, effectProperty.springProperty_);
69             break;
70         default:
71             return;
72     }
73     if (!springMotion) {
74         return;
75     }
76 
77     springMotion->AddListener([weakEffect = AceType::WeakClaim(this)](double value) {
78         auto effect = weakEffect.Upgrade();
79         if (effect) {
80             effect->SetScale(value);
81             effect->MarkRender();
82         }
83     });
84 
85     if (controller_) {
86         controller_->PlayMotion(springMotion);
87     }
88 }
89 
MarkRender()90 void ClickSpringEffect::MarkRender()
91 {
92     auto node = renderNode_.Upgrade();
93     if (node) {
94         node->MarkNeedLayout();
95     }
96 }
97 
98 } // namespace OHOS::Ace