1 /*
2 * Copyright (c) 2021 Bestechnic (Shanghai) Co., Ltd. All rights reserved.
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 #include "animator/easing_equation.h"
16 #include "common/screen.h"
17 #include "components/root_view.h"
18 #include "components/ui_label.h"
19 #include "components/ui_label_button.h"
20 #include "components/ui_scroll_view.h"
21 #include "core/render_manager.h"
22 #include "gfx_utils/sys_info.h"
23 #include "graphic_config.h"
24 #include "hal_tick.h"
25 #include "hilog/log.h"
26 #include "ui_test.h"
27 #include <stdio.h>
28 #include <stdlib.h>
29
30 #undef LOG_TAG
31 #define LOG_TAG "AnimatorDemo"
32
33 using namespace OHOS;
34
35 namespace
36 {
37 static constexpr uint16_t TITLE_LABEL_DEFAULT_HEIGHT = 29;
38 static constexpr uint16_t FONT_DEFAULT_SIZE = 20;
39 static constexpr uint16_t VIEW_DISTANCE_TO_LEFT_SIDE = 48;
40 static constexpr uint16_t VIEW_DISTANCE_TO_TOP_SIDE = 48;
41 static constexpr uint16_t VIEW_DISTANCE_TO_LEFT_SIDE2 = 24;
42 static constexpr uint16_t TEXT_DISTANCE_TO_LEFT_SIDE = 48;
43 static constexpr uint16_t TEXT_DISTANCE_TO_TOP_SIDE = 20;
44 static constexpr uint16_t HALF_OPA_OPAQUE = OPA_OPAQUE / 2;
45 static constexpr uint16_t VIEW_STYLE_BORDER_WIDTH = 2;
46 static constexpr uint16_t VIEW_STYLE_BORDER_RADIUS = 8;
47 static constexpr uint16_t BUTTON_LABEL_SIZE = 16;
48 static constexpr uint8_t BUTTON_STYLE_BORDER_RADIUS_VALUE = 20;
49 static constexpr uint32_t BUTTON_STYLE_BACKGROUND_COLOR_VALUE = 0xFF333333;
50 static constexpr uint32_t BUTTON_STYLE_BACKGROUND_COLOR_PRESS = 0xFF2D2D2D;
51 static constexpr int16_t BACK_BUTTON_HEIGHT = 64;
52 static constexpr uint16_t BUTTON_WIDHT1 = 80;
53 static constexpr uint16_t BUTTON_HEIGHT1 = 40;
54 static constexpr uint16_t BUTTON_WIDHT2 = 120;
55 static constexpr uint16_t BUTTON_HEIGHT2 = 40;
56 static constexpr uint16_t BUTTON_WIDHT3 = 150;
57 static constexpr uint16_t BUTTON_HEIGHT3 = 60;
58 const int16_t LABEL_HEIGHT = 29;
59 const int16_t BUTTON_WIDTH = BUTTON_WIDHT3;
60 const int16_t BUTTON_HEIGHT = BUTTON_HEIGHT3;
61 const int16_t DELTA_X_COORDINATE = 5;
62 const int16_t DELTA_Y_COORDINATE = 12;
63 } // namespace
64
DeleteChildren(UIView * view)65 static void DeleteChildren(UIView *view)
66 {
67 if (view == nullptr) {
68 return;
69 }
70 while (view) {
71 UIView *tempView = view;
72 view = view->GetNextSibling();
73 if (tempView->IsViewGroup()) {
74 DeleteChildren(static_cast<UIViewGroup *>(tempView)->GetChildrenHead());
75 }
76 if (tempView->GetParent()) {
77 static_cast<UIViewGroup *>(tempView->GetParent())->Remove(tempView);
78 }
79 delete tempView;
80 }
81 }
82
83 class AnimatorDemoCallback : public AnimatorCallback
84 {
85 public:
AnimatorDemoCallback(UIView * uiView,int16_t startPos,int16_t endPos)86 AnimatorDemoCallback(UIView *uiView, int16_t startPos, int16_t endPos)
87 : startPos_(startPos),
88 endPos_(endPos),
89 easingFunc_(EasingEquation::LinearEaseNone),
90 animator_(new Animator(this, uiView, 1000, false)) // duration = 1000ms, repeat = false
91 {
92 }
93
~AnimatorDemoCallback()94 virtual ~AnimatorDemoCallback()
95 {
96 delete animator_;
97 }
98
GetAnimator() const99 Animator *GetAnimator() const
100 {
101 return animator_;
102 }
103
SetEasingFunc(EasingFunc easingFunc)104 void SetEasingFunc(EasingFunc easingFunc)
105 {
106 easingFunc_ = easingFunc;
107 }
108
Callback(UIView * view)109 virtual void Callback(UIView *view)
110 {
111 if (view == nullptr) {
112 return;
113 }
114 int16_t pos = easingFunc_(startPos_, endPos_, animator_->GetRunTime(), animator_->GetTime());
115 Rect invalidatedArea = view->GetRect();
116 view->SetPosition(pos, view->GetY());
117 invalidatedArea.Join(invalidatedArea, view->GetRect());
118 view->InvalidateRect(invalidatedArea);
119 }
120
121 protected:
122 int16_t startPos_;
123 int16_t endPos_;
124 EasingFunc easingFunc_;
125 Animator *animator_;
126 };
127
128 class AnimatorDemo : public UIView::OnClickListener, public SysInfo::OnFPSChangedListener
129 {
130 public:
GetInstance()131 static AnimatorDemo *GetInstance()
132 {
133 static AnimatorDemo instance;
134 return &instance;
135 }
136
137 void SetUp();
138 UIView *GetTestView();
139
140 private:
AnimatorDemo()141 AnimatorDemo() {}
~AnimatorDemo()142 ~AnimatorDemo()
143 {
144 DeleteChildren(container_);
145 container_ = nullptr;
146 label_ = nullptr;
147 button_ = nullptr;
148 scroll_ = nullptr;
149 delete callback_;
150 callback_ = nullptr;
151 uiViewGroupFrame_ = nullptr;
152 };
153
154 void SetUpButton(UILabelButton *btn, const char *title, int16_t x, int16_t y);
155 void SetUpLabel(const char *title, int16_t x, int16_t y);
156
157 bool OnClick(UIView &view, const ClickEvent &event) override;
158 void OnFPSChanged(float newFPS) override;
159
160 void UIKit_Animator_Test_BackEasing_001();
161
GetTitleLabel(const char * titleName)162 UILabel *GetTitleLabel(const char *titleName)
163 {
164 if (titleName == nullptr) {
165 return nullptr;
166 }
167 UILabel *label = new UILabel();
168 if (label == nullptr) {
169 return nullptr;
170 }
171 // 2: half of screen width
172 label->SetPosition(0, 0, Screen::GetInstance().GetWidth() / 2, TITLE_LABEL_DEFAULT_HEIGHT);
173 label->SetText(titleName);
174 label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
175 return label;
176 }
177
178 UIScrollView *container_ = nullptr;
179 UIScrollView *scroll_ = nullptr;
180 UILabel *label_ = nullptr;
181 UIButton *button_ = nullptr;
182 AnimatorDemoCallback *callback_ = nullptr;
183 Animator *animator_ = nullptr;
184 UIViewGroup *uiViewGroupFrame_ = nullptr;
185
186 UILabelButton *backOvershootBtn_ = nullptr;
187 UILabelButton *backEaseInBtn_ = nullptr;
188 UILabelButton *backEaseOutBtn_ = nullptr;
189 UILabelButton *backEaseInOutBtn_ = nullptr;
190
191 int16_t positionX_ = 0;
192 int16_t positionY_ = 0;
193 };
194
SetUp()195 void AnimatorDemo::SetUp()
196 {
197 if (container_ == nullptr) {
198 container_ = new UIScrollView();
199 container_->SetPosition(25, 25, Screen::GetInstance().GetWidth() - 50, Screen::GetInstance().GetHeight() - 50);
200 uiViewGroupFrame_ = new UIViewGroup();
201 uiViewGroupFrame_->SetStyle(STYLE_BORDER_COLOR, Color::White().full);
202 uiViewGroupFrame_->SetStyle(STYLE_BORDER_OPA, HALF_OPA_OPAQUE);
203 uiViewGroupFrame_->SetStyle(STYLE_BORDER_WIDTH, VIEW_STYLE_BORDER_WIDTH);
204 uiViewGroupFrame_->SetStyle(STYLE_BORDER_RADIUS, VIEW_STYLE_BORDER_RADIUS);
205 uiViewGroupFrame_->SetStyle(STYLE_BACKGROUND_OPA, 0);
206 uiViewGroupFrame_->SetPosition(VIEW_DISTANCE_TO_LEFT_SIDE2, VIEW_DISTANCE_TO_TOP_SIDE);
207 container_->Add(uiViewGroupFrame_);
208 label_ = new UILabel();
209 container_->Add(label_);
210 label_->SetPosition(100, 20, 264, 48);
211 label_->SetText("动画效果");
212 label_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
213 button_ = new UIButton();
214 button_->SetPosition(0, 0, 100, 100);
215 button_->SetStyle(STYLE_BORDER_RADIUS, 90);
216 callback_ = new AnimatorDemoCallback(button_, 0, 320);
217 animator_ = callback_->GetAnimator();
218 uiViewGroupFrame_->Add(button_);
219 uiViewGroupFrame_->Resize(container_->GetWidth() - VIEW_DISTANCE_TO_LEFT_SIDE,
220 button_->GetHeight());
221 }
222 if (scroll_ == nullptr) {
223 scroll_ = new UIScrollView();
224 // 20: increase y-coordinate
225 uint16_t y = uiViewGroupFrame_->GetY() + uiViewGroupFrame_->GetHeight() + 20;
226 scroll_->SetPosition(0, y, Screen::GetInstance().GetWidth(),
227 Screen::GetInstance().GetHeight() - y - 64); // 64: decrease y-coordinate
228 container_->Add(scroll_);
229 positionX_ = 0;
230 }
231 RenderManager::GetInstance().RegisterFPSChangedListener(this);
232 // this->SetFPSCalculateType(SysInfo::FPS_CT_PRECISE_SAMPLING);
233 this->SetFPSCalculateType(SysInfo::FPS_CT_FIXED_TIME);
234 }
235
GetTestView()236 UIView *AnimatorDemo::GetTestView()
237 {
238 UIKit_Animator_Test_BackEasing_001();
239 return container_;
240 }
241
SetUpLabel(const char * title,int16_t x,int16_t y)242 void AnimatorDemo::SetUpLabel(const char *title, int16_t x, int16_t y)
243 {
244 UILabel *label = GetTitleLabel(title);
245 scroll_->Add(label);
246 label->SetPosition(x, y, 288, LABEL_HEIGHT); // 288: label width
247 }
248
SetUpButton(UILabelButton * btn,const char * title,int16_t x,int16_t y)249 void AnimatorDemo::SetUpButton(UILabelButton *btn, const char *title, int16_t x, int16_t y)
250 {
251 if (btn == nullptr) {
252 return;
253 }
254 scroll_->Add(btn);
255 btn->SetPosition(x, y, BUTTON_WIDHT3, BUTTON_HEIGHT3);
256 btn->SetText(title);
257 btn->SetFont(DEFAULT_VECTOR_FONT_FILENAME, BUTTON_LABEL_SIZE);
258 btn->SetOnClickListener(this);
259 btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::RELEASED);
260 btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::PRESSED);
261 btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::INACTIVE);
262 btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::RELEASED);
263 btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::PRESSED);
264 btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::INACTIVE);
265 scroll_->Invalidate();
266 }
267
UIKit_Animator_Test_BackEasing_001()268 void AnimatorDemo::UIKit_Animator_Test_BackEasing_001()
269 {
270 backOvershootBtn_ = new UILabelButton();
271 backEaseInBtn_ = new UILabelButton();
272 backEaseOutBtn_ = new UILabelButton();
273 backEaseInOutBtn_ = new UILabelButton();
274 positionX_ = TEXT_DISTANCE_TO_LEFT_SIDE;
275 positionY_ = 0;
276 SetUpLabel("back动画效果: ", positionX_, positionY_);
277 positionY_ += LABEL_HEIGHT + DELTA_X_COORDINATE;
278 SetUpButton(backOvershootBtn_, "overshoot+", positionX_, positionY_);
279 positionX_ += BUTTON_WIDTH + DELTA_Y_COORDINATE;
280 SetUpButton(backEaseInBtn_, "BackEaseIn", positionX_, positionY_);
281 positionX_ = TEXT_DISTANCE_TO_LEFT_SIDE;
282 positionY_ += BUTTON_HEIGHT + DELTA_Y_COORDINATE;
283 SetUpButton(backEaseOutBtn_, "BackEaseOut", positionX_, positionY_);
284 positionX_ += BUTTON_WIDTH + DELTA_Y_COORDINATE;
285 SetUpButton(backEaseInOutBtn_, "BackEaseInOut", positionX_, positionY_);
286 }
287
OnClick(UIView & view,const ClickEvent & event)288 bool AnimatorDemo::OnClick(UIView &view, const ClickEvent &event)
289 {
290 static double overshoot = 1.7;
291 if (&view == backOvershootBtn_) {
292 overshoot += 1;
293 EasingEquation::SetBackOvershoot(overshoot);
294 } else if (&view == backEaseInBtn_) {
295 callback_->SetEasingFunc(EasingEquation::BackEaseIn);
296 } else if (&view == backEaseOutBtn_) {
297 callback_->SetEasingFunc(EasingEquation::BackEaseOut);
298 } else if (&view == backEaseInOutBtn_) {
299 callback_->SetEasingFunc(EasingEquation::BackEaseInOut);
300 }
301 animator_->Start();
302 container_->Invalidate();
303 return true;
304 }
305
OnFPSChanged(float newFPS)306 void AnimatorDemo::OnFPSChanged(float newFPS)
307 {
308 static float lastFPS = 0.0f;
309 if (abs(newFPS - lastFPS) < 1) {
310 return;
311 }
312 lastFPS = newFPS;
313 char buf[32] = {0};
314 snprintf(buf, sizeof(buf), "动画效果 %.0f fps", newFPS);
315 HILOG_DEBUG(HILOG_MODULE_APP, "%.0f fps\n", newFPS);
316 label_->SetText(reinterpret_cast<char *>(buf));
317 label_->Invalidate();
318 }
319
320 /**
321 * UIScaleRotateDemo
322 */
323 class UIScaleRotateDemo : public AnimatorCallback
324 {
325 public:
GetInstance()326 static UIScaleRotateDemo *GetInstance()
327 {
328 static UIScaleRotateDemo instance;
329 return &instance;
330 }
331
SetUp()332 void SetUp()
333 {
334 container_ = new UIScrollView();
335 container_->SetPosition(0, 0, Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
336
337 label_ = new UILabel();
338 label_->SetText("label");
339 label_->SetPosition(100, 100, 100, 50);
340 container_->Add(label_);
341
342 button_ = new UILabelButton();
343 button_->SetText("button");
344 button_->SetPosition(100, 200, 100, 50);
345 container_->Add(button_);
346
347 animator_ = new Animator(this, nullptr, 50, true);
348 animator_->Start();
349 lastRun_ = HALTick::GetInstance().GetTime();
350 }
351
GetTestView()352 UIView *GetTestView()
353 {
354 return container_;
355 }
356
357 private:
UIScaleRotateDemo()358 UIScaleRotateDemo() {}
~UIScaleRotateDemo()359 ~UIScaleRotateDemo()
360 {
361 DeleteChildren(container_);
362 container_ = nullptr;
363 label_ = nullptr;
364 button_ = nullptr;
365 if (animator_) {
366 delete animator_;
367 animator_ = nullptr;
368 }
369 }
370
Callback(UIView * view)371 void Callback(UIView *view) override
372 {
373 angleValue_++;
374
375 if (scaleValue_.x_ < 0.5f) {
376 scaleStep_ = 0.02f;
377 } else if (scaleValue_.x_ > 1.5f) {
378 scaleStep_ = -0.02f;
379 }
380 scaleValue_.x_ += scaleStep_;
381 scaleValue_.y_ += scaleStep_;
382 label_->Rotate(angleValue_, VIEW_CENTER1);
383 label_->Scale(scaleValue_, VIEW_CENTER1);
384 button_->Rotate(angleValue_, VIEW_CENTER2);
385 button_->Scale(scaleValue_, VIEW_CENTER2);
386
387 frame_cnt_++;
388 if (HALTick::GetInstance().GetElapseTime(lastRun_) >= 1000) {
389 HILOG_DEBUG(HILOG_MODULE_APP, "%u fps\n", frame_cnt_);
390 lastRun_ = HALTick::GetInstance().GetTime();
391 frame_cnt_ = 0;
392 }
393 }
394
395 private:
396 UIViewGroup *container_ = nullptr;
397 UILabel *label_ = nullptr;
398 UILabelButton *button_ = nullptr;
399 Animator *animator_ = nullptr;
400 const Vector2<float> VIEW_CENTER1 = {100.0f, 100.0f};
401 const Vector2<float> VIEW_CENTER2 = {100.0f, 200.0f};
402 int16_t angleValue_ = 0;
403 Vector2<float> scaleValue_ = {1.0f, 1.0f};
404 float scaleStep_ = 0.02f;
405 uint32_t lastRun_ = 0;
406 uint32_t frame_cnt_ = 0;
407 };
408
AnimatorDemoStart(void)409 void AnimatorDemoStart(void)
410 {
411 RootView *rootView_ = RootView::GetInstance();
412 rootView_->SetPosition(0, 0, Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
413
414 AnimatorDemo *view = AnimatorDemo::GetInstance();
415 // UIScaleRotateDemo *view = UIScaleRotateDemo::GetInstance();
416 view->SetUp();
417 rootView_->Add(view->GetTestView());
418 rootView_->Invalidate();
419 }