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