1 /*
2 * Copyright (c) 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 "core/components_ng/pattern/text_picker/toss_animation_controller.h"
17
18 #include <sys/time.h>
19
20 #include "base/utils/utils.h"
21 #include "core/components_ng/base/frame_node.h"
22 #include "core/components_ng/pattern/text_picker/textpicker_column_pattern.h"
23
24 namespace OHOS::Ace::NG {
25 namespace {
26 constexpr float MIN_TIME = 1.0f;
27 constexpr int32_t DISMIN = 0;
28 constexpr float VMIN = 0.0f;
29 constexpr float PICKER_SPEED_TH = 0.25f;
30 constexpr int32_t VELOCTY_TRANS = 1000;
31 constexpr float PICKER_SPRING_MASS = 1.f;
32 constexpr float PICKER_SPRING_STIFFNESS = 20.f;
33 constexpr float PICKER_SPRING_DAMPING = 10.f;
34 constexpr int32_t DISMAX = 30;
35 constexpr float VMAX = 5.0f;
36 } // namespace
37
SetStart(double y)38 void TextPickerTossAnimationController::SetStart(double y)
39 {
40 auto weak = AceType::WeakClaim(this);
41 auto ref = weak.Upgrade();
42 auto column = AceType::DynamicCast<TextPickerColumnPattern>(ref->column_.Upgrade());
43 CHECK_NULL_VOID(column);
44 auto isTouchBreak = column->GetTouchBreakStatus();
45 if (isTouchBreak == false) {
46 column->SetYOffset(0.0);
47 }
48 if (property_) {
49 StopTossAnimation();
50 }
51 yStart_ = y;
52 timeStart_ = GetCurrentTime();
53 }
54
SetEnd(double y)55 void TextPickerTossAnimationController::SetEnd(double y)
56 {
57 if (property_) {
58 StopTossAnimation();
59 }
60
61 yEnd_ = y;
62 timeEnd_ = GetCurrentTime();
63 }
64
Play()65 bool TextPickerTossAnimationController::Play()
66 {
67 auto weak = AceType::WeakClaim(this);
68 auto ref = weak.Upgrade();
69 auto column = AceType::DynamicCast<TextPickerColumnPattern>(ref->column_.Upgrade());
70 CHECK_NULL_RETURN(column, false);
71 auto timeDiff = timeEnd_ - timeStart_;
72 if (timeDiff < MIN_TIME) {
73 return false;
74 }
75 speed_ = column->GetMainVelocity() / VELOCTY_TRANS;
76 if (std::abs(speed_) < PICKER_SPEED_TH) {
77 return false;
78 }
79 column->ResetTotalDelta();
80 StartSpringMotion();
81 SetTossPlaying(true);
82 return true;
83 }
84
StartSpringMotion()85 void TextPickerTossAnimationController::StartSpringMotion()
86 {
87 auto weak = AceType::WeakClaim(this);
88 auto ref = weak.Upgrade();
89 auto column = AceType::DynamicCast<TextPickerColumnPattern>(ref->column_.Upgrade());
90 CHECK_NULL_VOID(column);
91 auto columnNode = column->GetHost();
92 CHECK_NULL_VOID(columnNode);
93 auto offset = column->GetOffset();
94 auto renderContext = columnNode->GetRenderContext();
95 CHECK_NULL_VOID(renderContext);
96 auto springCurve = UpdatePlayAnimationValue();
97 CHECK_NULL_VOID(springCurve);
98 double midIndex = column->GetShowOptionCount() / 2;
99 auto optionProperties = column->GetMidShiftDistance();
100 auto midShiftDistance =
101 column->IsDownScroll() ? optionProperties[midIndex].nextDistance : optionProperties[midIndex].prevDistance;
102 column->SetYLast(0);
103 end_ = midShiftDistance * showCount_ - offset;
104 AnimationOption option = AnimationOption();
105 option.SetCurve(springCurve);
106 CreatePropertyCallback();
107 CHECK_NULL_VOID(property_);
108 property_->Set(0);
109 isManualStopToss_ = false;
110 renderContext->AttachNodeAnimatableProperty(property_);
111 property_->SetPropertyUnit(PropertyUnit::PIXEL_POSITION);
112 auto finishCallback = [weak, column]() {
113 auto ref = weak.Upgrade();
114 CHECK_NULL_VOID(ref);
115 if (ref->isManualStopToss_) {
116 return;
117 }
118 column->TossAnimationStoped();
119 auto isTouchBreak = column->GetTouchBreakStatus();
120 if (isTouchBreak == false) {
121 column->SetTossStatus(false);
122 column->SetYOffset(0.0);
123 }
124 };
125 AnimationUtils::Animate(
126 option,
127 [weak]() {
128 auto ref = weak.Upgrade();
129 CHECK_NULL_VOID(ref);
130 ref->property_->Set(ref->end_);
131 },
132 finishCallback);
133 }
134
StopTossAnimation()135 void TextPickerTossAnimationController::StopTossAnimation()
136 {
137 auto weak = AceType::WeakClaim(this);
138 auto ref = weak.Upgrade();
139 CHECK_NULL_VOID(ref);
140 auto column = AceType::DynamicCast<TextPickerColumnPattern>(ref->column_.Upgrade());
141 CHECK_NULL_VOID(column);
142 column->SetTossStatus(false);
143 AnimationOption option;
144 option.SetCurve(Curves::LINEAR);
145 option.SetDuration(0);
146 option.SetDelay(0);
147 AnimationUtils::Animate(option, [weak]() {
148 auto ref = weak.Upgrade();
149 ref->isManualStopToss_ = true;
150 ref->property_->Set(0.0);
151 });
152 }
153
UpdatePlayAnimationValue()154 RefPtr<Curve> TextPickerTossAnimationController::UpdatePlayAnimationValue()
155 {
156 speed_ = std::abs(speed_) >= std::abs(VMAX) ? std::abs(VMAX) : std::abs(speed_);
157 showCount_ = static_cast<int>(DISMIN + (DISMAX - DISMIN) * (std::abs(speed_) - VMIN) / (VMAX - VMIN));
158 return AceType::MakeRefPtr<InterpolatingSpring>(
159 speed_, PICKER_SPRING_MASS, PICKER_SPRING_STIFFNESS, PICKER_SPRING_DAMPING);
160 }
161
GetCurrentTime() const162 double TextPickerTossAnimationController::GetCurrentTime() const
163 {
164 struct timeval tv = { 0 };
165 int result = gettimeofday(&tv, nullptr);
166 if (result != 0) {
167 return 0.0;
168 }
169
170 double sec = static_cast<double>(tv.tv_sec);
171 double msec = static_cast<double>(tv.tv_usec / 1000.0); // usec / 1000 is msec
172 return (sec * 1000 + msec); // sec * 1000 is msec
173 }
174
CreatePropertyCallback()175 void TextPickerTossAnimationController::CreatePropertyCallback()
176 {
177 if (property_) {
178 return;
179 }
180 auto weak = AceType::WeakClaim(this);
181 auto ref = weak.Upgrade();
182 auto column = AceType::DynamicCast<TextPickerColumnPattern>(ref->column_.Upgrade());
183 CHECK_NULL_VOID(column);
184 auto propertyCallback = [weak, column](float position) {
185 auto isTouchBreak = column->GetTouchBreakStatus();
186 if ((isTouchBreak) || (static_cast<int32_t>(position) == DISMIN)) {
187 return;
188 }
189 column->SetTossStatus(true);
190 column->UpdateToss(position);
191 };
192 property_ = AceType::MakeRefPtr<NodeAnimatablePropertyFloat>(0.0f, std::move(propertyCallback));
193 }
194
GetTossOffset() const195 double TextPickerTossAnimationController::GetTossOffset() const
196 {
197 if (!property_) {
198 return 0.0;
199 }
200
201 return end_ - property_->Get();
202 }
203
GetTossPlaying() const204 bool TextPickerTossAnimationController::GetTossPlaying() const
205 {
206 return isTossPlaying_;
207 }
208
SetTossPlaying(bool playing)209 void TextPickerTossAnimationController::SetTossPlaying(bool playing)
210 {
211 isTossPlaying_ = playing;
212 }
213 } // namespace OHOS::Ace::NG
214